Thanks to visit codestin.com
Credit goes to github.com

Skip to content

v1.19 - Moving embedded bit from offset to name in reflect and reflectlite #1250

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
Nov 14, 2023
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
29 changes: 14 additions & 15 deletions compiler/natives/src/internal/reflectlite/reflectlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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: newNameOff(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool(), false)),
}
js.InternalObject(rt).Set(idJsType, typ)
typ.Set(idReflectType, js.InternalObject(rt))
Expand All @@ -69,7 +69,7 @@ func reflectType(typ *js.Object) *rtype {
continue
}
reflectMethods = append(reflectMethods, method{
name: newNameOff(newName(internalStr(m.Get("name")), "", exported)),
name: newNameOff(newName(internalStr(m.Get("name")), "", exported, false)),
mtyp: newTypeOff(reflectType(m.Get("typ"))),
})
}
Expand All @@ -81,12 +81,12 @@ func reflectType(typ *js.Object) *rtype {
continue
}
reflectMethods = append(reflectMethods, method{
name: newNameOff(newName(internalStr(m.Get("name")), "", exported)),
name: newNameOff(newName(internalStr(m.Get("name")), "", exported, false)),
mtyp: newTypeOff(reflectType(m.Get("typ"))),
})
}
ut := &uncommonType{
pkgPath: newNameOff(newName(internalStr(typ.Get("pkg")), "", false)),
pkgPath: newNameOff(newName(internalStr(typ.Get("pkg")), "", false, false)),
mcount: uint16(methodSet.Length()),
xcount: xcount,
_methods: reflectMethods,
Expand Down Expand Up @@ -141,13 +141,13 @@ func reflectType(typ *js.Object) *rtype {
for i := range imethods {
m := methods.Index(i)
imethods[i] = imethod{
name: newNameOff(newName(internalStr(m.Get("name")), "", internalStr(m.Get("pkg")) == "")),
name: newNameOff(newName(internalStr(m.Get("name")), "", internalStr(m.Get("pkg")) == "", false)),
typ: newTypeOff(reflectType(m.Get("typ"))),
}
}
setKindType(rt, &interfaceType{
rtype: *rt,
pkgPath: newName(internalStr(typ.Get("pkg")), "", false),
pkgPath: newName(internalStr(typ.Get("pkg")), "", false, false),
methods: imethods,
})
case Map:
Expand All @@ -168,19 +168,15 @@ func reflectType(typ *js.Object) *rtype {
reflectFields := make([]structField, fields.Length())
for i := range reflectFields {
f := fields.Index(i)
offsetEmbed := uintptr(i) << 1
if f.Get("embedded").Bool() {
offsetEmbed |= 1
}
reflectFields[i] = structField{
name: newName(internalStr(f.Get("name")), internalStr(f.Get("tag")), f.Get("exported").Bool()),
typ: reflectType(f.Get("typ")),
offsetEmbed: offsetEmbed,
name: newName(internalStr(f.Get("name")), internalStr(f.Get("tag")), f.Get("exported").Bool(), f.Get("embedded").Bool()),
typ: reflectType(f.Get("typ")),
offset: uintptr(i),
}
}
setKindType(rt, &structType{
rtype: *rt,
pkgPath: newName(internalStr(typ.Get("pkgPath")), "", false),
pkgPath: newName(internalStr(typ.Get("pkgPath")), "", false, false),
fields: reflectFields,
})
}
Expand Down Expand Up @@ -242,6 +238,7 @@ type nameData struct {
name string
tag string
exported bool
embedded bool
}

var nameMap = make(map[*byte]*nameData)
Expand All @@ -250,13 +247,15 @@ func (n name) name() (s string) { return nameMap[n.bytes].name }
func (n name) tag() (s string) { return nameMap[n.bytes].tag }
func (n name) pkgPath() string { return "" }
func (n name) isExported() bool { return nameMap[n.bytes].exported }
func (n name) embedded() bool { return nameMap[n.bytes].embedded }

func newName(n, tag string, exported bool) name {
func newName(n, tag string, exported, embedded bool) name {
b := new(byte)
nameMap[b] = &nameData{
name: n,
tag: tag,
exported: exported,
embedded: embedded,
}
return name{
bytes: b,
Expand Down
23 changes: 11 additions & 12 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: resolveReflectName(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool())),
str: resolveReflectName(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool(), false)),
}
js.InternalObject(rt).Set("jsType", typ)
typ.Set("reflectType", js.InternalObject(rt))
Expand Down Expand Up @@ -99,7 +99,7 @@ func reflectType(typ *js.Object) *rtype {
})
}
ut := &uncommonType{
pkgPath: resolveReflectName(newName(internalStr(typ.Get("pkg")), "", false)),
pkgPath: resolveReflectName(newName(internalStr(typ.Get("pkg")), "", false, false)),
mcount: uint16(methodSet.Length()),
xcount: xcount,
_methods: reflectMethods,
Expand Down Expand Up @@ -160,7 +160,7 @@ func reflectType(typ *js.Object) *rtype {
}
setKindType(rt, &interfaceType{
rtype: *rt,
pkgPath: newName(internalStr(typ.Get("pkg")), "", false),
pkgPath: newName(internalStr(typ.Get("pkg")), "", false, false),
methods: imethods,
})
case Map:
Expand All @@ -181,19 +181,15 @@ func reflectType(typ *js.Object) *rtype {
reflectFields := make([]structField, fields.Length())
for i := range reflectFields {
f := fields.Index(i)
offsetEmbed := uintptr(i) << 1
if f.Get("embedded").Bool() {
offsetEmbed |= 1
}
reflectFields[i] = structField{
name: newName(internalStr(f.Get("name")), internalStr(f.Get("tag")), f.Get("exported").Bool()),
typ: reflectType(f.Get("typ")),
offsetEmbed: offsetEmbed,
name: newName(internalStr(f.Index.Get("name")), internalStr(f.Get("tag")), f.Get("exported").Bool(), f.Get("embedded").Bool()),
typ: reflectType(f.Get("typ")),
offset: uintptr(i),
}
}
setKindType(rt, &structType{
rtype: *rt,
pkgPath: newName(internalStr(typ.Get("pkgPath")), "", false),
pkgPath: newName(internalStr(typ.Get("pkgPath")), "", false, false),
fields: reflectFields,
})
}
Expand Down Expand Up @@ -257,6 +253,7 @@ type nameData struct {
name string
tag string
exported bool
embedded bool
pkgPath string
}

Expand All @@ -266,16 +263,18 @@ func (n name) name() (s string) { return nameMap[n.bytes].name }
func (n name) tag() (s string) { return nameMap[n.bytes].tag }
func (n name) pkgPath() string { return nameMap[n.bytes].pkgPath }
func (n name) isExported() bool { return nameMap[n.bytes].exported }
func (n name) embedded() bool { return nameMap[n.bytes].embedded }
func (n name) setPkgPath(pkgpath string) {
nameMap[n.bytes].pkgPath = pkgpath
}

func newName(n, tag string, exported bool) name {
func newName(n, tag string, exported, embedded bool) name {
b := new(byte)
nameMap[b] = &nameData{
name: n,
tag: tag,
exported: exported,
embedded: embedded,
}
return name{
bytes: b,
Expand Down