Skip to content

Commit 25572f5

Browse files
committed
construct_type_error and construct_range_error
1 parent c144cbd commit 25572f5

File tree

11 files changed

+65
-50
lines changed

11 files changed

+65
-50
lines changed

boa/src/builtins/bigint/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ impl BigInt {
7878
}
7979

8080
// 3. Throw a TypeError exception.
81-
ctx.throw_type_error("'this' is not a BigInt")?;
82-
unreachable!();
81+
Err(ctx.construct_type_error("'this' is not a BigInt"))
8382
}
8483

8584
/// `BigInt()`

boa/src/builtins/boolean/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ impl Boolean {
5151
_ => {}
5252
}
5353

54-
Err(ctx
55-
.throw_type_error("'this' is not a boolean")
56-
.expect_err("throw_type_error() did not return an error"))
54+
Err(ctx.construct_type_error("'this' is not a boolean"))
5755
}
5856

5957
/// `[[Construct]]` Create a new boolean object

boa/src/builtins/function/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,9 @@ pub fn make_constructor_fn(
413413
prototype: Value,
414414
constructable: bool,
415415
) -> Value {
416+
let _timer =
417+
BoaProfiler::global().start_event(&format!("make_constructor_fn: {}", name), "init");
418+
416419
// Create the native function
417420
let mut function = Function::builtin(Vec::new(), body);
418421
function.constructable = constructable;
@@ -478,7 +481,7 @@ where
478481
N: Into<String>,
479482
{
480483
let name = name.into();
481-
let _timer = BoaProfiler::global().start_event(&name, "make_builtin_fn");
484+
let _timer = BoaProfiler::global().start_event(&format!("make_builtin_fn: {}", &name), "init");
482485

483486
let mut function = Object::function(Function::builtin(Vec::new(), function));
484487
function.insert_field("length", Value::from(length));

boa/src/builtins/global_this/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod tests;
1919
pub(crate) struct GlobalThis;
2020

2121
impl GlobalThis {
22+
/// The binding name of the property.
2223
pub(crate) const NAME: &'static str = "globalThis";
2324

2425
/// Initialize the `globalThis` property on the global object.

boa/src/builtins/nan/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{builtins::value::Value, BoaProfiler};
2020
pub(crate) struct NaN;
2121

2222
impl NaN {
23-
/// The name of the property.
23+
/// The binding name of the property.
2424
pub(crate) const NAME: &'static str = "NaN";
2525

2626
/// Initialize the `NaN` property on the global object.

boa/src/builtins/number/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ impl Number {
6868
_ => {}
6969
}
7070

71-
Err(ctx
72-
.throw_type_error("'this' is not a number")
73-
.expect_err("throw_type_error() did not return an error"))
71+
Err(ctx.construct_type_error("'this' is not a number"))
7472
}
7573

7674
/// Helper function that formats a float as a ES6-style exponential number string.

boa/src/builtins/string/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ impl String {
5454
_ => {}
5555
}
5656

57-
Err(ctx
58-
.throw_type_error("'this' is not a string")
59-
.expect_err("throw_type_error() did not return an error"))
57+
Err(ctx.construct_type_error("'this' is not a string"))
6058
}
6159

6260
/// [[Construct]] - Creates a new instance `this`

boa/src/builtins/symbol/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ impl Symbol {
5858
_ => {}
5959
}
6060

61-
ctx.throw_type_error("'this' is not a Symbol")?;
62-
unreachable!();
61+
Err(ctx.construct_type_error("'this' is not a Symbol"))
6362
}
6463

6564
/// The `Symbol()` constructor returns a value of type symbol.

boa/src/builtins/value/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ impl Value {
257257
.map(JSONValue::Number)
258258
.unwrap_or(JSONValue::Null)),
259259
ValueData::Integer(val) => Ok(JSONValue::Number(JSONNumber::from(val))),
260-
ValueData::BigInt(_) => Err(interpreter
261-
.throw_type_error("BigInt value can't be serialized in JSON")
262-
.expect_err("throw_type_error should always return an error")),
260+
ValueData::BigInt(_) => {
261+
Err(interpreter.construct_type_error("BigInt value can't be serialized in JSON"))
262+
}
263263
ValueData::Symbol(_) | ValueData::Undefined => {
264264
unreachable!("Symbols and Undefined JSON Values depend on parent type");
265265
}

boa/src/exec/exception.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::{
88
};
99

1010
impl Interpreter {
11-
/// Throws a `RangeError` with the specified message.
12-
pub fn throw_range_error<M>(&mut self, message: M) -> ResultValue
11+
/// Constructs a `RangeError` with the specified message.
12+
pub fn construct_range_error<M>(&mut self, message: M) -> Value
1313
where
1414
M: Into<String>,
1515
{
@@ -19,10 +19,19 @@ impl Interpreter {
1919
vec![Const::from(message.into()).into()],
2020
))
2121
.run(self)
22+
.expect_err("RangeError should always throw")
2223
}
2324

24-
/// Throws a `TypeError` with the specified message.
25-
pub fn throw_type_error<M>(&mut self, message: M) -> ResultValue
25+
/// Throws a `RangeError` with the specified message.
26+
pub fn throw_range_error<M>(&mut self, message: M) -> ResultValue
27+
where
28+
M: Into<String>,
29+
{
30+
Err(self.construct_range_error(message))
31+
}
32+
33+
/// Constructs a `TypeError` with the specified message.
34+
pub fn construct_type_error<M>(&mut self, message: M) -> Value
2635
where
2736
M: Into<String>,
2837
{
@@ -32,5 +41,30 @@ impl Interpreter {
3241
vec![Const::from(message.into()).into()],
3342
))
3443
.run(self)
44+
.expect_err("TypeError should always throw")
45+
}
46+
47+
/// Throws a `TypeError` with the specified message.
48+
pub fn throw_type_error<M>(&mut self, message: M) -> ResultValue
49+
where
50+
M: Into<String>,
51+
{
52+
Err(self.construct_type_error(message))
53+
}
54+
55+
/// Constructs a `ReferenceError` with the specified message.
56+
pub fn construct_reference_error<M>(&mut self, _message: M) -> Value
57+
where
58+
M: Into<String>,
59+
{
60+
unimplemented!("ReferenceError: is not implemented");
61+
}
62+
63+
/// Throws a `ReferenceError` with the specified message.
64+
pub fn throw_reference_error<M>(&mut self, message: M) -> ResultValue
65+
where
66+
M: Into<String>,
67+
{
68+
Err(self.construct_reference_error(message))
3569
}
3670
}

0 commit comments

Comments
 (0)