summaryrefslogtreecommitdiffstats
path: root/js/src/frontend/AsyncEmitter.h
blob: 778f708c4fe93b3c3ba5d3ac58f500fca3f5c97f (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
/* -*- 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/. */

#ifndef frontend_AsyncEmitter_h
#define frontend_AsyncEmitter_h

#include "mozilla/Attributes.h"  // MOZ_STACK_CLASS

#include "frontend/TryEmitter.h"  // TryEmitter

namespace js {
namespace frontend {

struct BytecodeEmitter;

// Class for emitting Bytecode associated with the AsyncFunctionGenerator.
//
// Usage:
//
//   For an async function, the params have to be handled separately,
//   because the body may have pushed an additional var environment, changing
//   the number of hops required to reach the |.generator| variable. In order
//   to handle this, we can't reuse the same TryCatch emitter.
//
//   Simple case - For a function without expression or destructuring
//   parameters:
//   `async function f(<params>) {<body>}`,
//     AsyncEmitter ae(this);
//
//     ae.prepareForParamsWithoutExpressionOrDestructuring();
//     // Emit Params.
//     ...
//     ae.paramsEpilogue(); // We need to emit the epilogue before the extra
//     VarScope emitExtraBodyVarScope();
//
//     // Emit new scope
//     ae.prepareForBody();
//
//     // Emit body of the Function.
//     ...
//
//     ae.emitEndFunction();
//
//   Complex case - For a function with expression or destructuring parameters:
//   `async function f(<expression>) {<body>}`,
//     AsyncEmitter ae(this);
//
//     ae.prepareForParamsWithExpressionOrDestructuring();
//
//     // Emit Params.
//     ...
//     ae.paramsEpilogue(); // We need to emit the epilogue before the extra
//                          // VarScope
//     emitExtraBodyVarScope();
//
//     // Emit new scope
//     ae.prepareForBody();
//
//     // Emit body of the Function.
//     ...
//
//     // The final yield is emitted in FunctionScriptEmitter::emitEndBody().
//     ae.emitEndFunction();
//
//
//   Async Module case - For a module with `await` in the top level:
//     AsyncEmitter ae(this);
//     ae.prepareForModule(); // prepareForModule is used to setup the generator
//                            // for the async module.
//     switchToMain();
//     ...
//
//     // Emit new scope
//     ae.prepareForBody();
//
//     // Emit body of the Script.
//
//     ae.emitEndModule();
//

class MOZ_STACK_CLASS AsyncEmitter {
 private:
  BytecodeEmitter* bce_;

  // try-catch block for async function parameter and body.
  mozilla::Maybe<TryEmitter> rejectTryCatch_;

#ifdef DEBUG
  // The state of this emitter.
  //
  //    +-------+
  //    | Start |-+
  //    +-------+ |
  //              |
  //   +----------+
  //   |
  //   | [Parameters with Expression or Destructuring]
  //   |   prepareForParamsWithExpressionOrDestructuring    +------------+
  //   +----------------------------------------------------| Parameters |-->+
  //   |                                                    +------------+   |
  //   |                                                                     |
  //   | [Parameters Without Expression or Destructuring]                    |
  //   |   prepareForParamsWithoutExpressionOrDestructuring +------------+   |
  //   +----------------------------------------------------| Parameters |-->+
  //   |                                                    +------------+   |
  //   | [Modules]                                                           |
  //   |   prepareForModule  +----------------+                              |
  //   +-------------------->| ModulePrologue |--+                           |
  //                         +----------------+  |                           |
  //                                             |                           |
  //                                             |                           |
  //   +-----------------------------------------+                           |
  //   |                                                                     |
  //   |                                                                     |
  //   V                     +------------+  paramsEpilogue                  |
  //   +<--------------------| PostParams |<---------------------------------+
  //   |                     +------------+
  //   |
  //   | [Script body]
  //   |   prepareForBody    +---------+
  //   +-------------------->|  Body   |--------+
  //                         +---------+        |  <emit script body>
  //   +----------------------------------------+
  //   |
  //   |  [Functions]
  //   |  emitEndFunction     +-----+
  //   +--------------------->| End |
  //   |                      +-----+
  //   |
  //   |  [Modules]
  //   |  emitEndModule       +-----+
  //   +--------------------->| End |
  //                          +-----+

  enum class State {
    // The initial state.
    Start,

    Parameters,

    ModulePrologue,

    PostParams,

    Body,

    End,
  };

  State state_ = State::Start;
#endif

  [[nodiscard]] bool emitRejectCatch();
  [[nodiscard]] bool emitFinalYield();

 public:
  explicit AsyncEmitter(BytecodeEmitter* bce) : bce_(bce){};

  [[nodiscard]] bool prepareForParamsWithoutExpressionOrDestructuring();
  [[nodiscard]] bool prepareForParamsWithExpressionOrDestructuring();
  [[nodiscard]] bool prepareForModule();
  [[nodiscard]] bool emitParamsEpilogue();
  [[nodiscard]] bool prepareForBody();
  [[nodiscard]] bool emitEndFunction();
  [[nodiscard]] bool emitEndModule();
};

} /* namespace frontend */
} /* namespace js */

#endif /* frontend_AsyncEmitter_h */