| 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/. */ |
| 6 |
|
| 7 |
#include "frontend/DefaultEmitter.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 10 |
|
| 11 |
#include "frontend/BytecodeEmitter.h" // BytecodeEmitter |
| 12 |
#include "vm/Opcodes.h" // JSOp |
| 13 |
|
| 14 |
using namespace js; |
| 15 |
using namespace js::frontend; |
| 16 |
|
| 17 |
using mozilla::Nothing; |
| 18 |
|
| 19 |
DefaultEmitter::DefaultEmitter(BytecodeEmitter* bce) : bce_(bce) {} |
| 20 |
|
| 21 |
bool DefaultEmitter::prepareForDefault() { |
| 22 |
MOZ_ASSERT(state_ == State::Start); |
| 23 |
|
| 24 |
// [stack] VALUE |
| 25 |
|
| 26 |
ifUndefined_.emplace(bce_); |
| 27 |
if (!ifUndefined_->emitIf(Nothing())) { |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
if (!bce_->emit1(JSOp::Dup)) { |
| 32 |
// [stack] VALUE VALUE |
| 33 |
return false; |
| 34 |
} |
| 35 |
if (!bce_->emit1(JSOp::Undefined)) { |
| 36 |
// [stack] VALUE VALUE UNDEFINED |
| 37 |
return false; |
| 38 |
} |
| 39 |
if (!bce_->emit1(JSOp::StrictEq)) { |
| 40 |
// [stack] VALUE EQ? |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
if (!ifUndefined_->emitThen()) { |
| 45 |
// [stack] VALUE |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
if (!bce_->emit1(JSOp::Pop)) { |
| 50 |
// [stack] |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
#ifdef DEBUG |
| 55 |
state_ = State::Default; |
| 56 |
#endif |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
bool DefaultEmitter::emitEnd() { |
| 61 |
MOZ_ASSERT(state_ == State::Default); |
| 62 |
|
| 63 |
// [stack] DEFAULTVALUE |
| 64 |
|
| 65 |
if (!ifUndefined_->emitEnd()) { |
| 66 |
// [stack] VALUE/DEFAULTVALUE |
| 67 |
return false; |
| 68 |
} |
| 69 |
ifUndefined_.reset(); |
| 70 |
|
| 71 |
#ifdef DEBUG |
| 72 |
state_ = State::End; |
| 73 |
#endif |
| 74 |
return true; |
| 75 |
} |