1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-23 18:25:08 +00:00

LibJS: Add basic support for "with" statements

with statements evaluate an expression and put the result of it at the
"front" of the scope chain. This is implemented by creating a WithScope
object and placing it in front of the VM's current call frame's scope.
This commit is contained in:
Andreas Kling 2020-11-28 16:14:26 +01:00
parent c3fe9b4df8
commit 9de6443ab7
4 changed files with 135 additions and 2 deletions

View file

@ -29,6 +29,7 @@
#include <AK/HashTable.h>
#include <AK/ScopeGuard.h>
#include <AK/StringBuilder.h>
#include <AK/TemporaryChange.h>
#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibJS/AST.h>
#include <LibJS/Interpreter.h>
@ -46,6 +47,7 @@
#include <LibJS/Runtime/ScriptFunction.h>
#include <LibJS/Runtime/Shape.h>
#include <LibJS/Runtime/StringObject.h>
#include <LibJS/Runtime/WithScope.h>
#include <stdio.h>
namespace JS {
@ -255,9 +257,22 @@ Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object
return js_undefined();
}
Value WithStatement::execute(Interpreter&, GlobalObject&) const
Value WithStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
{
ASSERT_NOT_REACHED();
auto object_value = m_object->execute(interpreter, global_object);
if (interpreter.exception())
return {};
auto* object = object_value.to_object(global_object);
if (interpreter.exception())
return {};
ASSERT(object);
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);
interpreter.execute_statement(global_object, m_body);
return {};
}
Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const