pipa-js 0.1.2

A fast, minimal ES2023 JavaScript runtime built in Rust.
Documentation
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
#[derive(Debug, Clone, Copy, Default)]
pub struct SourceLocation {
    pub start: u32,
    pub end: u32,
}

#[derive(Debug, Clone)]
pub struct Program {
    pub body: Vec<ASTNode>,
    pub lines: Vec<u32>,
    pub source_type: SourceType,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SourceType {
    Script,
    Module,
}

#[derive(Debug, Clone)]
pub enum ASTNode {
    BlockStatement(BlockStatement),
    VariableDeclaration(VariableDeclaration),
    FunctionDeclaration(FunctionDeclaration),
    ClassDeclaration(ClassDeclaration),
    IfStatement(IfStatement),
    ForStatement(ForStatement),
    ForInStatement(ForInStatement),
    ForOfStatement(ForOfStatement),
    WhileStatement(WhileStatement),
    DoWhileStatement(DoWhileStatement),
    SwitchStatement(SwitchStatement),
    TryStatement(TryStatement),
    WithStatement(WithStatement),
    ReturnStatement(ReturnStatement),
    ThrowStatement(ThrowStatement),
    BreakStatement(BreakStatement),
    ContinueStatement(ContinueStatement),
    LabelledStatement(LabelledStatement),
    DebuggerStatement,
    EmptyStatement,

    ImportDeclaration(ImportDeclaration),
    ExportNamedDeclaration(ExportNamedDeclaration),
    ExportDefaultDeclaration(ExportDefaultDeclaration),
    ExportAllDeclaration(ExportAllDeclaration),

    ExpressionStatement(ExpressionStatement),
}

#[derive(Debug, Clone)]
pub enum Expression {
    Identifier(Identifier),
    PrivateIdentifier(String),
    Literal(Literal),
    This,
    Super,
    ArrayExpression(ArrayExpression),
    ObjectExpression(ObjectExpression),
    FunctionExpression(FunctionExpression),
    ClassExpression(ClassExpression),
    TemplateLiteral(TemplateLiteral),
    RegExpLiteral(RegExpLiteral),

    UnaryExpression(UnaryExpression),
    UpdateExpression(UpdateExpression),
    BinaryExpression(BinaryExpression),
    LogicalExpression(LogicalExpression),
    ConditionalExpression(ConditionalExpression),
    AssignmentExpression(AssignmentExpression),
    SequenceExpression(SequenceExpression),

    MemberExpression(MemberExpression),
    CallExpression(CallExpression),
    NewExpression(NewExpression),
    OptionalMemberExpression(OptionalMemberExpression),
    OptionalCallExpression(OptionalCallExpression),

    ArrowFunction(ArrowFunction),
    AwaitExpression(AwaitExpression),
    YieldExpression(YieldExpression),
    SpreadElement(SpreadElement),
    TaggedTemplateExpression(TaggedTemplateExpression),
    MetaProperty(MetaProperty),

    ArrayPattern(ArrayPattern),
    ObjectPattern(ObjectPattern),
    RestElement(RestElement),
    AssignmentPattern(AssignmentPattern),
}

#[derive(Debug, Clone)]
pub struct Identifier {
    pub name: String,
}

#[derive(Debug, Clone)]
pub enum Literal {
    Number(f64),
    String(String, bool),
    Boolean(bool),
    Null,
    Undefined,
    BigInt(String),
    LegacyOctal(i64),
}

#[derive(Debug, Clone)]
pub struct ArrayExpression {
    pub elements: Vec<Option<ArrayElement>>,
}

#[derive(Debug, Clone)]
pub enum ArrayElement {
    Expression(Expression),
    Spread(Expression),
}

#[derive(Debug, Clone)]
pub struct ObjectExpression {
    pub properties: Vec<Property>,
}

#[derive(Debug, Clone)]
pub enum Property {
    Property {
        key: PropertyKey,
        value: Box<Expression>,
        computed: bool,
        shorthand: bool,
        method: bool,
        getter: bool,
        setter: bool,
    },

