MCPcopy
hub / github.com/gopherjs/gopherjs / Field

Method Field

compiler/natives/src/internal/reflectlite/export_test.go:25–74  ·  view source on GitHub ↗

gopherjs:new

(i int)

Source from the content-addressed store, hash-verified

23
24//gopherjs:new
25func (v Value) Field(i int) Value {
26 tt := v.typ.StructType()
27 if tt == nil {
28 panic(&ValueError{"reflect.Value.Field", v.kind()})
29 }
30
31 if uint(i) >= uint(len(tt.Fields)) {
32 panic("reflect: Field index out of range")
33 }
34
35 prop := jsType(v.typ).Get("fields").Index(i).Get("prop").String()
36 field := &tt.Fields[i]
37 typ := field.Typ
38
39 fl := v.flag&(flagStickyRO|flagIndir|flagAddr) | flag(typ.Kind())
40 if !field.Name.IsExported() {
41 if field.Embedded() {
42 fl |= flagEmbedRO
43 } else {
44 fl |= flagStickyRO
45 }
46 }
47
48 if tag := tt.Fields[i].Name.Tag(); tag != "" && i != 0 {
49 if jsTag := structTagGet(tag, `js`); jsTag != "" {
50 for {
51 v = v.Field(0)
52 if v.typ == abi.JsObjectPtr {
53 o := v.object().Get("object")
54 return Value{typ, unsafe.Pointer(typ.JsPtrTo().New(
55 js.InternalObject(func() *js.Object { return js.Global.Call("$internalize", o.Get(jsTag), jsType(typ)) }),
56 js.InternalObject(func(x *js.Object) { o.Set(jsTag, js.Global.Call("$externalize", x, jsType(typ))) }),
57 ).Unsafe()), fl}
58 }
59 if v.typ.Kind() == abi.Pointer {
60 v = v.Elem()
61 }
62 }
63 }
64 }
65
66 s := js.InternalObject(v.ptr)
67 if fl&flagIndir != 0 && typ.Kind() != abi.Array && typ.Kind() != abi.Struct {
68 return Value{typ, unsafe.Pointer(typ.JsPtrTo().New(
69 js.InternalObject(func() *js.Object { return abi.WrapJsObject(typ, s.Get(prop)) }),
70 js.InternalObject(func(x *js.Object) { s.Set(prop, abi.UnwrapJsObject(typ, x)) }),
71 ).Unsafe()), fl}
72 }
73 return makeValue(typ, abi.WrapJsObject(typ, s.Get(prop)), fl)
74}
75
76// This is very similar to the `reflect.StructTag` methods `Get` and `Lookup`.
77//

Callers 1

FieldFunction · 0.45

Calls 15

objectMethod · 0.95
SetMethod · 0.95
ElemMethod · 0.95
GetMethod · 0.95
InternalObjectFunction · 0.92
structTagGetFunction · 0.85
IsExportedMethod · 0.80
TagMethod · 0.80
PointerMethod · 0.80
UnsafeMethod · 0.80
JsPtrToMethod · 0.80
jsTypeFunction · 0.70

Tested by

no test coverage detected