mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:47:34 +00:00
LibJS: Make eval() return the last value from the executed statement
This is kinda awkward but since the statement we're executing is actually a JS::Program, we have to get the result via VM::last_value().
This commit is contained in:
parent
a955fd4156
commit
45e6b5e601
2 changed files with 14 additions and 2 deletions
|
@ -325,8 +325,10 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval)
|
||||||
auto& caller_frame = vm.call_stack().at(vm.call_stack().size() - 2);
|
auto& caller_frame = vm.call_stack().at(vm.call_stack().size() - 2);
|
||||||
TemporaryChange scope_change(vm.call_frame().scope, caller_frame->scope);
|
TemporaryChange scope_change(vm.call_frame().scope, caller_frame->scope);
|
||||||
|
|
||||||
// FIXME: eval() should return the result of the executed code. This currently does not work.
|
vm.interpreter().execute_statement(global_object, program);
|
||||||
return vm.interpreter().execute_statement(global_object, program);
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
return vm.last_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
10
Userland/Libraries/LibJS/Tests/eval-basic.js
Normal file
10
Userland/Libraries/LibJS/Tests/eval-basic.js
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
test("basic eval() functionality", () => {
|
||||||
|
expect(eval("1 + 2")).toBe(3);
|
||||||
|
|
||||||
|
function foo(a) {
|
||||||
|
var x = 5;
|
||||||
|
eval("x += a");
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
expect(foo(7)).toBe(12);
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue