1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 23:58:12 +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/HashTable.h>
#include <AK/ScopeGuard.h> #include <AK/ScopeGuard.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/TemporaryChange.h>
#include <LibCrypto/BigInt/SignedBigInteger.h> #include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibJS/AST.h> #include <LibJS/AST.h>
#include <LibJS/Interpreter.h> #include <LibJS/Interpreter.h>
@ -46,6 +47,7 @@
#include <LibJS/Runtime/ScriptFunction.h> #include <LibJS/Runtime/ScriptFunction.h>
#include <LibJS/Runtime/Shape.h> #include <LibJS/Runtime/Shape.h>
#include <LibJS/Runtime/StringObject.h> #include <LibJS/Runtime/StringObject.h>
#include <LibJS/Runtime/WithScope.h>
#include <stdio.h> #include <stdio.h>
namespace JS { namespace JS {
@ -255,9 +257,22 @@ Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object
return js_undefined(); 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 Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const

View file

@ -76,6 +76,7 @@ set(SOURCES
Runtime/Uint8ClampedArray.cpp Runtime/Uint8ClampedArray.cpp
Runtime/VM.cpp Runtime/VM.cpp
Runtime/Value.cpp Runtime/Value.cpp
Runtime/WithScope.cpp
Token.cpp Token.cpp
) )

View file

@ -0,0 +1,67 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibJS/AST.h>
#include <LibJS/Runtime/WithScope.h>
namespace JS {
WithScope::WithScope(Object& object, ScopeObject* parent_scope)
: ScopeObject(parent_scope)
, m_object(object)
{
}
void WithScope::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(&m_object);
}
Optional<Variable> WithScope::get_from_scope(const FlyString& name) const
{
auto value = m_object.get(name);
if (value.is_empty())
return {};
return Variable { value, DeclarationKind::Var };
}
void WithScope::put_to_scope(const FlyString& name, Variable variable)
{
m_object.put(name, variable.value);
}
bool WithScope::has_this_binding() const
{
return parent()->has_this_binding();
}
Value WithScope::get_this_binding(GlobalObject& global_object) const
{
return parent()->get_this_binding(global_object);
}
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibJS/Runtime/ScopeObject.h>
namespace JS {
class WithScope : public ScopeObject {
JS_OBJECT(WithScope, ScopeObject);
public:
WithScope(Object&, ScopeObject* parent_scope);
virtual Optional<Variable> get_from_scope(const FlyString&) const override;
virtual void put_to_scope(const FlyString&, Variable) override;
virtual bool has_this_binding() const override;
virtual Value get_this_binding(GlobalObject&) const override;
private:
virtual void visit_edges(Visitor&) override;
Object& m_object;
};
}