summaryrefslogtreecommitdiffstats
path: root/third_party/rust/jsparagus-emitter/src/reference_op_emitter.rs
blob: 4a784e5b04555dd182ae27f517768d268e76dc94 (plain)
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
use crate::ast_emitter::AstEmitter;
use crate::emitter::EmitError;
use crate::emitter_scope::NameLocation;
use ast::source_atom_set::SourceAtomSetIndex;
use stencil::env_coord::{EnvironmentHops, EnvironmentSlot};
use stencil::frame_slot::FrameSlot;
use stencil::gcthings::GCThingIndex;
use stencil::scope::BindingKind;

#[derive(Debug, PartialEq)]
enum AssignmentReferenceKind {
    GlobalVar(GCThingIndex),
    GlobalLexical(GCThingIndex),
    FrameSlotLexical(FrameSlot),
    FrameSlotNonLexical(FrameSlot),
    EnvironmentCoordLexical(EnvironmentHops, EnvironmentSlot),
    EnvironmentCoordNonLexical(EnvironmentHops, EnvironmentSlot),
    Dynamic(GCThingIndex),
    #[allow(dead_code)]
    Prop(GCThingIndex),
    #[allow(dead_code)]
    Elem,
}

// See AssignmentReferenceEmitter.
// This uses struct to hide the details from the consumer.
#[derive(Debug)]
#[must_use]
pub struct AssignmentReference {
    kind: AssignmentReferenceKind,
}
impl AssignmentReference {
    fn new(kind: AssignmentReferenceKind) -> Self {
        Self { kind }
    }

    fn stack_slots(&self) -> usize {
        match self.kind {
            AssignmentReferenceKind::GlobalVar(_) => 1,
            AssignmentReferenceKind::GlobalLexical(_) => 1,
            AssignmentReferenceKind::FrameSlotLexical(_) => 0,
            AssignmentReferenceKind::FrameSlotNonLexical(_) => 0,
            AssignmentReferenceKind::EnvironmentCoordLexical(_, _) => 0,
            AssignmentReferenceKind::EnvironmentCoordNonLexical(_, _) => 0,
            AssignmentReferenceKind::Dynamic(_) => 1,
            AssignmentReferenceKind::Prop(_) => 1,
            AssignmentReferenceKind::Elem => 2,
        }
    }
}

#[derive(Debug, PartialEq)]
enum DeclarationReferenceKind {
    GlobalVar(GCThingIndex),
    GlobalLexical(GCThingIndex),
    FrameSlot(FrameSlot),
    EnvironmentCoord(EnvironmentHops, EnvironmentSlot),
}

// See DeclarationReferenceEmitter.
// This uses struct to hide the details from the consumer.
#[derive(Debug)]
#[must_use]
pub struct DeclarationReference {
    kind: DeclarationReferenceKind,
}
impl DeclarationReference {
    fn new(kind: DeclarationReferenceKind) -> Self {
        Self { kind }
    }
}

#[derive(Debug, PartialEq)]
enum CallKind {
    Normal,
    // FIXME: Support eval, Function#call, Function#apply etc.
}

#[derive(Debug, PartialEq)]
enum ValueIsOnStack {
    No,
    Yes,
}

fn check_frame_temporary_dead_zone(
    emitter: &mut AstEmitter,
    slot: FrameSlot,
    is_on_stack: ValueIsOnStack,
) {
    // FIXME: Use cache to avoid emitting check_lexical twice or more.
    // FIXME: Support aliased lexical.

    //                  [stack] VAL?

    if is_on_stack == ValueIsOnStack::No {
        emitter.emit.get_local(slot.into());
        //              [stack] VAL
    }

    emitter.emit.check_lexical(slot.into());
    //                  [stack] VAL

    if is_on_stack == ValueIsOnStack::No {
        emitter.emit.pop();
        //              [stack]
    }

    //                  [stack] VAL?
}

fn check_env_temporary_dead_zone(
    emitter: &mut AstEmitter,
    hops: EnvironmentHops,
    slot: EnvironmentSlot,
    is_on_stack: ValueIsOnStack,
) {
    // FIXME: Use cache to avoid emitting check_lexical twice or more.
    // FIXME: Support aliased lexical.

    //                  [stack] VAL?

    if is_on_stack == ValueIsOnStack::No {
        emitter.emit.get_aliased_var(hops.into(), slot.into());
        //              [stack] VAL
    }

    emitter.emit.check_aliased_lexical(hops.into(), slot.into());
    //                  [stack] VAL

    if is_on_stack == ValueIsOnStack::No {
        emitter.emit.pop();
        //              [stack]
    }

    //                  [stack] VAL?
}

