forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfunc.go
618 lines (513 loc) · 13.7 KB
/
func.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package goja
import (
"context"
"reflect"
"github.com/dop251/goja/unistring"
)
type baseFuncObject struct {
ctx context.Context
baseObject
lenProp valueProperty
}
type baseJsFuncObject struct {
baseFuncObject
stash *stash
privEnv *privateEnv
prg *Program
src string
strict bool
}
type funcObjectImpl interface {
source() valueString
}
type funcObject struct {
baseJsFuncObject
}
type classFuncObject struct {
baseJsFuncObject
initFields *Program
computedKeys []Value
privateEnvType *privateEnvType
privateMethods []Value
derived bool
}
type methodFuncObject struct {
baseJsFuncObject
homeObject *Object
}
type arrowFuncObject struct {
baseJsFuncObject
funcObj *Object
newTarget Value
}
type nativeFuncObject struct {
ctx context.Context
baseFuncObject
f func(FunctionCall) Value
construct func(args []Value, newTarget *Object) *Object
}
type wrappedFuncObject struct {
nativeFuncObject
wrapped reflect.Value
}
type boundFuncObject struct {
nativeFuncObject
wrapped *Object
}
func (f *nativeFuncObject) export(*objectExportCtx) interface{} {
return f.f
}
func (f *wrappedFuncObject) exportType() reflect.Type {
return f.wrapped.Type()
}
func (f *wrappedFuncObject) export(*objectExportCtx) interface{} {
return f.wrapped.Interface()
}
func (f *funcObject) _addProto(n unistring.String) Value {
if n == "prototype" {
if _, exists := f.values[n]; !exists {
return f.addPrototype()
}
}
return nil
}
func (f *funcObject) getStr(p unistring.String, receiver Value) Value {
return f.getStrWithOwnProp(f.getOwnPropStr(p), p, receiver)
}
func (f *funcObject) getOwnPropStr(name unistring.String) Value {
if v := f._addProto(name); v != nil {
return v
}
return f.baseObject.getOwnPropStr(name)
}
func (f *funcObject) setOwnStr(name unistring.String, val Value, throw bool) bool {
f._addProto(name)
return f.baseObject.setOwnStr(name, val, throw)
}
func (f *funcObject) setForeignStr(name unistring.String, val, receiver Value, throw bool) (bool, bool) {
return f._setForeignStr(name, f.getOwnPropStr(name), val, receiver, throw)
}
func (f *funcObject) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
f._addProto(name)
return f.baseObject.defineOwnPropertyStr(name, descr, throw)
}
func (f *funcObject) deleteStr(name unistring.String, throw bool) bool {
f._addProto(name)
return f.baseObject.deleteStr(name, throw)
}
func (f *funcObject) addPrototype() Value {
proto := f.val.runtime.NewObject()
proto.self._putProp("constructor", f.val, true, false, true)
return f._putProp("prototype", proto, true, false, false)
}
func (f *funcObject) hasOwnPropertyStr(name unistring.String) bool {
if f.baseObject.hasOwnPropertyStr(name) {
return true
}
if name == "prototype" {
return true
}
return false
}
func (f *funcObject) stringKeys(all bool, accum []Value) []Value {
if all {
if _, exists := f.values["prototype"]; !exists {
accum = append(accum, asciiString("prototype"))
}
}
return f.baseFuncObject.stringKeys(all, accum)
}
func (f *funcObject) iterateStringKeys() iterNextFunc {
if _, exists := f.values["prototype"]; !exists {
f.addPrototype()
}
return f.baseFuncObject.iterateStringKeys()
}
func (f *baseFuncObject) createInstance(newTarget *Object) *Object {
r := f.val.runtime
if newTarget == nil {
newTarget = f.val
}
proto := r.getPrototypeFromCtor(newTarget, nil, r.global.ObjectPrototype)
return f.val.runtime.newBaseObject(proto, classObject).val
}
func (f *baseJsFuncObject) construct(args []Value, newTarget *Object) *Object {
if newTarget == nil {
newTarget = f.val
}
proto := newTarget.self.getStr("prototype", nil)
var protoObj *Object
if p, ok := proto.(*Object); ok {
protoObj = p
} else {
protoObj = f.val.runtime.global.ObjectPrototype
}
obj := f.val.runtime.newBaseObject(protoObj, classObject).val
ret := f.call(FunctionCall{
ctx: f.val.runtime.vm.ctx,
This: obj,
Arguments: args,
}, newTarget)
if ret, ok := ret.(*Object); ok {
return ret
}
return obj
}
func (f *classFuncObject) Call(FunctionCall) Value {
panic(f.val.runtime.NewTypeError("Class constructor cannot be invoked without 'new'"))
}
func (f *classFuncObject) assertCallable() (func(FunctionCall) Value, bool) {
return f.Call, true
}
func (f *classFuncObject) export(*objectExportCtx) interface{} {
return f.Call
}
func (f *classFuncObject) createInstance(args []Value, newTarget *Object) (instance *Object) {
if f.derived {
if ctor := f.prototype.self.assertConstructor(); ctor != nil {
instance = ctor(args, newTarget)
} else {
panic(f.val.runtime.NewTypeError("Super constructor is not a constructor"))
}
} else {
instance = f.baseFuncObject.createInstance(newTarget)
}
return
}
func (f *classFuncObject) _initFields(instance *Object) {
if f.privateEnvType != nil {
penv := instance.self.getPrivateEnv(f.privateEnvType, true)
penv.methods = f.privateMethods
}
if f.initFields != nil {
vm := f.val.runtime.vm
vm.pushCtx()
vm.prg = f.initFields
vm.stash = f.stash
vm.privEnv = f.privEnv
vm.newTarget = nil
// so that 'super' base could be correctly resolved (including from direct eval())
vm.push(f.val)
vm.sb = vm.sp
vm.push(instance)
vm.pc = 0
vm.run()
vm.popCtx()
vm.sp -= 2
vm.halt = false
}
}
func (f *classFuncObject) construct(args []Value, newTarget *Object) *Object {
if newTarget == nil {
newTarget = f.val
}
if f.prg == nil {
instance := f.createInstance(args, newTarget)
f._initFields(instance)
return instance
} else {
var instance *Object
var thisVal Value
if !f.derived {
instance = f.createInstance(args, newTarget)
f._initFields(instance)
thisVal = instance
}
ret := f._call(f.val.runtime.vm.ctx, args, newTarget, thisVal)
if ret, ok := ret.(*Object); ok {
return ret
}
if f.derived {
r := f.val.runtime
if ret != _undefined {
panic(r.NewTypeError("Derived constructors may only return object or undefined"))
}
if v := r.vm.stack[r.vm.sp+1]; v != nil { // using residual 'this' value (a bit hacky)
instance = r.toObject(v)
} else {
panic(r.newError(r.getReferenceError(), "Must call super constructor in derived class before returning from derived constructor"))
}
}
return instance
}
}
func (f *classFuncObject) assertConstructor() func(args []Value, newTarget *Object) *Object {
return f.construct
}
func (f *baseJsFuncObject) Call(call FunctionCall) Value {
return f.call(call, nil)
}
func (f *arrowFuncObject) Call(call FunctionCall) Value {
return f._call(call.Context(), call.Arguments, f.newTarget, nil)
}
func (f *baseJsFuncObject) _call(ctx context.Context, args []Value, newTarget, this Value) Value {
vm := f.val.runtime.vm
vm.stack.expand(vm.sp + len(args) + 1)
vm.stack[vm.sp] = f.val
vm.sp++
vm.stack[vm.sp] = this
vm.sp++
for _, arg := range args {
if arg != nil {
vm.stack[vm.sp] = arg
} else {
vm.stack[vm.sp] = _undefined
}
vm.sp++
}
pc := vm.pc
if pc != -1 {
vm.pc++ // fake "return address" so that captureStack() records the correct call location
vm.pushCtx()
vm.callStack = append(vm.callStack, vmContext{ctx: ctx, pc: -1}) // extra frame so that run() halts after ret
} else {
vm.pushCtx()
}
vm.args = len(args)
vm.prg = f.prg
vm.stash = f.stash
vm.privEnv = f.privEnv
vm.newTarget = newTarget
vm.pc = 0
vm.ctx = ctx
vm.run()
if pc != -1 {
vm.popCtx()
}
vm.pc = pc
vm.halt = false
return vm.pop()
}
func (f *baseJsFuncObject) call(call FunctionCall, newTarget Value) Value {
return f._call(call.Context(), call.Arguments, newTarget, nilSafe(call.This))
}
func (f *baseJsFuncObject) export(*objectExportCtx) interface{} {
return f.Call
}
func (f *baseFuncObject) exportType() reflect.Type {
return reflectTypeFunc
}
func (f *baseJsFuncObject) assertCallable() (func(FunctionCall) Value, bool) {
return f.Call, true
}
func (f *funcObject) assertConstructor() func(args []Value, newTarget *Object) *Object {
return f.construct
}
func (f *arrowFuncObject) assertCallable() (func(FunctionCall) Value, bool) {
return f.Call, true
}
func (f *arrowFuncObject) export(*objectExportCtx) interface{} {
return f.Call
}
func (f *baseFuncObject) init(name unistring.String, length Value) {
f.baseObject.init()
f.lenProp.configurable = true
f.lenProp.value = length
f._put("length", &f.lenProp)
f._putProp("name", stringValueFromRaw(name), false, false, true)
}
func hasInstance(val *Object, v Value) bool {
if v, ok := v.(*Object); ok {
o := val.self.getStr("prototype", nil)
if o1, ok := o.(*Object); ok {
for {
v = v.self.proto()
if v == nil {
return false
}
if o1 == v {
return true
}
}
} else {
panic(val.runtime.NewTypeError("prototype is not an object"))
}
}
return false
}
func (f *baseFuncObject) hasInstance(v Value) bool {
return hasInstance(f.val, v)
}
func (f *nativeFuncObject) defaultConstruct(ccall func(ConstructorCall) *Object, args []Value, newTarget *Object) *Object {
obj := f.createInstance(newTarget)
ret := ccall(ConstructorCall{
ctx: f.ctx,
This: obj,
Arguments: args,
NewTarget: newTarget,
})
if ret != nil {
return ret
}
return obj
}
func (f *nativeFuncObject) assertCallable() (func(FunctionCall) Value, bool) {
if f.f != nil {
return f.Call, true
}
return nil, false
}
func (f *nativeFuncObject) Call(call FunctionCall) Value {
vm := f.val.runtime.vm
prevFuncName := vm.getFuncName()
// This is done to display the correct function name in the stack trace when executing a
// native function with Function.prototype.apply/call
vm.setFuncName(f.getStr("name", nil).string())
rv := f.f(call)
vm.setFuncName(prevFuncName)
return rv
}
func (f *nativeFuncObject) vmCall(vm *vm, n int) {
if f.f != nil {
vm.pushCtx()
vm.prg = nil
vm.sb = vm.sp - n // so that [sb-1] points to the callee
ret := f.f(FunctionCall{
Arguments: vm.stack[vm.sp-n : vm.sp],
This: vm.stack[vm.sp-n-2],
})
if ret == nil {
ret = _undefined
}
vm.stack[vm.sp-n-2] = ret
vm.popCtx()
} else {
vm.stack[vm.sp-n-2] = _undefined
}
vm.sp -= n + 1
vm.pc++
}
func (f *nativeFuncObject) assertConstructor() func(args []Value, newTarget *Object) *Object {
return f.construct
}
/*func (f *boundFuncObject) getStr(p unistring.String, receiver Value) Value {
return f.getStrWithOwnProp(f.getOwnPropStr(p), p, receiver)
}
func (f *boundFuncObject) getOwnPropStr(name unistring.String) Value {
if name == "caller" || name == "arguments" {
return f.val.runtime.global.throwerProperty
}
return f.nativeFuncObject.getOwnPropStr(name)
}
func (f *boundFuncObject) deleteStr(name unistring.String, throw bool) bool {
if name == "caller" || name == "arguments" {
return true
}
return f.nativeFuncObject.deleteStr(name, throw)
}
func (f *boundFuncObject) setOwnStr(name unistring.String, val Value, throw bool) bool {
if name == "caller" || name == "arguments" {
panic(f.val.runtime.NewTypeError("'caller' and 'arguments' are restricted function properties and cannot be accessed in this context."))
}
return f.nativeFuncObject.setOwnStr(name, val, throw)
}
func (f *boundFuncObject) setForeignStr(name unistring.String, val, receiver Value, throw bool) (bool, bool) {
return f._setForeignStr(name, f.getOwnPropStr(name), val, receiver, throw)
}
*/
func (f *boundFuncObject) hasInstance(v Value) bool {
return instanceOfOperator(v, f.wrapped)
}
func (f *nativeFuncObject) MemUsage(ctx *MemUsageContext) (memUsage uint64, err error) {
if f == nil || ctx.IsObjVisited(f) {
return SizeEmptyStruct, nil
}
ctx.VisitObj(f)
return f.baseFuncObject.MemUsage(ctx)
}
func (f *funcObject) MemUsage(ctx *MemUsageContext) (memUsage uint64, err error) {
if f == nil || ctx.IsObjVisited(f) {
return SizeEmptyStruct, nil
}
ctx.VisitObj(f)
return f.baseJsFuncObject.MemUsage(ctx)
}
func (b *baseJsFuncObject) MemUsage(ctx *MemUsageContext) (memUsage uint64, err error) {
if b == nil || ctx.IsObjVisited(b) {
return SizeEmptyStruct, nil
}
ctx.VisitObj(b)
memUsage, err = b.baseFuncObject.MemUsage(ctx)
if err != nil {
return memUsage, err
}
if b.stash != nil {
inc, err := b.stash.MemUsage(ctx)
memUsage += inc
if err != nil {
return memUsage, err
}
}
return memUsage, nil
}
func (c *classFuncObject) MemUsage(ctx *MemUsageContext) (memUsage uint64, err error) {
if c == nil || ctx.IsObjVisited(c) {
return SizeEmptyStruct, nil
}
ctx.VisitObj(c)
memUsage, err = c.baseJsFuncObject.MemUsage(ctx)
if err != nil {
return memUsage, err
}
if c.initFields != nil {
inc, err := c.initFields.MemUsage(ctx)
memUsage += inc
if err != nil {
return memUsage, err
}
}
for _, v := range c.computedKeys {
inc, err := v.MemUsage(ctx)
memUsage += inc
if err != nil {
return memUsage, err
}
}
for _, v := range c.privateMethods {
inc, err := v.MemUsage(ctx)
memUsage += inc
if err != nil {
return memUsage, err
}
}
return memUsage, err
}
func (m *methodFuncObject) MemUsage(ctx *MemUsageContext) (memUsage uint64, err error) {
if m == nil || ctx.IsObjVisited(m) {
return SizeEmptyStruct, nil
}
ctx.VisitObj(m)
memUsage, err = m.baseJsFuncObject.MemUsage(ctx)
if err != nil {
return memUsage, err
}
inc, err := m.homeObject.MemUsage(ctx)
memUsage += inc
if err != nil {
return memUsage, err
}
return memUsage, err
}
func (a *arrowFuncObject) MemUsage(ctx *MemUsageContext) (memUsage uint64, err error) {
if a == nil || ctx.IsObjVisited(a) {
return SizeEmptyStruct, nil
}
ctx.VisitObj(a)
memUsage, err = a.baseJsFuncObject.MemUsage(ctx)
if err != nil {
return memUsage, err
}
inc, err := a.funcObj.MemUsage(ctx)
memUsage += inc
if err != nil {
return memUsage, err
}
if a.newTarget != nil {
inc, err = a.newTarget.MemUsage(ctx)
memUsage += inc
if err != nil {
return memUsage, err
}
}
return memUsage, err
}