Skip to content

compiler/natives/src/reflect: compatible go reflect #1160

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 2 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
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
22 changes: 12 additions & 10 deletions compiler/natives/src/reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func reflectType(typ *js.Object) *rtype {
rt := &rtype{
size: uintptr(typ.Get("size").Int()),
kind: uint8(typ.Get("kind").Int()),
str: newNameOff(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool())),
str: resolveReflectName(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool())),
}
js.InternalObject(rt).Set("jsType", typ)
typ.Set("reflectType", js.InternalObject(rt))
Expand All @@ -82,7 +82,7 @@ func reflectType(typ *js.Object) *rtype {
continue
}
reflectMethods = append(reflectMethods, method{
name: newNameOff(newMethodName(m)),
name: resolveReflectName(newMethodName(m)),
mtyp: newTypeOff(reflectType(m.Get("typ"))),
})
}
Expand All @@ -94,18 +94,18 @@ func reflectType(typ *js.Object) *rtype {
continue
}
reflectMethods = append(reflectMethods, method{
name: newNameOff(newMethodName(m)),
name: resolveReflectName(newMethodName(m)),
mtyp: newTypeOff(reflectType(m.Get("typ"))),
})
}
ut := &uncommonType{
pkgPath: newNameOff(newName(internalStr(typ.Get("pkg")), "", false)),
pkgPath: resolveReflectName(newName(internalStr(typ.Get("pkg")), "", false)),
mcount: uint16(methodSet.Length()),
xcount: xcount,
_methods: reflectMethods,
}
uncommonTypeMap[rt] = ut
js.InternalObject(ut).Set("jsType", typ)
js.InternalObject(rt).Set("uncommonType", js.InternalObject(ut))
}

switch rt.Kind() {
Expand Down Expand Up @@ -154,7 +154,7 @@ func reflectType(typ *js.Object) *rtype {
for i := range imethods {
m := methods.Index(i)
imethods[i] = imethod{
name: newNameOff(newMethodName(m)),
name: resolveReflectName(newMethodName(m)),
typ: newTypeOff(reflectType(m.Get("typ"))),
}
}
Expand Down Expand Up @@ -224,10 +224,12 @@ func (t *uncommonType) exportedMethods() []method {
return t._methods[:t.xcount:t.xcount]
}

var uncommonTypeMap = make(map[*rtype]*uncommonType)

func (t *rtype) uncommon() *uncommonType {
return uncommonTypeMap[t]
obj := js.InternalObject(t).Get("uncommonType")
if obj == js.Undefined {
return nil
}
return (*uncommonType)(unsafe.Pointer(obj.Unsafe()))
}

type funcType struct {
Expand Down Expand Up @@ -298,7 +300,7 @@ func (t *rtype) nameOff(off nameOff) name {
return nameOffList[int(off)]
}

func newNameOff(n name) nameOff {
func resolveReflectName(n name) nameOff {
i := len(nameOffList)
nameOffList = append(nameOffList, n)
return nameOff(i)
Expand Down
28 changes: 28 additions & 0 deletions tests/linkname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package tests
import (
"testing"

_ "reflect"
_ "unsafe"

"github.com/google/go-cmp/cmp"
"github.com/gopherjs/gopherjs/tests/testdata/linkname/method"
"github.com/gopherjs/gopherjs/tests/testdata/linkname/one"
Expand Down Expand Up @@ -34,3 +37,28 @@ func TestLinknameMethods(t *testing.T) {
}()
method.TestLinkname(t)
}

type name struct{ bytes *byte }
type nameOff int32
type rtype struct{}

//go:linkname rtype_nameOff reflect.(*rtype).nameOff
func rtype_nameOff(r *rtype, off nameOff) name

//go:linkname newName reflect.newName
func newName(n, tag string, exported bool) name

//go:linkname name_name reflect.name.name
func name_name(name) string

//go:linkname resolveReflectName reflect.resolveReflectName
func resolveReflectName(n name) nameOff

func TestLinknameReflectName(t *testing.T) {
info := "myinfo"
off := resolveReflectName(newName(info, "", false))
n := rtype_nameOff(nil, off)
if s := name_name(n); s != info {
t.Fatalf("to reflect.name got %q: want %q", s, info)
}
}