// See *ReferenceEmitter.
// This uses struct to hide the details from the consumer.
#[derive(Debug)]
#[must_use]
pub struct CallReference {
    kind: CallKind,
}
impl CallReference {
    fn new(kind: CallKind) -> Self {
        Self { kind }
    }
}

// Struct for emitting bytecode for get `name` operation.
pub struct GetNameEmitter {
    pub name: SourceAtomSetIndex,
}
impl GetNameEmitter {
    pub fn emit(self, emitter: &mut AstEmitter) {
        let name_index = emitter.emit.get_atom_gcthing_index(self.name);
        let loc = emitter.lookup_name(self.name);

        //              [stack]

        match loc {
            NameLocation::Global(_kind) => {
                emitter.emit.get_g_name(name_index);
                //      [stack] VAL
            }
            NameLocation::Dynamic => {
                emitter.emit.get_name(name_index);
                //      [stack] VAL
            }
            NameLocation::FrameSlot(slot, kind) => {
                emitter.emit.get_local(slot.into());
                //      [stack] VAL

                if kind == BindingKind::Let || kind == BindingKind::Const {
                    check_frame_temporary_dead_zone(emitter, slot, ValueIsOnStack::Yes);
                    //  [stack] VAL
                }
            }
            NameLocation::EnvironmentCoord(hops, slot, kind) => {
                emitter.emit.get_aliased_var(hops.into(), slot.into());

                if kind == BindingKind::Let || kind == BindingKind::Const {
                    check_env_temporary_dead_zone(emitter, hops, slot, ValueIsOnStack::Yes);
                    //  [stack] VAL
                }
            }
        }
    }
}

// Struct for emitting bytecode for get `obj.key` operation.
pub struct GetPropEmitter<F>
where
    F: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub obj: F,
    pub key: SourceAtomSetIndex,
}
impl<F> GetPropEmitter<F>
where
    F: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        let key_index = emitter.emit.get_atom_gcthing_index(self.key);

        //              [stack]

        let depth = emitter.emit.stack_depth();
        (self.obj)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] OBJ

        emitter.emit.get_prop(key_index);
        //              [stack] VAL

        Ok(())
    }
}

// Struct for emitting bytecode for get `super.key` operation.
pub struct GetSuperPropEmitter<F>
where
    F: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub this: F,
    pub key: SourceAtomSetIndex,
}
impl<F> GetSuperPropEmitter<F>
where
    F: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        let key_index = emitter.emit.get_atom_gcthing_index(self.key);

        //              [stack]

        let depth = emitter.emit.stack_depth();
        (self.this)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] THIS

        emitter.emit.callee();
        //              [stack] THIS CALLEE

        emitter.emit.super_base();
        //              [stack] THIS OBJ

        emitter.emit.get_prop_super(key_index);
        //              [stack] VAL

        Ok(())
    }
}

// Struct for emitting bytecode for get `obj[key]` operation.
pub struct GetElemEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<(), EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub obj: F1,
    pub key: F2,
}
impl<F1, F2> GetElemEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<(), EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        //              [stack]

        let depth = emitter.emit.stack_depth();
        (self.obj)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] OBJ

        let depth = emitter.emit.stack_depth();
        (self.key)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] OBJ KEY

        emitter.emit.get_elem();
        //              [stack] VAL

        Ok(())
    }
}

// Struct for emitting bytecode for get `super[key]` operation.
pub struct GetSuperElemEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<(), EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub this: F1,
    pub key: F2,
}
impl<F1, F2> GetSuperElemEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<(), EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        //              [stack]

        let depth = emitter.emit.stack_depth();
        (self.this)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] THIS

        let depth = emitter.emit.stack_depth();
        (self.key)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] THIS KEY

        emitter.emit.callee();
        //              [stack] THIS KEY CALLEE

        emitter.emit.super_base();
        //              [stack] THIS KEY OBJ

        emitter.emit.get_elem_super();
        //              [stack] VAL

        Ok(())
    }
}

