This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
forked from dop251/goja
/
builtin_proxy.go
304 lines (271 loc) · 12.1 KB
/
builtin_proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package goja
import (
"fmt"
"github.com/cleancluster/goja/unistring"
)
func (r *Runtime) newNativeProxyHandler(nativeHandler *ProxyTrapConfig) *Object {
handler := r.NewObject()
r.proxyproto_nativehandler_gen_obj_obj(proxy_trap_getPrototypeOf, nativeHandler.GetPrototypeOf, handler)
r.proxyproto_nativehandler_setPrototypeOf(nativeHandler.SetPrototypeOf, handler)
r.proxyproto_nativehandler_gen_obj_bool(proxy_trap_isExtensible, nativeHandler.IsExtensible, handler)
r.proxyproto_nativehandler_gen_obj_bool(proxy_trap_preventExtensions, nativeHandler.PreventExtensions, handler)
r.proxyproto_nativehandler_getOwnPropertyDescriptor(nativeHandler.GetOwnPropertyDescriptor, handler)
r.proxyproto_nativehandler_defineProperty(nativeHandler.DefineProperty, handler)
r.proxyproto_nativehandler_gen_obj_string_bool(proxy_trap_has, nativeHandler.Has, handler)
r.proxyproto_nativehandler_get(nativeHandler.Get, handler)
r.proxyproto_nativehandler_set(nativeHandler.Set, handler)
r.proxyproto_nativehandler_gen_obj_string_bool(proxy_trap_deleteProperty, nativeHandler.DeleteProperty, handler)
r.proxyproto_nativehandler_gen_obj_obj(proxy_trap_ownKeys, nativeHandler.OwnKeys, handler)
r.proxyproto_nativehandler_apply(nativeHandler.Apply, handler)
r.proxyproto_nativehandler_construct(nativeHandler.Construct, handler)
return handler
}
func (r *Runtime) proxyproto_nativehandler_gen_obj_obj(name proxyTrap, native func(*Object) *Object, handler *Object) {
if native != nil {
handler.self._putProp(unistring.String(name), r.newNativeFunc(func(call FunctionCall) Value {
if len(call.Arguments) >= 1 {
if t, ok := call.Argument(0).(*Object); ok {
return native(t)
}
}
panic(r.NewTypeError("%s needs to be called with target as Object", name))
}, nil, unistring.String(fmt.Sprintf("[native %s]", name)), nil, 1), true, true, true)
}
}
func (r *Runtime) proxyproto_nativehandler_setPrototypeOf(native func(*Object, *Object) bool, handler *Object) {
if native != nil {
handler.self._putProp("setPrototypeOf", r.newNativeFunc(func(call FunctionCall) Value {
if len(call.Arguments) >= 2 {
if t, ok := call.Argument(0).(*Object); ok {
if p, ok := call.Argument(1).(*Object); ok {
s := native(t, p)
return r.ToValue(s)
}
}
}
panic(r.NewTypeError("setPrototypeOf needs to be called with target and prototype as Object"))
}, nil, "[native setPrototypeOf]", nil, 2), true, true, true)
}
}
func (r *Runtime) proxyproto_nativehandler_gen_obj_bool(name proxyTrap, native func(*Object) bool, handler *Object) {
if native != nil {
handler.self._putProp(unistring.String(name), r.newNativeFunc(func(call FunctionCall) Value {
if len(call.Arguments) >= 1 {
if t, ok := call.Argument(0).(*Object); ok {
s := native(t)
return r.ToValue(s)
}
}
panic(r.NewTypeError("%s needs to be called with target as Object", name))
}, nil, unistring.String(fmt.Sprintf("[native %s]", name)), nil, 1), true, true, true)
}
}
func (r *Runtime) proxyproto_nativehandler_getOwnPropertyDescriptor(native func(*Object, string) PropertyDescriptor, handler *Object) {
if native != nil {
handler.self._putProp("getOwnPropertyDescriptor", r.newNativeFunc(func(call FunctionCall) Value {
if len(call.Arguments) >= 2 {
if t, ok := call.Argument(0).(*Object); ok {
switch p := call.Argument(1).(type) {
case *valueSymbol:
return _undefined
default:
desc := native(t, p.String())
return desc.toValue(r)
}
}
}
panic(r.NewTypeError("getOwnPropertyDescriptor needs to be called with target as Object and prop as string"))
}, nil, "[native getOwnPropertyDescriptor]", nil, 2), true, true, true)
}
}
func (r *Runtime) proxyproto_nativehandler_defineProperty(native func(*Object, string, PropertyDescriptor) bool, handler *Object) {
if native != nil {
handler.self._putProp("defineProperty", r.newNativeFunc(func(call FunctionCall) Value {
if len(call.Arguments) >= 3 {
if t, ok := call.Argument(0).(*Object); ok {
if k, ok := call.Argument(1).(valueString); ok {
propertyDescriptor := r.toPropertyDescriptor(call.Argument(2))
s := native(t, k.String(), propertyDescriptor)
return r.ToValue(s)
}
}
}
panic(r.NewTypeError("defineProperty needs to be called with target as Object and propertyDescriptor as string and key as string"))
}, nil, "[native defineProperty]", nil, 3), true, true, true)
}
}
func (r *Runtime) proxyproto_nativehandler_gen_obj_string_bool(name proxyTrap, native func(*Object, string) bool, handler *Object) {
if native != nil {
handler.self._putProp(unistring.String(name), r.newNativeFunc(func(call FunctionCall) Value {
if len(call.Arguments) >= 2 {
if t, ok := call.Argument(0).(*Object); ok {
switch p := call.Argument(1).(type) {
case *valueSymbol:
return valueFalse
default:
o := native(t, p.String())
return r.ToValue(o)
}
}
}
panic(r.NewTypeError("%s needs to be called with target as Object and property as string", name))
}, nil, unistring.String(fmt.Sprintf("[native %s]", name)), nil, 2), true, true, true)
}
}
func (r *Runtime) proxyproto_nativehandler_get(native func(*Object, string, *Object) Value, handler *Object) {
if native != nil {
handler.self._putProp("get", r.newNativeFunc(func(call FunctionCall) Value {
if len(call.Arguments) >= 3 {
if t, ok := call.Argument(0).(*Object); ok {
if r, ok := call.Argument(2).(*Object); ok {
switch p := call.Argument(1).(type) {
case *valueSymbol:
return _undefined
default:
return native(t, p.String(), r)
}
}
}
}
panic(r.NewTypeError("get needs to be called with target and receiver as Object and property as string"))
}, nil, "[native get]", nil, 3), true, true, true)
}
}
func (r *Runtime) proxyproto_nativehandler_set(native func(*Object, string, Value, *Object) bool, handler *Object) {
if native != nil {
handler.self._putProp("set", r.newNativeFunc(func(call FunctionCall) Value {
if len(call.Arguments) >= 4 {
if t, ok := call.Argument(0).(*Object); ok {
if p, ok := call.Argument(1).(valueString); ok {
v := call.Argument(2)
if re, ok := call.Argument(3).(*Object); ok {
s := native(t, p.String(), v, re)
return r.ToValue(s)
}
}
}
}
panic(r.NewTypeError("set needs to be called with target and receiver as Object, property as string and value as a legal javascript value"))
}, nil, "[native set]", nil, 4), true, true, true)
}
}
func (r *Runtime) proxyproto_nativehandler_apply(native func(*Object, *Object, []Value) Value, handler *Object) {
if native != nil {
handler.self._putProp("apply", r.newNativeFunc(func(call FunctionCall) Value {
if len(call.Arguments) >= 3 {
if t, ok := call.Argument(0).(*Object); ok {
if this, ok := call.Argument(1).(*Object); ok {
if v, ok := call.Argument(2).(*Object); ok {
if a, ok := v.self.(*arrayObject); ok {
v := native(t, this, a.values)
return r.ToValue(v)
}
}
}
}
}
panic(r.NewTypeError("apply needs to be called with target and this as Object and argumentsList as an array of legal javascript values"))
}, nil, "[native apply]", nil, 3), true, true, true)
}
}
func (r *Runtime) proxyproto_nativehandler_construct(native func(*Object, []Value, *Object) *Object, handler *Object) {
if native != nil {
handler.self._putProp("construct", r.newNativeFunc(func(call FunctionCall) Value {
if len(call.Arguments) >= 3 {
if t, ok := call.Argument(0).(*Object); ok {
if v, ok := call.Argument(1).(*Object); ok {
if newTarget, ok := call.Argument(2).(*Object); ok {
if a, ok := v.self.(*arrayObject); ok {
return native(t, a.values, newTarget)
}
}
}
}
}
panic(r.NewTypeError("construct needs to be called with target and newTarget as Object and argumentsList as an array of legal javascript values"))
}, nil, "[native construct]", nil, 3), true, true, true)
}
}
// ProxyTrapConfig provides a simplified Go-friendly API for implementing Proxy traps.
// Note that the Proxy may not have Symbol properties when using this as a handler because property keys are
// passed as strings.
// get() and getOwnPropertyDescriptor() for Symbol properties will always return undefined;
// has() and deleteProperty() for Symbol properties will always return false;
// set() and defineProperty() for Symbol properties will throw a TypeError.
// If you need Symbol properties implement the handler in JavaScript.
type ProxyTrapConfig struct {
// A trap for Object.getPrototypeOf, Reflect.getPrototypeOf, __proto__, Object.prototype.isPrototypeOf, instanceof
GetPrototypeOf func(target *Object) (prototype *Object)
// A trap for Object.setPrototypeOf, Reflect.setPrototypeOf
SetPrototypeOf func(target *Object, prototype *Object) (success bool)
// A trap for Object.isExtensible, Reflect.isExtensible
IsExtensible func(target *Object) (success bool)
// A trap for Object.preventExtensions, Reflect.preventExtensions
PreventExtensions func(target *Object) (success bool)
// A trap for Object.getOwnPropertyDescriptor, Reflect.getOwnPropertyDescriptor
GetOwnPropertyDescriptor func(target *Object, prop string) (propertyDescriptor PropertyDescriptor)
// A trap for Object.defineProperty, Reflect.defineProperty
DefineProperty func(target *Object, key string, propertyDescriptor PropertyDescriptor) (success bool)
// A trap for the in operator, with operator, Reflect.has
Has func(target *Object, property string) (available bool)
// A trap for getting property values, Reflect.get
Get func(target *Object, property string, receiver *Object) (value Value)
// A trap for setting property values, Reflect.set
Set func(target *Object, property string, value Value, receiver *Object) (success bool)
// A trap for the delete operator, Reflect.deleteProperty
DeleteProperty func(target *Object, property string) (success bool)
// A trap for Object.getOwnPropertyNames, Object.getOwnPropertySymbols, Object.keys, Reflect.ownKeys
OwnKeys func(target *Object) (object *Object)
// A trap for a function call, Function.prototype.apply, Function.prototype.call, Reflect.apply
Apply func(target *Object, this *Object, argumentsList []Value) (value Value)
// A trap for the new operator, Reflect.construct
Construct func(target *Object, argumentsList []Value, newTarget *Object) (value *Object)
}
func (r *Runtime) newProxy(args []Value, proto *Object) *Object {
if len(args) >= 2 {
if target, ok := args[0].(*Object); ok {
if proxyHandler, ok := args[1].(*Object); ok {
return r.newProxyObject(target, proxyHandler, proto).val
}
}
}
panic(r.NewTypeError("Cannot create proxy with a non-object as target or handler"))
}
func (r *Runtime) builtin_newProxy(args []Value, newTarget *Object) *Object {
if newTarget == nil {
panic(r.needNew("Proxy"))
}
return r.newProxy(args, r.getPrototypeFromCtor(newTarget, r.global.Proxy, r.global.ObjectPrototype))
}
func (r *Runtime) NewProxy(target *Object, nativeHandler *ProxyTrapConfig) Proxy {
handler := r.newNativeProxyHandler(nativeHandler)
proxy := r.newProxyObject(target, handler, nil)
return Proxy{proxy: proxy}
}
func (r *Runtime) builtin_proxy_revocable(call FunctionCall) Value {
if len(call.Arguments) >= 2 {
if target, ok := call.Argument(0).(*Object); ok {
if proxyHandler, ok := call.Argument(1).(*Object); ok {
proxy := r.newProxyObject(target, proxyHandler, nil)
revoke := r.newNativeFunc(func(FunctionCall) Value {
proxy.revoke()
return _undefined
}, nil, "", nil, 0)
ret := r.NewObject()
ret.self._putProp("proxy", proxy.val, true, true, true)
ret.self._putProp("revoke", revoke, true, true, true)
return ret
}
}
}
panic(r.NewTypeError("Cannot create proxy with a non-object as target or handler"))
}
func (r *Runtime) createProxy(val *Object) objectImpl {
o := r.newNativeConstructOnly(val, r.builtin_newProxy, nil, "Proxy", 2)
o._putProp("revocable", r.newNativeFunc(r.builtin_proxy_revocable, nil, "revocable", nil, 2), true, false, true)
return o
}
func (r *Runtime) initProxy() {
r.global.Proxy = r.newLazyObject(r.createProxy)
r.addToGlobal("Proxy", r.global.Proxy)
}