    SpreadElement(Expression),
}

#[derive(Debug, Clone)]
pub enum PropertyKey {
    Identifier(String),
    Literal(Literal),
    Computed(Box<Expression>),
    PrivateIdentifier(String),
}

#[derive(Debug, Clone)]
pub struct FunctionExpression {
    pub name: Option<String>,
    pub params: Vec<Parameter>,
    pub body: BlockStatement,
    pub generator: bool,
    pub is_async: bool,
}

#[derive(Debug, Clone)]
pub struct ClassExpression {
    pub name: Option<String>,
    pub super_class: Option<Box<Expression>>,
    pub body: ClassBody,
}

#[derive(Debug, Clone)]
pub struct TemplateLiteral {
    pub quasis: Vec<TemplateElement>,
    pub expressions: Vec<Expression>,
}

#[derive(Debug, Clone)]
pub struct TemplateElement {
    pub value: String,
    pub tail: bool,
}

#[derive(Debug, Clone)]
pub struct RegExpLiteral {
    pub pattern: String,
    pub flags: String,
}

#[derive(Debug, Clone)]
pub struct UnaryExpression {
    pub op: UnaryOp,
    pub argument: Box<Expression>,
    pub prefix: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub enum UnaryOp {
    Minus,
    Plus,
    Not,
    BitNot,
    TypeOf,
    Void,
    Delete,
}

#[derive(Debug, Clone)]
pub struct UpdateExpression {
    pub op: UpdateOp,
    pub argument: Box<Expression>,
    pub prefix: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub enum UpdateOp {
    Increment,
    Decrement,
}

#[derive(Debug, Clone)]
pub struct BinaryExpression {
    pub op: BinaryOp,
    pub left: Box<Expression>,
    pub right: Box<Expression>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum BinaryOp {
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    Pow,
    Eq,
    Neq,
    StrictEq,
    StrictNeq,
    Lt,
    Lte,
    Gt,
    Gte,
    BitAnd,
    BitOr,
    BitXor,
    Shl,
    Shr,
    UShr,
    In,
    InstanceOf,
}

#[derive(Debug, Clone)]
pub struct LogicalExpression {
    pub op: LogicalOp,
    pub left: Box<Expression>,
    pub right: Box<Expression>,
}

#[derive(Debug, Clone)]
pub enum LogicalOp {
    And,
    Or,
    NullishCoalescing,
}

#[derive(Debug, Clone)]
pub struct ConditionalExpression {
    pub test: Box<Expression>,
    pub consequent: Box<Expression>,
    pub alternate: Box<Expression>,
}

#[derive(Debug, Clone)]
pub struct AssignmentExpression {
    pub op: AssignOp,
    pub left: AssignmentTarget,
    pub right: Box<Expression>,
}

#[derive(Debug, Clone)]
pub enum AssignOp {
    Assign,
    AddAssign,
    SubAssign,
    MulAssign,
    DivAssign,
    ModAssign,
    PowAssign,
    BitAndAssign,
    BitOrAssign,
    BitXorAssign,
    ShlAssign,
    ShrAssign,
    UShrAssign,
    LogicalAndAssign,
    LogicalOrAssign,
    NullishAssign,
}

#[derive(Debug, Clone)]
pub enum AssignmentTarget {
    Identifier(String),
    MemberExpression(MemberExpression),
    OptionalMemberExpression(OptionalMemberExpression),
    ComputedMember(Box<Expression>, Box<Expression>),
    ObjectPattern(ObjectPattern),
    ArrayPattern(ArrayPattern),
    RestElement(RestElement),
    AssignmentPattern(AssignmentPattern),
}

#[derive(Debug, Clone)]
pub struct SequenceExpression {
    pub expressions: Vec<Expression>,
}

#[derive(Debug, Clone)]
pub struct MemberExpression {
    pub object: Box<Expression>,
    pub property: MemberProperty,
    pub computed: bool,
}

#[derive(Debug, Clone)]
pub enum MemberProperty {
    Identifier(String),
    Computed(Box<Expression>),
    PrivateIdentifier(String),
}

#[derive(Debug, Clone)]
pub struct OptionalMemberExpression {
    pub object: Box<Expression>,
    pub property: MemberProperty,
    pub computed: bool,
    pub optional: bool,
}

#[derive(Debug, Clone)]
pub struct CallExpression {
    pub callee: Box<Expression>,
    pub arguments: Vec<Argument>,
}

#[derive(Debug, Clone)]
pub enum Argument {
    Expression(Expression),
    Spread(Expression),
}

#[derive(Debug, Clone)]
pub struct OptionalCallExpression {
    pub callee: Box<Expression>,
    pub arguments: Vec<Argument>,
    pub optional: bool,
}

#[derive(Debug, Clone)]
pub struct NewExpression {
    pub callee: Box<Expression>,
    pub arguments: Vec<Argument>,
}

#[derive(Debug, Clone)]
pub struct ArrowFunction {
    pub params: Vec<Parameter>,
    pub body: ArrowBody,
    pub is_async: bool,
}

#[derive(Debug, Clone)]
pub enum ArrowBody {
    Expression(Box<Expression>),
    Block(BlockStatement),
}

#[derive(Debug, Clone)]
pub struct AwaitExpression {
    pub argument: Box<Expression>,
}

#[derive(Debug, Clone)]
pub struct YieldExpression {
    pub argument: Option<Box<Expression>>,
    pub delegate: bool,
}

#[derive(Debug, Clone)]
pub struct SpreadElement {
    pub argument: Box<Expression>,
}

#[derive(Debug, Clone)]
pub struct TaggedTemplateExpression {
    pub tag: Box<Expression>,
    pub quasi: TemplateLiteral,
}

#[derive(Debug, Clone)]
pub struct MetaProperty {
    pub meta: String,
    pub property: String,
}

#[derive(Debug, Clone)]
pub struct ArrayPattern {
    pub elements: Vec<Option<PatternElement>>,
}

#[derive(Debug, Clone)]
pub enum PatternElement {
    Pattern(AssignmentTarget),
    RestElement(RestElement),
    AssignmentPattern(AssignmentPattern),
}

#[derive(Debug, Clone)]
pub struct ObjectPattern {
    pub properties: Vec<ObjectPatternProperty>,
}

#[derive(Debug, Clone)]
pub enum ObjectPatternProperty {
    Property {
        key: PropertyKey,
        value: AssignmentTarget,
        computed: bool,
        shorthand: bool,
    },
    RestElement(RestElement),
}

#[derive(Debug, Clone)]
pub struct RestElement {
    pub argument: Box<AssignmentTarget>,
}

#[derive(Debug, Clone)]
pub struct AssignmentPattern {
    pub left: Box<AssignmentTarget>,
    pub right: Box<Expression>,
}

#[derive(Debug, Clone)]
pub enum Parameter {
    Identifier(String),
    Pattern(BindingPattern),
    AssignmentPattern(AssignmentPattern),
    RestElement(RestElement),
}

#[derive(Debug, Clone)]
pub struct ExpressionStatement {
    pub expression: Expression,
}

#[derive(Debug, Clone)]
pub struct BlockStatement {
    pub body: Vec<ASTNode>,
    pub lines: Vec<u32>,
}

#[derive(Debug, Clone)]
pub struct VariableDeclaration {
    pub kind: VariableKind,
    pub declarations: Vec<VariableDeclarator>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VariableKind {
    Var,
    Let,
    Const,
}

#[derive(Debug, Clone)]
pub struct VariableDeclarator {
    pub id: BindingPattern,
    pub init: Option<Expression>,
}

#[derive(Debug, Clone)]
pub enum BindingPattern {
    Identifier(String),
    ArrayPattern(ArrayPattern),
    ObjectPattern(ObjectPattern),
    AssignmentPattern(AssignmentPattern),
}

#[derive(Debug, Clone)]
pub struct FunctionDeclaration {
    pub name: String,
    pub params: Vec<Parameter>,
    pub body: BlockStatement,
    pub generator: bool,
    pub is_async: bool,
}

#[derive(Debug, Clone)]
pub struct ClassDeclaration {
    pub name: String,
    pub super_class: Option<Box<Expression>>,
    pub body: ClassBody,
}

#[derive(Debug, Clone)]
pub struct ClassBody {
    pub elements: Vec<ClassElement>,
}

#[derive(Debug, Clone)]
pub enum ClassElement {
    Method(ClassMethod),
    StaticBlock(StaticBlock),
    Property(ClassProperty),
}

#[derive(Debug, Clone)]
pub struct ClassMethod {
    pub name: PropertyKey,
    pub params: Vec<Parameter>,
    pub body: BlockStatement,
    pub kind: MethodKind,
    pub is_async: bool,
    pub generator: bool,
    pub is_static: bool,
    pub is_private: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub enum MethodKind {
    Constructor,
    Method,
    Get,
    Set,
}

#[derive(Debug, Clone)]
pub struct ClassProperty {
    pub name: PropertyKey,
    pub value: Option<Expression>,
    pub is_static: bool,
    pub is_private: bool,
}

#[derive(Debug, Clone)]
pub struct StaticBlock {
    pub body: Vec<ASTNode>,
    pub lines: Vec<u32>,
}

#[derive(Debug, Clone)]
pub struct IfStatement {
    pub test: Expression,
    pub consequent: Box<ASTNode>,
    pub alternate: Option<Box<ASTNode>>,
}

#[derive(Debug, Clone)]
pub struct ForStatement {
    pub init: Option<ForInit>,
    pub test: Option<Expression>,
    pub update: Option<Expression>,
    pub body: Box<ASTNode>,
}

#[derive(Debug, Clone)]
pub enum ForInit {
    VariableDeclaration(VariableDeclaration),
    Expression(Expression),
}

#[derive(Debug, Clone)]
pub struct ForInStatement {
    pub left: ForInOfLeft,
    pub right: Expression,
    pub body: Box<ASTNode>,
}

#[derive(Debug, Clone)]
pub struct ForOfStatement {
    pub left: ForInOfLeft,
    pub right: Expression,
    pub body: Box<ASTNode>,
    pub is_await: bool,
}

#[derive(Debug, Clone)]
pub enum ForInOfLeft {
    VariableDeclaration(VariableDeclaration),
    AssignmentTarget(AssignmentTarget),
}

#[derive(Debug, Clone)]
pub struct WhileStatement {
    pub test: Expression,
    pub body: Box<ASTNode>,
}

#[derive(Debug, Clone)]
pub struct DoWhileStatement {
    pub body: Box<ASTNode>,
    pub test: Expression,
}

#[derive(Debug, Clone)]
pub struct SwitchStatement {
    pub discriminant: Expression,
    pub cases: Vec<SwitchCase>,
}

#[derive(Debug, Clone)]
pub struct SwitchCase {
    pub test: Option<Expression>,
    pub consequent: Vec<ASTNode>,
}

#[derive(Debug, Clone)]
pub struct TryStatement {
    pub block: BlockStatement,
    pub handler: Option<CatchClause>,
    pub finalizer: Option<BlockStatement>,
}

#[derive(Debug, Clone)]
pub struct CatchClause {
    pub param: Option<BindingPattern>,
    pub body: BlockStatement,
}

#[derive(Debug, Clone)]
pub struct WithStatement {
    pub object: Expression,
    pub body: Box<ASTNode>,
}

#[derive(Debug, Clone)]
pub struct ReturnStatement {
    pub argument: Option<Expression>,
}

#[derive(Debug, Clone)]
pub struct ThrowStatement {
    pub argument: Expression,
}

#[derive(Debug, Clone)]
pub struct BreakStatement {
    pub label: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ContinueStatement {
    pub label: Option<String>,
}

#[derive(Debug, Clone)]
pub struct LabelledStatement {
    pub label: String,
    pub body: Box<ASTNode>,
}

#[derive(Debug, Clone)]
pub struct ImportDeclaration {
    pub specifiers: Vec<ImportSpecifier>,
    pub source: String,
}

#[derive(Debug, Clone)]
pub enum ImportSpecifier {
    Named { imported: String, local: String },

    Default { local: String },

    Namespace { local: String },
}

#[derive(Debug, Clone)]
pub struct ExportNamedDeclaration {
    pub declaration: Option<ExportDeclaration>,
    pub specifiers: Vec<ExportSpecifier>,
    pub source: Option<String>,
}

#[derive(Debug, Clone)]
pub enum ExportDeclaration {
    Variable(VariableDeclaration),
    Function(FunctionDeclaration),
    Class(ClassDeclaration),
}

#[derive(Debug, Clone)]
pub struct ExportSpecifier {
    pub exported: String,
    pub local: String,
}

#[derive(Debug, Clone)]
pub struct ExportDefaultDeclaration {
    pub declaration: ExportDefaultDeclarationKind,
}

#[derive(Debug, Clone)]
pub enum ExportDefaultDeclarationKind {
    Function(FunctionDeclaration),
    Class(ClassDeclaration),
    Expression(Expression),
}

#[derive(Debug, Clone)]
pub struct ExportAllDeclaration {
    pub source: String,
    pub exported: Option<String>,
}