-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsfun.c
2330 lines (2082 loc) · 72 KB
/
jsfun.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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sw=4 et tw=78:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* JS function support.
*/
#include "jsstddef.h"
#include <string.h>
#include "jstypes.h"
#include "jsbit.h"
#include "jsutil.h" /* Added by JSIFY */
#include "jsapi.h"
#include "jsarray.h"
#include "jsatom.h"
#include "jscntxt.h"
#include "jsconfig.h"
#include "jsdbgapi.h"
#include "jsfun.h"
#include "jsgc.h"
#include "jsinterp.h"
#include "jslock.h"
#include "jsnum.h"
#include "jsobj.h"
#include "jsopcode.h"
#include "jsparse.h"
#include "jsscan.h"
#include "jsscope.h"
#include "jsscript.h"
#include "jsstr.h"
#include "jsexn.h"
#if JS_HAS_GENERATORS
# include "jsiter.h"
#endif
/* Generic function/call/arguments tinyids -- also reflected bit numbers. */
enum {
CALL_ARGUMENTS = -1, /* predefined arguments local variable */
CALL_CALLEE = -2, /* reference to active function's object */
ARGS_LENGTH = -3, /* number of actual args, arity if inactive */
ARGS_CALLEE = -4, /* reference from arguments to active funobj */
FUN_ARITY = -5, /* number of formal parameters; desired argc */
FUN_NAME = -6, /* function name, "" if anonymous */
FUN_CALLER = -7 /* Function.prototype.caller, backward compat */
};
#if JSFRAME_OVERRIDE_BITS < 8
# error "not enough override bits in JSStackFrame.flags!"
#endif
#define TEST_OVERRIDE_BIT(fp, tinyid) \
((fp)->flags & JS_BIT(JSFRAME_OVERRIDE_SHIFT - ((tinyid) + 1)))
#define SET_OVERRIDE_BIT(fp, tinyid) \
((fp)->flags |= JS_BIT(JSFRAME_OVERRIDE_SHIFT - ((tinyid) + 1)))
JSBool
js_GetArgsValue(JSContext *cx, JSStackFrame *fp, jsval *vp)
{
JSObject *argsobj;
if (TEST_OVERRIDE_BIT(fp, CALL_ARGUMENTS)) {
JS_ASSERT(fp->callobj);
return OBJ_GET_PROPERTY(cx, fp->callobj,
ATOM_TO_JSID(cx->runtime->atomState
.argumentsAtom),
vp);
}
argsobj = js_GetArgsObject(cx, fp);
if (!argsobj)
return JS_FALSE;
*vp = OBJECT_TO_JSVAL(argsobj);
return JS_TRUE;
}
static JSBool
MarkArgDeleted(JSContext *cx, JSStackFrame *fp, uintN slot)
{
JSObject *argsobj;
jsval bmapval, bmapint;
size_t nbits, nbytes;
jsbitmap *bitmap;
argsobj = fp->argsobj;
(void) JS_GetReservedSlot(cx, argsobj, 0, &bmapval);
nbits = fp->argc;
JS_ASSERT(slot < nbits);
if (JSVAL_IS_VOID(bmapval)) {
if (nbits <= JSVAL_INT_BITS) {
bmapint = 0;
bitmap = (jsbitmap *) &bmapint;
} else {
nbytes = JS_HOWMANY(nbits, JS_BITS_PER_WORD) * sizeof(jsbitmap);
bitmap = (jsbitmap *) JS_malloc(cx, nbytes);
if (!bitmap)
return JS_FALSE;
memset(bitmap, 0, nbytes);
bmapval = PRIVATE_TO_JSVAL(bitmap);
JS_SetReservedSlot(cx, argsobj, 0, bmapval);
}
} else {
if (nbits <= JSVAL_INT_BITS) {
bmapint = JSVAL_TO_INT(bmapval);
bitmap = (jsbitmap *) &bmapint;
} else {
bitmap = (jsbitmap *) JSVAL_TO_PRIVATE(bmapval);
}
}
JS_SET_BIT(bitmap, slot);
if (bitmap == (jsbitmap *) &bmapint) {
bmapval = INT_TO_JSVAL(bmapint);
JS_SetReservedSlot(cx, argsobj, 0, bmapval);
}
return JS_TRUE;
}
/* NB: Infallible predicate, false does not mean error/exception. */
static JSBool
ArgWasDeleted(JSContext *cx, JSStackFrame *fp, uintN slot)
{
JSObject *argsobj;
jsval bmapval, bmapint;
jsbitmap *bitmap;
argsobj = fp->argsobj;
(void) JS_GetReservedSlot(cx, argsobj, 0, &bmapval);
if (JSVAL_IS_VOID(bmapval))
return JS_FALSE;
if (fp->argc <= JSVAL_INT_BITS) {
bmapint = JSVAL_TO_INT(bmapval);
bitmap = (jsbitmap *) &bmapint;
} else {
bitmap = (jsbitmap *) JSVAL_TO_PRIVATE(bmapval);
}
return JS_TEST_BIT(bitmap, slot) != 0;
}
JSBool
js_GetArgsProperty(JSContext *cx, JSStackFrame *fp, jsid id,
JSObject **objp, jsval *vp)
{
jsval val;
JSObject *obj;
uintN slot;
if (TEST_OVERRIDE_BIT(fp, CALL_ARGUMENTS)) {
JS_ASSERT(fp->callobj);
if (!OBJ_GET_PROPERTY(cx, fp->callobj,
ATOM_TO_JSID(cx->runtime->atomState
.argumentsAtom),
&val)) {
return JS_FALSE;
}
if (JSVAL_IS_PRIMITIVE(val)) {
obj = js_ValueToNonNullObject(cx, val);
if (!obj)
return JS_FALSE;
} else {
obj = JSVAL_TO_OBJECT(val);
}
*objp = obj;
return OBJ_GET_PROPERTY(cx, obj, id, vp);
}
*objp = NULL;
*vp = JSVAL_VOID;
if (JSID_IS_INT(id)) {
slot = (uintN) JSID_TO_INT(id);
if (slot < fp->argc) {
if (fp->argsobj && ArgWasDeleted(cx, fp, slot))
return OBJ_GET_PROPERTY(cx, fp->argsobj, id, vp);
*vp = fp->argv[slot];
} else {
/*
* Per ECMA-262 Ed. 3, 10.1.8, last bulleted item, do not share
* storage between the formal parameter and arguments[k] for all
* k >= fp->argc && k < fp->fun->nargs. For example, in
*
* function f(x) { x = 42; return arguments[0]; }
* f();
*
* the call to f should return undefined, not 42. If fp->argsobj
* is null at this point, as it would be in the example, return
* undefined in *vp.
*/
if (fp->argsobj)
return OBJ_GET_PROPERTY(cx, fp->argsobj, id, vp);
}
} else {
if (id == ATOM_TO_JSID(cx->runtime->atomState.lengthAtom)) {
if (fp->argsobj && TEST_OVERRIDE_BIT(fp, ARGS_LENGTH))
return OBJ_GET_PROPERTY(cx, fp->argsobj, id, vp);
*vp = INT_TO_JSVAL((jsint) fp->argc);
}
}
return JS_TRUE;
}
JSObject *
js_GetArgsObject(JSContext *cx, JSStackFrame *fp)
{
JSObject *argsobj, *global, *parent;
/*
* We must be in a function activation; the function must be lightweight
* or else fp must have a variable object.
*/
JS_ASSERT(fp->fun && (!(fp->fun->flags & JSFUN_HEAVYWEIGHT) || fp->varobj));
/* Skip eval and debugger frames. */
while (fp->flags & JSFRAME_SPECIAL)
fp = fp->down;
/* Create an arguments object for fp only if it lacks one. */
argsobj = fp->argsobj;
if (argsobj)
return argsobj;
/* Link the new object to fp so it can get actual argument values. */
argsobj = js_NewObject(cx, &js_ArgumentsClass, NULL, NULL);
if (!argsobj || !JS_SetPrivate(cx, argsobj, fp)) {
cx->weakRoots.newborn[GCX_OBJECT] = NULL;
return NULL;
}
/*
* Give arguments an intrinsic scope chain link to fp's global object.
* Since the arguments object lacks a prototype because js_ArgumentsClass
* is not initialized, js_NewObject won't assign a default parent to it.
*
* Therefore if arguments is used as the head of an eval scope chain (via
* a direct or indirect call to eval(program, arguments)), any reference
* to a standard class object in the program will fail to resolve due to
* js_GetClassPrototype not being able to find a global object containing
* the standard prototype by starting from arguments and following parent.
*/
global = fp->scopeChain;
while ((parent = OBJ_GET_PARENT(cx, global)) != NULL)
global = parent;
argsobj->slots[JSSLOT_PARENT] = OBJECT_TO_JSVAL(global);
fp->argsobj = argsobj;
return argsobj;
}
static JSBool
args_enumerate(JSContext *cx, JSObject *obj);
JSBool
js_PutArgsObject(JSContext *cx, JSStackFrame *fp)
{
JSObject *argsobj;
jsval bmapval, rval;
JSBool ok;
JSRuntime *rt;
/*
* Reuse args_enumerate here to reflect fp's actual arguments as indexed
* elements of argsobj. Do this first, before clearing and freeing the
* deleted argument slot bitmap, because args_enumerate depends on that.
*/
argsobj = fp->argsobj;
ok = args_enumerate(cx, argsobj);
/*
* Now clear the deleted argument number bitmap slot and free the bitmap,
* if one was actually created due to 'delete arguments[0]' or similar.
*/
(void) JS_GetReservedSlot(cx, argsobj, 0, &bmapval);
if (!JSVAL_IS_VOID(bmapval)) {
JS_SetReservedSlot(cx, argsobj, 0, JSVAL_VOID);
if (fp->argc > JSVAL_INT_BITS)
JS_free(cx, JSVAL_TO_PRIVATE(bmapval));
}
/*
* Now get the prototype properties so we snapshot fp->fun and fp->argc
* before fp goes away.
*/
rt = cx->runtime;
ok &= js_GetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.calleeAtom),
&rval);
ok &= js_SetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.calleeAtom),
&rval);
ok &= js_GetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.lengthAtom),
&rval);
ok &= js_SetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.lengthAtom),
&rval);
/*
* Clear the private pointer to fp, which is about to go away (js_Invoke).
* Do this last because the args_enumerate and js_GetProperty calls above
* need to follow the private slot to find fp.
*/
ok &= JS_SetPrivate(cx, argsobj, NULL);
fp->argsobj = NULL;
return ok;
}
static JSBool
args_delProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
jsint slot;
JSStackFrame *fp;
if (!JSVAL_IS_INT(id))
return JS_TRUE;
fp = (JSStackFrame *)
JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
if (!fp)
return JS_TRUE;
JS_ASSERT(fp->argsobj);
slot = JSVAL_TO_INT(id);
switch (slot) {
case ARGS_CALLEE:
case ARGS_LENGTH:
SET_OVERRIDE_BIT(fp, slot);
break;
default:
if ((uintN)slot < fp->argc && !MarkArgDeleted(cx, fp, slot))
return JS_FALSE;
break;
}
return JS_TRUE;
}
static JSBool
args_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
jsint slot;
JSStackFrame *fp;
if (!JSVAL_IS_INT(id))
return JS_TRUE;
fp = (JSStackFrame *)
JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
if (!fp)
return JS_TRUE;
JS_ASSERT(fp->argsobj);
slot = JSVAL_TO_INT(id);
switch (slot) {
case ARGS_CALLEE:
if (!TEST_OVERRIDE_BIT(fp, slot))
*vp = fp->argv ? fp->argv[-2] : OBJECT_TO_JSVAL(fp->fun->object);
break;
case ARGS_LENGTH:
if (!TEST_OVERRIDE_BIT(fp, slot))
*vp = INT_TO_JSVAL((jsint)fp->argc);
break;
default:
if ((uintN)slot < fp->argc && !ArgWasDeleted(cx, fp, slot))
*vp = fp->argv[slot];
break;
}
return JS_TRUE;
}
static JSBool
args_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
JSStackFrame *fp;
jsint slot;
if (!JSVAL_IS_INT(id))
return JS_TRUE;
fp = (JSStackFrame *)
JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
if (!fp)
return JS_TRUE;
JS_ASSERT(fp->argsobj);
slot = JSVAL_TO_INT(id);
switch (slot) {
case ARGS_CALLEE:
case ARGS_LENGTH:
SET_OVERRIDE_BIT(fp, slot);
break;
default:
if (FUN_INTERPRETED(fp->fun) &&
(uintN)slot < fp->argc &&
!ArgWasDeleted(cx, fp, slot)) {
fp->argv[slot] = *vp;
}
break;
}
return JS_TRUE;
}
static JSBool
args_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
JSObject **objp)
{
JSStackFrame *fp;
uintN slot;
JSString *str;
JSAtom *atom;
intN tinyid;
jsval value;
*objp = NULL;
fp = (JSStackFrame *)
JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
if (!fp)
return JS_TRUE;
JS_ASSERT(fp->argsobj);
if (JSVAL_IS_INT(id)) {
slot = JSVAL_TO_INT(id);
if (slot < fp->argc && !ArgWasDeleted(cx, fp, slot)) {
/* XXX ECMA specs DontEnum, contrary to other array-like objects */
if (!js_DefineProperty(cx, obj, INT_JSVAL_TO_JSID(id),
fp->argv[slot],
args_getProperty, args_setProperty,
JS_VERSION_IS_ECMA(cx)
? 0
: JSPROP_ENUMERATE,
NULL)) {
return JS_FALSE;
}
*objp = obj;
}
} else {
str = JSVAL_TO_STRING(id);
atom = cx->runtime->atomState.lengthAtom;
if (str == ATOM_TO_STRING(atom)) {
tinyid = ARGS_LENGTH;
value = INT_TO_JSVAL(fp->argc);
} else {
atom = cx->runtime->atomState.calleeAtom;
if (str == ATOM_TO_STRING(atom)) {
tinyid = ARGS_CALLEE;
value = fp->argv ? fp->argv[-2]
: OBJECT_TO_JSVAL(fp->fun->object);
} else {
atom = NULL;
/* Quell GCC overwarnings. */
tinyid = 0;
value = JSVAL_NULL;
}
}
if (atom && !TEST_OVERRIDE_BIT(fp, tinyid)) {
if (!js_DefineNativeProperty(cx, obj, ATOM_TO_JSID(atom), value,
args_getProperty, args_setProperty, 0,
SPROP_HAS_SHORTID, tinyid, NULL)) {
return JS_FALSE;
}
*objp = obj;
}
}
return JS_TRUE;
}
static JSBool
args_enumerate(JSContext *cx, JSObject *obj)
{
JSStackFrame *fp;
JSObject *pobj;
JSProperty *prop;
uintN slot, argc;
fp = (JSStackFrame *)
JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
if (!fp)
return JS_TRUE;
JS_ASSERT(fp->argsobj);
/*
* Trigger reflection with value snapshot in args_resolve using a series
* of js_LookupProperty calls. We handle length, callee, and the indexed
* argument properties. We know that args_resolve covers all these cases
* and creates direct properties of obj, but that it may fail to resolve
* length or callee if overridden.
*/
if (!js_LookupProperty(cx, obj,
ATOM_TO_JSID(cx->runtime->atomState.lengthAtom),
&pobj, &prop)) {
return JS_FALSE;
}
if (prop)
OBJ_DROP_PROPERTY(cx, pobj, prop);
if (!js_LookupProperty(cx, obj,
ATOM_TO_JSID(cx->runtime->atomState.calleeAtom),
&pobj, &prop)) {
return JS_FALSE;
}
if (prop)
OBJ_DROP_PROPERTY(cx, pobj, prop);
argc = fp->argc;
for (slot = 0; slot < argc; slot++) {
if (!js_LookupProperty(cx, obj, INT_TO_JSID((jsint)slot), &pobj, &prop))
return JS_FALSE;
if (prop)
OBJ_DROP_PROPERTY(cx, pobj, prop);
}
return JS_TRUE;
}
#if JS_HAS_GENERATORS
/*
* If a generator-iterator's arguments or call object escapes, it needs to
* mark its generator object.
*/
static uint32
args_or_call_mark(JSContext *cx, JSObject *obj, void *arg)
{
JSStackFrame *fp;
fp = JS_GetPrivate(cx, obj);
if (fp && (fp->flags & JSFRAME_GENERATOR))
GC_MARK(cx, FRAME_TO_GENERATOR(fp)->obj, "FRAME_TO_GENERATOR(fp)->obj");
return 0;
}
#else
# define args_or_call_mark NULL
#endif
/*
* The Arguments class is not initialized via JS_InitClass, and must not be,
* because its name is "Object". Per ECMA, that causes instances of it to
* delegate to the object named by Object.prototype. It also ensures that
* arguments.toString() returns "[object Object]".
*
* The JSClass functions below collaborate to lazily reflect and synchronize
* actual argument values, argument count, and callee function object stored
* in a JSStackFrame with their corresponding property values in the frame's
* arguments object.
*/
JSClass js_ArgumentsClass = {
js_Object_str,
JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE | JSCLASS_HAS_RESERVED_SLOTS(1) |
JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
JS_PropertyStub, args_delProperty,
args_getProperty, args_setProperty,
args_enumerate, (JSResolveOp) args_resolve,
JS_ConvertStub, JS_FinalizeStub,
NULL, NULL,
NULL, NULL,
NULL, NULL,
args_or_call_mark, NULL
};
JSObject *
js_GetCallObject(JSContext *cx, JSStackFrame *fp, JSObject *parent)
{
JSObject *callobj, *funobj;
/* Create a call object for fp only if it lacks one. */
JS_ASSERT(fp->fun);
callobj = fp->callobj;
if (callobj)
return callobj;
JS_ASSERT(fp->fun);
/* The default call parent is its function's parent (static link). */
if (!parent) {
funobj = fp->argv ? JSVAL_TO_OBJECT(fp->argv[-2]) : fp->fun->object;
if (funobj)
parent = OBJ_GET_PARENT(cx, funobj);
}
/* Create the call object and link it to its stack frame. */
callobj = js_NewObject(cx, &js_CallClass, NULL, parent);
if (!callobj || !JS_SetPrivate(cx, callobj, fp)) {
cx->weakRoots.newborn[GCX_OBJECT] = NULL;
return NULL;
}
fp->callobj = callobj;
/* Make callobj be the scope chain and the variables object. */
JS_ASSERT(fp->scopeChain == parent);
fp->scopeChain = callobj;
fp->varobj = callobj;
return callobj;
}
static JSBool
call_enumerate(JSContext *cx, JSObject *obj);
JSBool
js_PutCallObject(JSContext *cx, JSStackFrame *fp)
{
JSObject *callobj;
JSBool ok;
jsid argsid;
jsval aval;
/*
* Reuse call_enumerate here to reflect all actual args and vars into the
* call object from fp.
*/
callobj = fp->callobj;
if (!callobj)
return JS_TRUE;
ok = call_enumerate(cx, callobj);
/*
* Get the arguments object to snapshot fp's actual argument values.
*/
if (fp->argsobj) {
argsid = ATOM_TO_JSID(cx->runtime->atomState.argumentsAtom);
ok &= js_GetProperty(cx, callobj, argsid, &aval);
ok &= js_SetProperty(cx, callobj, argsid, &aval);
ok &= js_PutArgsObject(cx, fp);
}
/*
* Clear the private pointer to fp, which is about to go away (js_Invoke).
* Do this last because the call_enumerate and js_GetProperty calls above
* need to follow the private slot to find fp.
*/
ok &= JS_SetPrivate(cx, callobj, NULL);
fp->callobj = NULL;
return ok;
}
static JSPropertySpec call_props[] = {
{js_arguments_str, CALL_ARGUMENTS, JSPROP_PERMANENT,0,0},
{"__callee__", CALL_CALLEE, 0,0,0},
{0,0,0,0,0}
};
static JSBool
call_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
JSStackFrame *fp;
jsint slot;
if (!JSVAL_IS_INT(id))
return JS_TRUE;
fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
if (!fp)
return JS_TRUE;
JS_ASSERT(fp->fun);
slot = JSVAL_TO_INT(id);
switch (slot) {
case CALL_ARGUMENTS:
if (!TEST_OVERRIDE_BIT(fp, slot)) {
JSObject *argsobj = js_GetArgsObject(cx, fp);
if (!argsobj)
return JS_FALSE;
*vp = OBJECT_TO_JSVAL(argsobj);
}
break;
case CALL_CALLEE:
if (!TEST_OVERRIDE_BIT(fp, slot))
*vp = fp->argv ? fp->argv[-2] : OBJECT_TO_JSVAL(fp->fun->object);
break;
default:
if ((uintN)slot < JS_MAX(fp->argc, fp->fun->nargs))
*vp = fp->argv[slot];
break;
}
return JS_TRUE;
}
static JSBool
call_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
JSStackFrame *fp;
jsint slot;
if (!JSVAL_IS_INT(id))
return JS_TRUE;
fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
if (!fp)
return JS_TRUE;
JS_ASSERT(fp->fun);
slot = JSVAL_TO_INT(id);
switch (slot) {
case CALL_ARGUMENTS:
case CALL_CALLEE:
SET_OVERRIDE_BIT(fp, slot);
break;
default:
if ((uintN)slot < JS_MAX(fp->argc, fp->fun->nargs))
fp->argv[slot] = *vp;
break;
}
return JS_TRUE;
}
JSBool
js_GetCallVariable(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
JSStackFrame *fp;
JS_ASSERT(JSVAL_IS_INT(id));
fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
if (fp) {
/* XXX no jsint slot commoning here to avoid MSVC1.52 crashes */
if ((uintN)JSVAL_TO_INT(id) < fp->nvars)
*vp = fp->vars[JSVAL_TO_INT(id)];
}
return JS_TRUE;
}
JSBool
js_SetCallVariable(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
JSStackFrame *fp;
JS_ASSERT(JSVAL_IS_INT(id));
fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
if (fp) {
/* XXX jsint slot is block-local here to avoid MSVC1.52 crashes */
jsint slot = JSVAL_TO_INT(id);
if ((uintN)slot < fp->nvars)
fp->vars[slot] = *vp;
}
return JS_TRUE;
}
static JSBool
call_enumerate(JSContext *cx, JSObject *obj)
{
JSStackFrame *fp;
JSObject *funobj, *pobj;
JSScope *scope;
JSScopeProperty *sprop, *cprop;
JSPropertyOp getter;
jsval *vec;
JSAtom *atom;
JSProperty *prop;
fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
if (!fp)
return JS_TRUE;
/*
* Do not enumerate a cloned function object at fp->argv[-2], it may have
* gained its own (mutable) scope (e.g., a brutally-shared XUL script sets
* the clone's prototype property). We must enumerate the function object
* that was decorated with parameter and local variable properties by the
* compiler when the compiler created fp->fun, namely fp->fun->object.
*
* Contrast with call_resolve, where we prefer fp->argv[-2], because we'll
* use js_LookupProperty to find any overridden properties in that object,
* if it was a mutated clone; and if not, we will search its prototype,
* fp->fun->object, to find compiler-created params and locals.
*/
funobj = fp->fun->object;
if (!funobj)
return JS_TRUE;
/*
* Reflect actual args from fp->argv for formal parameters, and local vars
* and functions in fp->vars for declared variables and nested-at-top-level
* local functions.
*/
scope = OBJ_SCOPE(funobj);
for (sprop = SCOPE_LAST_PROP(scope); sprop; sprop = sprop->parent) {
getter = sprop->getter;
if (getter == js_GetArgument)
vec = fp->argv;
else if (getter == js_GetLocalVariable)
vec = fp->vars;
else
continue;
/* Trigger reflection by looking up the unhidden atom for sprop->id. */
JS_ASSERT(JSID_IS_ATOM(sprop->id));
atom = JSID_TO_ATOM(sprop->id);
JS_ASSERT(atom->flags & ATOM_HIDDEN);
atom = atom->entry.value;
if (!js_LookupProperty(cx, obj, ATOM_TO_JSID(atom), &pobj, &prop))
return JS_FALSE;
/*
* If we found the property in a different object, don't try sticking
* it into wrong slots vector. This can occur because we have a mutable
* __proto__ slot, and cloned function objects rely on their __proto__
* to delegate to the object that contains the var and arg properties.
*/
if (!prop || pobj != obj) {
if (prop)
OBJ_DROP_PROPERTY(cx, pobj, prop);
continue;
}
cprop = (JSScopeProperty *)prop;
LOCKED_OBJ_SET_SLOT(obj, cprop->slot, vec[(uint16) sprop->shortid]);
OBJ_DROP_PROPERTY(cx, obj, prop);
}
return JS_TRUE;
}
static JSBool
call_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
JSObject **objp)
{
JSStackFrame *fp;
JSObject *funobj;
JSString *str;
JSAtom *atom;
JSObject *obj2;
JSProperty *prop;
JSScopeProperty *sprop;
JSPropertyOp getter, setter;
uintN attrs, slot, nslots, spflags;
jsval *vp, value;
intN shortid;
fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
if (!fp)
return JS_TRUE;
JS_ASSERT(fp->fun);
if (!JSVAL_IS_STRING(id))
return JS_TRUE;
funobj = fp->argv ? JSVAL_TO_OBJECT(fp->argv[-2]) : fp->fun->object;
if (!funobj)
return JS_TRUE;
JS_ASSERT((JSFunction *) JS_GetPrivate(cx, funobj) == fp->fun);
str = JSVAL_TO_STRING(id);
atom = js_AtomizeString(cx, str, 0);
if (!atom)
return JS_FALSE;
if (!js_LookupHiddenProperty(cx, funobj, ATOM_TO_JSID(atom), &obj2, &prop))
return JS_FALSE;
if (prop) {
if (!OBJ_IS_NATIVE(obj2)) {
OBJ_DROP_PROPERTY(cx, obj2, prop);
return JS_TRUE;
}
sprop = (JSScopeProperty *) prop;
getter = sprop->getter;
attrs = sprop->attrs & ~JSPROP_SHARED;
slot = (uintN) sprop->shortid;
OBJ_DROP_PROPERTY(cx, obj2, prop);
/* Ensure we found an arg or var property for the same function. */
if ((sprop->flags & SPROP_IS_HIDDEN) &&
(obj2 == funobj ||
(JSFunction *) JS_GetPrivate(cx, obj2) == fp->fun)) {
if (getter == js_GetArgument) {
vp = fp->argv;
nslots = JS_MAX(fp->argc, fp->fun->nargs);
getter = setter = NULL;
} else {
JS_ASSERT(getter == js_GetLocalVariable);
vp = fp->vars;
nslots = fp->nvars;
getter = js_GetCallVariable;
setter = js_SetCallVariable;
}
if (slot < nslots) {
value = vp[slot];
spflags = SPROP_HAS_SHORTID;
shortid = (intN) slot;
} else {
value = JSVAL_VOID;
spflags = 0;
shortid = 0;
}
if (!js_DefineNativeProperty(cx, obj, ATOM_TO_JSID(atom), value,
getter, setter, attrs,
spflags, shortid, NULL)) {
return JS_FALSE;
}
*objp = obj;
}
}
return JS_TRUE;
}
static JSBool
call_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp)
{
JSStackFrame *fp;
if (type == JSTYPE_FUNCTION) {
fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
if (fp) {
JS_ASSERT(fp->fun);
*vp = fp->argv ? fp->argv[-2] : OBJECT_TO_JSVAL(fp->fun->object);
}
}
return JS_TRUE;
}
JSClass js_CallClass = {
js_Call_str,
JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE | JSCLASS_IS_ANONYMOUS |
JSCLASS_HAS_CACHED_PROTO(JSProto_Call),
JS_PropertyStub, JS_PropertyStub,
call_getProperty, call_setProperty,
call_enumerate, (JSResolveOp)call_resolve,
call_convert, JS_FinalizeStub,
NULL, NULL,
NULL, NULL,
NULL, NULL,
args_or_call_mark, NULL,
};
/*
* ECMA-262 specifies that length is a property of function object instances,
* but we can avoid that space cost by delegating to a prototype property that
* is JSPROP_PERMANENT and JSPROP_SHARED. Each fun_getProperty call computes
* a fresh length value based on the arity of the individual function object's
* private data.
*
* The extensions below other than length, i.e., the ones not in ECMA-262,
* are neither JSPROP_READONLY nor JSPROP_SHARED, because for compatibility
* with ECMA we must allow a delegating object to override them.
*/
#define LENGTH_PROP_ATTRS (JSPROP_READONLY|JSPROP_PERMANENT|JSPROP_SHARED)
static JSPropertySpec function_props[] = {
{js_arguments_str, CALL_ARGUMENTS, JSPROP_PERMANENT, 0,0},
{js_arity_str, FUN_ARITY, JSPROP_PERMANENT, 0,0},
{js_caller_str, FUN_CALLER, JSPROP_PERMANENT, 0,0},
{js_length_str, ARGS_LENGTH, LENGTH_PROP_ATTRS, 0,0},
{js_name_str, FUN_NAME, JSPROP_PERMANENT, 0,0},
{0,0,0,0,0}
};
static JSBool
fun_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
jsint slot;
JSFunction *fun;
JSStackFrame *fp;
if (!JSVAL_IS_INT(id))
return JS_TRUE;
slot = JSVAL_TO_INT(id);
/*
* Loop because getter and setter can be delegated from another class,
* but loop only for ARGS_LENGTH because we must pretend that f.length
* is in each function instance f, per ECMA-262, instead of only in the
* Function.prototype object (we use JSPROP_PERMANENT with JSPROP_SHARED
* to make it appear so).
*
* This code couples tightly to the attributes for the function_props[]
* initializers above, and to js_SetProperty and js_HasOwnPropertyHelper.
*
* It's important to allow delegating objects, even though they inherit
* this getter (fun_getProperty), to override arguments, arity, caller,
* and name. If we didn't return early for slot != ARGS_LENGTH, we would
* clobber *vp with the native property value, instead of letting script
* override that value in delegating objects.