gitee.com/quant1x/pkg@v0.2.8/goja/builtin_reflect.go (about) 1 package goja 2 3 func (r *Runtime) builtin_reflect_apply(call FunctionCall) Value { 4 return r.toCallable(call.Argument(0))(FunctionCall{ 5 This: call.Argument(1), 6 Arguments: r.createListFromArrayLike(call.Argument(2))}) 7 } 8 9 func (r *Runtime) toConstructor(v Value) func(args []Value, newTarget *Object) *Object { 10 if ctor := r.toObject(v).self.assertConstructor(); ctor != nil { 11 return ctor 12 } 13 panic(r.NewTypeError("Value is not a constructor")) 14 } 15 16 func (r *Runtime) builtin_reflect_construct(call FunctionCall) Value { 17 target := call.Argument(0) 18 ctor := r.toConstructor(target) 19 var newTarget Value 20 if len(call.Arguments) > 2 { 21 newTarget = call.Argument(2) 22 r.toConstructor(newTarget) 23 } else { 24 newTarget = target 25 } 26 return ctor(r.createListFromArrayLike(call.Argument(1)), r.toObject(newTarget)) 27 } 28 29 func (r *Runtime) builtin_reflect_defineProperty(call FunctionCall) Value { 30 target := r.toObject(call.Argument(0)) 31 key := toPropertyKey(call.Argument(1)) 32 desc := r.toPropertyDescriptor(call.Argument(2)) 33 34 return r.toBoolean(target.defineOwnProperty(key, desc, false)) 35 } 36 37 func (r *Runtime) builtin_reflect_deleteProperty(call FunctionCall) Value { 38 target := r.toObject(call.Argument(0)) 39 key := toPropertyKey(call.Argument(1)) 40 41 return r.toBoolean(target.delete(key, false)) 42 } 43 44 func (r *Runtime) builtin_reflect_get(call FunctionCall) Value { 45 target := r.toObject(call.Argument(0)) 46 key := toPropertyKey(call.Argument(1)) 47 var receiver Value 48 if len(call.Arguments) > 2 { 49 receiver = call.Arguments[2] 50 } 51 return target.get(key, receiver) 52 } 53 54 func (r *Runtime) builtin_reflect_getOwnPropertyDescriptor(call FunctionCall) Value { 55 target := r.toObject(call.Argument(0)) 56 key := toPropertyKey(call.Argument(1)) 57 return r.valuePropToDescriptorObject(target.getOwnProp(key)) 58 } 59 60 func (r *Runtime) builtin_reflect_getPrototypeOf(call FunctionCall) Value { 61 target := r.toObject(call.Argument(0)) 62 if proto := target.self.proto(); proto != nil { 63 return proto 64 } 65 66 return _null 67 } 68 69 func (r *Runtime) builtin_reflect_has(call FunctionCall) Value { 70 target := r.toObject(call.Argument(0)) 71 key := toPropertyKey(call.Argument(1)) 72 return r.toBoolean(target.hasProperty(key)) 73 } 74 75 func (r *Runtime) builtin_reflect_isExtensible(call FunctionCall) Value { 76 target := r.toObject(call.Argument(0)) 77 return r.toBoolean(target.self.isExtensible()) 78 } 79 80 func (r *Runtime) builtin_reflect_ownKeys(call FunctionCall) Value { 81 target := r.toObject(call.Argument(0)) 82 return r.newArrayValues(target.self.keys(true, nil)) 83 } 84 85 func (r *Runtime) builtin_reflect_preventExtensions(call FunctionCall) Value { 86 target := r.toObject(call.Argument(0)) 87 return r.toBoolean(target.self.preventExtensions(false)) 88 } 89 90 func (r *Runtime) builtin_reflect_set(call FunctionCall) Value { 91 target := r.toObject(call.Argument(0)) 92 var receiver Value 93 if len(call.Arguments) >= 4 { 94 receiver = call.Argument(3) 95 } else { 96 receiver = target 97 } 98 return r.toBoolean(target.set(call.Argument(1), call.Argument(2), receiver, false)) 99 } 100 101 func (r *Runtime) builtin_reflect_setPrototypeOf(call FunctionCall) Value { 102 target := r.toObject(call.Argument(0)) 103 var proto *Object 104 if arg := call.Argument(1); arg != _null { 105 proto = r.toObject(arg) 106 } 107 return r.toBoolean(target.self.setProto(proto, false)) 108 } 109 110 func (r *Runtime) createReflect(val *Object) objectImpl { 111 o := newBaseObjectObj(val, r.global.ObjectPrototype, classObject) 112 113 o._putProp("apply", r.newNativeFunc(r.builtin_reflect_apply, "apply", 3), true, false, true) 114 o._putProp("construct", r.newNativeFunc(r.builtin_reflect_construct, "construct", 2), true, false, true) 115 o._putProp("defineProperty", r.newNativeFunc(r.builtin_reflect_defineProperty, "defineProperty", 3), true, false, true) 116 o._putProp("deleteProperty", r.newNativeFunc(r.builtin_reflect_deleteProperty, "deleteProperty", 2), true, false, true) 117 o._putProp("get", r.newNativeFunc(r.builtin_reflect_get, "get", 2), true, false, true) 118 o._putProp("getOwnPropertyDescriptor", r.newNativeFunc(r.builtin_reflect_getOwnPropertyDescriptor, "getOwnPropertyDescriptor", 2), true, false, true) 119 o._putProp("getPrototypeOf", r.newNativeFunc(r.builtin_reflect_getPrototypeOf, "getPrototypeOf", 1), true, false, true) 120 o._putProp("has", r.newNativeFunc(r.builtin_reflect_has, "has", 2), true, false, true) 121 o._putProp("isExtensible", r.newNativeFunc(r.builtin_reflect_isExtensible, "isExtensible", 1), true, false, true) 122 o._putProp("ownKeys", r.newNativeFunc(r.builtin_reflect_ownKeys, "ownKeys", 1), true, false, true) 123 o._putProp("preventExtensions", r.newNativeFunc(r.builtin_reflect_preventExtensions, "preventExtensions", 1), true, false, true) 124 o._putProp("set", r.newNativeFunc(r.builtin_reflect_set, "set", 3), true, false, true) 125 o._putProp("setPrototypeOf", r.newNativeFunc(r.builtin_reflect_setPrototypeOf, "setPrototypeOf", 2), true, false, true) 126 127 o._putSym(SymToStringTag, valueProp(asciiString("Reflect"), false, false, true)) 128 129 return o 130 } 131 132 func (r *Runtime) getReflect() *Object { 133 ret := r.global.Reflect 134 if ret == nil { 135 ret = &Object{runtime: r} 136 r.global.Reflect = ret 137 ret.self = r.createReflect(ret) 138 } 139 return ret 140 }