github.com/nuvolaris/goja@v0.0.0-20230825100449-967811910c6d/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, nil, "apply", nil, 3), true, false, true) 114 o._putProp("construct", r.newNativeFunc(r.builtin_reflect_construct, nil, "construct", nil, 2), true, false, true) 115 o._putProp("defineProperty", r.newNativeFunc(r.builtin_reflect_defineProperty, nil, "defineProperty", nil, 3), true, false, true) 116 o._putProp("deleteProperty", r.newNativeFunc(r.builtin_reflect_deleteProperty, nil, "deleteProperty", nil, 2), true, false, true) 117 o._putProp("get", r.newNativeFunc(r.builtin_reflect_get, nil, "get", nil, 2), true, false, true) 118 o._putProp("getOwnPropertyDescriptor", r.newNativeFunc(r.builtin_reflect_getOwnPropertyDescriptor, nil, "getOwnPropertyDescriptor", nil, 2), true, false, true) 119 o._putProp("getPrototypeOf", r.newNativeFunc(r.builtin_reflect_getPrototypeOf, nil, "getPrototypeOf", nil, 1), true, false, true) 120 o._putProp("has", r.newNativeFunc(r.builtin_reflect_has, nil, "has", nil, 2), true, false, true) 121 o._putProp("isExtensible", r.newNativeFunc(r.builtin_reflect_isExtensible, nil, "isExtensible", nil, 1), true, false, true) 122 o._putProp("ownKeys", r.newNativeFunc(r.builtin_reflect_ownKeys, nil, "ownKeys", nil, 1), true, false, true) 123 o._putProp("preventExtensions", r.newNativeFunc(r.builtin_reflect_preventExtensions, nil, "preventExtensions", nil, 1), true, false, true) 124 o._putProp("set", r.newNativeFunc(r.builtin_reflect_set, nil, "set", nil, 3), true, false, true) 125 o._putProp("setPrototypeOf", r.newNativeFunc(r.builtin_reflect_setPrototypeOf, nil, "setPrototypeOf", nil, 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) initReflect() { 133 r.addToGlobal("Reflect", r.newLazyObject(r.createReflect)) 134 }