Skip to content

Commit 1e9a41b

Browse files
working on reflectlite and abi
1 parent ff4bd26 commit 1e9a41b

File tree

3 files changed

+80
-72
lines changed

3 files changed

+80
-72
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ var (
3939
idRtype = "_rtype"
4040
)
4141

42-
func jsType(typ Type) *js.Object {
42+
func jsType(typ *abi.Type) *js.Object {
4343
return js.InternalObject(typ).Get(idJsType)
4444
}
4545

46+
func jsPtrTo(typ *abi.Type) *js.Object {
47+
return jsType(PtrTo(toRType(typ)).(*rtype).Type)
48+
}
49+
4650
func reflectType(typ *js.Object) *rtype {
4751
if typ.Get(idReflectType) == js.Undefined {
4852
rt := &rtype{
@@ -223,19 +227,19 @@ func internalStr(strObj *js.Object) string {
223227
return c.str
224228
}
225229

226-
func isWrapped(typ Type) bool {
230+
func isWrapped(typ *abi.Type) bool {
227231
return jsType(typ).Get("wrapped").Bool()
228232
}
229233

230-
func copyStruct(dst, src *js.Object, typ Type) {
234+
func copyStruct(dst, src *js.Object, typ *abi.Type) {
231235
fields := jsType(typ).Get("fields")
232236
for i := 0; i < fields.Length(); i++ {
233237
prop := fields.Index(i).Get("prop").String()
234238
dst.Set(prop, src.Get(prop))
235239
}
236240
}
237241

238-
func makeValue(t Type, v *js.Object, fl flag) Value {
242+
func makeValue(t *abi.Type, v *js.Object, fl flag) Value {
239243
rt := t.common()
240244
switch t.Kind() {
241245
case abi.Array, abi.Struct, abi.Pointer:
@@ -422,7 +426,7 @@ func mapaccess(t *rtype, m, key unsafe.Pointer) unsafe.Pointer {
422426
if entry == js.Undefined {
423427
return nil
424428
}
425-
return unsafe.Pointer(js.Global.Call("$newDataPointer", entry.Get("v"), jsType(PtrTo(t.Elem()))).Unsafe())
429+
return unsafe.Pointer(js.Global.Call("$newDataPointer", entry.Get("v"), jsPtrTo(t.Elem())).Unsafe())
426430
}
427431

428432
func mapassign(t *rtype, m, key, val unsafe.Pointer) {
@@ -493,7 +497,7 @@ func mapiterkey(it unsafe.Pointer) unsafe.Pointer {
493497
// Record the key-value pair for later accesses.
494498
iter.last = kv
495499
}
496-
return unsafe.Pointer(js.Global.Call("$newDataPointer", kv.Get("k"), jsType(PtrTo(iter.t.(TypeEx).Key()))).Unsafe())
500+
return unsafe.Pointer(js.Global.Call("$newDataPointer", kv.Get("k"), jsPtrTo(iter.t.(TypeEx).Key())).Unsafe())
497501
}
498502

499503
func mapiternext(it unsafe.Pointer) {
@@ -519,7 +523,7 @@ func cvtDirect(v Value, typ Type) Value {
519523
slice.Set("$offset", srcVal.Get("$offset"))
520524
slice.Set("$length", srcVal.Get("$length"))
521525
slice.Set("$capacity", srcVal.Get("$capacity"))
522-
val = js.Global.Call("$newDataPointer", slice, jsType(PtrTo(typ)))
526+
val = js.Global.Call("$newDataPointer", slice, jsPtrTo(typ))
523527
case abi.Pointer:
524528
if typ.Elem().Kind() == abi.Struct {
525529
if typ.Elem() == v.typ.Elem() {
@@ -635,7 +639,7 @@ func valueInterface(v Value) any {
635639
return any(unsafe.Pointer(v.object().Unsafe()))
636640
}
637641

638-
func ifaceE2I(t *rtype, src any, dst unsafe.Pointer) {
642+
func ifaceE2I(t *abi.Type, src any, dst unsafe.Pointer) {
639643
js.InternalObject(dst).Call("$set", js.InternalObject(src))
640644
}
641645

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ func newTypeOff(t *rtype) typeOff {
3636

3737
func (t rtype) Comparable() bool {
3838
switch t.Kind() {
39-
case Func, Slice, Map:
39+
case abi.Func, abi.Slice, abi.Map:
4040
return false
41-
case Array:
41+
case abi.Array:
4242
return t.Elem().Comparable()
43-
case Struct:
43+
case abi.Struct:
4444
for i := 0; i < t.NumField(); i++ {
4545
ft := t.Field(i)
4646
if !ft.typ.Comparable() {
@@ -52,7 +52,7 @@ func (t rtype) Comparable() bool {
5252
}
5353

5454
func (t *rtype) IsVariadic() bool {
55-
if t.Kind() != Func {
55+
if t.Kind() != abi.Func {
5656
panic("reflect: IsVariadic of non-func type")
5757
}
5858
tt := (*funcType)(unsafe.Pointer(t))
@@ -64,23 +64,23 @@ func (t *rtype) kindType() *rtype {
6464
}
6565

6666
func (t *rtype) Key() Type {
67-
if t.Kind() != Map {
67+
if t.Kind() != abi.Map {
6868
panic("reflect: Key of non-map type")
6969
}
7070
tt := (*mapType)(unsafe.Pointer(t))
7171
return toType(tt.key)
7272
}
7373

7474
func (t *rtype) NumField() int {
75-
if t.Kind() != Struct {
75+
if t.Kind() != abi.Struct {
7676
panic("reflect: NumField of non-struct type")
7777
}
7878
tt := (*structType)(unsafe.Pointer(t))
7979
return len(tt.fields)
8080
}
8181

8282
func (t *rtype) Method(i int) (m Method) {
83-
if t.Kind() == Interface {
83+
if t.Kind() == abi.Interface {
8484
tt := (*interfaceType)(unsafe.Pointer(t))
8585
return tt.Method(i)
8686
}

0 commit comments

Comments
 (0)