diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp index e7e0ac9b00e..6c2bc8d31a4 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -316,10 +316,10 @@ Value GlobalObject::get_this_binding(GlobalObject&) const JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval) { - auto code = vm.argument(0).to_string(global_object); - if (code.is_null()) - return {}; - JS::Parser parser { JS::Lexer { code } }; + if (!vm.argument(0).is_string()) + return vm.argument(0); + auto& code_string = vm.argument(0).as_string(); + JS::Parser parser { JS::Lexer { code_string.string() } }; auto program = parser.parse_program(); if (parser.has_errors()) { diff --git a/Userland/Libraries/LibJS/Tests/eval-basic.js b/Userland/Libraries/LibJS/Tests/eval-basic.js index e3f8a8cb5ef..b1853b5ac83 100644 --- a/Userland/Libraries/LibJS/Tests/eval-basic.js +++ b/Userland/Libraries/LibJS/Tests/eval-basic.js @@ -24,3 +24,8 @@ test("syntax error", () => { "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); +});