summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-06-08 16:49:06 +0300
committerLinus Groh <mail@linusgroh.de>2021-06-08 15:31:46 +0100
commitaf58779def630d58c677c8a845623ac575c51da0 (patch)
treea82694c6ef1b51319b142ac681a9f481300572cc
parent98897ff6769d8858a4ddc40c21e5ae53c71b301a (diff)
downloadserenity-af58779def630d58c677c8a845623ac575c51da0.zip
LibJS: Return undefined from a with statement if no value was generated
Co-authored-by: Linus Groh <mail@linusgroh.de>
-rw-r--r--Userland/Libraries/LibJS/AST.cpp2
-rw-r--r--Userland/Libraries/LibJS/Interpreter.cpp16
-rw-r--r--Userland/Libraries/LibJS/Runtime/GlobalObject.cpp6
3 files changed, 9 insertions, 15 deletions
diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp
index 8ce026096b..3a036cf6a7 100644
--- a/Userland/Libraries/LibJS/AST.cpp
+++ b/Userland/Libraries/LibJS/AST.cpp
@@ -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);
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
diff --git a/Userland/Libraries/LibJS/Interpreter.cpp b/Userland/Libraries/LibJS/Interpreter.cpp
index eb5a45f03a..ebad86ede1 100644
--- a/Userland/Libraries/LibJS/Interpreter.cpp
+++ b/Userland/Libraries/LibJS/Interpreter.cpp
@@ -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()
diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp
index 855bbdac40..bbef7109d9 100644
--- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp
@@ -317,10 +317,8 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval)
auto& caller_frame = vm.call_stack().at(vm.call_stack().size() - 2);
TemporaryChange scope_change(vm.call_frame().scope, caller_frame->scope);
- vm.interpreter().execute_statement(global_object, program);
- if (vm.exception())
- return {};
- return vm.last_value().value_or(js_undefined());
+ auto& interpreter = vm.interpreter();
+ return interpreter.execute_statement(global_object, program).value_or(js_undefined());
}
// 19.2.6.1.1 Encode ( string, unescapedSet )