Skip to content

Commit 0662b09

Browse files
updating reflect and abi
1 parent c613514 commit 0662b09

File tree

6 files changed

+137
-290
lines changed

6 files changed

+137
-290
lines changed

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

Lines changed: 97 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ import (
88

99
//gopherjs:new
1010
const (
11-
idJsType = "_jsType"
12-
idReflectType = "_reflectType"
13-
idKindType = "kindType"
14-
idRtype = "_rtype"
11+
idJsType = `jsType`
12+
idReflectType = `reflectType`
13+
idKindType = `kindType`
14+
idUncommonType = `uncommonType`
1515
)
1616

17-
//gopherjs:new
18-
var UncommonTypeMap = make(map[*Type]*UncommonType)
19-
2017
//gopherjs:replace
2118
func (t *Type) Uncommon() *UncommonType {
22-
return UncommonTypeMap[t]
19+
obj := js.InternalObject(t).Get(idUncommonType)
20+
if obj == js.Undefined {
21+
return nil
22+
}
23+
return (*UncommonType)(unsafe.Pointer(obj.Unsafe()))
24+
}
25+
26+
//gopherjs:add
27+
func (t *Type) setUncommon(ut *UncommonType) {
28+
js.InternalObject(t).Set(idUncommonType, js.InternalObject(ut))
2329
}
2430

2531
//gopherjs:replace
@@ -72,6 +78,7 @@ type Name struct {
7278
tag string
7379
exported bool
7480
embedded bool
81+
pkgPath string
7582
}
7683

7784
//gopherjs:replace
@@ -104,6 +111,14 @@ func (n Name) Data(off int) *byte
104111
//gopherjs:purge Used for byte encoding of name, not used in JS
105112
func (n Name) ReadVarint(off int) (int, int)
106113

114+
//gopherjs:add
115+
func (n Name) PkgPath() string { return n.pkgPath }
116+
117+
//gopherjs:add
118+
func (n Name) SetPkgPath(pkgpath string) {
119+
n.pkgPath = pkgpath
120+
}
121+
107122
//gopherjs:replace
108123
func NewName(n, tag string, exported, embedded bool) Name {
109124
return Name{
@@ -114,41 +129,92 @@ func NewName(n, tag string, exported, embedded bool) Name {
114129
}
115130
}
116131

117-
//gopherjs:new
118-
var nameOffList []Name
132+
// NewMethodName creates name instance for a method.
133+
//
134+
// Input object is expected to be an entry of the "methods" list of the
135+
// corresponding JS type.
136+
func NewMethodName(m *js.Object) Name {
137+
return Name{
138+
name: internalStr(m.Get("name")),
139+
tag: "",
140+
pkgPath: internalStr(m.Get("pkg")),
141+
exported: internalStr(m.Get("pkg")) == "",
142+
}
143+
}
144+
145+
// Instead of using this as an offset from a pointer to look up a name,
146+
// just store the name as a pointer.
147+
//
148+
//gopherjs:replace
149+
type NameOff *Name
119150

151+
// Added to mirror the rtype's nameOff method to keep how the nameOff is
152+
// created and read in one spot of the code.
153+
//
120154
//gopherjs:new
121155
func (typ *Type) NameOff(off NameOff) Name {
122-
return nameOffList[int(off)]
156+
return *off
123157
}
124158

159+
// Added to mirror the resolveReflectName method in reflect
160+
//
125161
//gopherjs:new
126-
func newNameOff(n Name) NameOff {
127-
i := len(nameOffList)
128-
nameOffList = append(nameOffList, n)
129-
return NameOff(i)
162+
func ResolveReflectName(n Name) NameOff {
163+
return &n
130164
}
131165

132-
//gopherjs:new
133-
var typeOffList []*Type
166+
// Instead of using this as an offset from a pointer to look up a type,
167+
// just store the type as a pointer.
168+
//
169+
//gopherjs:replace
170+
type TypeOff *Type
134171

172+
// Added to mirror the rtype's typeOff method to keep how the typeOff is
173+
// created and read in one spot of the code.
174+
//
135175
//gopherjs:new
136176
func (typ *Type) TypeOff(off TypeOff) *Type {
137-
return typeOffList[int(off)]
177+
return off
138178
}
139179

180+
// Added to mirror the resolveReflectType method in reflect
181+
//
182+
//gopherjs:new
183+
func ResolveReflectType(t *Type) TypeOff {
184+
return t
185+
}
186+
187+
// Instead of using this as an offset from a pointer to look up a pointer,
188+
// just store the paointer itself.
189+
//
190+
//gopherjs:replace
191+
type TextOff unsafe.Pointer
192+
193+
// Added to mirror the rtype's textOff method to keep how the textOff is
194+
// created and read in one spot of the code.
195+
//
140196
//gopherjs:new
141-
func newTypeOff(t *Type) TypeOff {
142-
i := len(typeOffList)
143-
typeOffList = append(typeOffList, t)
144-
return TypeOff(i)
197+
func (typ *Type) TextOff(off TextOff) unsafe.Pointer {
198+
return unsafe.Pointer(off)
199+
}
200+
201+
// Added to mirror the resolveReflectText method in reflect
202+
//
203+
//gopherjs:new
204+
func ResolveReflectText(ptr unsafe.Pointer) TextOff {
205+
return TextOff(ptr)
145206
}
146207

147208
//gopherjs:new
148209
func (typ *Type) JsType() *js.Object {
149210
return js.InternalObject(typ).Get(idJsType)
150211
}
151212

213+
//gopherjs:new
214+
func (typ *Type) setJsType(t *js.Object) {
215+
js.InternalObject(typ).Set(idJsType, typ)
216+
}
217+
152218
//gopherjs:new
153219
func (typ *Type) PtrTo() *Type {
154220
return ReflectType(js.Global.Call("$ptrType", typ.JsType()))
@@ -177,7 +243,7 @@ func ReflectType(typ *js.Object) *Type {
177243
abiTyp := &Type{
178244
Size_: uintptr(typ.Get("size").Int()),
179245
Kind_: uint8(typ.Get("kind").Int()),
180-
Str: newNameOff(NewName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool(), false)),
246+
Str: ResolveReflectName(NewName(internalStr(typ.Get("string")), "", typ.Get("exported").Bool(), false)),
181247
}
182248
js.InternalObject(abiTyp).Set(idJsType, typ)
183249
typ.Set(idReflectType, js.InternalObject(abiTyp))
@@ -196,8 +262,8 @@ func ReflectType(typ *js.Object) *Type {
196262
continue
197263
}
198264
reflectMethods = append(reflectMethods, Method{
199-
Name: newNameOff(NewName(internalStr(m.Get("name")), "", exported, false)),
200-
Mtyp: newTypeOff(ReflectType(m.Get("typ"))),
265+
Name: ResolveReflectName(NewName(internalStr(m.Get("name")), "", exported, false)),
266+
Mtyp: ResolveReflectType(ReflectType(m.Get("typ"))),
201267
})
202268
}
203269
xcount := uint16(len(reflectMethods))
@@ -208,18 +274,17 @@ func ReflectType(typ *js.Object) *Type {
208274
continue
209275
}
210276
reflectMethods = append(reflectMethods, Method{
211-
Name: newNameOff(NewName(internalStr(m.Get("name")), "", exported, false)),
212-
Mtyp: newTypeOff(ReflectType(m.Get("typ"))),
277+
Name: ResolveReflectName(NewName(internalStr(m.Get("name")), "", exported, false)),
278+
Mtyp: ResolveReflectType(ReflectType(m.Get("typ"))),
213279
})
214280
}
215281
ut := &UncommonType{
216-
PkgPath: newNameOff(NewName(internalStr(typ.Get("pkg")), "", false, false)),
282+
PkgPath: ResolveReflectName(NewName(internalStr(typ.Get("pkg")), "", false, false)),
217283
Mcount: uint16(methodSet.Length()),
218284
Xcount: xcount,
219285
Methods_: reflectMethods,
220286
}
221-
UncommonTypeMap[abiTyp] = ut
222-
js.InternalObject(ut).Set(idJsType, typ)
287+
abiTyp.setUncommon(ut)
223288
}
224289

225290
switch abiTyp.Kind() {
@@ -270,8 +335,8 @@ func ReflectType(typ *js.Object) *Type {
270335
for i := range imethods {
271336
m := methods.Index(i)
272337
imethods[i] = Imethod{
273-
Name: newNameOff(NewName(internalStr(m.Get("name")), "", internalStr(m.Get("pkg")) == "", false)),
274-
Typ: newTypeOff(ReflectType(m.Get("typ"))),
338+
Name: ResolveReflectName(NewName(internalStr(m.Get("name")), "", internalStr(m.Get("pkg")) == "", false)),
339+
Typ: ResolveReflectType(ReflectType(m.Get("typ"))),
275340
}
276341
}
277342
setKindType(abiTyp, &InterfaceType{
@@ -320,5 +385,4 @@ func ReflectType(typ *js.Object) *Type {
320385
//gopherjs:new
321386
func setKindType(abiTyp *Type, kindType any) {
322387
js.InternalObject(abiTyp).Set(idKindType, js.InternalObject(kindType))
323-
js.InternalObject(kindType).Set(idRtype, js.InternalObject(abiTyp))
324388
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package reflectlite
44

55
import "github.com/gopherjs/gopherjs/js"
66

7+
//gopherjs:replace
78
func Swapper(slice any) func(i, j int) {
89
v := ValueOf(slice)
910
if v.Kind() != Slice {

0 commit comments

Comments
 (0)