1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:47:35 +00:00

LibJS: Return undefined from a with statement if no value was generated

Co-authored-by: Linus Groh <mail@linusgroh.de>
This commit is contained in:
Idan Horowitz 2021-06-08 16:49:06 +03:00 committed by Linus Groh
parent 98897ff676
commit af58779def
3 changed files with 9 additions and 15 deletions

View file

@ -55,19 +55,14 @@ void Interpreter::run(GlobalObject& global_object, const Program& program)
global_call_frame.is_strict_mode = program.is_strict_mode();
vm.push_call_frame(global_call_frame, global_object);
VERIFY(!vm.exception());
program.execute(*this, global_object);
// Whatever the promise jobs or on_call_stack_emptied do should not affect the effective
// 'last value'.
auto last_value = vm.last_value();
auto value = program.execute(*this, global_object);
vm.set_last_value({}, value.value_or(js_undefined()));
vm.pop_call_frame();
// At this point we may have already run any queued promise jobs via on_call_stack_emptied,
// in which case this is a no-op.
vm.run_queued_promise_jobs();
vm.set_last_value({}, last_value.value_or(js_undefined()));
}
GlobalObject& Interpreter::global_object()
@ -166,10 +161,11 @@ Value Interpreter::execute_statement(GlobalObject& global_object, const Statemen
auto& block = static_cast<const ScopeNode&>(statement);
enter_scope(block, scope_type, global_object);
Value last_value;
for (auto& node : block.children()) {
auto value = node.execute(*this, global_object);
if (!value.is_empty())
vm().set_last_value({}, value);
last_value = value;
if (vm().should_unwind()) {
if (!block.label().is_null() && vm().should_unwind_until(ScopeType::Breakable, block.label()))
vm().stop_unwind();
@ -180,7 +176,7 @@ Value Interpreter::execute_statement(GlobalObject& global_object, const Statemen
if (scope_type == ScopeType::Function) {
bool did_return = vm().unwind_until() == ScopeType::Function;
if (!did_return)
vm().set_last_value({}, js_undefined());
last_value = js_undefined();
}
if (vm().unwind_until() == scope_type)
@ -188,7 +184,7 @@ Value Interpreter::execute_statement(GlobalObject& global_object, const Statemen
exit_scope(block);
return vm().last_value();
return last_value;
}
LexicalEnvironment* Interpreter::current_environment()