// Struct for emitting bytecode for `name` reference.
pub struct NameReferenceEmitter {
    pub name: SourceAtomSetIndex,
}
impl NameReferenceEmitter {
    pub fn emit_for_call(self, emitter: &mut AstEmitter) -> CallReference {
        let name_index = emitter.emit.get_atom_gcthing_index(self.name);
        let loc = emitter.lookup_name(self.name);

        //              [stack]

        match loc {
            NameLocation::Global(_kind) => {
                emitter.emit.get_g_name(name_index);
                //      [stack] CALLEE

                // NOTE: We don't support non-syntactic scope.
                //       See NameOpEmitter::emitGet in SpiderMonkey for omitted
                //       cases.

                emitter.emit.undefined();
                //      [stack] CALLEE THIS
            }
            NameLocation::Dynamic => {
                emitter.emit.get_name(name_index);
                //      [stack] CALLEE

                // NOTE: We don't support non-syntactic scope or with statement.
                //       See NameOpEmitter::emitGet in SpiderMonkey for omitted
                //       cases.

                emitter.emit.undefined();
                //      [stack] CALLEE THIS
            }
            NameLocation::FrameSlot(slot, kind) => {
                emitter.emit.get_local(slot.into());
                //      [stack] CALLEE

                if kind == BindingKind::Let || kind == BindingKind::Const {
                    check_frame_temporary_dead_zone(emitter, slot, ValueIsOnStack::Yes);
                    //  [stack] CALLEE
                }

                emitter.emit.undefined();
                //      [stack] CALLEE THIS
            }
            NameLocation::EnvironmentCoord(hops, slot, kind) => {
                emitter.emit.get_aliased_var(hops.into(), slot.into());
                //      [stack] CALLEE

                if kind == BindingKind::Let || kind == BindingKind::Const {
                    check_env_temporary_dead_zone(emitter, hops, slot, ValueIsOnStack::Yes);
                    //  [stack] CALLEE
                }

                emitter.emit.undefined();
                //      [stack] CALLEE THIS
            }
        }

        CallReference::new(CallKind::Normal)
    }

    pub fn emit_for_assignment_with_loc(
        self,
        emitter: &mut AstEmitter,
        loc: NameLocation,
    ) -> AssignmentReference {
        let name_index = emitter.emit.get_atom_gcthing_index(self.name);

        //              [stack]

        match loc {
            NameLocation::Global(kind) => match kind {
                BindingKind::Var => {
                    emitter.emit.bind_g_name(name_index);
                    //  [stack] GLOBAL
                    AssignmentReference::new(AssignmentReferenceKind::GlobalVar(name_index))
                }
                BindingKind::Let | BindingKind::Const => {
                    emitter.emit.bind_g_name(name_index);
                    //  [stack] GLOBAL
                    AssignmentReference::new(AssignmentReferenceKind::GlobalLexical(name_index))
                }
            },
            NameLocation::Dynamic => {
                emitter.emit.bind_name(name_index);
                //      [stack] ENV

                AssignmentReference::new(AssignmentReferenceKind::Dynamic(name_index))
            }
            NameLocation::FrameSlot(slot, kind) => {
                if kind == BindingKind::Let || kind == BindingKind::Const {
                    AssignmentReference::new(AssignmentReferenceKind::FrameSlotLexical(slot))
                } else {
                    AssignmentReference::new(AssignmentReferenceKind::FrameSlotNonLexical(slot))
                }
            }
            NameLocation::EnvironmentCoord(hops, slot, kind) => {
                if kind == BindingKind::Let || kind == BindingKind::Const {
                    AssignmentReference::new(AssignmentReferenceKind::EnvironmentCoordLexical(
                        hops, slot,
                    ))
                } else {
                    AssignmentReference::new(AssignmentReferenceKind::EnvironmentCoordNonLexical(
                        hops, slot,
                    ))
                }
            }
        }
    }

    pub fn emit_for_assignment(self, emitter: &mut AstEmitter) -> AssignmentReference {
        let loc = emitter.lookup_name(self.name);
        self.emit_for_assignment_with_loc(emitter, loc)
    }

    /// Ignore any lexical scope and assign to var scope.
    /// Used by Annex B function.
    pub fn emit_for_var_assignment(self, emitter: &mut AstEmitter) -> AssignmentReference {
        let loc = emitter.lookup_name_in_var(self.name);
        self.emit_for_assignment_with_loc(emitter, loc)
    }

