mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:38:10 +00:00
LibJS/Bytecode: Check if eval function is a function
When overriding 'eval' to a non-function value, the interpreter would crash. Now it handles this case swiftly, throwing a TypeError.
This commit is contained in:
parent
a9ef65e64a
commit
1850652881
2 changed files with 7 additions and 1 deletions
|
@ -233,7 +233,8 @@ static Completion throw_type_error_for_callee(Bytecode::Interpreter& interpreter
|
||||||
|
|
||||||
ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter& interpreter, Value callee, Op::CallType call_type, Optional<StringTableIndex> const& expression_string)
|
ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter& interpreter, Value callee, Op::CallType call_type, Optional<StringTableIndex> const& expression_string)
|
||||||
{
|
{
|
||||||
if (call_type == Op::CallType::Call && !callee.is_function())
|
if ((call_type == Op::CallType::Call || call_type == Op::CallType::DirectEval)
|
||||||
|
&& !callee.is_function())
|
||||||
return throw_type_error_for_callee(interpreter, callee, "function"sv, expression_string);
|
return throw_type_error_for_callee(interpreter, callee, "function"sv, expression_string);
|
||||||
if (call_type == Op::CallType::Construct && !callee.is_constructor())
|
if (call_type == Op::CallType::Construct && !callee.is_constructor())
|
||||||
return throw_type_error_for_callee(interpreter, callee, "constructor"sv, expression_string);
|
return throw_type_error_for_callee(interpreter, callee, "constructor"sv, expression_string);
|
||||||
|
|
|
@ -13,3 +13,8 @@ test("variable named 'eval' pointing to real eval works as a direct eval", funct
|
||||||
var eval = globalThis.eval;
|
var eval = globalThis.eval;
|
||||||
expect(eval("testValue")).toEqual("inner");
|
expect(eval("testValue")).toEqual("inner");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("variable named 'eval' pointing to a non-function raises a TypeError", function () {
|
||||||
|
var eval = "borked";
|
||||||
|
expect(() => eval("something").toThrowWithMessage(TypeError, "borked is not a function"));
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue