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

Skip to content

Commit c146af0

Browse files
updated reflect and reflectlite
1 parent a0b7135 commit c146af0

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

compiler/natives/src/internal/reflectlite/reflectlite.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func reflectType(typ *js.Object) *rtype {
5050
rt := &rtype{
5151
size: uintptr(typ.Get("size").Int()),
5252
kind: uint8(typ.Get("kind").Int()),
53-
str: newNameOff(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool())),
53+
str: newNameOff(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool(), false)),
5454
}
5555
js.InternalObject(rt).Set(idJsType, typ)
5656
typ.Set(idReflectType, js.InternalObject(rt))
@@ -69,7 +69,7 @@ func reflectType(typ *js.Object) *rtype {
6969
continue
7070
}
7171
reflectMethods = append(reflectMethods, method{
72-
name: newNameOff(newName(internalStr(m.Get("name")), "", exported)),
72+
name: newNameOff(newName(internalStr(m.Get("name")), "", exported, false)),
7373
mtyp: newTypeOff(reflectType(m.Get("typ"))),
7474
})
7575
}
@@ -81,12 +81,12 @@ func reflectType(typ *js.Object) *rtype {
8181
continue
8282
}
8383
reflectMethods = append(reflectMethods, method{
84-
name: newNameOff(newName(internalStr(m.Get("name")), "", exported)),
84+
name: newNameOff(newName(internalStr(m.Get("name")), "", exported, false)),
8585
mtyp: newTypeOff(reflectType(m.Get("typ"))),
8686
})
8787
}
8888
ut := &uncommonType{
89-
pkgPath: newNameOff(newName(internalStr(typ.Get("pkg")), "", false)),
89+
pkgPath: newNameOff(newName(internalStr(typ.Get("pkg")), "", false, false)),
9090
mcount: uint16(methodSet.Length()),
9191
xcount: xcount,
9292
_methods: reflectMethods,
@@ -141,13 +141,13 @@ func reflectType(typ *js.Object) *rtype {
141141
for i := range imethods {
142142
m := methods.Index(i)
143143
imethods[i] = imethod{
144-
name: newNameOff(newName(internalStr(m.Get("name")), "", internalStr(m.Get("pkg")) == "")),
144+
name: newNameOff(newName(internalStr(m.Get("name")), "", internalStr(m.Get("pkg")) == "", false)),
145145
typ: newTypeOff(reflectType(m.Get("typ"))),
146146
}
147147
}
148148
setKindType(rt, &interfaceType{
149149
rtype: *rt,
150-
pkgPath: newName(internalStr(typ.Get("pkg")), "", false),
150+
pkgPath: newName(internalStr(typ.Get("pkg")), "", false, false),
151151
methods: imethods,
152152
})
153153
case Map:
@@ -168,19 +168,15 @@ func reflectType(typ *js.Object) *rtype {
168168
reflectFields := make([]structField, fields.Length())
169169
for i := range reflectFields {
170170
f := fields.Index(i)
171-
offsetEmbed := uintptr(i) << 1
172-
if f.Get("embedded").Bool() {
173-
offsetEmbed |= 1
174-
}
175171
reflectFields[i] = structField{
176-
name: newName(internalStr(f.Get("name")), internalStr(f.Get("tag")), f.Get("exported").Bool()),
177-
typ: reflectType(f.Get("typ")),
178-
offsetEmbed: offsetEmbed,
172+
name: newName(internalStr(f.Get("name")), internalStr(f.Get("tag")), f.Get("exported").Bool(), f.Get("embedded").Bool()),
173+
typ: reflectType(f.Get("typ")),
174+
offset: uintptr(i),
179175
}
180176
}
181177
setKindType(rt, &structType{
182178
rtype: *rt,
183-
pkgPath: newName(internalStr(typ.Get("pkgPath")), "", false),
179+
pkgPath: newName(internalStr(typ.Get("pkgPath")), "", false, false),
184180
fields: reflectFields,
185181
})
186182
}
@@ -242,6 +238,7 @@ type nameData struct {
242238
name string
243239
tag string
244240
exported bool
241+
embedded bool
245242
}
246243

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

254-
func newName(n, tag string, exported bool) name {
252+
func newName(n, tag string, exported, embedded bool) name {
255253
b := new(byte)
256254
nameMap[b] = &nameData{
257255
name: n,
258256
tag: tag,
259257
exported: exported,
258+
embedded: embedded,
260259
}
261260
return name{
262261
bytes: b,

compiler/natives/src/reflect/reflect.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func reflectType(typ *js.Object) *rtype {
6363
rt := &rtype{
6464
size: uintptr(typ.Get("size").Int()),
6565
kind: uint8(typ.Get("kind").Int()),
66-
str: resolveReflectName(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool())),
66+
str: resolveReflectName(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool(), false)),
6767
}
6868
js.InternalObject(rt).Set("jsType", typ)
6969
typ.Set("reflectType", js.InternalObject(rt))
@@ -99,7 +99,7 @@ func reflectType(typ *js.Object) *rtype {
9999
})
100100
}
101101
ut := &uncommonType{
102-
pkgPath: resolveReflectName(newName(internalStr(typ.Get("pkg")), "", false)),
102+
pkgPath: resolveReflectName(newName(internalStr(typ.Get("pkg")), "", false, false)),
103103
mcount: uint16(methodSet.Length()),
104104
xcount: xcount,
105105
_methods: reflectMethods,
@@ -160,7 +160,7 @@ func reflectType(typ *js.Object) *rtype {
160160
}
161161
setKindType(rt, &interfaceType{
162162
rtype: *rt,
163-
pkgPath: newName(internalStr(typ.Get("pkg")), "", false),
163+
pkgPath: newName(internalStr(typ.Get("pkg")), "", false, false),
164164
methods: imethods,
165165
})
166166
case Map:
@@ -181,19 +181,15 @@ func reflectType(typ *js.Object) *rtype {
181181
reflectFields := make([]structField, fields.Length())
182182
for i := range reflectFields {
183183
f := fields.Index(i)
184-
offsetEmbed := uintptr(i) << 1
185-
if f.Get("embedded").Bool() {
186-
offsetEmbed |= 1
187-
}
188184
reflectFields[i] = structField{
189-
name: newName(internalStr(f.Get("name")), internalStr(f.Get("tag")), f.Get("exported").Bool()),
190-
typ: reflectType(f.Get("typ")),
191-
offsetEmbed: offsetEmbed,
185+
name: newName(internalStr(f.Index.Get("name")), internalStr(f.Get("tag")), f.Get("exported").Bool(), f.Get("embedded").Bool()),
186+
typ: reflectType(f.Get("typ")),
187+
offset: uintptr(i),
192188
}
193189
}
194190
setKindType(rt, &structType{
195191
rtype: *rt,
196-
pkgPath: newName(internalStr(typ.Get("pkgPath")), "", false),
192+
pkgPath: newName(internalStr(typ.Get("pkgPath")), "", false, false),
197193
fields: reflectFields,
198194
})
199195
}
@@ -257,6 +253,7 @@ type nameData struct {
257253
name string
258254
tag string
259255
exported bool
256+
embedded bool
260257
pkgPath string
261258
}
262259

@@ -266,16 +263,18 @@ func (n name) name() (s string) { return nameMap[n.bytes].name }
266263
func (n name) tag() (s string) { return nameMap[n.bytes].tag }
267264
func (n name) pkgPath() string { return nameMap[n.bytes].pkgPath }
268265
func (n name) isExported() bool { return nameMap[n.bytes].exported }
266+
func (n name) embedded() bool { return nameMap[n.bytes].embedded }
269267
func (n name) setPkgPath(pkgpath string) {
270268
nameMap[n.bytes].pkgPath = pkgpath
271269
}
272270

273-
func newName(n, tag string, exported bool) name {
271+
func newName(n, tag string, exported, embedded bool) name {
274272
b := new(byte)
275273
nameMap[b] = &nameData{
276274
name: n,
277275
tag: tag,
278276
exported: exported,
277+
embedded: embedded,
279278
}
280279
return name{
281280
bytes: b,

0 commit comments

Comments
 (0)