Skip to content

Commit 5e955ea

Browse files
working on reflectlite and abi
1 parent 6698cc2 commit 5e955ea

File tree

4 files changed

+78
-92
lines changed

4 files changed

+78
-92
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package abi
2+
3+
var UncommonTypeMap = make(map[*Type]*UncommonType)
4+
5+
func (t *Type) Uncommon() *UncommonType {
6+
return UncommonTypeMap[t]
7+
}
8+
9+
type UncommonType struct {
10+
PkgPath NameOff // import path
11+
Mcount uint16 // method count
12+
Xcount uint16 // exported method count
13+
Methods_ []Method
14+
}
15+
16+
func (t *UncommonType) Methods() []Method {
17+
return t.Methods_
18+
}
19+
20+
func (t *UncommonType) ExportedMethods() []Method {
21+
return t.Methods_[:t.Xcount:t.Xcount]
22+
}

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

Lines changed: 28 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package reflectlite
55
import (
66
"unsafe"
77

8+
"internal/abi"
9+
810
"github.com/gopherjs/gopherjs/js"
911
)
1012

@@ -14,8 +16,6 @@ func init() {
1416
// avoid dead code elimination
1517
used := func(i any) {}
1618
used(rtype{})
17-
used(uncommonType{})
18-
used(method{})
1919
used(arrayType{})
2020
used(chanType{})
2121
used(funcType{})
@@ -25,7 +25,6 @@ func init() {
2525
used(sliceType{})
2626
used(structType{})
2727
used(imethod{})
28-
used(structField{})
2928

3029
initialized = true
3130
uint8Type = TypeOf(uint8(0)).(*rtype) // set for real
@@ -47,29 +46,29 @@ func jsType(typ Type) *js.Object {
4746
func reflectType(typ *js.Object) *rtype {
4847
if typ.Get(idReflectType) == js.Undefined {
4948
rt := &rtype{
50-
size: uintptr(typ.Get("size").Int()),
51-
kind: uint8(typ.Get("kind").Int()),
52-
str: newNameOff(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool(), false)),
49+
Size: uintptr(typ.Get("size").Int()),
50+
Kind: uint8(typ.Get("kind").Int()),
51+
Str: newNameOff(newName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool(), false)),
5352
}
5453
js.InternalObject(rt).Set(idJsType, typ)
5554
typ.Set(idReflectType, js.InternalObject(rt))
5655

5756
methodSet := js.Global.Call("$methodSet", typ)
5857
if methodSet.Length() != 0 || typ.Get("named").Bool() {
59-
rt.tflag |= tflagUncommon
58+
rt.TFlag |= abi.TFlagUncommon
6059
if typ.Get("named").Bool() {
61-
rt.tflag |= tflagNamed
60+
rt.TFlag |= abi.TFlagNamed
6261
}
63-
var reflectMethods []method
62+
var reflectMethods []abi.Method
6463
for i := 0; i < methodSet.Length(); i++ { // Exported methods first.
6564
m := methodSet.Index(i)
6665
exported := internalStr(m.Get("pkg")) == ""
6766
if !exported {
6867
continue
6968
}
70-
reflectMethods = append(reflectMethods, method{
71-
name: newNameOff(newName(internalStr(m.Get("name")), "", exported, false)),
72-
mtyp: newTypeOff(reflectType(m.Get("typ"))),
69+
reflectMethods = append(reflectMethods, abi.Method{
70+
Name: newNameOff(newName(internalStr(m.Get("name")), "", exported, false)),
71+
Mtyp: newTypeOff(reflectType(m.Get("typ"))),
7372
})
7473
}
7574
xcount := uint16(len(reflectMethods))
@@ -79,18 +78,18 @@ func reflectType(typ *js.Object) *rtype {
7978
if exported {
8079
continue
8180
}
82-
reflectMethods = append(reflectMethods, method{
83-
name: newNameOff(newName(internalStr(m.Get("name")), "", exported, false)),
84-
mtyp: newTypeOff(reflectType(m.Get("typ"))),
81+
reflectMethods = append(reflectMethods, abi.Method{
82+
Name: newNameOff(newName(internalStr(m.Get("name")), "", exported, false)),
83+
Mtyp: newTypeOff(reflectType(m.Get("typ"))),
8584
})
8685
}
87-
ut := &uncommonType{
88-
pkgPath: newNameOff(newName(internalStr(typ.Get("pkg")), "", false, false)),
89-
mcount: uint16(methodSet.Length()),
90-
xcount: xcount,
91-
_methods: reflectMethods,
86+
ut := &abi.UncommonType{
87+
PkgPath: newNameOff(newName(internalStr(typ.Get("pkg")), "", false, false)),
88+
Mcount: uint16(methodSet.Length()),
89+
Xcount: xcount,
90+
Methods_: reflectMethods,
9291
}
93-
uncommonTypeMap[rt] = ut
92+
abi.UncommonTypeMap[rt] = ut
9493
js.InternalObject(ut).Set(idJsType, typ)
9594
}
9695

@@ -164,13 +163,13 @@ func reflectType(typ *js.Object) *rtype {
164163
})
165164
case Struct:
166165
fields := typ.Get("fields")
167-
reflectFields := make([]structField, fields.Length())
166+
reflectFields := make([]abi.StructField, fields.Length())
168167
for i := range reflectFields {
169168
f := fields.Index(i)
170-
reflectFields[i] = structField{
171-
name: newName(internalStr(f.Get("name")), internalStr(f.Get("tag")), f.Get("exported").Bool(), f.Get("embedded").Bool()),
172-
typ: reflectType(f.Get("typ")),
173-
offset: uintptr(i),
169+
reflectFields[i] = abi.StructField{
170+
Name: newName(internalStr(f.Get("name")), internalStr(f.Get("tag")), f.Get("exported").Bool(), f.Get("embedded").Bool()),
171+
Typ: reflectType(f.Get("typ")),
172+
Offset: uintptr(i),
174173
}
175174
}
176175
setKindType(rt, &structType{
@@ -189,29 +188,6 @@ func setKindType(rt *rtype, kindType any) {
189188
js.InternalObject(kindType).Set(idRtype, js.InternalObject(rt))
190189
}
191190

192-
type uncommonType struct {
193-
pkgPath nameOff
194-
mcount uint16
195-
xcount uint16
196-
moff uint32
197-
198-
_methods []method
199-
}
200-
201-
func (t *uncommonType) methods() []method {
202-
return t._methods
203-
}
204-
205-
func (t *uncommonType) exportedMethods() []method {
206-
return t._methods[:t.xcount:t.xcount]
207-
}
208-
209-
var uncommonTypeMap = make(map[*rtype]*uncommonType)
210-
211-
func (t *rtype) uncommon() *uncommonType {
212-
return uncommonTypeMap[t]
213-
}
214-
215191
type funcType struct {
216192
rtype `reflect:"func"`
217193
inCount uint16
@@ -229,10 +205,6 @@ func (t *funcType) out() []*rtype {
229205
return t._out
230206
}
231207

232-
type name struct {
233-
bytes *byte
234-
}
235-
236208
type nameData struct {
237209
name string
238210
tag string
@@ -244,7 +216,6 @@ var nameMap = make(map[*byte]*nameData)
244216

245217
func (n name) name() (s string) { return nameMap[n.bytes].name }
246218
func (n name) tag() (s string) { return nameMap[n.bytes].tag }
247-
func (n name) pkgPath() string { return "" }
248219
func (n name) isExported() bool { return nameMap[n.bytes].exported }
249220
func (n name) embedded() bool { return nameMap[n.bytes].embedded }
250221

@@ -261,29 +232,7 @@ func newName(n, tag string, exported, embedded bool) name {
261232
}
262233
}
263234

264-
var nameOffList []name
265-
266-
func (t *rtype) nameOff(off nameOff) name {
267-
return nameOffList[int(off)]
268-
}
269-
270-
func newNameOff(n name) nameOff {
271-
i := len(nameOffList)
272-
nameOffList = append(nameOffList, n)
273-
return nameOff(i)
274-
}
275-
276-
var typeOffList []*rtype
277-
278-
func (t *rtype) typeOff(off typeOff) *rtype {
279-
return typeOffList[int(off)]
280-
}
281-
282-
func newTypeOff(t *rtype) typeOff {
283-
i := len(typeOffList)
284-
typeOffList = append(typeOffList, t)
285-
return typeOff(i)
286-
}
235+
func pkgPath(n abi.Name) string { return "" }
287236

288237
func internalStr(strObj *js.Object) string {
289238
var c struct{ str string }
@@ -390,7 +339,7 @@ func Zero(typ Type) Value {
390339
return makeValue(typ, jsType(typ).Call("zero"), 0)
391340
}
392341

393-
func unsafe_New(typ *rtype) unsafe.Pointer {
342+
func unsafe_New(typ *abi.Type) unsafe.Pointer {
394343
switch typ.Kind() {
395344
case Struct:
396345
return unsafe.Pointer(jsType(typ).Get("ptr").New().Unsafe())
@@ -457,7 +406,7 @@ func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value {
457406
return Value{t, unsafe.Pointer(fv.Unsafe()), flag(Func)}
458407
}
459408

460-
func typedmemmove(t *rtype, dst, src unsafe.Pointer) {
409+
func typedmemmove(t *abi.Type, dst, src unsafe.Pointer) {
461410
js.InternalObject(dst).Call("$set", js.InternalObject(src).Call("$get"))
462411
}
463412

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,36 @@ package reflectlite
55
import (
66
"unsafe"
77

8+
"internal/abi"
9+
810
"github.com/gopherjs/gopherjs/js"
911
)
1012

11-
func (t *rtype) Comparable() bool {
13+
var nameOffList []abi.Name
14+
15+
func (t rtype) nameOff(off nameOff) abi.Name {
16+
return nameOffList[int(off)]
17+
}
18+
19+
func newNameOff(n abi.Name) nameOff {
20+
i := len(nameOffList)
21+
nameOffList = append(nameOffList, n)
22+
return nameOff(i)
23+
}
24+
25+
var typeOffList []*abi.Type
26+
27+
func (t *rtype) typeOff(off typeOff) *abi.Type {
28+
return typeOffList[int(off)]
29+
}
30+
31+
func newTypeOff(t *rtype) typeOff {
32+
i := len(typeOffList)
33+
typeOffList = append(typeOffList, t)
34+
return typeOff(i)
35+
}
36+
37+
func (t rtype) Comparable() bool {
1238
switch t.Kind() {
1339
case Func, Slice, Map:
1440
return false
@@ -37,17 +63,6 @@ func (t *rtype) kindType() *rtype {
3763
return (*rtype)(unsafe.Pointer(js.InternalObject(t).Get(idKindType)))
3864
}
3965

40-
func (t *rtype) Field(i int) structField {
41-
if t.Kind() != Struct {
42-
panic("reflect: Field of non-struct type")
43-
}
44-
tt := (*structType)(unsafe.Pointer(t))
45-
if i < 0 || i >= len(tt.fields) {
46-
panic("reflect: Field index out of bounds")
47-
}
48-
return tt.fields[i]
49-
}
50-
5166
func (t *rtype) Key() Type {
5267
if t.Kind() != Map {
5368
panic("reflect: Key of non-map type")

tests/linkname_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ func TestLinknameReflectName(t *testing.T) {
6262
off := resolveReflectName(newName(info, "", false))
6363
n := rtype_nameOff(nil, off)
6464
if s := name_name(n); s != info {
65-
t.Fatalf("to reflect.name got %q: want %q", s, info)
65+
t.Fatalf("to reflect's abi.Name got %q: want %q", s, info)
6666
}
6767
}

0 commit comments

Comments
 (0)