1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "frontend/WhileEmitter.h"
9 #include "frontend/BytecodeEmitter.h"
10 #include "vm/Opcodes.h"
11 #include "vm/StencilEnums.h" // TryNoteKind
14 using namespace js::frontend
;
16 WhileEmitter::WhileEmitter(BytecodeEmitter
* bce
) : bce_(bce
) {}
18 bool WhileEmitter::emitCond(uint32_t whilePos
, uint32_t condPos
,
20 MOZ_ASSERT(state_
== State::Start
);
22 // If we have a single-line while, like "while (x) ;", we want to emit the
23 // line note before the loop, so that the debugger sees a single entry point.
24 // This way, if there is a breakpoint on the line, it will only fire once; and
25 // "next"ing will skip the whole loop. However, for the multi-line case we
26 // want to emit the line note for the JSOp::LoopHead, so that "cont" stops on
27 // each iteration -- but without a stop before the first iteration.
28 if (bce_
->errorReporter().lineAt(whilePos
) ==
29 bce_
->errorReporter().lineAt(endPos
)) {
30 if (!bce_
->updateSourceCoordNotes(whilePos
)) {
33 // Emit a Nop to ensure the source position is not part of the loop.
34 if (!bce_
->emit1(JSOp::Nop
)) {
39 loopInfo_
.emplace(bce_
, StatementKind::WhileLoop
);
41 if (!loopInfo_
->emitLoopHead(bce_
, mozilla::Some(condPos
))) {
51 bool WhileEmitter::emitBody() {
52 MOZ_ASSERT(state_
== State::Cond
);
54 if (!bce_
->emitJump(JSOp::JumpIfFalse
, &loopInfo_
->breaks
)) {
58 tdzCacheForBody_
.emplace(bce_
);
66 bool WhileEmitter::emitEnd() {
67 MOZ_ASSERT(state_
== State::Body
);
69 tdzCacheForBody_
.reset();
71 if (!loopInfo_
->emitContinueTarget(bce_
)) {
75 if (!loopInfo_
->emitLoopEnd(bce_
, JSOp::Goto
, TryNoteKind::Loop
)) {
87 #ifdef ENABLE_DECORATORS
88 bool InternalWhileEmitter::emitCond() {
89 MOZ_ASSERT(state_
== State::Start
);
91 loopInfo_
.emplace(bce_
, StatementKind::WhileLoop
);
93 if (!loopInfo_
->emitLoopHead(bce_
, mozilla::Nothing())) {