Skip to content

[go1.20] Removing some uses of unsafeheader.Slice from reflect #1321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions compiler/natives/src/reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,10 @@ func getJsTag(tag string) string {
return ""
}

func (v Value) UnsafePointer() unsafe.Pointer {
return unsafe.Pointer(v.Pointer())
}

func (v Value) grow(n int) {
if n < 0 {
panic(`reflect.Value.Grow: negative len`)
Expand All @@ -1343,6 +1347,32 @@ func (v Value) grow(n int) {
}
}

// extendSlice is used by native reflect.Append and reflect.AppendSlice
// Overridden to avoid the use of `unsafeheader.Slice` since GopherJS
// uses different slice implementation.
func (v Value) extendSlice(n int) Value {
v.mustBeExported()
v.mustBe(Slice)

s := v.object()
sNil := jsType(v.typ).Get(`nil`)
fl := flagIndir | flag(Slice)
if s == sNil && n <= 0 {
return makeValue(v.typ, wrapJsObject(v.typ, sNil), fl)
}

newSlice := jsType(v.typ).New(s.Get("$array"))
newSlice.Set("$offset", s.Get("$offset"))
newSlice.Set("$length", s.Get("$length"))
newSlice.Set("$capacity", s.Get("$capacity"))

v2 := makeValue(v.typ, wrapJsObject(v.typ, newSlice), fl)
v2.grow(n)
s2 := v2.object()
s2.Set(`$length`, s2.Get(`$length`).Int()+n)
return v2
}

func (v Value) Index(i int) Value {
switch k := v.kind(); k {
case Array:
Expand Down Expand Up @@ -1399,6 +1429,11 @@ func (v Value) InterfaceData() [2]uintptr {
panic(errors.New("InterfaceData is not supported by GopherJS"))
}

func (v Value) SetZero() {
v.mustBeAssignable()
v.Set(Zero(v.typ))
}

func (v Value) IsNil() bool {
switch k := v.kind(); k {
case Ptr, Slice:
Expand Down Expand Up @@ -1438,6 +1473,9 @@ func (v Value) Len() int {
}
}

//gopherjs:purge Not used since Len() is overridden.
func (v Value) lenNonSlice() int

func (v Value) Pointer() uintptr {
switch k := v.kind(); k {
case Chan, Map, Ptr, UnsafePointer:
Expand Down
Loading