Переглянути джерело

LibJS: Fallback to undefined if last value in eval() is empty

For something like eval(""), the VM's 'last value' is an empty value,
which we must not leak.

Fixes #6643.
Linus Groh 4 роки тому
батько
коміт
7b1ba4bd5c

+ 1 - 1
Userland/Libraries/LibJS/Runtime/GlobalObject.cpp

@@ -323,7 +323,7 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval)
     vm.interpreter().execute_statement(global_object, program);
     if (vm.exception())
         return {};
-    return vm.last_value();
+    return vm.last_value().value_or(js_undefined());
 }
 
 // 19.2.6.1.1 Encode ( string, unescapedSet )

+ 1 - 0
Userland/Libraries/LibJS/Tests/eval-basic.js

@@ -11,6 +11,7 @@ test("basic eval() functionality", () => {
 
 test("returns value of last value-producing statement", () => {
     // See https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
+    expect(eval("")).toBeUndefined();
     expect(eval("1;;;;;")).toBe(1);
     expect(eval("1;{}")).toBe(1);
     expect(eval("1;var a;")).toBe(1);