2020-03-07 18:42:11 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-03-09 20:29:22 +00:00
|
|
|
#include <AK/Badge.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
#include <LibJS/AST.h>
|
|
|
|
#include <LibJS/Interpreter.h>
|
2020-03-20 20:01:36 +00:00
|
|
|
#include <LibJS/Runtime/ArrayPrototype.h>
|
2020-04-07 03:51:16 +00:00
|
|
|
#include <LibJS/Runtime/BooleanPrototype.h>
|
2020-03-29 23:21:56 +00:00
|
|
|
#include <LibJS/Runtime/DatePrototype.h>
|
2020-03-24 13:37:39 +00:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
#include <LibJS/Runtime/ErrorPrototype.h>
|
2020-04-04 13:34:31 +00:00
|
|
|
#include <LibJS/Runtime/FunctionPrototype.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
2020-04-04 21:13:13 +00:00
|
|
|
#include <LibJS/Runtime/NumberPrototype.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
#include <LibJS/Runtime/ObjectPrototype.h>
|
2020-04-02 17:32:21 +00:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/StringPrototype.h>
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
Interpreter::Interpreter()
|
2020-03-08 18:23:58 +00:00
|
|
|
: m_heap(*this)
|
2020-03-07 18:42:11 +00:00
|
|
|
{
|
2020-04-02 17:32:21 +00:00
|
|
|
m_empty_object_shape = heap().allocate<Shape>();
|
|
|
|
|
2020-04-10 12:06:52 +00:00
|
|
|
// These are done first since other prototypes depend on their presence.
|
2020-03-15 14:25:43 +00:00
|
|
|
m_object_prototype = heap().allocate<ObjectPrototype>();
|
2020-04-04 13:34:31 +00:00
|
|
|
m_function_prototype = heap().allocate<FunctionPrototype>();
|
2020-04-10 12:06:52 +00:00
|
|
|
|
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
|
|
|
|
if (!m_##snake_name##_prototype) \
|
|
|
|
m_##snake_name##_prototype = heap().allocate<PrototypeName>();
|
|
|
|
JS_ENUMERATE_BUILTIN_TYPES
|
|
|
|
#undef __JS_ENUMERATE
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Interpreter::~Interpreter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-06 17:22:12 +00:00
|
|
|
Value Interpreter::run(const Statement& statement, ArgumentVector arguments, ScopeType scope_type)
|
2020-03-07 18:42:11 +00:00
|
|
|
{
|
2020-03-23 15:46:41 +00:00
|
|
|
if (!statement.is_scope_node())
|
|
|
|
return statement.execute(*this);
|
|
|
|
|
2020-04-02 07:53:27 +00:00
|
|
|
auto& block = static_cast<const ScopeNode&>(statement);
|
2020-03-23 15:46:41 +00:00
|
|
|
enter_scope(block, move(arguments), scope_type);
|
2020-03-07 18:42:11 +00:00
|
|
|
|
2020-04-06 18:24:45 +00:00
|
|
|
m_last_value = js_undefined();
|
2020-03-23 15:46:41 +00:00
|
|
|
for (auto& node : block.children()) {
|
2020-04-04 21:45:13 +00:00
|
|
|
m_last_value = node.execute(*this);
|
2020-03-23 18:19:03 +00:00
|
|
|
if (m_unwind_until != ScopeType::None)
|
|
|
|
break;
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 21:45:13 +00:00
|
|
|
bool did_return = m_unwind_until == ScopeType::Function;
|
|
|
|
|
2020-03-23 18:19:03 +00:00
|
|
|
if (m_unwind_until == scope_type)
|
|
|
|
m_unwind_until = ScopeType::None;
|
|
|
|
|
2020-03-23 15:46:41 +00:00
|
|
|
exit_scope(block);
|
2020-04-04 21:45:13 +00:00
|
|
|
|
|
|
|
return did_return ? m_last_value : js_undefined();
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 17:22:12 +00:00
|
|
|
void Interpreter::enter_scope(const ScopeNode& scope_node, ArgumentVector arguments, ScopeType scope_type)
|
2020-03-07 18:42:11 +00:00
|
|
|
{
|
2020-04-08 09:59:18 +00:00
|
|
|
HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
|
2020-04-13 14:42:54 +00:00
|
|
|
|
|
|
|
for (auto& declaration : scope_node.variables()) {
|
|
|
|
for (auto& declarator : declaration.declarations()) {
|
|
|
|
if (scope_node.is_program())
|
|
|
|
global_object().put(declarator.id().string(), js_undefined());
|
|
|
|
else
|
|
|
|
scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:53:31 +00:00
|
|
|
for (auto& argument : arguments) {
|
2020-04-08 09:59:18 +00:00
|
|
|
scope_variables_with_declaration_kind.set(argument.name, { argument.value, DeclarationKind::Var });
|
2020-03-12 12:24:34 +00:00
|
|
|
}
|
2020-04-13 14:42:54 +00:00
|
|
|
|
2020-04-08 09:59:18 +00:00
|
|
|
m_scope_stack.append({ scope_type, scope_node, move(scope_variables_with_declaration_kind) });
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::exit_scope(const ScopeNode& scope_node)
|
|
|
|
{
|
2020-03-27 10:34:58 +00:00
|
|
|
while (!m_scope_stack.is_empty()) {
|
|
|
|
auto popped_scope = m_scope_stack.take_last();
|
|
|
|
if (popped_scope.scope_node.ptr() == &scope_node)
|
|
|
|
break;
|
|
|
|
}
|
2020-03-07 18:42:11 +00:00
|
|
|
|
2020-03-23 18:19:03 +00:00
|
|
|
// If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
|
|
|
|
if (m_scope_stack.is_empty())
|
|
|
|
m_unwind_until = ScopeType::None;
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 10:07:55 +00:00
|
|
|
void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment)
|
2020-03-09 20:13:55 +00:00
|
|
|
{
|
|
|
|
for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) {
|
|
|
|
auto& scope = m_scope_stack.at(i);
|
2020-03-12 12:24:34 +00:00
|
|
|
|
|
|
|
auto possible_match = scope.variables.get(name);
|
|
|
|
if (possible_match.has_value()) {
|
2020-04-12 22:50:49 +00:00
|
|
|
if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) {
|
|
|
|
throw_exception<TypeError>("Assignment to constant variable");
|
|
|
|
return;
|
|
|
|
}
|
2020-03-12 12:24:34 +00:00
|
|
|
|
2020-04-08 09:59:18 +00:00
|
|
|
scope.variables.set(move(name), { move(value), possible_match.value().declaration_kind });
|
2020-03-09 20:13:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global_object().put(move(name), move(value));
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:54:18 +00:00
|
|
|
Optional<Value> Interpreter::get_variable(const FlyString& name)
|
2020-03-09 20:13:55 +00:00
|
|
|
{
|
|
|
|
for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) {
|
|
|
|
auto& scope = m_scope_stack.at(i);
|
|
|
|
auto value = scope.variables.get(name);
|
|
|
|
if (value.has_value())
|
2020-03-12 12:24:34 +00:00
|
|
|
return value.value().value;
|
2020-03-09 20:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return global_object().get(name);
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:12:34 +00:00
|
|
|
void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
|
2020-03-09 20:29:22 +00:00
|
|
|
{
|
2020-04-02 17:32:21 +00:00
|
|
|
roots.set(m_empty_object_shape);
|
2020-03-09 20:29:22 +00:00
|
|
|
roots.set(m_global_object);
|
2020-03-24 13:37:39 +00:00
|
|
|
roots.set(m_exception);
|
2020-03-09 20:29:22 +00:00
|
|
|
|
2020-04-10 12:06:52 +00:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
|
|
|
|
roots.set(m_##snake_name##_prototype);
|
|
|
|
JS_ENUMERATE_BUILTIN_TYPES
|
|
|
|
#undef __JS_ENUMERATE
|
|
|
|
|
2020-04-04 21:45:13 +00:00
|
|
|
if (m_last_value.is_cell())
|
|
|
|
roots.set(m_last_value.as_cell());
|
|
|
|
|
2020-03-09 20:29:22 +00:00
|
|
|
for (auto& scope : m_scope_stack) {
|
|
|
|
for (auto& it : scope.variables) {
|
2020-03-12 12:24:34 +00:00
|
|
|
if (it.value.value.is_cell())
|
|
|
|
roots.set(it.value.value.as_cell());
|
2020-03-09 20:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-15 14:07:49 +00:00
|
|
|
|
2020-03-17 10:00:09 +00:00
|
|
|
for (auto& call_frame : m_call_stack) {
|
|
|
|
if (call_frame.this_value.is_cell())
|
|
|
|
roots.set(call_frame.this_value.as_cell());
|
|
|
|
for (auto& argument : call_frame.arguments) {
|
|
|
|
if (argument.is_cell())
|
|
|
|
roots.set(argument.as_cell());
|
|
|
|
}
|
2020-03-15 14:07:49 +00:00
|
|
|
}
|
2020-03-09 20:29:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 14:20:49 +00:00
|
|
|
Value Interpreter::call(Function* function, Value this_value, const Vector<Value>& arguments)
|
|
|
|
{
|
|
|
|
auto& call_frame = push_call_frame();
|
2020-04-11 11:56:20 +00:00
|
|
|
call_frame.function_name = function->name();
|
2020-03-18 14:20:49 +00:00
|
|
|
call_frame.this_value = this_value;
|
|
|
|
call_frame.arguments = arguments;
|
2020-03-28 21:48:35 +00:00
|
|
|
auto result = function->call(*this);
|
2020-03-18 14:20:49 +00:00
|
|
|
pop_call_frame();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:03:50 +00:00
|
|
|
Value Interpreter::throw_exception(Exception* exception)
|
2020-03-24 13:37:39 +00:00
|
|
|
{
|
2020-04-04 20:14:03 +00:00
|
|
|
if (exception->value().is_object() && exception->value().as_object().is_error()) {
|
|
|
|
auto& error = static_cast<Error&>(exception->value().as_object());
|
|
|
|
dbg() << "Throwing JavaScript Error: " << error.name() << ", " << error.message();
|
|
|
|
}
|
2020-03-24 13:37:39 +00:00
|
|
|
m_exception = exception;
|
|
|
|
unwind(ScopeType::Try);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:05:38 +00:00
|
|
|
GlobalObject& Interpreter::global_object()
|
|
|
|
{
|
|
|
|
return static_cast<GlobalObject&>(*m_global_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
const GlobalObject& Interpreter::global_object() const
|
|
|
|
{
|
|
|
|
return static_cast<const GlobalObject&>(*m_global_object);
|
|
|
|
}
|
|
|
|
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|