#include "frontend/LabelEmitter.h"
#include "mozilla/Assertions.h"
#include "frontend/BytecodeEmitter.h"
#include "vm/Opcodes.h"
using namespace js;
using namespace js::frontend;
bool LabelEmitter::emitLabel(JSAtom* name) {
MOZ_ASSERT(state_ == State::Start);
uint32_t index;
if (!bce_->makeAtomIndex(name, &index)) {
return false;
}
if (!bce_->emitN(JSOP_LABEL, 4, &top_)) {
return false;
}
controlInfo_.emplace(bce_, name, bce_->offset());
#ifdef DEBUG
state_ = State::Label;
#endif
return true;
}
bool LabelEmitter::emitEnd() {
MOZ_ASSERT(state_ == State::Label);
jsbytecode* labelpc = bce_->code(top_);
int32_t offset = bce_->lastNonJumpTargetOffset() - top_;
MOZ_ASSERT(*labelpc == JSOP_LABEL);
SET_CODE_OFFSET(labelpc, offset);
if (!controlInfo_->patchBreaks(bce_)) {
return false;
}
controlInfo_.reset();
#ifdef DEBUG
state_ = State::End;
#endif
return true;
}