Skip to content

add internal/reflectlite for Go1.13 support #957

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
compiler/natives/src/internal/reflectlite,compiler/natives/src/reflec…
…t: update for Go1.13
  • Loading branch information
visualfc committed Jan 25, 2020
commit 752b6700ff02d35e81bb82cfe7f4e7485fff006b
25 changes: 16 additions & 9 deletions compiler/natives/src/internal/reflectlite/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@ func init() {
//uint8Type = TypeOf(uint8(0)).(*rtype) // set for real
}

var (
idJsType = "__jsType"
idReflectType = "__reflectType"
idKindType = "__kindType"
idRtype = "__rtype"
)

func jsType(typ Type) *js.Object {
return js.InternalObject(typ).Get("jsType")
return js.InternalObject(typ).Get(idJsType)
}

func reflectType(typ *js.Object) *rtype {
if typ.Get("reflectType") == js.Undefined {
if typ.Get(idReflectType) == js.Undefined {
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())),
}
js.InternalObject(rt).Set("jsType", typ)
typ.Set("reflectType", js.InternalObject(rt))
js.InternalObject(rt).Set(idJsType, typ)
typ.Set(idReflectType, js.InternalObject(rt))

methodSet := js.Global.Call("$methodSet", typ)
if methodSet.Length() != 0 || typ.Get("named").Bool() {
Expand Down Expand Up @@ -82,7 +89,7 @@ func reflectType(typ *js.Object) *rtype {
_methods: reflectMethods,
}
uncommonTypeMap[rt] = ut
js.InternalObject(ut).Set("jsType", typ)
js.InternalObject(ut).Set(idJsType, typ)
}

switch rt.Kind() {
Expand Down Expand Up @@ -176,12 +183,12 @@ func reflectType(typ *js.Object) *rtype {
}
}

return (*rtype)(unsafe.Pointer(typ.Get("reflectType").Unsafe()))
return (*rtype)(unsafe.Pointer(typ.Get(idReflectType).Unsafe()))
}

func setKindType(rt *rtype, kindType interface{}) {
js.InternalObject(rt).Set("kindType", js.InternalObject(kindType))
js.InternalObject(kindType).Set("rtype", js.InternalObject(rt))
js.InternalObject(rt).Set(idKindType, js.InternalObject(kindType))
js.InternalObject(kindType).Set(idRtype, js.InternalObject(rt))
}

type uncommonType struct {
Expand Down Expand Up @@ -818,7 +825,7 @@ func (t *rtype) pointers() bool {
// }
// mt := FuncOf(in, out, ft.IsVariadic())
// m.Type = mt
// prop := js.Global.Call("$methodSet", js.InternalObject(t).Get("jsType")).Index(i).Get("prop").String()
// prop := js.Global.Call("$methodSet", js.InternalObject(t).Get(idJsType)).Index(i).Get("prop").String()
// fn := js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
// rcvr := arguments[0]
// return rcvr.Get(prop).Call("apply", rcvr, arguments[1:])
Expand Down
11 changes: 10 additions & 1 deletion compiler/natives/src/reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@ func newTypeOff(t *rtype) typeOff {
return typeOff(i)
}

// addReflectOff adds a pointer to the reflection lookup map in the runtime.
// It returns a new ID that can be used as a typeOff or textOff, and will
// be resolved correctly. Implemented in the runtime package.
func addReflectOff(ptr unsafe.Pointer) int32 {
i := len(typeOffList)
typeOffList = append(typeOffList, (*rtype)(ptr))
return int32(i)
}

func internalStr(strObj *js.Object) string {
var c struct{ str string }
js.InternalObject(c).Set("str", strObj) // get string without internalizing
Expand Down Expand Up @@ -591,7 +600,7 @@ func mapiterkey(it unsafe.Pointer) unsafe.Pointer {
return unsafe.Pointer(js.Global.Call("$newDataPointer", kv.Get("k"), jsType(PtrTo(iter.t.Key()))).Unsafe())
}

func mapitervalue(it unsafe.Pointer) unsafe.Pointer {
func mapiterelem(it unsafe.Pointer) unsafe.Pointer {
iter := (*mapIter)(it)
var kv *js.Object
if iter.last != nil {
Expand Down