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
|
/* -*- 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/. */
#include "frontend/TryEmitter.h"
#include "mozilla/Assertions.h" // MOZ_ASSERT
#include "frontend/BytecodeEmitter.h" // BytecodeEmitter
#include "frontend/SharedContext.h" // StatementKind
#include "vm/JSScript.h" // JSTRY_CATCH, JSTRY_FINALLY
#include "vm/Opcodes.h" // JSOp
using namespace js;
using namespace js::frontend;
using mozilla::Maybe;
TryEmitter::TryEmitter(BytecodeEmitter* bce, Kind kind, ControlKind controlKind)
: bce_(bce),
kind_(kind),
controlKind_(controlKind),
depth_(0),
tryOpOffset_(0)
#ifdef DEBUG
,
state_(State::Start)
#endif
{
if (controlKind_ == ControlKind::Syntactic) {
controlInfo_.emplace(
bce_, hasFinally() ? StatementKind::Finally : StatementKind::Try);
}
}
bool TryEmitter::emitTry() {
MOZ_ASSERT(state_ == State::Start);
// Since an exception can be thrown at any place inside the try block,
// we need to restore the stack and the scope chain before we transfer
// the control to the exception handler.
//
// For that we store in a try note associated with the catch or
// finally block the stack depth upon the try entry. The interpreter
// uses this depth to properly unwind the stack and the scope chain.
depth_ = bce_->bytecodeSection().stackDepth();
tryOpOffset_ = bce_->bytecodeSection().offset();
if (!bce_->emit1(JSOp::Try)) {
return false;
}
#ifdef DEBUG
state_ = State::Try;
#endif
return true;
}
bool TryEmitter::emitTryEnd() {
MOZ_ASSERT(state_ == State::Try);
MOZ_ASSERT(depth_ == bce_->bytecodeSection().stackDepth());
// Gosub to finally, if present.
if (hasFinally() && controlInfo_) {
if (!bce_->emitGoSub(&controlInfo_->gosubs)) {
return false;
}
}
// Emit jump over catch and/or finally.
if (!bce_->emitJump(JSOp::Goto, &catchAndFinallyJump_)) {
return false;
}
if (!bce_->emitJumpTarget(&tryEnd_)) {
return false;
}
return true;
}
bool TryEmitter::emitCatch() {
MOZ_ASSERT(state_ == State::Try);
if (!emitTryEnd()) {
return false;
}
MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_);
if (controlKind_ == ControlKind::Syntactic) {
// Clear the frame's return value that might have been set by the
// try block:
//
// eval("try { 1; throw 2 } catch(e) {}"); // undefined, not 1
if (!bce_->emit1(JSOp::Undefined)) {
return false;
}
if (!bce_->emit1(JSOp::SetRval)) {
return false;
}
}
if (!bce_->emit1(JSOp::Exception)) {
return false;
}
if (!instrumentEntryPoint()) {
return false;
}
#ifdef DEBUG
state_ = State::Catch;
#endif
return true;
}
bool TryEmitter::emitCatchEnd() {
MOZ_ASSERT(state_ == State::Catch);
if (!controlInfo_) {
return true;
}
// gosub <finally>, if required.
if (hasFinally()) {
if (!bce_->emitGoSub(&controlInfo_->gosubs)) {
return false;
}
MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_);
// Jump over the finally block.
if (!bce_->emitJump(JSOp::Goto, &catchAndFinallyJump_)) {
return false;
}
}
return true;
}
bool TryEmitter::emitFinally(
const Maybe<uint32_t>& finallyPos /* = Nothing() */) {
// If we are using controlInfo_ (i.e., emitting a syntactic try
// blocks), we must have specified up front if there will be a finally
// close. For internal non-syntactic try blocks, like those emitted for
// yield* and IteratorClose inside for-of loops, we can emitFinally even
// without specifying up front, since the internal non-syntactic try
// blocks emit no GOSUBs.
if (!controlInfo_) {
if (kind_ == Kind::TryCatch) {
kind_ = Kind::TryCatchFinally;
}
} else {
MOZ_ASSERT(hasFinally());
}
if (!hasCatch()) {
MOZ_ASSERT(state_ == State::Try);
if (!emitTryEnd()) {
return false;
}
} else {
MOZ_ASSERT(state_ == State::Catch);
if (!emitCatchEnd()) {
return false;
}
}
MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_);
if (!bce_->emitJumpTarget(&finallyStart_)) {
return false;
}
if (controlInfo_) {
// Fix up the gosubs that might have been emitted before non-local
// jumps to the finally code.
bce_->patchJumpsToTarget(controlInfo_->gosubs, finallyStart_);
// Indicate that we're emitting a subroutine body.
controlInfo_->setEmittingSubroutine();
}
if (finallyPos) {
if (!bce_->updateSourceCoordNotes(finallyPos.value())) {
return false;
}
}
if (!bce_->emit1(JSOp::Finally)) {
return false;
}
if (controlKind_ == ControlKind::Syntactic) {
if (!bce_->emit1(JSOp::GetRval)) {
return false;
}
// Clear the frame's return value to make break/continue return
// correct value even if there's no other statement before them:
//
// eval("x: try { 1 } finally { break x; }"); // undefined, not 1
if (!bce_->emit1(JSOp::Undefined)) {
return false;
}
if (!bce_->emit1(JSOp::SetRval)) {
return false;
}
}
if (!instrumentEntryPoint()) {
return false;
}
#ifdef DEBUG
state_ = State::Finally;
#endif
return true;
}
bool TryEmitter::emitFinallyEnd() {
MOZ_ASSERT(state_ == State::Finally);
if (controlKind_ == ControlKind::Syntactic) {
if (!bce_->emit1(JSOp::SetRval)) {
return false;
}
}
if (!bce_->emit1(JSOp::Retsub)) {
return false;
}
bce_->hasTryFinally = true;
return true;
}
bool TryEmitter::emitEnd() {
if (!hasFinally()) {
MOZ_ASSERT(state_ == State::Catch);
if (!emitCatchEnd()) {
return false;
}
} else {
MOZ_ASSERT(state_ == State::Finally);
if (!emitFinallyEnd()) {
return false;
}
}
MOZ_ASSERT(bce_->bytecodeSection().stackDepth() == depth_);
// Fix up the end-of-try/catch jumps to come here.
if (!bce_->emitJumpTargetAndPatch(catchAndFinallyJump_)) {
return false;
}
// Add the try note last, to let post-order give us the right ordering
// (first to last for a given nesting level, inner to outer by level).
if (hasCatch()) {
if (!bce_->addTryNote(TryNoteKind::Catch, depth_, offsetAfterTryOp(),
tryEnd_.offset)) {
return false;
}
}
// If we've got a finally, mark try+catch region with additional
// trynote to catch exceptions (re)thrown from a catch block or
// for the try{}finally{} case.
if (hasFinally()) {
if (!bce_->addTryNote(TryNoteKind::Finally, depth_, offsetAfterTryOp(),
finallyStart_.offset)) {
return false;
}
}
#ifdef DEBUG
state_ = State::End;
#endif
return true;
}
bool TryEmitter::instrumentEntryPoint() {
// Frames for async functions can resume execution at catch or finally blocks
// if an await operation threw an exception. While the frame might already be
// on the stack, the Entry instrumentation kind only indicates that a new
// frame *might* have been pushed.
if (bce_->sc->isFunctionBox() && bce_->sc->asFunctionBox()->isAsync()) {
return bce_->emitInstrumentation(InstrumentationKind::Entry);
}
return true;
}
|