Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/engine/src/builtins/function/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn function_prototype() {
TestAction::assert_native_error(
"new Function.prototype()",
JsNativeErrorKind::Type,
"not a constructor",
"function is not a constructor",
),
]);
}
Expand Down
11 changes: 9 additions & 2 deletions core/engine/src/object/internal_methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,12 +1207,19 @@ fn non_existent_call(
}

fn non_existent_construct(
_obj: &JsObject,
obj: &JsObject,
_argument_count: usize,
context: &mut InternalMethodCallContext<'_>,
) -> JsResult<CallValue> {
// A callable object reaching this point is a function without a [[Construct]]
// slot (e.g. an arrow function or method), so report it as a function.
let type_of = if obj.is_callable() {
"function"
} else {
"object"
};
Err(JsNativeError::typ()
.with_message("not a constructor")
.with_message(format!("{type_of} is not a constructor"))
.with_realm(context.realm().clone())
.into())
}
29 changes: 28 additions & 1 deletion core/engine/src/tests/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,37 @@ fn should_type_error_when_new_is_not_constructor() {
run_test_actions([TestAction::assert_native_error(
"new ''()",
JsNativeErrorKind::Type,
"not a constructor",
"string is not a constructor",
)]);
}

#[test]
fn new_on_non_constructor_reports_the_value_type() {
run_test_actions([
TestAction::assert_native_error(
"new 5()",
JsNativeErrorKind::Type,
"number is not a constructor",
),
TestAction::assert_native_error(
"new undefined()",
JsNativeErrorKind::Type,
"undefined is not a constructor",
),
TestAction::assert_native_error(
"new true()",
JsNativeErrorKind::Type,
"boolean is not a constructor",
),
// A callable object without a [[Construct]] slot, such as an arrow function.
TestAction::assert_native_error(
"new (() => {})()",
JsNativeErrorKind::Type,
"function is not a constructor",
),
]);
}

#[test]
fn new_instance_should_point_to_prototype() {
// A new instance should point to a prototype object created with the constructor function
Expand Down
10 changes: 8 additions & 2 deletions core/engine/src/vm/opcode/new/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ impl New {

let cons = func
.as_object()
.ok_or_else(|| JsNativeError::typ().with_message("not a constructor"))?
.ok_or_else(|| {
JsNativeError::typ()
.with_message(format!("{} is not a constructor", func.type_of()))
})?
.clone();

context.vm.stack.push(cons.clone()); // Push new.target
Expand Down Expand Up @@ -59,7 +62,10 @@ impl NewSpread {

let cons = func
.as_object()
.ok_or_else(|| JsNativeError::typ().with_message("not a constructor"))?
.ok_or_else(|| {
JsNativeError::typ()
.with_message(format!("{} is not a constructor", func.type_of()))
})?
.clone();

let argument_count = arguments.len();
Expand Down
Loading