1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:27:44 +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

@ -302,7 +302,7 @@ Value WithStatement::execute(Interpreter& interpreter, GlobalObject& global_obje
auto* with_scope = interpreter.heap().allocate<WithScope>(global_object, *object, interpreter.vm().call_frame().scope); auto* with_scope = interpreter.heap().allocate<WithScope>(global_object, *object, interpreter.vm().call_frame().scope);
TemporaryChange<ScopeObject*> scope_change(interpreter.vm().call_frame().scope, with_scope); TemporaryChange<ScopeObject*> scope_change(interpreter.vm().call_frame().scope, with_scope);
return interpreter.execute_statement(global_object, m_body); return interpreter.execute_statement(global_object, m_body).value_or(js_undefined());
} }
Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const

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

View file

@ -317,10 +317,8 @@ 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);
vm.interpreter().execute_statement(global_object, program); auto& interpreter = vm.interpreter();
if (vm.exception()) return interpreter.execute_statement(global_object, program).value_or(js_undefined());
return {};
return vm.last_value().value_or(js_undefined());
} }
// 19.2.6.1.1 Encode ( string, unescapedSet ) // 19.2.6.1.1 Encode ( string, unescapedSet )