-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsexec.js
944 lines (827 loc) · 28 KB
/
jsexec.js
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
/* ***** BEGIN LICENSE BLOCK *****
* vim: set ts=4 sw=4 et tw=80:
*
* 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 the Narcissus JavaScript engine.
*
* The Initial Developer of the Original Code is
* Brendan Eich <brendan@mozilla.org>.
* Portions created by the Initial Developer are Copyright (C) 2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either 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 ***** */
/*
* Narcissus - JS implemented in JS.
*
* Execution of parse trees.
*
* Standard classes except for eval, Function, Array, and String are borrowed
* from the host JS environment. Function is metacircular. Array and String
* are reflected via wrapping the corresponding native constructor and adding
* an extra level of prototype-based delegation.
*/
const GLOBAL_CODE = 0, EVAL_CODE = 1, FUNCTION_CODE = 2;
function ExecutionContext(type) {
this.type = type;
}
var global = {
// Value properties.
NaN: NaN, Infinity: Infinity, undefined: undefined,
// Function properties.
eval: function eval(s) {
if (typeof s != "string")
return s;
var x = ExecutionContext.current;
var x2 = new ExecutionContext(EVAL_CODE);
x2.thisObject = x.thisObject;
x2.caller = x.caller;
x2.callee = x.callee;
x2.scope = x.scope;
ExecutionContext.current = x2;
try {
execute(parse(s), x2);
} catch (e if e == THROW) {
x.result = x2.result;
throw e;
} finally {
ExecutionContext.current = x;
}
return x2.result;
},
parseInt: parseInt, parseFloat: parseFloat,
isNaN: isNaN, isFinite: isFinite,
decodeURI: decodeURI, encodeURI: encodeURI,
decodeURIComponent: decodeURIComponent,
encodeURIComponent: encodeURIComponent,
// Class constructors. Where ECMA-262 requires C.length == 1, we declare
// a dummy formal parameter.
Object: Object,
Function: function Function(dummy) {
var p = "", b = "", n = arguments.length;
if (n) {
var m = n - 1;
if (m) {
p += arguments[0];
for (var k = 1; k < m; k++)
p += "," + arguments[k];
}
b += arguments[m];
}
// XXX We want to pass a good file and line to the tokenizer.
// Note the anonymous name to maintain parity with Spidermonkey.
var t = new Tokenizer("anonymous(" + p + ") {" + b + "}");
// NB: Use the STATEMENT_FORM constant since we don't want to push this
// function onto the null compilation context.
var f = FunctionDefinition(t, null, false, STATEMENT_FORM);
var s = {object: global, parent: null};
return new FunctionObject(f, s);
},
Array: function Array(dummy) {
// Array when called as a function acts as a constructor.
return GLOBAL.Array.apply(this, arguments);
},
String: function String(s) {
// Called as function or constructor: convert argument to string type.
s = arguments.length ? "" + s : "";
if (this instanceof String) {
// Called as constructor: save the argument as the string value
// of this String object and return this object.
this.value = s;
return this;
}
return s;
},
Boolean: Boolean, Number: Number, Date: Date, RegExp: RegExp,
Error: Error, EvalError: EvalError, RangeError: RangeError,
ReferenceError: ReferenceError, SyntaxError: SyntaxError,
TypeError: TypeError, URIError: URIError,
// Other properties.
Math: Math,
// Extensions to ECMA.
snarf: snarf, evaluate: evaluate,
load: function load(s) {
if (typeof s != "string")
return s;
evaluate(snarf(s), s, 1)
},
print: print, version: null
};
// Helper to avoid Object.prototype.hasOwnProperty polluting scope objects.
function hasDirectProperty(o, p) {
return Object.prototype.hasOwnProperty.call(o, p);
}
// Reflect a host class into the target global environment by delegation.
function reflectClass(name, proto) {
var gctor = global[name];
gctor.__defineProperty__('prototype', proto, true, true, true);
proto.__defineProperty__('constructor', gctor, false, false, true);
return proto;
}
// Reflect Array -- note that all Array methods are generic.
reflectClass('Array', new Array);
// Reflect String, overriding non-generic methods.
var gSp = reflectClass('String', new String);
gSp.toSource = function () { return this.value.toSource(); };
gSp.toString = function () { return this.value; };
gSp.valueOf = function () { return this.value; };
global.String.fromCharCode = String.fromCharCode;
var XCp = ExecutionContext.prototype;
ExecutionContext.current = XCp.caller = XCp.callee = null;
XCp.scope = {object: global, parent: null};
XCp.thisObject = global;
XCp.result = undefined;
XCp.target = null;
XCp.ecmaStrictMode = false;
function Reference(base, propertyName, node) {
this.base = base;
this.propertyName = propertyName;
this.node = node;
}
Reference.prototype.toString = function () { return this.node.getSource(); }
function getValue(v) {
if (v instanceof Reference) {
if (!v.base) {
throw new ReferenceError(v.propertyName + " is not defined",
v.node.filename, v.node.lineno);
}
return v.base[v.propertyName];
}
return v;
}
function putValue(v, w, vn) {
if (v instanceof Reference)
return (v.base || global)[v.propertyName] = w;
throw new ReferenceError("Invalid assignment left-hand side",
vn.filename, vn.lineno);
}
function isPrimitive(v) {
var t = typeof v;
return (t == "object") ? v === null : t != "function";
}
function isObject(v) {
var t = typeof v;
return (t == "object") ? v !== null : t == "function";
}
// If r instanceof Reference, v == getValue(r); else v === r. If passed, rn
// is the node whose execute result was r.
function toObject(v, r, rn) {
switch (typeof v) {
case "boolean":
return new global.Boolean(v);
case "number":
return new global.Number(v);
case "string":
return new global.String(v);
case "function":
return v;
case "object":
if (v !== null)
return v;
}
var message = r + " (type " + (typeof v) + ") has no properties";
throw rn ? new TypeError(message, rn.filename, rn.lineno)
: new TypeError(message);
}
function execute(n, x) {
var a, f, i, j, r, s, t, u, v;
switch (n.type) {
case FUNCTION:
if (n.functionForm != DECLARED_FORM) {
if (!n.name || n.functionForm == STATEMENT_FORM) {
v = new FunctionObject(n, x.scope);
if (n.functionForm == STATEMENT_FORM)
x.scope.object.__defineProperty__(n.name, v, true);
} else {
t = new Object;
x.scope = {object: t, parent: x.scope};
try {
v = new FunctionObject(n, x.scope);
t.__defineProperty__(n.name, v, true, true);
} finally {
x.scope = x.scope.parent;
}
}
}
break;
case SCRIPT:
t = x.scope.object;
a = n.funDecls;
for (i = 0, j = a.length; i < j; i++) {
s = a[i].name;
f = new FunctionObject(a[i], x.scope);
t.__defineProperty__(s, f, x.type != EVAL_CODE);
}
a = n.varDecls;
for (i = 0, j = a.length; i < j; i++) {
u = a[i];
s = u.name;
if (u.readOnly && hasDirectProperty(t, s)) {
throw new TypeError("Redeclaration of const " + s,
u.filename, u.lineno);
}
if (u.readOnly || !hasDirectProperty(t, s)) {
t.__defineProperty__(s, undefined, x.type != EVAL_CODE,
u.readOnly);
}
}
// FALL THROUGH
case BLOCK:
for (i = 0, j = n.length; i < j; i++)
execute(n[i], x);
break;
case IF:
if (getValue(execute(n.condition, x)))
execute(n.thenPart, x);
else if (n.elsePart)
execute(n.elsePart, x);
break;
case SWITCH:
s = getValue(execute(n.discriminant, x));
a = n.cases;
var matchDefault = false;
switch_loop:
for (i = 0, j = a.length; ; i++) {
if (i == j) {
if (n.defaultIndex >= 0) {
i = n.defaultIndex - 1; // no case matched, do default
matchDefault = true;
continue;
}
break; // no default, exit switch_loop
}
t = a[i]; // next case (might be default!)
if (t.type == CASE) {
u = getValue(execute(t.caseLabel, x));
} else {
if (!matchDefault) // not defaulting, skip for now
continue;
u = s; // force match to do default
}
if (u === s) {
for (;;) { // this loop exits switch_loop
if (t.statements.length) {
try {
execute(t.statements, x);
} catch (e if e == BREAK && x.target == n) {
break switch_loop;
}
}
if (++i == j)
break switch_loop;
t = a[i];
}
// NOT REACHED
}
}
break;
case FOR:
n.setup && getValue(execute(n.setup, x));
// FALL THROUGH
case WHILE:
while (!n.condition || getValue(execute(n.condition, x))) {
try {
execute(n.body, x);
} catch (e if e == BREAK && x.target == n) {
break;
} catch (e if e == CONTINUE && x.target == n) {
continue;
}
n.update && getValue(execute(n.update, x));
}
break;
case FOR_IN:
u = n.varDecl;
if (u)
execute(u, x);
r = n.iterator;
s = execute(n.object, x);
v = getValue(s);
// ECMA deviation to track extant browser JS implementation behavior.
t = (v == null && !x.ecmaStrictMode) ? v : toObject(v, s, n.object);
a = [];
for (i in t)
a.push(i);
for (i = 0, j = a.length; i < j; i++) {
putValue(execute(r, x), a[i], r);
try {
execute(n.body, x);
} catch (e if e == BREAK && x.target == n) {
break;
} catch (e if e == CONTINUE && x.target == n) {
continue;
}
}
break;
case DO:
do {
try {
execute(n.body, x);
} catch (e if e == BREAK && x.target == n) {
break;
} catch (e if e == CONTINUE && x.target == n) {
continue;
}
} while (getValue(execute(n.condition, x)));
break;
case BREAK:
case CONTINUE:
x.target = n.target;
throw n.type;
case TRY:
try {
execute(n.tryBlock, x);
} catch (e if e == THROW && (j = n.catchClauses.length)) {
e = x.result;
x.result = undefined;
for (i = 0; ; i++) {
if (i == j) {
x.result = e;
throw THROW;
}
t = n.catchClauses[i];
x.scope = {object: {}, parent: x.scope};
x.scope.object.__defineProperty__(t.varName, e, true);
try {
if (t.guard && !getValue(execute(t.guard, x)))
continue;
execute(t.block, x);
break;
} finally {
x.scope = x.scope.parent;
}
}
} finally {
if (n.finallyBlock)
execute(n.finallyBlock, x);
}
break;
case THROW:
x.result = getValue(execute(n.exception, x));
throw THROW;
case RETURN:
x.result = getValue(execute(n.value, x));
throw RETURN;
case WITH:
r = execute(n.object, x);
t = toObject(getValue(r), r, n.object);
x.scope = {object: t, parent: x.scope};
try {
execute(n.body, x);
} finally {
x.scope = x.scope.parent;
}
break;
case VAR:
case CONST:
for (i = 0, j = n.length; i < j; i++) {
u = n[i].initializer;
if (!u)
continue;
t = n[i].name;
for (s = x.scope; s; s = s.parent) {
if (hasDirectProperty(s.object, t))
break;
}
u = getValue(execute(u, x));
if (n.type == CONST)
s.object.__defineProperty__(t, u, x.type != EVAL_CODE, true);
else
s.object[t] = u;
}
break;
case DEBUGGER:
throw "NYI: " + tokens[n.type];
case SEMICOLON:
if (n.expression)
x.result = getValue(execute(n.expression, x));
break;
case LABEL:
try {
execute(n.statement, x);
} catch (e if e == BREAK && x.target == n) {
}
break;
case COMMA:
for (i = 0, j = n.length; i < j; i++)
v = getValue(execute(n[i], x));
break;
case ASSIGN:
r = execute(n[0], x);
t = n[0].assignOp;
if (t)
u = getValue(r);
v = getValue(execute(n[1], x));
if (t) {
switch (t) {
case BITWISE_OR: v = u | v; break;
case BITWISE_XOR: v = u ^ v; break;
case BITWISE_AND: v = u & v; break;
case LSH: v = u << v; break;
case RSH: v = u >> v; break;
case URSH: v = u >>> v; break;
case PLUS: v = u + v; break;
case MINUS: v = u - v; break;
case MUL: v = u * v; break;
case DIV: v = u / v; break;
case MOD: v = u % v; break;
}
}
putValue(r, v, n[0]);
break;
case HOOK:
v = getValue(execute(n[0], x)) ? getValue(execute(n[1], x))
: getValue(execute(n[2], x));
break;
case OR:
v = getValue(execute(n[0], x)) || getValue(execute(n[1], x));
break;
case AND:
v = getValue(execute(n[0], x)) && getValue(execute(n[1], x));
break;
case BITWISE_OR:
v = getValue(execute(n[0], x)) | getValue(execute(n[1], x));
break;
case BITWISE_XOR:
v = getValue(execute(n[0], x)) ^ getValue(execute(n[1], x));
break;
case BITWISE_AND:
v = getValue(execute(n[0], x)) & getValue(execute(n[1], x));
break;
case EQ:
v = getValue(execute(n[0], x)) == getValue(execute(n[1], x));
break;
case NE:
v = getValue(execute(n[0], x)) != getValue(execute(n[1], x));
break;
case STRICT_EQ:
v = getValue(execute(n[0], x)) === getValue(execute(n[1], x));
break;
case STRICT_NE:
v = getValue(execute(n[0], x)) !== getValue(execute(n[1], x));
break;
case LT:
v = getValue(execute(n[0], x)) < getValue(execute(n[1], x));
break;
case LE:
v = getValue(execute(n[0], x)) <= getValue(execute(n[1], x));
break;
case GE:
v = getValue(execute(n[0], x)) >= getValue(execute(n[1], x));
break;
case GT:
v = getValue(execute(n[0], x)) > getValue(execute(n[1], x));
break;
case IN:
v = getValue(execute(n[0], x)) in getValue(execute(n[1], x));
break;
case INSTANCEOF:
t = getValue(execute(n[0], x));
u = getValue(execute(n[1], x));
if (isObject(u) && typeof u.__hasInstance__ == "function")
v = u.__hasInstance__(t);
else
v = t instanceof u;
break;
case LSH:
v = getValue(execute(n[0], x)) << getValue(execute(n[1], x));
break;
case RSH:
v = getValue(execute(n[0], x)) >> getValue(execute(n[1], x));
break;
case URSH:
v = getValue(execute(n[0], x)) >>> getValue(execute(n[1], x));
break;
case PLUS:
v = getValue(execute(n[0], x)) + getValue(execute(n[1], x));
break;
case MINUS:
v = getValue(execute(n[0], x)) - getValue(execute(n[1], x));
break;
case MUL:
v = getValue(execute(n[0], x)) * getValue(execute(n[1], x));
break;
case DIV:
v = getValue(execute(n[0], x)) / getValue(execute(n[1], x));
break;
case MOD:
v = getValue(execute(n[0], x)) % getValue(execute(n[1], x));
break;
case DELETE:
t = execute(n[0], x);
v = !(t instanceof Reference) || delete t.base[t.propertyName];
break;
case VOID:
getValue(execute(n[0], x));
break;
case TYPEOF:
t = execute(n[0], x);
if (t instanceof Reference)
t = t.base ? t.base[t.propertyName] : undefined;
v = typeof t;
break;
case NOT:
v = !getValue(execute(n[0], x));
break;
case BITWISE_NOT:
v = ~getValue(execute(n[0], x));
break;
case UNARY_PLUS:
v = +getValue(execute(n[0], x));
break;
case UNARY_MINUS:
v = -getValue(execute(n[0], x));
break;
case INCREMENT:
case DECREMENT:
t = execute(n[0], x);
u = Number(getValue(t));
if (n.postfix)
v = u;
putValue(t, (n.type == INCREMENT) ? ++u : --u, n[0]);
if (!n.postfix)
v = u;
break;
case DOT:
r = execute(n[0], x);
t = getValue(r);
u = n[1].value;
v = new Reference(toObject(t, r, n[0]), u, n);
break;
case INDEX:
r = execute(n[0], x);
t = getValue(r);
u = getValue(execute(n[1], x));
v = new Reference(toObject(t, r, n[0]), String(u), n);
break;
case LIST:
// Curse ECMA for specifying that arguments is not an Array object!
v = {};
for (i = 0, j = n.length; i < j; i++) {
u = getValue(execute(n[i], x));
v.__defineProperty__(i, u, false, false, true);
}
v.__defineProperty__('length', i, false, false, true);
break;
case CALL:
r = execute(n[0], x);
a = execute(n[1], x);
f = getValue(r);
if (isPrimitive(f) || typeof f.__call__ != "function") {
throw new TypeError(r + " is not callable",
n[0].filename, n[0].lineno);
}
t = (r instanceof Reference) ? r.base : null;
if (t instanceof Activation)
t = null;
v = f.__call__(t, a, x);
break;
case NEW:
case NEW_WITH_ARGS:
r = execute(n[0], x);
f = getValue(r);
if (n.type == NEW) {
a = {};
a.__defineProperty__('length', 0, false, false, true);
} else {
a = execute(n[1], x);
}
if (isPrimitive(f) || typeof f.__construct__ != "function") {
throw new TypeError(r + " is not a constructor",
n[0].filename, n[0].lineno);
}
v = f.__construct__(a, x);
break;
case ARRAY_INIT:
v = [];
for (i = 0, j = n.length; i < j; i++) {
if (n[i])
v[i] = getValue(execute(n[i], x));
}
v.length = j;
break;
case OBJECT_INIT:
v = {};
for (i = 0, j = n.length; i < j; i++) {
t = n[i];
if (t.type == PROPERTY_INIT) {
v[t[0].value] = getValue(execute(t[1], x));
} else {
f = new FunctionObject(t, x.scope);
u = (t.type == GETTER) ? '__defineGetter__'
: '__defineSetter__';
v[u](t.name, thunk(f, x));
}
}
break;
case NULL:
v = null;
break;
case THIS:
v = x.thisObject;
break;
case TRUE:
v = true;
break;
case FALSE:
v = false;
break;
case IDENTIFIER:
for (s = x.scope; s; s = s.parent) {
if (n.value in s.object)
break;
}
v = new Reference(s && s.object, n.value, n);
break;
case NUMBER:
case STRING:
case REGEXP:
v = n.value;
break;
case GROUP:
v = execute(n[0], x);
break;
default:
throw "PANIC: unknown operation " + n.type + ": " + uneval(n);
}
return v;
}
function Activation(f, a) {
for (var i = 0, j = f.params.length; i < j; i++)
this.__defineProperty__(f.params[i], a[i], true);
this.__defineProperty__('arguments', a, true);
}
// Null Activation.prototype's proto slot so that Object.prototype.* does not
// pollute the scope of heavyweight functions. Also delete its 'constructor'
// property so that it doesn't pollute function scopes. But first, we must
// copy __defineProperty__ down from Object.prototype.
Activation.prototype.__defineProperty__ = Object.prototype.__defineProperty__;
Activation.prototype.__proto__ = null;
delete Activation.prototype.constructor;
function FunctionObject(node, scope) {
this.node = node;
this.scope = scope;
this.__defineProperty__('length', node.params.length, true, true, true);
var proto = {};
this.__defineProperty__('prototype', proto, true);
proto.__defineProperty__('constructor', this, false, false, true);
}
var FOp = FunctionObject.prototype = {
// Internal methods.
__call__: function (t, a, x) {
var x2 = new ExecutionContext(FUNCTION_CODE);
x2.thisObject = t || global;
x2.caller = x;
x2.callee = this;
a.__defineProperty__('callee', this, false, false, true);
var f = this.node;
x2.scope = {object: new Activation(f, a), parent: this.scope};
ExecutionContext.current = x2;
try {
execute(f.body, x2);
} catch (e if e == RETURN) {
return x2.result;
} catch (e if e == THROW) {
x.result = x2.result;
throw THROW;
} finally {
ExecutionContext.current = x;
}
return undefined;
},
__construct__: function (a, x) {
var o = new Object;
var p = this.prototype;
if (isObject(p))
o.__proto__ = p;
// else o.__proto__ defaulted to Object.prototype
var v = this.__call__(o, a, x);
if (isObject(v))
return v;
return o;
},
__hasInstance__: function (v) {
if (isPrimitive(v))
return false;
var p = this.prototype;
if (isPrimitive(p)) {
throw new TypeError("'prototype' property is not an object",
this.node.filename, this.node.lineno);
}
var o;
while ((o = v.__proto__)) {
if (o == p)
return true;
v = o;
}
return false;
},
// Standard methods.
toString: function () {
return this.node.getSource();
},
apply: function (t, a) {
// Curse ECMA again!
if (typeof this.__call__ != "function") {
throw new TypeError("Function.prototype.apply called on" +
" uncallable object");
}
if (t === undefined || t === null)
t = global;
else if (typeof t != "object")
t = toObject(t, t);
if (a === undefined || a === null) {
a = {};
a.__defineProperty__('length', 0, false, false, true);
} else if (a instanceof Array) {
var v = {};
for (var i = 0, j = a.length; i < j; i++)
v.__defineProperty__(i, a[i], false, false, true);
v.__defineProperty__('length', i, false, false, true);
a = v;
} else if (!(a instanceof Object)) {
// XXX check for a non-arguments object
throw new TypeError("Second argument to Function.prototype.apply" +
" must be an array or arguments object",
this.node.filename, this.node.lineno);
}
return this.__call__(t, a, ExecutionContext.current);
},
call: function (t) {
// Curse ECMA a third time!
var a = Array.prototype.splice.call(arguments, 1);
return this.apply(t, a);
}
};
// Connect Function.prototype and Function.prototype.constructor in global.
reflectClass('Function', FOp);
// Help native and host-scripted functions be like FunctionObjects.
var Fp = Function.prototype;
var REp = RegExp.prototype;
if (!('__call__' in Fp)) {
Fp.__defineProperty__('__call__', function (t, a, x) {
// Curse ECMA yet again!
a = Array.prototype.splice.call(a, 0, a.length);
return this.apply(t, a);
}, true, true, true);
REp.__defineProperty__('__call__', function (t, a, x) {
a = Array.prototype.splice.call(a, 0, a.length);
return this.exec.apply(this, a);
}, true, true, true);
Fp.__defineProperty__('__construct__', function (a, x) {
a = Array.prototype.splice.call(a, 0, a.length);
return this.__applyConstructor__(a);
}, true, true, true);
// Since we use native functions such as Date along with host ones such
// as global.eval, we want both to be considered instances of the native
// Function constructor.
Fp.__defineProperty__('__hasInstance__', function (v) {
return v instanceof Function || v instanceof global.Function;
}, true, true, true);
}
function thunk(f, x) {
return function () { return f.__call__(this, arguments, x); };
}
function evaluate(s, f, l) {
if (typeof s != "string")
return s;
var x = ExecutionContext.current;
var x2 = new ExecutionContext(GLOBAL_CODE);
ExecutionContext.current = x2;
try {
execute(parse(s, f, l), x2);
} catch (e if e == THROW) {
if (x) {
x.result = x2.result;
throw THROW;
}
throw x2.result;
} finally {
ExecutionContext.current = x;
}
return x2.result;
}