    pub fn emit_for_declaration(self, emitter: &mut AstEmitter) -> DeclarationReference {
        let name_index = emitter.emit.get_atom_gcthing_index(self.name);
        let loc = emitter.lookup_name(self.name);

        //              [stack]

        match loc {
            NameLocation::Global(kind) => match kind {
                BindingKind::Var => {
                    emitter.emit.bind_g_name(name_index);
                    //  [stack] GLOBAL
                    DeclarationReference::new(DeclarationReferenceKind::GlobalVar(name_index))
                }
                BindingKind::Let | BindingKind::Const => {
                    DeclarationReference::new(DeclarationReferenceKind::GlobalLexical(name_index))
                }
            },
            NameLocation::Dynamic => {
                panic!("declaration should have non-dynamic location");
            }
            NameLocation::FrameSlot(slot, _kind) => {
                DeclarationReference::new(DeclarationReferenceKind::FrameSlot(slot))
            }
            NameLocation::EnvironmentCoord(hops, slot, _kind) => {
                // FIXME: does this happen????
                DeclarationReference::new(DeclarationReferenceKind::EnvironmentCoord(hops, slot))
            }
        }
    }
}

// Struct for emitting bytecode for `obj.key` reference.
pub struct PropReferenceEmitter<F>
where
    F: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub obj: F,
    pub key: SourceAtomSetIndex,
}
impl<F> PropReferenceEmitter<F>
where
    F: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub fn emit_for_call(self, emitter: &mut AstEmitter) -> Result<CallReference, EmitError> {
        let key_index = emitter.emit.get_atom_gcthing_index(self.key);

        //              [stack]

        let depth = emitter.emit.stack_depth();
        (self.obj)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] THIS

        emitter.emit.dup();
        //              [stack] THIS THIS

        // FIXME: Support super.
        emitter.emit.get_prop(key_index);
        //              [stack] THIS CALLEE

        emitter.emit.swap();
        //              [stack] CALLEE THIS

        Ok(CallReference::new(CallKind::Normal))
    }

    #[allow(dead_code)]
    pub fn emit_for_assignment(
        self,
        emitter: &mut AstEmitter,
    ) -> Result<AssignmentReference, EmitError> {
        let key_index = emitter.emit.get_atom_gcthing_index(self.key);

        //              [stack]

        let depth = emitter.emit.stack_depth();
        (self.obj)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] OBJ

        Ok(AssignmentReference::new(AssignmentReferenceKind::Prop(
            key_index,
        )))
    }
}

// Struct for emitting bytecode for `obj[key]` reference.
pub struct ElemReferenceEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<(), EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub obj: F1,
    pub key: F2,
}
impl<F1, F2> ElemReferenceEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<(), EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub fn emit_for_call(self, emitter: &mut AstEmitter) -> Result<CallReference, EmitError> {
        //              [stack]

        let depth = emitter.emit.stack_depth();
        (self.obj)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] THIS

        emitter.emit.dup();
        //              [stack] THIS THIS

        let depth = emitter.emit.stack_depth();
        (self.key)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] THIS THIS KEY

        // FIXME: Support super.
        emitter.emit.get_elem();
        //              [stack] THIS CALLEE

        emitter.emit.swap();
        //              [stack] CALLEE THIS

        Ok(CallReference::new(CallKind::Normal))
    }

    #[allow(dead_code)]
    pub fn emit_for_assignment(
        self,
        emitter: &mut AstEmitter,
    ) -> Result<AssignmentReference, EmitError> {
        //              [stack]

        let depth = emitter.emit.stack_depth();
        (self.obj)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] OBJ

        let depth = emitter.emit.stack_depth();
        (self.key)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] OBJ KEY

        Ok(AssignmentReference::new(AssignmentReferenceKind::Elem))
    }
}

// Struct for emitting bytecode for call `callee(arguments)` operation.
pub struct CallEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<CallReference, EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<usize, EmitError>,
{
    pub callee: F1,
    pub arguments: F2,
}
impl<F1, F2> CallEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<CallReference, EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<usize, EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        //              [stack]

        let depth = emitter.emit.stack_depth();
        let reference = (self.callee)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 2);
        //              [stack] CALLEE THIS

        // FIXME: Support spread.
        let depth = emitter.emit.stack_depth();
        let len = (self.arguments)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + len);
        //              [stack] CALLEE THIS ARGS...

        match reference.kind {
            CallKind::Normal => {
                emitter.emit.call(len as u16);
                //      [stack] VAL
            }
        }

        Ok(())
    }
}

// Struct for emitting bytecode for `new callee(arguments)` operation.
pub struct NewEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<(), EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<usize, EmitError>,
{
    pub callee: F1,
    pub arguments: F2,
}
impl<F1, F2> NewEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<(), EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<usize, EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        //              [stack]

        let depth = emitter.emit.stack_depth();
        (self.callee)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] CALLEE

        emitter.emit.is_constructing();
        //              [stack] CALLEE JS_IS_CONSTRUCTING

        // FIXME: Support spread.
        let depth = emitter.emit.stack_depth();
        let len = (self.arguments)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + len);
        //              [stack] CALLEE JS_IS_CONSTRUCTING ARGS...

        emitter.emit.dup_at(len as u32 + 1);
        //              [stack] CALLEE JS_IS_CONSTRUCTING ARGS... CALLEE

        emitter.emit.new_(len as u16);
        //              [stack] VAL

        Ok(())
    }
}

