Skip to content

Commit e08d727

Browse files
cleaning up some changes
1 parent f0230f7 commit e08d727

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

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

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func init() {
2525
used(structType{})
2626
}
2727

28-
// TODO(grantnelson-wf): After this is in the go1.21 branch, remove it, this is just to minimize diffs.
28+
// TODO(grantnelson-wf): This is just to minimize diffs. After this is merged into the go1.21 branch, remove it.
2929
func jsType(typ *abi.Type) *js.Object {
3030
return typ.JsType()
3131
}
@@ -40,11 +40,6 @@ func jsPtrTo(t Type) *js.Object {
4040
return toAbiType(t).JsPtrTo()
4141
}
4242

43-
// TODO(grantnelson-wf): After this is in the go1.21 branch, remove it, this is just to minimize diffs.
44-
func reflectType(typ *js.Object) rtype {
45-
return toRType(abi.ReflectType(typ))
46-
}
47-
4843
//gopherjs:purge The name type is mostly unused, replaced by abi.Name, except in pkgPath which we don't implement.
4944
type name struct {
5045
bytes *byte
@@ -69,40 +64,29 @@ func (t rtype) typeOff(off typeOff) *abi.Type {
6964
return t.TypeOff(off)
7065
}
7166

72-
// TODO(grantnelson-wf): After this is in the go1.21 branch, remove it, this is just to minimize diffs.
73-
func isWrapped(typ *abi.Type) bool {
74-
return typ.IsWrapped()
75-
}
76-
77-
// TODO(grantnelson-wf): After this is in the go1.21 branch, remove it, this is just to minimize diffs.
78-
func copyStruct(dst, src *js.Object, typ *abi.Type) {
79-
abi.CopyStruct(dst, src, typ)
80-
}
81-
8267
//gopherjs:new
83-
func makeValue(t Type, v *js.Object, fl flag) Value {
84-
rt := t.common()
68+
func makeValue(t *abi.Type, v *js.Object, fl flag) Value {
8569
switch t.Kind() {
8670
case abi.Array, abi.Struct, abi.Pointer:
87-
return Value{rt, unsafe.Pointer(v.Unsafe()), fl | flag(t.Kind())}
71+
return Value{t, unsafe.Pointer(v.Unsafe()), fl | flag(t.Kind())}
8872
}
89-
return Value{rt, unsafe.Pointer(js.Global.Call("$newDataPointer", v, rt.JsPtrTo()).Unsafe()), fl | flag(t.Kind()) | flagIndir}
73+
return Value{t, unsafe.Pointer(js.Global.Call("$newDataPointer", v, t.JsPtrTo()).Unsafe()), fl | flag(t.Kind()) | flagIndir}
9074
}
9175

9276
//gopherjs:replace
9377
func TypeOf(i any) Type {
9478
if i == nil {
9579
return nil
9680
}
97-
return reflectType(js.InternalObject(i).Get("constructor"))
81+
return toRType(abi.ReflectType(js.InternalObject(i).Get("constructor")))
9882
}
9983

10084
//gopherjs:replace
10185
func ValueOf(i any) Value {
10286
if i == nil {
10387
return Value{}
10488
}
105-
return makeValue(reflectType(js.InternalObject(i).Get("constructor")), js.InternalObject(i).Get("$val"), 0)
89+
return makeValue(abi.ReflectType(js.InternalObject(i).Get("constructor")), js.InternalObject(i).Get("$val"), 0)
10690
}
10791

10892
//gopherjs:replace
@@ -140,7 +124,7 @@ func methodReceiver(op string, v Value, i int) (fn unsafe.Pointer) {
140124
prop = js.Global.Call("$methodSet", jsType(v.typ)).Index(i).Get("prop").String()
141125
}
142126
rcvr := v.object()
143-
if isWrapped(v.typ) {
127+
if v.typ.IsWrapped() {
144128
rcvr = jsType(v.typ).New(rcvr)
145129
}
146130
fn = unsafe.Pointer(rcvr.Get(prop).Unsafe())
@@ -157,10 +141,10 @@ func valueInterface(v Value) any {
157141
v = makeMethodValue("Interface", v)
158142
}
159143

160-
if isWrapped(v.typ) {
144+
if v.typ.IsWrapped() {
161145
if v.flag&flagIndir != 0 && v.Kind() == abi.Struct {
162146
cv := jsType(v.typ).Call("zero")
163-
copyStruct(cv, v.object(), v.typ)
147+
abi.CopyStruct(cv, v.object(), v.typ)
164148
return any(unsafe.Pointer(jsType(v.typ).New(cv).Unsafe()))
165149
}
166150
return any(unsafe.Pointer(jsType(v.typ).New(v.object()).Unsafe()))
@@ -173,10 +157,12 @@ func ifaceE2I(t *abi.Type, src any, dst unsafe.Pointer) {
173157
abi.IfaceE2I(t, src, dst)
174158
}
175159

160+
// TODO(grantnelson-wf): methodName returns the name of the calling method,
161+
// assumed to be two stack frames above. Determine if we can get this value now
162+
// and if methodName is needed
163+
//
176164
//gopherjs:replace
177165
func methodName() string {
178-
// TODO(grantnelson-wf): methodName returns the name of the calling method,
179-
// assumed to be two stack frames above.
180166
return "?FIXME?"
181167
}
182168

@@ -188,7 +174,7 @@ func makeMethodValue(op string, v Value) Value {
188174

189175
fn := methodReceiver(op, v, int(v.flag)>>flagMethodShift)
190176
rcvr := v.object()
191-
if isWrapped(v.typ) {
177+
if v.typ.IsWrapped() {
192178
rcvr = jsType(v.typ).New(rcvr)
193179
}
194180
fv := js.MakeFunc(func(this *js.Object, arguments []*js.Object) any {

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (v Value) assignTo(context string, dst *abi.Type, target unsafe.Pointer) Va
7777
func (v Value) IsNil() bool {
7878
switch k := v.kind(); k {
7979
case abi.Pointer, abi.Slice:
80-
return v.object() == v.typ.JsType().Get("nil")
80+
return v.object() == jsType(v.typ).Get("nil")
8181
case abi.Chan:
8282
return v.object() == js.Global.Get("$chanNil")
8383
case abi.Func:
@@ -112,16 +112,16 @@ func (v Value) Len() int {
112112
//gopherjs:replace
113113
func (v Value) Set(x Value) {
114114
v.mustBeAssignable()
115-
x.mustBeExported()
115+
x.mustBeExported() // TODO(grantnelson-wf): This uses the unimplemented `methodName()`. When methodName is fixed we can use Set to test.
116116
x = x.assignTo("reflect.Set", v.typ, nil)
117117
if v.flag&flagIndir != 0 {
118118
switch v.typ.Kind() {
119119
case abi.Array:
120-
v.typ.JsType().Call("copy", js.InternalObject(v.ptr), js.InternalObject(x.ptr))
120+
jsType(v.typ).Call("copy", js.InternalObject(v.ptr), js.InternalObject(x.ptr))
121121
case abi.Interface:
122122
js.InternalObject(v.ptr).Call("$set", js.InternalObject(valueInterface(x)))
123123
case abi.Struct:
124-
copyStruct(js.InternalObject(v.ptr), js.InternalObject(x.ptr), v.typ)
124+
abi.CopyStruct(js.InternalObject(v.ptr), js.InternalObject(x.ptr), v.typ)
125125
default:
126126
js.InternalObject(v.ptr).Call("$set", x.object())
127127
}
@@ -138,7 +138,7 @@ func (v Value) Elem() Value {
138138
if val == js.Global.Get("$ifaceNil") {
139139
return Value{}
140140
}
141-
typ := reflectType(val.Get("constructor"))
141+
typ := abi.ReflectType(val.Get("constructor"))
142142
return makeValue(typ, val.Get("$val"), v.flag.ro())
143143

144144
case abi.Pointer:
@@ -149,18 +149,14 @@ func (v Value) Elem() Value {
149149
tt := v.typ.PtrType()
150150
fl := v.flag&flagRO | flagIndir | flagAddr
151151
fl |= flag(tt.Elem.Kind())
152-
return Value{
153-
typ: tt.Elem,
154-
ptr: unsafe.Pointer(abi.WrapJsObject(tt.Elem, val).Unsafe()),
155-
flag: fl,
156-
}
152+
return Value{tt.Elem, unsafe.Pointer(abi.WrapJsObject(tt.Elem, val).Unsafe()), fl}
157153

158154
default:
159155
panic(&ValueError{"reflect.Value.Elem", k})
160156
}
161157
}
162158

163-
//gopherjs:new This is added for export_test but is otherwise unused.
159+
// TODO(grantnelson-wf): Move this to export_test since it is defined there in the original code and only used for tests.
164160
func (v Value) Field(i int) Value {
165161
tt := v.typ.StructType()
166162
if tt == nil {
@@ -209,5 +205,5 @@ func (v Value) Field(i int) Value {
209205
js.InternalObject(func(x *js.Object) { s.Set(prop, abi.UnwrapJsObject(typ, x)) }),
210206
).Unsafe()), fl}
211207
}
212-
return makeValue(toRType(typ), abi.WrapJsObject(typ, s.Get(prop)), fl)
208+
return makeValue(typ, abi.WrapJsObject(typ, s.Get(prop)), fl)
213209
}

0 commit comments

Comments
 (0)