mirror of https://github.com/boa-dev/boa.git
croraf
5 years ago
committed by
GitHub
8 changed files with 204 additions and 31 deletions
@ -0,0 +1,91 @@ |
|||||||
|
//! This module implements the global `ReferenceError` object.
|
||||||
|
//!
|
||||||
|
//! Indicates an error that occurs when de-referencing an invalid reference
|
||||||
|
//!
|
||||||
|
//! More information:
|
||||||
|
//! - [MDN documentation][mdn]
|
||||||
|
//! - [ECMAScript reference][spec]
|
||||||
|
//!
|
||||||
|
//! [spec]: https://tc39.es/ecma262/#sec-native-error-types-used-in-this-standard-referenceerror
|
||||||
|
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError
|
||||||
|
|
||||||
|
use crate::{ |
||||||
|
builtins::{ |
||||||
|
function::make_builtin_fn, |
||||||
|
function::make_constructor_fn, |
||||||
|
object::ObjectData, |
||||||
|
value::{ResultValue, Value}, |
||||||
|
}, |
||||||
|
exec::Interpreter, |
||||||
|
profiler::BoaProfiler, |
||||||
|
}; |
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)] |
||||||
|
pub(crate) struct ReferenceError; |
||||||
|
|
||||||
|
impl ReferenceError { |
||||||
|
/// The name of the object.
|
||||||
|
pub(crate) const NAME: &'static str = "ReferenceError"; |
||||||
|
|
||||||
|
/// The amount of arguments this function object takes.
|
||||||
|
pub(crate) const LENGTH: usize = 1; |
||||||
|
|
||||||
|
/// Create a new error object.
|
||||||
|
pub(crate) fn make_error(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { |
||||||
|
if !args.is_empty() { |
||||||
|
this.set_field( |
||||||
|
"message", |
||||||
|
Value::from( |
||||||
|
args.get(0) |
||||||
|
.expect("failed getting error message") |
||||||
|
.to_string(), |
||||||
|
), |
||||||
|
); |
||||||
|
} |
||||||
|
// This value is used by console.log and other routines to match Object type
|
||||||
|
// to its Javascript Identifier (global constructor method name)
|
||||||
|
this.set_data(ObjectData::Error); |
||||||
|
Err(this.clone()) |
||||||
|
} |
||||||
|
|
||||||
|
/// `Error.prototype.toString()`
|
||||||
|
///
|
||||||
|
/// The toString() method returns a string representing the specified Error object.
|
||||||
|
///
|
||||||
|
/// More information:
|
||||||
|
/// - [MDN documentation][mdn]
|
||||||
|
/// - [ECMAScript reference][spec]
|
||||||
|
///
|
||||||
|
/// [spec]: https://tc39.es/ecma262/#sec-error.prototype.tostring
|
||||||
|
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString
|
||||||
|
#[allow(clippy::wrong_self_convention)] |
||||||
|
pub(crate) fn to_string(this: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultValue { |
||||||
|
let name = this.get_field("name"); |
||||||
|
let message = this.get_field("message"); |
||||||
|
Ok(Value::from(format!("{}: {}", name, message))) |
||||||
|
} |
||||||
|
|
||||||
|
/// Create a new `ReferenceError` object.
|
||||||
|
pub(crate) fn create(global: &Value) -> Value { |
||||||
|
let prototype = Value::new_object(Some(global)); |
||||||
|
prototype.set_field("message", Value::from("")); |
||||||
|
|
||||||
|
make_builtin_fn(Self::to_string, "toString", &prototype, 0); |
||||||
|
|
||||||
|
make_constructor_fn( |
||||||
|
Self::NAME, |
||||||
|
Self::LENGTH, |
||||||
|
Self::make_error, |
||||||
|
global, |
||||||
|
prototype, |
||||||
|
true, |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
/// Initialise the global object with the `ReferenceError` object.
|
||||||
|
pub(crate) fn init(global: &Value) -> (&str, Value) { |
||||||
|
let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); |
||||||
|
|
||||||
|
(Self::NAME, Self::create(global)) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,21 @@ |
|||||||
|
use super::{Executable, Interpreter}; |
||||||
|
use crate::{ |
||||||
|
builtins::value::{ResultValue, Value, ValueData}, |
||||||
|
syntax::ast::node::identifier::Identifier, |
||||||
|
}; |
||||||
|
|
||||||
|
impl Executable for Identifier { |
||||||
|
fn run(&self, interpreter: &mut Interpreter) -> ResultValue { |
||||||
|
let reference = resolve_binding(interpreter, self.as_ref()); |
||||||
|
match reference.data() { |
||||||
|
ValueData::Undefined => Err(interpreter |
||||||
|
.throw_reference_error(self.as_ref()) |
||||||
|
.expect_err("throw_reference_error() must return an error")), |
||||||
|
_ => Ok(reference), |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub(crate) fn resolve_binding(interpreter: &mut Interpreter, name: &str) -> Value { |
||||||
|
interpreter.realm().environment.get_binding_value(name) |
||||||
|
} |
Loading…
Reference in new issue