2020-03-07 19:42:11 +01: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 21:29:22 +01:00
|
|
|
#include <AK/Badge.h>
|
2020-05-04 12:34:49 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <LibJS/AST.h>
|
|
|
|
#include <LibJS/Interpreter.h>
|
2020-03-24 14:37:39 +01:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-04-15 21:58:22 +02:00
|
|
|
#include <LibJS/Runtime/LexicalEnvironment.h>
|
2020-04-19 17:24:56 +02:00
|
|
|
#include <LibJS/Runtime/MarkedValueList.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
|
|
|
#include <LibJS/Runtime/Object.h>
|
2020-04-27 12:37:27 +02:00
|
|
|
#include <LibJS/Runtime/Reference.h>
|
2020-06-04 14:48:36 +02:00
|
|
|
#include <LibJS/Runtime/ScriptFunction.h>
|
2020-04-02 19:32:21 +02:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
2020-04-29 23:25:21 -07:00
|
|
|
#include <LibJS/Runtime/SymbolObject.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-07-02 18:01:09 -07:00
|
|
|
//#define INTERPRETER_DEBUG
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
namespace JS {
|
|
|
|
|
2020-09-20 19:24:44 +02:00
|
|
|
Interpreter::Interpreter(VM& vm)
|
|
|
|
: m_vm(vm)
|
2020-05-01 14:40:43 +02:00
|
|
|
, m_console(*this)
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Interpreter::~Interpreter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-11 22:47:43 +01:00
|
|
|
Value Interpreter::run(GlobalObject& global_object, const Program& program)
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2020-09-27 15:18:55 +02:00
|
|
|
ASSERT(!vm().exception());
|
2020-04-13 16:42:54 +02:00
|
|
|
|
2020-09-27 15:18:55 +02:00
|
|
|
VM::InterpreterExecutionScope scope(*this);
|
2020-03-09 21:13:55 +01:00
|
|
|
|
2020-09-27 15:18:55 +02:00
|
|
|
CallFrame global_call_frame;
|
|
|
|
global_call_frame.this_value = &global_object;
|
|
|
|
global_call_frame.function_name = "(global execution context)";
|
|
|
|
global_call_frame.environment = heap().allocate<LexicalEnvironment>(global_object, LexicalEnvironment::EnvironmentRecordType::Global);
|
|
|
|
global_call_frame.environment->bind_this_value(&global_object);
|
|
|
|
if (vm().exception())
|
|
|
|
return {};
|
|
|
|
vm().call_stack().append(move(global_call_frame));
|
2020-03-09 21:13:55 +01:00
|
|
|
|
2020-09-27 15:18:55 +02:00
|
|
|
auto result = program.execute(*this, global_object);
|
|
|
|
vm().pop_call_frame();
|
|
|
|
return result;
|
2020-03-09 21:13:55 +01:00
|
|
|
}
|
|
|
|
|
2020-09-27 15:18:55 +02:00
|
|
|
GlobalObject& Interpreter::global_object()
|
2020-04-27 12:37:27 +02:00
|
|
|
{
|
2020-09-27 15:18:55 +02:00
|
|
|
return static_cast<GlobalObject&>(*m_global_object.cell());
|
2020-04-27 12:37:27 +02:00
|
|
|
}
|
|
|
|
|
2020-09-27 15:18:55 +02:00
|
|
|
const GlobalObject& Interpreter::global_object() const
|
2020-03-09 21:29:22 +01:00
|
|
|
{
|
2020-09-27 15:18:55 +02:00
|
|
|
return static_cast<const GlobalObject&>(*m_global_object.cell());
|
2020-03-09 21:29:22 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 22:18:32 +04:30
|
|
|
Value Interpreter::call_internal(Function& function, Value this_value, Optional<MarkedValueList> arguments)
|
2020-03-18 15:20:49 +01:00
|
|
|
{
|
2020-08-14 17:30:34 +02:00
|
|
|
ASSERT(!exception());
|
|
|
|
|
2020-09-21 13:36:32 +02:00
|
|
|
VM::InterpreterExecutionScope scope(*this);
|
2020-09-20 19:24:44 +02:00
|
|
|
|
2020-09-27 15:18:55 +02:00
|
|
|
auto& call_frame = vm().push_call_frame();
|
2020-04-29 13:43:57 +02:00
|
|
|
call_frame.function_name = function.name();
|
2020-05-30 01:07:02 -05:00
|
|
|
call_frame.this_value = function.bound_this().value_or(this_value);
|
|
|
|
call_frame.arguments = function.bound_arguments();
|
2020-04-19 17:24:56 +02:00
|
|
|
if (arguments.has_value())
|
2020-05-30 01:07:02 -05:00
|
|
|
call_frame.arguments.append(arguments.value().values());
|
2020-04-29 13:43:57 +02:00
|
|
|
call_frame.environment = function.create_environment();
|
2020-06-08 13:31:21 -05:00
|
|
|
|
|
|
|
ASSERT(call_frame.environment->this_binding_status() == LexicalEnvironment::ThisBindingStatus::Uninitialized);
|
|
|
|
call_frame.environment->bind_this_value(call_frame.this_value);
|
|
|
|
|
2020-04-29 13:43:57 +02:00
|
|
|
auto result = function.call(*this);
|
2020-09-27 15:18:55 +02:00
|
|
|
vm().pop_call_frame();
|
2020-03-18 15:20:49 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|