forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduk_js_call.c
2467 lines (2068 loc) · 86.7 KB
/
duk_js_call.c
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Call handling.
*
* The main work horse functions are:
* - duk_handle_call(): call to a C/Ecmascript functions
* - duk_handle_safe_call(): make a protected C call within current activation
* - duk_handle_ecma_call_setup(): Ecmascript-to-Ecmascript calls, including
* tail calls and coroutine resume
*/
#include "duk_internal.h"
/*
* Misc
*/
#if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
DUK_LOCAL void duk__interrupt_fixup(duk_hthread *thr, duk_hthread *entry_curr_thread) {
/* XXX: Currently the bytecode executor and executor interrupt
* instruction counts are off because we don't execute the
* interrupt handler when we're about to exit from the initial
* user call into Duktape.
*
* If we were to execute the interrupt handler here, the counts
* would match. You can enable this block manually to check
* that this is the case.
*/
DUK_ASSERT(thr != NULL);
DUK_ASSERT(thr->heap != NULL);
#if 0
if (entry_curr_thread == NULL) {
thr->interrupt_init = thr->interrupt_init - thr->interrupt_counter;
thr->heap->inst_count_interrupt += thr->interrupt_init;
DUK_DD(DUK_DDPRINT("debug test: updated interrupt count on exit to "
"user code, instruction counts: executor=%ld, interrupt=%ld",
(long) thr->heap->inst_count_exec, (long) thr->heap->inst_count_interrupt));
DUK_ASSERT(thr->heap->inst_count_exec == thr->heap->inst_count_interrupt);
}
#else
DUK_UNREF(thr);
DUK_UNREF(entry_curr_thread);
#endif
}
#endif
/*
* Arguments object creation.
*
* Creating arguments objects is a bit finicky, see E5 Section 10.6 for the
* specific requirements. Much of the arguments object exotic behavior is
* implemented in duk_hobject_props.c, and is enabled by the object flag
* DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS.
*/
DUK_LOCAL
void duk__create_arguments_object(duk_hthread *thr,
duk_hobject *func,
duk_hobject *varenv,
duk_idx_t idx_argbase, /* idx of first argument on stack */
duk_idx_t num_stack_args) { /* num args starting from idx_argbase */
duk_context *ctx = (duk_context *) thr;
duk_hobject *arg; /* 'arguments' */
duk_hobject *formals; /* formals for 'func' (may be NULL if func is a C function) */
duk_idx_t i_arg;
duk_idx_t i_map;
duk_idx_t i_mappednames;
duk_idx_t i_formals;
duk_idx_t i_argbase;
duk_idx_t n_formals;
duk_idx_t idx;
duk_bool_t need_map;
DUK_DDD(DUK_DDDPRINT("creating arguments object for func=%!iO, varenv=%!iO, "
"idx_argbase=%ld, num_stack_args=%ld",
(duk_heaphdr *) func, (duk_heaphdr *) varenv,
(long) idx_argbase, (long) num_stack_args));
DUK_ASSERT(thr != NULL);
DUK_ASSERT(func != NULL);
DUK_ASSERT(DUK_HOBJECT_IS_NONBOUND_FUNCTION(func));
DUK_ASSERT(varenv != NULL);
DUK_ASSERT(idx_argbase >= 0); /* assumed to bottom relative */
DUK_ASSERT(num_stack_args >= 0);
need_map = 0;
i_argbase = idx_argbase;
DUK_ASSERT(i_argbase >= 0);
duk_push_hobject(ctx, func);
duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_FORMALS);
formals = duk_get_hobject(ctx, -1);
n_formals = 0;
if (formals) {
duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH);
n_formals = (duk_idx_t) duk_require_int(ctx, -1);
duk_pop(ctx);
}
duk_remove(ctx, -2); /* leave formals on stack for later use */
i_formals = duk_require_top_index(ctx);
DUK_ASSERT(n_formals >= 0);
DUK_ASSERT(formals != NULL || n_formals == 0);
DUK_DDD(DUK_DDDPRINT("func=%!O, formals=%!O, n_formals=%ld",
(duk_heaphdr *) func, (duk_heaphdr *) formals,
(long) n_formals));
/* [ ... formals ] */
/*
* Create required objects:
* - 'arguments' object: array-like, but not an array
* - 'map' object: internal object, tied to 'arguments'
* - 'mappedNames' object: temporary value used during construction
*/
i_arg = duk_push_object_helper(ctx,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_ARRAY_PART |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARGUMENTS),
DUK_BIDX_OBJECT_PROTOTYPE);
DUK_ASSERT(i_arg >= 0);
arg = duk_require_hobject(ctx, -1);
DUK_ASSERT(arg != NULL);
i_map = duk_push_object_helper(ctx,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
-1); /* no prototype */
DUK_ASSERT(i_map >= 0);
i_mappednames = duk_push_object_helper(ctx,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
-1); /* no prototype */
DUK_ASSERT(i_mappednames >= 0);
/* [... formals arguments map mappedNames] */
DUK_DDD(DUK_DDDPRINT("created arguments related objects: "
"arguments at index %ld -> %!O "
"map at index %ld -> %!O "
"mappednames at index %ld -> %!O",
(long) i_arg, (duk_heaphdr *) duk_get_hobject(ctx, i_arg),
(long) i_map, (duk_heaphdr *) duk_get_hobject(ctx, i_map),
(long) i_mappednames, (duk_heaphdr *) duk_get_hobject(ctx, i_mappednames)));
/*
* Init arguments properties, map, etc.
*/
duk_push_int(ctx, num_stack_args);
duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_WC);
/*
* Init argument related properties
*/
/* step 11 */
idx = num_stack_args - 1;
while (idx >= 0) {
DUK_DDD(DUK_DDDPRINT("arg idx %ld, argbase=%ld, argidx=%ld",
(long) idx, (long) i_argbase, (long) (i_argbase + idx)));
DUK_DDD(DUK_DDDPRINT("define arguments[%ld]=arg", (long) idx));
duk_dup(ctx, i_argbase + idx);
duk_xdef_prop_index_wec(ctx, i_arg, (duk_uarridx_t) idx);
DUK_DDD(DUK_DDDPRINT("defined arguments[%ld]=arg", (long) idx));
/* step 11.c is relevant only if non-strict (checked in 11.c.ii) */
if (!DUK_HOBJECT_HAS_STRICT(func) && idx < n_formals) {
DUK_ASSERT(formals != NULL);
DUK_DDD(DUK_DDDPRINT("strict function, index within formals (%ld < %ld)",
(long) idx, (long) n_formals));
duk_get_prop_index(ctx, i_formals, idx);
DUK_ASSERT(duk_is_string(ctx, -1));
duk_dup(ctx, -1); /* [... name name] */
if (!duk_has_prop(ctx, i_mappednames)) {
/* steps 11.c.ii.1 - 11.c.ii.4, but our internal book-keeping
* differs from the reference model
*/
/* [... name] */
need_map = 1;
DUK_DDD(DUK_DDDPRINT("set mappednames[%s]=%ld",
(const char *) duk_get_string(ctx, -1),
(long) idx));
duk_dup(ctx, -1); /* name */
duk_push_uint(ctx, (duk_uint_t) idx); /* index */
duk_to_string(ctx, -1);
duk_xdef_prop_wec(ctx, i_mappednames); /* out of spec, must be configurable */
DUK_DDD(DUK_DDDPRINT("set map[%ld]=%s",
(long) idx,
duk_get_string(ctx, -1)));
duk_dup(ctx, -1); /* name */
duk_xdef_prop_index_wec(ctx, i_map, (duk_uarridx_t) idx); /* out of spec, must be configurable */
} else {
/* duk_has_prop() popped the second 'name' */
}
/* [... name] */
duk_pop(ctx); /* pop 'name' */
}
idx--;
}
DUK_DDD(DUK_DDDPRINT("actual arguments processed"));
/* step 12 */
if (need_map) {
DUK_DDD(DUK_DDDPRINT("adding 'map' and 'varenv' to arguments object"));
/* should never happen for a strict callee */
DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(func));
duk_dup(ctx, i_map);
duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_INT_MAP, DUK_PROPDESC_FLAGS_NONE); /* out of spec, don't care */
/* The variable environment for magic variable bindings needs to be
* given by the caller and recorded in the arguments object.
*
* See E5 Section 10.6, the creation of setters/getters.
*
* The variable environment also provides access to the callee, so
* an explicit (internal) callee property is not needed.
*/
duk_push_hobject(ctx, varenv);
duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_INT_VARENV, DUK_PROPDESC_FLAGS_NONE); /* out of spec, don't care */
}
/* steps 13-14 */
if (DUK_HOBJECT_HAS_STRICT(func)) {
/*
* Note: callee/caller are throwers and are not deletable etc.
* They could be implemented as virtual properties, but currently
* there is no support for virtual properties which are accessors
* (only plain virtual properties). This would not be difficult
* to change in duk_hobject_props, but we can make the throwers
* normal, concrete properties just as easily.
*
* Note that the specification requires that the *same* thrower
* built-in object is used here! See E5 Section 10.6 main
* algoritm, step 14, and Section 13.2.3 which describes the
* thrower. See test case test-arguments-throwers.js.
*/
DUK_DDD(DUK_DDDPRINT("strict function, setting caller/callee to throwers"));
duk_xdef_prop_stridx_thrower(ctx, i_arg, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
duk_xdef_prop_stridx_thrower(ctx, i_arg, DUK_STRIDX_CALLEE, DUK_PROPDESC_FLAGS_NONE);
} else {
DUK_DDD(DUK_DDDPRINT("non-strict function, setting callee to actual value"));
duk_push_hobject(ctx, func);
duk_xdef_prop_stridx(ctx, i_arg, DUK_STRIDX_CALLEE, DUK_PROPDESC_FLAGS_WC);
}
/* set exotic behavior only after we're done */
if (need_map) {
/*
* Note: exotic behaviors are only enabled for arguments
* objects which have a parameter map (see E5 Section 10.6
* main algorithm, step 12).
*
* In particular, a non-strict arguments object with no
* mapped formals does *NOT* get exotic behavior, even
* for e.g. "caller" property. This seems counterintuitive
* but seems to be the case.
*/
/* cannot be strict (never mapped variables) */
DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(func));
DUK_DDD(DUK_DDDPRINT("enabling exotic behavior for arguments object"));
DUK_HOBJECT_SET_EXOTIC_ARGUMENTS(arg);
} else {
DUK_DDD(DUK_DDDPRINT("not enabling exotic behavior for arguments object"));
}
/* nice log */
DUK_DDD(DUK_DDDPRINT("final arguments related objects: "
"arguments at index %ld -> %!O "
"map at index %ld -> %!O "
"mappednames at index %ld -> %!O",
(long) i_arg, (duk_heaphdr *) duk_get_hobject(ctx, i_arg),
(long) i_map, (duk_heaphdr *) duk_get_hobject(ctx, i_map),
(long) i_mappednames, (duk_heaphdr *) duk_get_hobject(ctx, i_mappednames)));
/* [args(n) [crud] formals arguments map mappednames] -> [args [crud] arguments] */
duk_pop_2(ctx);
duk_remove(ctx, -2);
}
/* Helper for creating the arguments object and adding it to the env record
* on top of the value stack. This helper has a very strict dependency on
* the shape of the input stack.
*/
DUK_LOCAL
void duk__handle_createargs_for_call(duk_hthread *thr,
duk_hobject *func,
duk_hobject *env,
duk_idx_t num_stack_args) {
duk_context *ctx = (duk_context *) thr;
DUK_DDD(DUK_DDDPRINT("creating arguments object for function call"));
DUK_ASSERT(thr != NULL);
DUK_ASSERT(func != NULL);
DUK_ASSERT(env != NULL);
DUK_ASSERT(DUK_HOBJECT_HAS_CREATEARGS(func));
DUK_ASSERT(duk_get_top(ctx) >= num_stack_args + 1);
/* [... arg1 ... argN envobj] */
duk__create_arguments_object(thr,
func,
env,
duk_get_top(ctx) - num_stack_args - 1, /* idx_argbase */
num_stack_args);
/* [... arg1 ... argN envobj argobj] */
duk_xdef_prop_stridx(ctx,
-2,
DUK_STRIDX_LC_ARGUMENTS,
DUK_HOBJECT_HAS_STRICT(func) ? DUK_PROPDESC_FLAGS_E : /* strict: non-deletable, non-writable */
DUK_PROPDESC_FLAGS_WE); /* non-strict: non-deletable, writable */
/* [... arg1 ... argN envobj] */
}
/*
* Helper for handling a "bound function" chain when a call is being made.
*
* Follows the bound function chain until a non-bound function is found.
* Prepends the bound arguments to the value stack (at idx_func + 2),
* updating 'num_stack_args' in the process. The 'this' binding is also
* updated if necessary (at idx_func + 1). Note that for constructor calls
* the 'this' binding is never updated by [[BoundThis]].
*
* XXX: bound function chains could be collapsed at bound function creation
* time so that each bound function would point directly to a non-bound
* function. This would make call time handling much easier.
*/
DUK_LOCAL
void duk__handle_bound_chain_for_call(duk_hthread *thr,
duk_idx_t idx_func,
duk_idx_t *p_num_stack_args, /* may be changed by call */
duk_bool_t is_constructor_call) {
duk_context *ctx = (duk_context *) thr;
duk_idx_t num_stack_args;
duk_tval *tv_func;
duk_hobject *func;
duk_uint_t sanity;
DUK_ASSERT(thr != NULL);
DUK_ASSERT(p_num_stack_args != NULL);
/* On entry, item at idx_func is a bound, non-lightweight function,
* but we don't rely on that below.
*/
num_stack_args = *p_num_stack_args;
sanity = DUK_HOBJECT_BOUND_CHAIN_SANITY;
do {
duk_idx_t i, len;
tv_func = duk_require_tval(ctx, idx_func);
DUK_ASSERT(tv_func != NULL);
if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
/* Lightweight function: never bound, so terminate. */
break;
} else if (DUK_TVAL_IS_OBJECT(tv_func)) {
func = DUK_TVAL_GET_OBJECT(tv_func);
if (!DUK_HOBJECT_HAS_BOUND(func)) {
/* Normal non-bound function. */
break;
}
} else {
/* Function.prototype.bind() should never let this happen,
* ugly error message is enough.
*/
DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_INTERNAL_ERROR);
}
DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv_func) != NULL);
/* XXX: this could be more compact by accessing the internal properties
* directly as own properties (they cannot be inherited, and are not
* externally visible).
*/
DUK_DDD(DUK_DDDPRINT("bound function encountered, ptr=%p, num_stack_args=%ld: %!T",
(void *) DUK_TVAL_GET_OBJECT(tv_func), (long) num_stack_args, tv_func));
/* [ ... func this arg1 ... argN ] */
if (is_constructor_call) {
/* See: tests/ecmascript/test-spec-bound-constructor.js */
DUK_DDD(DUK_DDDPRINT("constructor call: don't update this binding"));
} else {
duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_THIS);
duk_replace(ctx, idx_func + 1); /* idx_this = idx_func + 1 */
}
/* [ ... func this arg1 ... argN ] */
/* XXX: duk_get_length? */
duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_ARGS); /* -> [ ... func this arg1 ... argN _Args ] */
duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LENGTH); /* -> [ ... func this arg1 ... argN _Args length ] */
len = (duk_idx_t) duk_require_int(ctx, -1);
duk_pop(ctx);
for (i = 0; i < len; i++) {
/* XXX: very slow - better to bulk allocate a gap, and copy
* from args_array directly (we know it has a compact array
* part, etc).
*/
/* [ ... func this <some bound args> arg1 ... argN _Args ] */
duk_get_prop_index(ctx, -1, i);
duk_insert(ctx, idx_func + 2 + i); /* idx_args = idx_func + 2 */
}
num_stack_args += len; /* must be updated to work properly (e.g. creation of 'arguments') */
duk_pop(ctx);
/* [ ... func this <bound args> arg1 ... argN ] */
duk_get_prop_stridx(ctx, idx_func, DUK_STRIDX_INT_TARGET);
duk_replace(ctx, idx_func); /* replace in stack */
DUK_DDD(DUK_DDDPRINT("bound function handled, num_stack_args=%ld, idx_func=%ld, curr func=%!T",
(long) num_stack_args, (long) idx_func, duk_get_tval(ctx, idx_func)));
} while (--sanity > 0);
if (sanity == 0) {
DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, DUK_STR_BOUND_CHAIN_LIMIT);
}
DUK_DDD(DUK_DDDPRINT("final non-bound function is: %!T", duk_get_tval(ctx, idx_func)));
#ifdef DUK_USE_ASSERTIONS
tv_func = duk_require_tval(ctx, idx_func);
DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv_func) || DUK_TVAL_IS_OBJECT(tv_func));
if (DUK_TVAL_IS_OBJECT(tv_func)) {
func = DUK_TVAL_GET_OBJECT(tv_func);
DUK_ASSERT(func != NULL);
DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func));
DUK_ASSERT(DUK_HOBJECT_HAS_COMPILEDFUNCTION(func) ||
DUK_HOBJECT_HAS_NATIVEFUNCTION(func));
}
#endif
/* write back */
*p_num_stack_args = num_stack_args;
}
/*
* Helper for setting up var_env and lex_env of an activation,
* assuming it does NOT have the DUK_HOBJECT_FLAG_NEWENV flag.
*/
DUK_LOCAL
void duk__handle_oldenv_for_call(duk_hthread *thr,
duk_hobject *func,
duk_activation *act) {
duk_tval *tv;
DUK_ASSERT(thr != NULL);
DUK_ASSERT(func != NULL);
DUK_ASSERT(act != NULL);
DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV(func));
DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(func));
tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_LEXENV(thr));
if (tv) {
DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
DUK_ASSERT(DUK_HOBJECT_IS_ENV(DUK_TVAL_GET_OBJECT(tv)));
act->lex_env = DUK_TVAL_GET_OBJECT(tv);
tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_VARENV(thr));
if (tv) {
DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
DUK_ASSERT(DUK_HOBJECT_IS_ENV(DUK_TVAL_GET_OBJECT(tv)));
act->var_env = DUK_TVAL_GET_OBJECT(tv);
} else {
act->var_env = act->lex_env;
}
} else {
act->lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
act->var_env = act->lex_env;
}
DUK_HOBJECT_INCREF_ALLOWNULL(thr, act->lex_env);
DUK_HOBJECT_INCREF_ALLOWNULL(thr, act->var_env);
}
/*
* Helper for updating callee 'caller' property.
*/
#ifdef DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
DUK_LOCAL void duk__update_func_caller_prop(duk_hthread *thr, duk_hobject *func) {
duk_tval *tv_caller;
duk_hobject *h_tmp;
duk_activation *act_callee;
duk_activation *act_caller;
DUK_ASSERT(thr != NULL);
DUK_ASSERT(func != NULL);
DUK_ASSERT(!DUK_HOBJECT_HAS_BOUND(func)); /* bound chain resolved */
DUK_ASSERT(thr->callstack_top >= 1);
if (DUK_HOBJECT_HAS_STRICT(func)) {
/* Strict functions don't get their 'caller' updated. */
return;
}
act_callee = thr->callstack + thr->callstack_top - 1;
act_caller = (thr->callstack_top >= 2 ? act_callee - 1 : NULL);
/* Backup 'caller' property and update its value. */
tv_caller = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_CALLER(thr));
if (tv_caller) {
/* If caller is global/eval code, 'caller' should be set to
* 'null'.
*
* XXX: there is no exotic flag to infer this correctly now.
* The NEWENV flag is used now which works as intended for
* everything (global code, non-strict eval code, and functions)
* except strict eval code. Bound functions are never an issue
* because 'func' has been resolved to a non-bound function.
*/
if (act_caller) {
/* act_caller->func may be NULL in some finalization cases,
* just treat like we don't know the caller.
*/
if (act_caller->func && !DUK_HOBJECT_HAS_NEWENV(act_caller->func)) {
/* Setting to NULL causes 'caller' to be set to
* 'null' as desired.
*/
act_caller = NULL;
}
}
if (DUK_TVAL_IS_OBJECT(tv_caller)) {
h_tmp = DUK_TVAL_GET_OBJECT(tv_caller);
DUK_ASSERT(h_tmp != NULL);
act_callee->prev_caller = h_tmp;
/* Previous value doesn't need refcount changes because its ownership
* is transferred to prev_caller.
*/
if (act_caller) {
DUK_ASSERT(act_caller->func != NULL);
DUK_TVAL_SET_OBJECT(tv_caller, act_caller->func);
DUK_TVAL_INCREF(thr, tv_caller);
} else {
DUK_TVAL_SET_NULL(tv_caller); /* no incref */
}
} else {
/* 'caller' must only take on 'null' or function value */
DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_caller));
DUK_ASSERT(act_callee->prev_caller == NULL);
if (act_caller && act_caller->func) {
/* Tolerate act_caller->func == NULL which happens in
* some finalization cases; treat like unknown caller.
*/
DUK_TVAL_SET_OBJECT(tv_caller, act_caller->func);
DUK_TVAL_INCREF(thr, tv_caller);
} else {
DUK_TVAL_SET_NULL(tv_caller); /* no incref */
}
}
}
}
#endif /* DUK_USE_NONSTD_FUNC_CALLER_PROPERTY */
/*
* Determine the effective 'this' binding and coerce the current value
* on the valstack to the effective one (in-place, at idx_this).
*
* The current this value in the valstack (at idx_this) represents either:
* - the caller's requested 'this' binding; or
* - a 'this' binding accumulated from the bound function chain
*
* The final 'this' binding for the target function may still be
* different, and is determined as described in E5 Section 10.4.3.
*
* For global and eval code (E5 Sections 10.4.1 and 10.4.2), we assume
* that the caller has provided the correct 'this' binding explicitly
* when calling, i.e.:
*
* - global code: this=global object
* - direct eval: this=copy from eval() caller's this binding
* - other eval: this=global object
*
* Note: this function may cause a recursive function call with arbitrary
* side effects, because ToObject() may be called.
*/
DUK_LOCAL
void duk__coerce_effective_this_binding(duk_hthread *thr,
duk_hobject *func,
duk_idx_t idx_this) {
duk_context *ctx = (duk_context *) thr;
duk_small_int_t strict;
if (func) {
strict = DUK_HOBJECT_HAS_STRICT(func);
} else {
/* Lightfuncs are always considered strict. */
strict = 1;
}
if (strict) {
DUK_DDD(DUK_DDDPRINT("this binding: strict -> use directly"));
} else {
duk_tval *tv_this = duk_require_tval(ctx, idx_this);
duk_hobject *obj_global;
if (DUK_TVAL_IS_OBJECT(tv_this)) {
DUK_DDD(DUK_DDDPRINT("this binding: non-strict, object -> use directly"));
} else if (DUK_TVAL_IS_LIGHTFUNC(tv_this)) {
/* Lightfuncs are treated like objects and not coerced. */
DUK_DDD(DUK_DDDPRINT("this binding: non-strict, lightfunc -> use directly"));
} else if (DUK_TVAL_IS_UNDEFINED(tv_this) || DUK_TVAL_IS_NULL(tv_this)) {
DUK_DDD(DUK_DDDPRINT("this binding: non-strict, undefined/null -> use global object"));
obj_global = thr->builtins[DUK_BIDX_GLOBAL];
if (obj_global) {
duk_push_hobject(ctx, obj_global);
} else {
/*
* This may only happen if built-ins are being "torn down".
* This behavior is out of specification scope.
*/
DUK_D(DUK_DPRINT("this binding: wanted to use global object, but it is NULL -> using undefined instead"));
duk_push_undefined(ctx);
}
duk_replace(ctx, idx_this);
} else {
DUK_DDD(DUK_DDDPRINT("this binding: non-strict, not object/undefined/null -> use ToObject(value)"));
duk_to_object(ctx, idx_this); /* may have side effects */
}
}
}
/*
* Shared helper for non-bound func lookup.
*
* Returns duk_hobject * to the final non-bound function (NULL for lightfunc).
*/
DUK_LOCAL
duk_hobject *duk__nonbound_func_lookup(duk_context *ctx,
duk_idx_t idx_func,
duk_idx_t *out_num_stack_args,
duk_tval **out_tv_func,
duk_small_uint_t call_flags) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_tval *tv_func;
duk_hobject *func;
for (;;) {
/* Use loop to minimize code size of relookup after bound function case */
tv_func = duk_get_tval(ctx, idx_func);
DUK_ASSERT(tv_func != NULL);
if (DUK_TVAL_IS_OBJECT(tv_func)) {
func = DUK_TVAL_GET_OBJECT(tv_func);
if (!DUK_HOBJECT_IS_CALLABLE(func)) {
goto not_callable_error;
}
if (DUK_HOBJECT_HAS_BOUND(func)) {
duk__handle_bound_chain_for_call(thr, idx_func, out_num_stack_args, call_flags & DUK_CALL_FLAG_CONSTRUCTOR_CALL);
/* The final object may be a normal function or a lightfunc.
* We need to re-lookup tv_func because it may have changed
* (also value stack may have been resized). Loop again to
* do that; we're guaranteed not to come here again.
*/
DUK_ASSERT(DUK_TVAL_IS_OBJECT(duk_require_tval(ctx, idx_func)) ||
DUK_TVAL_IS_LIGHTFUNC(duk_require_tval(ctx, idx_func)));
continue;
}
} else if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
func = NULL;
} else {
goto not_callable_error;
}
break;
}
DUK_ASSERT((DUK_TVAL_IS_OBJECT(tv_func) && DUK_HOBJECT_IS_CALLABLE(DUK_TVAL_GET_OBJECT(tv_func))) ||
DUK_TVAL_IS_LIGHTFUNC(tv_func));
DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUND(func));
DUK_ASSERT(func == NULL || (DUK_HOBJECT_IS_COMPILEDFUNCTION(func) ||
DUK_HOBJECT_IS_NATIVEFUNCTION(func)));
*out_tv_func = tv_func;
return func;
not_callable_error:
DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CALLABLE);
DUK_UNREACHABLE();
return NULL; /* never executed */
}
/*
* Value stack resize and stack top adjustment helper
*
* XXX: This should all be merged to duk_valstack_resize_raw().
*/
DUK_LOCAL
void duk__adjust_valstack_and_top(duk_hthread *thr, duk_idx_t num_stack_args, duk_idx_t idx_args, duk_idx_t nregs, duk_idx_t nargs, duk_hobject *func) {
duk_context *ctx = (duk_context *) thr;
duk_size_t vs_min_size;
duk_bool_t adjusted_top = 0;
vs_min_size = (thr->valstack_bottom - thr->valstack) + /* bottom of current func */
idx_args; /* bottom of new func */
if (nregs >= 0) {
DUK_ASSERT(nargs >= 0);
DUK_ASSERT(nregs >= nargs);
vs_min_size += nregs;
} else {
/* 'func' wants stack "as is" */
vs_min_size += num_stack_args; /* num entries of new func at entry */
}
if (func == NULL || DUK_HOBJECT_IS_NATIVEFUNCTION(func)) {
vs_min_size += DUK_VALSTACK_API_ENTRY_MINIMUM; /* Duktape/C API guaranteed entries (on top of args) */
}
vs_min_size += DUK_VALSTACK_INTERNAL_EXTRA; /* + spare */
/* XXX: Awkward fix for GH-107: we can't resize the value stack to
* a size smaller than the current top, so the order of the resize
* and adjusting the stack top depends on the current vs. final size
* of the value stack. Ideally duk_valstack_resize_raw() would have
* a combined algorithm to avoid this.
*/
if (vs_min_size < (duk_size_t) (thr->valstack_top - thr->valstack)) {
DUK_DDD(DUK_DDDPRINT(("final size smaller, set top before resize")));
DUK_ASSERT(nregs >= 0); /* can't happen when keeping current stack size */
duk_set_top(ctx, idx_args + nargs); /* clamp anything above nargs */
duk_set_top(ctx, idx_args + nregs); /* extend with undefined */
adjusted_top = 1;
}
(void) duk_valstack_resize_raw((duk_context *) thr,
vs_min_size,
DUK_VSRESIZE_FLAG_SHRINK | /* flags */
0 /* no compact */ |
DUK_VSRESIZE_FLAG_THROW);
if (!adjusted_top) {
if (nregs >= 0) {
DUK_ASSERT(nregs >= nargs);
duk_set_top(ctx, idx_args + nargs); /* clamp anything above nargs */
duk_set_top(ctx, idx_args + nregs); /* extend with undefined */
}
}
}
/*
* Helper for making various kinds of calls.
*
* Call flags:
*
* DUK_CALL_FLAG_PROTECTED <--> protected call
* DUK_CALL_FLAG_IGNORE_RECLIMIT <--> ignore C recursion limit,
* for errhandler calls
* DUK_CALL_FLAG_CONSTRUCTOR_CALL <--> for 'new Foo()' calls
*
* Input stack (thr):
*
* [ func this arg1 ... argN ]
*
* Output stack (thr):
*
* [ retval ] (DUK_EXEC_SUCCESS)
* [ errobj ] (DUK_EXEC_ERROR (normal error), protected call)
*
* Even when executing a protected call an error may be thrown in rare cases.
* For instance, if we run out of memory when setting up the return stack
* after a caught error, the out of memory is propagated to the caller.
* Similarly, API errors (such as invalid input stack shape and invalid
* indices) cause an error to propagate out of this function. If there is
* no catchpoint for this error, the fatal error handler is called.
*
* See 'execution.rst'.
*
* The allowed thread states for making a call are:
* - thr matches heap->curr_thread, and thr is already RUNNING
* - thr does not match heap->curr_thread (may be NULL or other),
* and thr is INACTIVE (in this case, a setjmp() catchpoint is
* always used for thread book-keeping to work properly)
*
* Like elsewhere, gotos are used to keep indent level minimal and
* avoiding a dozen helpers with awkward plumbing.
*
* Note: setjmp() and local variables have a nasty interaction,
* see execution.rst; non-volatile locals modified after setjmp()
* call are not guaranteed to keep their value.
*/
DUK_INTERNAL
duk_int_t duk_handle_call(duk_hthread *thr,
duk_idx_t num_stack_args,
duk_small_uint_t call_flags) {
duk_context *ctx = (duk_context *) thr;
duk_size_t entry_valstack_bottom_index;
duk_size_t entry_valstack_end;
duk_size_t entry_callstack_top;
duk_size_t entry_catchstack_top;
duk_int_t entry_call_recursion_depth;
duk_hthread *entry_curr_thread;
duk_uint_fast8_t entry_thread_state;
duk_instr_t **entry_ptr_curr_pc;
volatile duk_bool_t need_setjmp;
duk_jmpbuf * volatile old_jmpbuf_ptr = NULL; /* ptr is volatile (not the target) */
duk_idx_t idx_func; /* valstack index of 'func' and retval (relative to entry valstack_bottom) */
duk_idx_t idx_args; /* valstack index of start of args (arg1) (relative to entry valstack_bottom) */
duk_idx_t nargs; /* # argument registers target function wants (< 0 => "as is") */
duk_idx_t nregs; /* # total registers target function wants on entry (< 0 => "as is") */
duk_hobject *func; /* 'func' on stack (borrowed reference) */
duk_tval *tv_func; /* duk_tval ptr for 'func' on stack (borrowed reference) or tv_func_copy */
duk_tval tv_func_copy; /* to avoid relookups */
duk_activation *act;
duk_hobject *env;
duk_jmpbuf our_jmpbuf;
duk_tval tv_tmp;
duk_int_t retval = DUK_EXEC_ERROR;
duk_ret_t rc;
DUK_ASSERT(thr != NULL);
DUK_ASSERT(ctx != NULL);
DUK_ASSERT(num_stack_args >= 0);
/* XXX: currently NULL allocations are not supported; remove if later allowed */
DUK_ASSERT(thr->valstack != NULL);
DUK_ASSERT(thr->callstack != NULL);
DUK_ASSERT(thr->catchstack != NULL);
/*
* Preliminaries, required by setjmp() handler.
*
* Must be careful not to throw an unintended error here.
*
* Note: careful with indices like '-x'; if 'x' is zero, it
* refers to valstack_bottom.
*/
entry_valstack_bottom_index = (duk_size_t) (thr->valstack_bottom - thr->valstack);
entry_valstack_end = (duk_size_t) (thr->valstack_end - thr->valstack);
entry_callstack_top = thr->callstack_top;
entry_catchstack_top = thr->catchstack_top;
entry_call_recursion_depth = thr->heap->call_recursion_depth;
entry_curr_thread = thr->heap->curr_thread; /* Note: may be NULL if first call */
entry_thread_state = thr->state;
entry_ptr_curr_pc = thr->ptr_curr_pc; /* may be NULL */
idx_func = duk_normalize_index(ctx, -num_stack_args - 2); /* idx_func must be valid, note: non-throwing! */
idx_args = idx_func + 2; /* idx_args is not necessarily valid if num_stack_args == 0 (idx_args then equals top) */
/* Need a setjmp() catchpoint if a protected call OR if we need to
* do mandatory cleanup.
*/
need_setjmp = ((call_flags & DUK_CALL_FLAG_PROTECTED) != 0) || (thr->heap->curr_thread != thr);
DUK_DD(DUK_DDPRINT("duk_handle_call: thr=%p, num_stack_args=%ld, "
"call_flags=0x%08lx (protected=%ld, ignorerec=%ld, constructor=%ld), need_setjmp=%ld, "
"valstack_top=%ld, idx_func=%ld, idx_args=%ld, rec_depth=%ld/%ld, "
"entry_valstack_bottom_index=%ld, entry_callstack_top=%ld, entry_catchstack_top=%ld, "
"entry_call_recursion_depth=%ld, entry_curr_thread=%p, entry_thread_state=%ld",
(void *) thr,
(long) num_stack_args,
(unsigned long) call_flags,
(long) ((call_flags & DUK_CALL_FLAG_PROTECTED) != 0 ? 1 : 0),
(long) ((call_flags & DUK_CALL_FLAG_IGNORE_RECLIMIT) != 0 ? 1 : 0),
(long) ((call_flags & DUK_CALL_FLAG_CONSTRUCTOR_CALL) != 0 ? 1 : 0),
(long) need_setjmp,
(long) duk_get_top(ctx),
(long) idx_func,
(long) idx_args,
(long) thr->heap->call_recursion_depth,
(long) thr->heap->call_recursion_limit,
(long) entry_valstack_bottom_index,
(long) entry_callstack_top,
(long) entry_catchstack_top,
(long) entry_call_recursion_depth,
(void *) entry_curr_thread,
(long) entry_thread_state));
/* If thr->ptr_curr_pc is set, sync curr_pc to act->pc. Then NULL
* thr->ptr_curr_pc so that it's not accidentally used with an incorrect
* activation when side effects occur.
*/
duk_hthread_sync_and_null_currpc(thr);
/* XXX: Multiple tv_func lookups are now avoided by making a local
* copy of tv_func. Another approach would be to compute an offset
* for tv_func from valstack bottom and recomputing the tv_func
* pointer quickly as valstack + offset instead of calling duk_get_tval().
*/
if (idx_func < 0 || idx_args < 0) {
/*
* Since stack indices are not reliable, we can't do anything useful
* here. Invoke the existing setjmp catcher, or if it doesn't exist,
* call the fatal error handler.
*/
DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_CALL_ARGS);
}
/*
* Setup a setjmp() catchpoint first because even the call setup
* may fail.
*/
if (!need_setjmp) {
DUK_DDD(DUK_DDDPRINT("don't need a setjmp catchpoint"));
goto handle_call;
}
old_jmpbuf_ptr = thr->heap->lj.jmpbuf_ptr;
thr->heap->lj.jmpbuf_ptr = &our_jmpbuf;
if (DUK_SETJMP(thr->heap->lj.jmpbuf_ptr->jb) == 0) {
DUK_DDD(DUK_DDDPRINT("setjmp catchpoint setup complete"));
goto handle_call;
}
/*
* Error during setup, call, or postprocessing of the call.
* The error value is in heap->lj.value1.
*
* Note: any local variables accessed here must have their value
* assigned *before* the setjmp() call, OR they must be declared
* volatile. Otherwise their value is not guaranteed to be correct.
*
* The following are such variables:
* - duk_handle_call() parameters
* - entry_*
* - idx_func
* - idx_args
*
* The very first thing we do is restore the previous setjmp catcher.
* This means that any error in error handling will propagate outwards
* instead of causing a setjmp() re-entry above. The *only* actual
* errors that should happen here are allocation errors.
*/
DUK_DDD(DUK_DDDPRINT("error caught during protected duk_handle_call(): %!T",
(duk_tval *) &thr->heap->lj.value1));
DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW);
DUK_ASSERT(thr->callstack_top >= entry_callstack_top);
DUK_ASSERT(thr->catchstack_top >= entry_catchstack_top);
/* We don't need to sync back thr->curr_pc here because the
* bytecode executor always has a setjmp catchpoint which
* does that before errors propagate to here.
*/
/*
* Restore previous setjmp catchpoint
*/
/* Note: either pointer may be NULL (at entry), so don't assert */
DUK_DDD(DUK_DDDPRINT("restore jmpbuf_ptr: %p -> %p",
(void *) (thr && thr->heap ? thr->heap->lj.jmpbuf_ptr : NULL),
(void *) old_jmpbuf_ptr));
thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
if (!(call_flags & DUK_CALL_FLAG_PROTECTED)) {
/*
* Caller did not request a protected call but a setjmp
* catchpoint was set up to allow cleanup. So, clean up
* and rethrow.
*
* We must restore curr_thread here to ensure that its