forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectParse.cpp
3650 lines (3005 loc) · 112 KB
/
ReflectParse.cpp
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: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* JS reflection package. */
#include "mozilla/DebugOnly.h"
#include "mozilla/Maybe.h"
#include "mozilla/Move.h"
#include <stdlib.h>
#include "jspubtd.h"
#include "builtin/Array.h"
#include "builtin/Reflect.h"
#include "frontend/ModuleSharedContext.h"
#include "frontend/ParseNode.h"
#include "frontend/Parser.h"
#include "js/CharacterEncoding.h"
#include "js/StableStringChars.h"
#ifdef ENABLE_BIGINT
#include "vm/BigIntType.h"
#endif
#include "vm/JSAtom.h"
#include "vm/JSObject.h"
#include "vm/RegExpObject.h"
#include "vm/JSObject-inl.h"
using namespace js;
using namespace js::frontend;
using JS::AutoStableStringChars;
using JS::AutoValueArray;
using JS::CompileOptions;
using mozilla::DebugOnly;
enum ASTType {
AST_ERROR = -1,
#define ASTDEF(ast, str, method) ast,
#include "jsast.tbl"
#undef ASTDEF
AST_LIMIT
};
enum AssignmentOperator {
AOP_ERR = -1,
/* assign */
AOP_ASSIGN = 0,
/* operator-assign */
AOP_PLUS,
AOP_MINUS,
AOP_STAR,
AOP_DIV,
AOP_MOD,
AOP_POW,
/* shift-assign */
AOP_LSH,
AOP_RSH,
AOP_URSH,
/* binary */
AOP_BITOR,
AOP_BITXOR,
AOP_BITAND,
AOP_LIMIT
};
enum BinaryOperator {
BINOP_ERR = -1,
/* eq */
BINOP_EQ = 0,
BINOP_NE,
BINOP_STRICTEQ,
BINOP_STRICTNE,
/* rel */
BINOP_LT,
BINOP_LE,
BINOP_GT,
BINOP_GE,
/* shift */
BINOP_LSH,
BINOP_RSH,
BINOP_URSH,
/* arithmetic */
BINOP_ADD,
BINOP_SUB,
BINOP_STAR,
BINOP_DIV,
BINOP_MOD,
BINOP_POW,
/* binary */
BINOP_BITOR,
BINOP_BITXOR,
BINOP_BITAND,
/* misc */
BINOP_IN,
BINOP_INSTANCEOF,
BINOP_PIPELINE,
BINOP_LIMIT
};
enum UnaryOperator {
UNOP_ERR = -1,
UNOP_DELETE = 0,
UNOP_NEG,
UNOP_POS,
UNOP_NOT,
UNOP_BITNOT,
UNOP_TYPEOF,
UNOP_VOID,
UNOP_AWAIT,
UNOP_LIMIT
};
enum VarDeclKind {
VARDECL_ERR = -1,
VARDECL_VAR = 0,
VARDECL_CONST,
VARDECL_LET,
VARDECL_LIMIT
};
enum PropKind {
PROP_ERR = -1,
PROP_INIT = 0,
PROP_GETTER,
PROP_SETTER,
PROP_MUTATEPROTO,
PROP_LIMIT
};
static const char* const aopNames[] = {
"=", /* AOP_ASSIGN */
"+=", /* AOP_PLUS */
"-=", /* AOP_MINUS */
"*=", /* AOP_STAR */
"/=", /* AOP_DIV */
"%=", /* AOP_MOD */
"**=", /* AOP_POW */
"<<=", /* AOP_LSH */
">>=", /* AOP_RSH */
">>>=", /* AOP_URSH */
"|=", /* AOP_BITOR */
"^=", /* AOP_BITXOR */
"&=" /* AOP_BITAND */
};
static const char* const binopNames[] = {
"==", /* BINOP_EQ */
"!=", /* BINOP_NE */
"===", /* BINOP_STRICTEQ */
"!==", /* BINOP_STRICTNE */
"<", /* BINOP_LT */
"<=", /* BINOP_LE */
">", /* BINOP_GT */
">=", /* BINOP_GE */
"<<", /* BINOP_LSH */
">>", /* BINOP_RSH */
">>>", /* BINOP_URSH */
"+", /* BINOP_PLUS */
"-", /* BINOP_MINUS */
"*", /* BINOP_STAR */
"/", /* BINOP_DIV */
"%", /* BINOP_MOD */
"**", /* BINOP_POW */
"|", /* BINOP_BITOR */
"^", /* BINOP_BITXOR */
"&", /* BINOP_BITAND */
"in", /* BINOP_IN */
"instanceof", /* BINOP_INSTANCEOF */
"|>", /* BINOP_PIPELINE */
};
static const char* const unopNames[] = {
"delete", /* UNOP_DELETE */
"-", /* UNOP_NEG */
"+", /* UNOP_POS */
"!", /* UNOP_NOT */
"~", /* UNOP_BITNOT */
"typeof", /* UNOP_TYPEOF */
"void", /* UNOP_VOID */
"await" /* UNOP_AWAIT */
};
static const char* const nodeTypeNames[] = {
#define ASTDEF(ast, str, method) str,
#include "jsast.tbl"
#undef ASTDEF
nullptr};
static const char* const callbackNames[] = {
#define ASTDEF(ast, str, method) method,
#include "jsast.tbl"
#undef ASTDEF
nullptr};
enum YieldKind { Delegating, NotDelegating };
typedef AutoValueVector NodeVector;
/*
* ParseNode is a somewhat intricate data structure, and its invariants have
* evolved, making it more likely that there could be a disconnect between the
* parser and the AST serializer. We use these macros to check invariants on a
* parse node and raise a dynamic error on failure.
*/
#define LOCAL_ASSERT(expr) \
JS_BEGIN_MACRO \
MOZ_ASSERT(expr); \
if (!(expr)) { \
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, \
JSMSG_BAD_PARSE_NODE); \
return false; \
} \
JS_END_MACRO
#define LOCAL_NOT_REACHED(expr) \
JS_BEGIN_MACRO \
MOZ_ASSERT(false); \
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, \
JSMSG_BAD_PARSE_NODE); \
return false; \
JS_END_MACRO
namespace {
/* Set 'result' to obj[id] if any such property exists, else defaultValue. */
static bool GetPropertyDefault(JSContext* cx, HandleObject obj, HandleId id,
HandleValue defaultValue,
MutableHandleValue result) {
bool found;
if (!HasProperty(cx, obj, id, &found)) {
return false;
}
if (!found) {
result.set(defaultValue);
return true;
}
return GetProperty(cx, obj, obj, id, result);
}
enum class GeneratorStyle { None, ES6 };
/*
* Builder class that constructs JavaScript AST node objects. See:
*
* https://developer.mozilla.org/en/SpiderMonkey/Parser_API
*
* Bug 569487: generalize builder interface
*/
class NodeBuilder {
typedef AutoValueArray<AST_LIMIT> CallbackArray;
JSContext* cx;
frontend::Parser<frontend::FullParseHandler, char16_t>* parser;
bool saveLoc; /* save source location information? */
char const* src; /* source filename or null */
RootedValue srcval; /* source filename JS value or null */
CallbackArray callbacks; /* user-specified callbacks */
RootedValue userv; /* user-specified builder object or null */
public:
NodeBuilder(JSContext* c, bool l, char const* s)
: cx(c),
parser(nullptr),
saveLoc(l),
src(s),
srcval(c),
callbacks(cx),
userv(c) {}
MOZ_MUST_USE bool init(HandleObject userobj = nullptr) {
if (src) {
if (!atomValue(src, &srcval)) {
return false;
}
} else {
srcval.setNull();
}
if (!userobj) {
userv.setNull();
for (unsigned i = 0; i < AST_LIMIT; i++) {
callbacks[i].setNull();
}
return true;
}
userv.setObject(*userobj);
RootedValue nullVal(cx, NullValue());
RootedValue funv(cx);
for (unsigned i = 0; i < AST_LIMIT; i++) {
const char* name = callbackNames[i];
RootedAtom atom(cx, Atomize(cx, name, strlen(name)));
if (!atom) {
return false;
}
RootedId id(cx, AtomToId(atom));
if (!GetPropertyDefault(cx, userobj, id, nullVal, &funv)) {
return false;
}
if (funv.isNullOrUndefined()) {
callbacks[i].setNull();
continue;
}
if (!funv.isObject() || !funv.toObject().is<JSFunction>()) {
ReportValueError(cx, JSMSG_NOT_FUNCTION, JSDVG_SEARCH_STACK, funv,
nullptr);
return false;
}
callbacks[i].set(funv);
}
return true;
}
void setParser(frontend::Parser<frontend::FullParseHandler, char16_t>* p) {
parser = p;
}
private:
MOZ_MUST_USE bool callbackHelper(HandleValue fun, const InvokeArgs& args,
size_t i, TokenPos* pos,
MutableHandleValue dst) {
// The end of the implementation of callback(). All arguments except
// loc have already been stored in range [0, i).
if (saveLoc) {
if (!newNodeLoc(pos, args[i])) {
return false;
}
}
return js::Call(cx, fun, userv, args, dst);
}
// Helper function for callback(). Note that all Arguments must be types
// that convert to HandleValue, so this isn't as template-y as it seems,
// just variadic.
template <typename... Arguments>
MOZ_MUST_USE bool callbackHelper(HandleValue fun, const InvokeArgs& args,
size_t i, HandleValue head,
Arguments&&... tail) {
// Recursive loop to store the arguments into args. This eventually
// bottoms out in a call to the non-template callbackHelper() above.
args[i].set(head);
return callbackHelper(fun, args, i + 1, std::forward<Arguments>(tail)...);
}
// Invoke a user-defined callback. The actual signature is:
//
// bool callback(HandleValue fun, HandleValue... args, TokenPos* pos,
// MutableHandleValue dst);
template <typename... Arguments>
MOZ_MUST_USE bool callback(HandleValue fun, Arguments&&... args) {
InvokeArgs iargs(cx);
if (!iargs.init(cx, sizeof...(args) - 2 + size_t(saveLoc))) {
return false;
}
return callbackHelper(fun, iargs, 0, std::forward<Arguments>(args)...);
}
// WARNING: Returning a Handle is non-standard, but it works in this case
// because both |v| and |UndefinedHandleValue| are definitely rooted on a
// previous stack frame (i.e. we're just choosing between two
// already-rooted values).
HandleValue opt(HandleValue v) {
MOZ_ASSERT_IF(v.isMagic(), v.whyMagic() == JS_SERIALIZE_NO_NODE);
return v.isMagic(JS_SERIALIZE_NO_NODE) ? JS::UndefinedHandleValue : v;
}
MOZ_MUST_USE bool atomValue(const char* s, MutableHandleValue dst) {
/*
* Bug 575416: instead of Atomize, lookup constant atoms in tbl file
*/
RootedAtom atom(cx, Atomize(cx, s, strlen(s)));
if (!atom) {
return false;
}
dst.setString(atom);
return true;
}
MOZ_MUST_USE bool newObject(MutableHandleObject dst) {
RootedPlainObject nobj(cx, NewBuiltinClassInstance<PlainObject>(cx));
if (!nobj) {
return false;
}
dst.set(nobj);
return true;
}
MOZ_MUST_USE bool newArray(NodeVector& elts, MutableHandleValue dst);
MOZ_MUST_USE bool createNode(ASTType type, TokenPos* pos,
MutableHandleObject dst);
MOZ_MUST_USE bool newNodeHelper(HandleObject obj, MutableHandleValue dst) {
// The end of the implementation of newNode().
MOZ_ASSERT(obj);
dst.setObject(*obj);
return true;
}
template <typename... Arguments>
MOZ_MUST_USE bool newNodeHelper(HandleObject obj, const char* name,
HandleValue value, Arguments&&... rest) {
// Recursive loop to define properties. Note that the newNodeHelper()
// call below passes two fewer arguments than we received, as we omit
// `name` and `value`. This eventually bottoms out in a call to the
// non-template newNodeHelper() above.
return defineProperty(obj, name, value) &&
newNodeHelper(obj, std::forward<Arguments>(rest)...);
}
// Create a node object with "type" and "loc" properties, as well as zero
// or more properties passed in as arguments. The signature is really more
// like:
//
// bool newNode(ASTType type, TokenPos* pos,
// {const char *name0, HandleValue value0,}...
// MutableHandleValue dst);
template <typename... Arguments>
MOZ_MUST_USE bool newNode(ASTType type, TokenPos* pos, Arguments&&... args) {
RootedObject node(cx);
return createNode(type, pos, &node) &&
newNodeHelper(node, std::forward<Arguments>(args)...);
}
MOZ_MUST_USE bool listNode(ASTType type, const char* propName,
NodeVector& elts, TokenPos* pos,
MutableHandleValue dst) {
RootedValue array(cx);
if (!newArray(elts, &array)) {
return false;
}
RootedValue cb(cx, callbacks[type]);
if (!cb.isNull()) {
return callback(cb, array, pos, dst);
}
return newNode(type, pos, propName, array, dst);
}
MOZ_MUST_USE bool defineProperty(HandleObject obj, const char* name,
HandleValue val) {
MOZ_ASSERT_IF(val.isMagic(), val.whyMagic() == JS_SERIALIZE_NO_NODE);
/*
* Bug 575416: instead of Atomize, lookup constant atoms in tbl file
*/
RootedAtom atom(cx, Atomize(cx, name, strlen(name)));
if (!atom) {
return false;
}
// Represent "no node" as null and ensure users are not exposed to magic
// values.
RootedValue optVal(cx,
val.isMagic(JS_SERIALIZE_NO_NODE) ? NullValue() : val);
return DefineDataProperty(cx, obj, atom->asPropertyName(), optVal);
}
MOZ_MUST_USE bool newNodeLoc(TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool setNodeLoc(HandleObject node, TokenPos* pos);
public:
/*
* All of the public builder methods take as their last two
* arguments a nullable token position and a non-nullable, rooted
* outparam.
*
* Any Value arguments representing optional subnodes may be a
* JS_SERIALIZE_NO_NODE magic value.
*/
/*
* misc nodes
*/
MOZ_MUST_USE bool program(NodeVector& elts, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool literal(HandleValue val, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool identifier(HandleValue name, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool function(ASTType type, TokenPos* pos, HandleValue id,
NodeVector& args, NodeVector& defaults,
HandleValue body, HandleValue rest,
GeneratorStyle generatorStyle, bool isAsync,
bool isExpression, MutableHandleValue dst);
MOZ_MUST_USE bool variableDeclarator(HandleValue id, HandleValue init,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool switchCase(HandleValue expr, NodeVector& elts,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool catchClause(HandleValue var, HandleValue body,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool prototypeMutation(HandleValue val, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool propertyInitializer(HandleValue key, HandleValue val,
PropKind kind, bool isShorthand,
bool isMethod, TokenPos* pos,
MutableHandleValue dst);
/*
* statements
*/
MOZ_MUST_USE bool blockStatement(NodeVector& elts, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool expressionStatement(HandleValue expr, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool emptyStatement(TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool ifStatement(HandleValue test, HandleValue cons,
HandleValue alt, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool breakStatement(HandleValue label, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool continueStatement(HandleValue label, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool labeledStatement(HandleValue label, HandleValue stmt,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool throwStatement(HandleValue arg, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool returnStatement(HandleValue arg, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool forStatement(HandleValue init, HandleValue test,
HandleValue update, HandleValue stmt,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool forInStatement(HandleValue var, HandleValue expr,
HandleValue stmt, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool forOfStatement(HandleValue var, HandleValue expr,
HandleValue stmt, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool withStatement(HandleValue expr, HandleValue stmt,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool whileStatement(HandleValue test, HandleValue stmt,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool doWhileStatement(HandleValue stmt, HandleValue test,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool switchStatement(HandleValue disc, NodeVector& elts,
bool lexical, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool tryStatement(HandleValue body, HandleValue handler,
HandleValue finally, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool debuggerStatement(TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool importDeclaration(NodeVector& elts, HandleValue moduleSpec,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool importSpecifier(HandleValue importName,
HandleValue bindingName, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool exportDeclaration(HandleValue decl, NodeVector& elts,
HandleValue moduleSpec,
HandleValue isDefault, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool exportSpecifier(HandleValue bindingName,
HandleValue exportName, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool exportBatchSpecifier(TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool classDefinition(bool expr, HandleValue name,
HandleValue heritage, HandleValue block,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool classMembers(NodeVector& members, MutableHandleValue dst);
MOZ_MUST_USE bool classMethod(HandleValue name, HandleValue body,
PropKind kind, bool isStatic, TokenPos* pos,
MutableHandleValue dst);
/*
* expressions
*/
MOZ_MUST_USE bool binaryExpression(BinaryOperator op, HandleValue left,
HandleValue right, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool unaryExpression(UnaryOperator op, HandleValue expr,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool assignmentExpression(AssignmentOperator op, HandleValue lhs,
HandleValue rhs, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool updateExpression(HandleValue expr, bool incr, bool prefix,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool logicalExpression(bool lor, HandleValue left,
HandleValue right, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool conditionalExpression(HandleValue test, HandleValue cons,
HandleValue alt, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool sequenceExpression(NodeVector& elts, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool newExpression(HandleValue callee, NodeVector& args,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool callExpression(HandleValue callee, NodeVector& args,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool memberExpression(bool computed, HandleValue expr,
HandleValue member, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool arrayExpression(NodeVector& elts, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool templateLiteral(NodeVector& elts, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool taggedTemplate(HandleValue callee, NodeVector& args,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool callSiteObj(NodeVector& raw, NodeVector& cooked,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool spreadExpression(HandleValue expr, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool computedName(HandleValue name, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool objectExpression(NodeVector& elts, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool thisExpression(TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool yieldExpression(HandleValue arg, YieldKind kind,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool metaProperty(HandleValue meta, HandleValue property,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool callImportExpression(HandleValue ident, HandleValue arg,
TokenPos* pos, MutableHandleValue dst);
MOZ_MUST_USE bool super(TokenPos* pos, MutableHandleValue dst);
/*
* declarations
*/
MOZ_MUST_USE bool variableDeclaration(NodeVector& elts, VarDeclKind kind,
TokenPos* pos, MutableHandleValue dst);
/*
* patterns
*/
MOZ_MUST_USE bool arrayPattern(NodeVector& elts, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool objectPattern(NodeVector& elts, TokenPos* pos,
MutableHandleValue dst);
MOZ_MUST_USE bool propertyPattern(HandleValue key, HandleValue patt,
bool isShorthand, TokenPos* pos,
MutableHandleValue dst);
};
} /* anonymous namespace */
bool NodeBuilder::createNode(ASTType type, TokenPos* pos,
MutableHandleObject dst) {
MOZ_ASSERT(type > AST_ERROR && type < AST_LIMIT);
RootedValue tv(cx);
RootedPlainObject node(cx, NewBuiltinClassInstance<PlainObject>(cx));
if (!node || !setNodeLoc(node, pos) || !atomValue(nodeTypeNames[type], &tv) ||
!defineProperty(node, "type", tv)) {
return false;
}
dst.set(node);
return true;
}
bool NodeBuilder::newArray(NodeVector& elts, MutableHandleValue dst) {
const size_t len = elts.length();
if (len > UINT32_MAX) {
ReportAllocationOverflow(cx);
return false;
}
RootedObject array(cx, NewDenseFullyAllocatedArray(cx, uint32_t(len)));
if (!array) {
return false;
}
for (size_t i = 0; i < len; i++) {
RootedValue val(cx, elts[i]);
MOZ_ASSERT_IF(val.isMagic(), val.whyMagic() == JS_SERIALIZE_NO_NODE);
/* Represent "no node" as an array hole by not adding the value. */
if (val.isMagic(JS_SERIALIZE_NO_NODE)) {
continue;
}
if (!DefineDataElement(cx, array, i, val)) {
return false;
}
}
dst.setObject(*array);
return true;
}
bool NodeBuilder::newNodeLoc(TokenPos* pos, MutableHandleValue dst) {
if (!pos) {
dst.setNull();
return true;
}
RootedObject loc(cx);
RootedObject to(cx);
RootedValue val(cx);
if (!newObject(&loc)) {
return false;
}
dst.setObject(*loc);
uint32_t startLineNum, startColumnIndex;
uint32_t endLineNum, endColumnIndex;
parser->tokenStream.computeLineAndColumn(pos->begin, &startLineNum,
&startColumnIndex);
parser->tokenStream.computeLineAndColumn(pos->end, &endLineNum,
&endColumnIndex);
if (!newObject(&to)) {
return false;
}
val.setObject(*to);
if (!defineProperty(loc, "start", val)) {
return false;
}
val.setNumber(startLineNum);
if (!defineProperty(to, "line", val)) {
return false;
}
val.setNumber(startColumnIndex);
if (!defineProperty(to, "column", val)) {
return false;
}
if (!newObject(&to)) {
return false;
}
val.setObject(*to);
if (!defineProperty(loc, "end", val)) {
return false;
}
val.setNumber(endLineNum);
if (!defineProperty(to, "line", val)) {
return false;
}
val.setNumber(endColumnIndex);
if (!defineProperty(to, "column", val)) {
return false;
}
if (!defineProperty(loc, "source", srcval)) {
return false;
}
return true;
}
bool NodeBuilder::setNodeLoc(HandleObject node, TokenPos* pos) {
if (!saveLoc) {
RootedValue nullVal(cx, NullValue());
return defineProperty(node, "loc", nullVal);
}
RootedValue loc(cx);
return newNodeLoc(pos, &loc) && defineProperty(node, "loc", loc);
}
bool NodeBuilder::program(NodeVector& elts, TokenPos* pos,
MutableHandleValue dst) {
return listNode(AST_PROGRAM, "body", elts, pos, dst);
}
bool NodeBuilder::blockStatement(NodeVector& elts, TokenPos* pos,
MutableHandleValue dst) {
return listNode(AST_BLOCK_STMT, "body", elts, pos, dst);
}
bool NodeBuilder::expressionStatement(HandleValue expr, TokenPos* pos,
MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_EXPR_STMT]);
if (!cb.isNull()) {
return callback(cb, expr, pos, dst);
}
return newNode(AST_EXPR_STMT, pos, "expression", expr, dst);
}
bool NodeBuilder::emptyStatement(TokenPos* pos, MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_EMPTY_STMT]);
if (!cb.isNull()) {
return callback(cb, pos, dst);
}
return newNode(AST_EMPTY_STMT, pos, dst);
}
bool NodeBuilder::ifStatement(HandleValue test, HandleValue cons,
HandleValue alt, TokenPos* pos,
MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_IF_STMT]);
if (!cb.isNull()) {
return callback(cb, test, cons, opt(alt), pos, dst);
}
return newNode(AST_IF_STMT, pos, "test", test, "consequent", cons,
"alternate", alt, dst);
}
bool NodeBuilder::breakStatement(HandleValue label, TokenPos* pos,
MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_BREAK_STMT]);
if (!cb.isNull()) {
return callback(cb, opt(label), pos, dst);
}
return newNode(AST_BREAK_STMT, pos, "label", label, dst);
}
bool NodeBuilder::continueStatement(HandleValue label, TokenPos* pos,
MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_CONTINUE_STMT]);
if (!cb.isNull()) {
return callback(cb, opt(label), pos, dst);
}
return newNode(AST_CONTINUE_STMT, pos, "label", label, dst);
}
bool NodeBuilder::labeledStatement(HandleValue label, HandleValue stmt,
TokenPos* pos, MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_LAB_STMT]);
if (!cb.isNull()) {
return callback(cb, label, stmt, pos, dst);
}
return newNode(AST_LAB_STMT, pos, "label", label, "body", stmt, dst);
}
bool NodeBuilder::throwStatement(HandleValue arg, TokenPos* pos,
MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_THROW_STMT]);
if (!cb.isNull()) {
return callback(cb, arg, pos, dst);
}
return newNode(AST_THROW_STMT, pos, "argument", arg, dst);
}
bool NodeBuilder::returnStatement(HandleValue arg, TokenPos* pos,
MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_RETURN_STMT]);
if (!cb.isNull()) {
return callback(cb, opt(arg), pos, dst);
}
return newNode(AST_RETURN_STMT, pos, "argument", arg, dst);
}
bool NodeBuilder::forStatement(HandleValue init, HandleValue test,
HandleValue update, HandleValue stmt,
TokenPos* pos, MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_FOR_STMT]);
if (!cb.isNull()) {
return callback(cb, opt(init), opt(test), opt(update), stmt, pos, dst);
}
return newNode(AST_FOR_STMT, pos, "init", init, "test", test, "update",
update, "body", stmt, dst);
}
bool NodeBuilder::forInStatement(HandleValue var, HandleValue expr,
HandleValue stmt, TokenPos* pos,
MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_FOR_IN_STMT]);
if (!cb.isNull()) {
RootedValue isForEach(
cx, JS::FalseValue()); // obsolete E4X `for each` statement
return callback(cb, var, expr, stmt, isForEach, pos, dst);
}
return newNode(AST_FOR_IN_STMT, pos, "left", var, "right", expr, "body", stmt,
dst);
}
bool NodeBuilder::forOfStatement(HandleValue var, HandleValue expr,
HandleValue stmt, TokenPos* pos,
MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_FOR_OF_STMT]);
if (!cb.isNull()) {
return callback(cb, var, expr, stmt, pos, dst);
}
return newNode(AST_FOR_OF_STMT, pos, "left", var, "right", expr, "body", stmt,
dst);
}
bool NodeBuilder::withStatement(HandleValue expr, HandleValue stmt,
TokenPos* pos, MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_WITH_STMT]);
if (!cb.isNull()) {
return callback(cb, expr, stmt, pos, dst);
}
return newNode(AST_WITH_STMT, pos, "object", expr, "body", stmt, dst);
}
bool NodeBuilder::whileStatement(HandleValue test, HandleValue stmt,
TokenPos* pos, MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_WHILE_STMT]);
if (!cb.isNull()) {
return callback(cb, test, stmt, pos, dst);
}
return newNode(AST_WHILE_STMT, pos, "test", test, "body", stmt, dst);
}
bool NodeBuilder::doWhileStatement(HandleValue stmt, HandleValue test,
TokenPos* pos, MutableHandleValue dst) {
RootedValue cb(cx, callbacks[AST_DO_STMT]);
if (!cb.isNull()) {
return callback(cb, stmt, test, pos, dst);
}
return newNode(AST_DO_STMT, pos, "body", stmt, "test", test, dst);
}
bool NodeBuilder::switchStatement(HandleValue disc, NodeVector& elts,
bool lexical, TokenPos* pos,
MutableHandleValue dst) {
RootedValue array(cx);
if (!newArray(elts, &array)) {
return false;
}
RootedValue lexicalVal(cx, BooleanValue(lexical));
RootedValue cb(cx, callbacks[AST_SWITCH_STMT]);