mirror of https://github.com/boa-dev/boa.git
HalidOdat
5 years ago
committed by
GitHub
14 changed files with 172 additions and 63 deletions
@ -0,0 +1,83 @@ |
|||||||
|
//! This module implements the global `TypeError` object.
|
||||||
|
//!
|
||||||
|
//! The `TypeError` object represents an error when an operation could not be performed,
|
||||||
|
//! typically (but not exclusively) when a value is not of the expected type.
|
||||||
|
//!
|
||||||
|
//! A `TypeError` may be thrown when:
|
||||||
|
//! - an operand or argument passed to a function is incompatible with the type expected by that operator or function.
|
||||||
|
//! - when attempting to modify a value that cannot be changed.
|
||||||
|
//! - when attempting to use a value in an inappropriate way.
|
||||||
|
//!
|
||||||
|
//! More information:
|
||||||
|
//! - [MDN documentation][mdn]
|
||||||
|
//! - [ECMAScript reference][spec]
|
||||||
|
//!
|
||||||
|
//! [spec]: https://tc39.es/ecma262/#sec-native-error-types-used-in-this-standard-typeerror
|
||||||
|
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError
|
||||||
|
|
||||||
|
use crate::{ |
||||||
|
builtins::{ |
||||||
|
function::make_builtin_fn, |
||||||
|
function::make_constructor_fn, |
||||||
|
object::ObjectKind, |
||||||
|
value::{ResultValue, Value}, |
||||||
|
}, |
||||||
|
exec::Interpreter, |
||||||
|
}; |
||||||
|
|
||||||
|
/// JavaScript `TypeError` implementation.
|
||||||
|
#[derive(Debug, Clone, Copy)] |
||||||
|
pub(crate) struct TypeError; |
||||||
|
|
||||||
|
impl TypeError { |
||||||
|
/// 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_kind(ObjectKind::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 `RangeError` 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("TypeError", 1, Self::make_error, global, prototype, true) |
||||||
|
} |
||||||
|
|
||||||
|
/// Initialise the global object with the `RangeError` object.
|
||||||
|
pub(crate) fn init(global: &Value) { |
||||||
|
global.set_field("TypeError", Self::create(global)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
use super::*; |
||||||
|
use crate::{ |
||||||
|
exec::Executable, |
||||||
|
syntax::ast::{ |
||||||
|
node::{Call, Identifier, New}, |
||||||
|
Const, |
||||||
|
}, |
||||||
|
}; |
||||||
|
|
||||||
|
impl Interpreter { |
||||||
|
/// Throws a `RangeError` with the specified message.
|
||||||
|
pub fn throw_range_error<M>(&mut self, message: M) -> ResultValue |
||||||
|
where |
||||||
|
M: Into<String>, |
||||||
|
{ |
||||||
|
// Runs a `new RangeError(message)`.
|
||||||
|
New::from(Call::new( |
||||||
|
Identifier::from("RangeError"), |
||||||
|
vec![Const::from(message.into()).into()], |
||||||
|
)) |
||||||
|
.run(self) |
||||||
|
} |
||||||
|
|
||||||
|
/// Throws a `TypeError` with the specified message.
|
||||||
|
pub fn throw_type_error<M>(&mut self, message: M) -> ResultValue |
||||||
|
where |
||||||
|
M: Into<String>, |
||||||
|
{ |
||||||
|
// Runs a `new TypeError(message)`.
|
||||||
|
New::from(Call::new( |
||||||
|
Identifier::from("TypeError"), |
||||||
|
vec![Const::from(message.into()).into()], |
||||||
|
)) |
||||||
|
.run(self) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue