-
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding native error types, starting with
RangeError
- Loading branch information
Showing
4 changed files
with
82 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//! This module implements the global `RangeError` object. | ||
//! | ||
//! Indicates a value that is not in the set or range of allowable values. | ||
//! | ||
//! More information: | ||
//! - [MDN documentation][mdn] | ||
//! - [ECMAScript reference][spec] | ||
//! | ||
//! [spec]: https://tc39.es/ecma262/#sec-native-error-types-used-in-this-standard-rangeerror | ||
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError | ||
use crate::{ | ||
builtins::{ | ||
function::make_constructor_fn, | ||
object::ObjectKind, | ||
value::{ResultValue, Value}, | ||
}, | ||
exec::Interpreter, | ||
}; | ||
|
||
/// Create a new error object. | ||
pub fn make_error(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { | ||
if !args.is_empty() { | ||
this.set_field_slice( | ||
"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); | ||
Ok(Value::undefined()) | ||
} | ||
|
||
/// `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 | ||
pub fn to_string(this: &mut Value, _: &[Value], _: &mut Interpreter) -> ResultValue { | ||
let name = this.get_field_slice("name"); | ||
let message = this.get_field_slice("message"); | ||
Ok(Value::from(format!("{}: {}", name, message))) | ||
} | ||
|
||
/// Create a new `RangeError` object. | ||
pub fn create(global: &Value) -> Value { | ||
let prototype = Value::new_object(Some(global)); | ||
prototype.set_field_slice("message", Value::from("")); | ||
prototype.set_field_slice("name", Value::from("RangeError")); | ||
make_builtin_fn!(to_string, named "toString", of prototype); | ||
make_constructor_fn(make_error, global, prototype) | ||
} | ||
|
||
/// Initialise the global object with the `RangeError` object. | ||
pub fn init(global: &Value) { | ||
global.set_field_slice("RangeError", create(global)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters