LibJS: eval(x) should return x without evaluation if x is not a string

This commit is contained in:
Andreas Kling 2021-03-17 20:57:29 +01:00
parent d792200a55
commit 60e630d5a0
Notes: sideshowbarker 2024-07-18 21:16:35 +09:00
2 changed files with 9 additions and 4 deletions

View file

@ -316,10 +316,10 @@ Value GlobalObject::get_this_binding(GlobalObject&) const
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval) JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval)
{ {
auto code = vm.argument(0).to_string(global_object); if (!vm.argument(0).is_string())
if (code.is_null()) return vm.argument(0);
return {}; auto& code_string = vm.argument(0).as_string();
JS::Parser parser { JS::Lexer { code } }; JS::Parser parser { JS::Lexer { code_string.string() } };
auto program = parser.parse_program(); auto program = parser.parse_program();
if (parser.has_errors()) { if (parser.has_errors()) {

View file

@ -24,3 +24,8 @@ test("syntax error", () => {
"Unexpected token Eof. Expected CurlyClose (line: 1, column: 2)" "Unexpected token Eof. Expected CurlyClose (line: 1, column: 2)"
); );
}); });
test("returns 1st argument unless 1st argument is a string", () => {
var string_object = new String("1 + 2");
expect(string_object).toBe(string_object);
});