// Struct for emitting bytecode for assignment `lhs = rhs` operation.
pub struct AssignmentEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<AssignmentReference, EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub lhs: F1,
    pub rhs: F2,
}
impl<F1, F2> AssignmentEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<AssignmentReference, EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        //              [stack]

        let depth = emitter.emit.stack_depth();
        let reference = (self.lhs)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + reference.stack_slots());
        //              [stack] REF...

        let depth = emitter.emit.stack_depth();
        (self.rhs)(emitter)?;
        debug_assert_eq!(emitter.emit.stack_depth(), depth + 1);
        //              [stack] REF... VAL

        match reference.kind {
            AssignmentReferenceKind::GlobalVar(name_index) => {
                //      [stack] GLOBAL VAL

                emitter.emit.set_g_name(name_index);
                //      [stack] VAL
            }
            AssignmentReferenceKind::GlobalLexical(name_index) => {
                //      [stack] VAL

                emitter.emit.set_g_name(name_index);
                //      [stack] VAL
            }
            AssignmentReferenceKind::Dynamic(name_index) => {
                //      [stack] ENV VAL

                emitter.emit.set_name(name_index);
                //      [stack] VAL
            }
            AssignmentReferenceKind::FrameSlotLexical(slot) => {
                //      [stack] VAL

                check_frame_temporary_dead_zone(emitter, slot, ValueIsOnStack::No);
                //      [stack] VAL

                emitter.emit.set_local(slot.into());
                //      [stack] VAL
            }
            AssignmentReferenceKind::FrameSlotNonLexical(slot) => {
                //      [stack] VAL

                emitter.emit.set_local(slot.into());
                //      [stack] VAL
            }
            AssignmentReferenceKind::EnvironmentCoordLexical(hops, slot) => {
                //      [stack] VAL

                check_env_temporary_dead_zone(emitter, hops, slot, ValueIsOnStack::No);
                //      [stack] VAL

                emitter.emit.set_aliased_var(hops.into(), slot.into());
                //      [stack] VAL
            }
            AssignmentReferenceKind::EnvironmentCoordNonLexical(hops, slot) => {
                //      [stack] VAL

                emitter.emit.set_aliased_var(hops.into(), slot.into());
                //      [stack] VAL
            }
            AssignmentReferenceKind::Prop(key_index) => {
                //      [stack] OBJ VAL

                // FIXME: Support strict mode and super.
                emitter.emit.set_prop(key_index);
                //      [stack] VAL
            }
            AssignmentReferenceKind::Elem => {
                //      [stack] OBJ KEY VAL

                // FIXME: Support strict mode and super.
                emitter.emit.set_elem();
                //      [stack] VAL
            }
        }

        Ok(())
    }

    // FIXME: Support compound assignment
}

// Struct for emitting bytecode for declaration `lhs = rhs` operation.
pub struct DeclarationEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<DeclarationReference, EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub lhs: F1,
    pub rhs: F2,
}
impl<F1, F2> DeclarationEmitter<F1, F2>
where
    F1: Fn(&mut AstEmitter) -> Result<DeclarationReference, EmitError>,
    F2: Fn(&mut AstEmitter) -> Result<(), EmitError>,
{
    pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> {
        let reference = (self.lhs)(emitter)?;

        (self.rhs)(emitter)?;

        match reference.kind {
            DeclarationReferenceKind::GlobalVar(name_index) => {
                //      [stack] GLOBAL VAL

                emitter.emit.set_g_name(name_index);
                //      [stack] VAL
            }
            DeclarationReferenceKind::GlobalLexical(name_index) => {
                //      [stack] VAL

                emitter.emit.init_g_lexical(name_index);
                //      [stack] VAL
            }
            DeclarationReferenceKind::FrameSlot(slot) => {
                //      [stack] VAL

                emitter.emit.init_lexical(slot.into());
                //      [stack] VAL
            }
            DeclarationReferenceKind::EnvironmentCoord(hops, slot) => {
                //      [stack] VAL

                emitter.emit.init_aliased_lexical(hops.into(), slot.into());
                //      [stack] VAL
            }
        }

        Ok(())
    }
}

// FIXME: Add increment