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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-03-24 22:03:50 +01:00
|
|
|
#include <AK/FlyString.h>
|
2020-03-09 21:13:55 +01:00
|
|
|
#include <AK/HashMap.h>
|
2020-03-12 19:53:31 +01:00
|
|
|
#include <AK/String.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibJS/Forward.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Heap/Heap.h>
|
2020-03-24 22:03:50 +01:00
|
|
|
#include <LibJS/Runtime/Exception.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-03-11 21:09:20 +02:00
|
|
|
enum class ScopeType {
|
2020-03-23 19:19:03 +01:00
|
|
|
None,
|
2020-03-11 21:09:20 +02:00
|
|
|
Function,
|
|
|
|
Block,
|
2020-03-24 14:37:39 +01:00
|
|
|
Try,
|
2020-03-29 14:34:25 +02:00
|
|
|
Breakable,
|
2020-04-05 00:22:42 +02:00
|
|
|
Continuable,
|
2020-03-11 21:09:20 +02:00
|
|
|
};
|
|
|
|
|
2020-03-12 14:24:34 +02:00
|
|
|
struct Variable {
|
|
|
|
Value value;
|
2020-04-08 11:59:18 +02:00
|
|
|
DeclarationKind declaration_kind;
|
2020-03-12 14:24:34 +02:00
|
|
|
};
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
struct ScopeFrame {
|
2020-03-11 21:09:20 +02:00
|
|
|
ScopeType type;
|
2020-03-18 11:23:53 +01:00
|
|
|
NonnullRefPtr<ScopeNode> scope_node;
|
2020-03-22 11:07:55 +01:00
|
|
|
HashMap<FlyString, Variable> variables;
|
2020-03-07 19:42:11 +01:00
|
|
|
};
|
|
|
|
|
2020-03-17 11:00:09 +01:00
|
|
|
struct CallFrame {
|
2020-04-11 12:56:20 +01:00
|
|
|
FlyString function_name;
|
2020-03-17 11:00:09 +01:00
|
|
|
Value this_value;
|
|
|
|
Vector<Value> arguments;
|
|
|
|
};
|
|
|
|
|
2020-03-12 19:53:31 +01:00
|
|
|
struct Argument {
|
2020-03-22 11:07:55 +01:00
|
|
|
FlyString name;
|
2020-03-12 19:53:31 +01:00
|
|
|
Value value;
|
|
|
|
};
|
|
|
|
|
2020-04-06 19:22:12 +02:00
|
|
|
typedef Vector<Argument, 8> ArgumentVector;
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
class Interpreter {
|
|
|
|
public:
|
2020-04-01 21:04:51 +02:00
|
|
|
template<typename GlobalObjectType, typename... Args>
|
|
|
|
static NonnullOwnPtr<Interpreter> create(Args&&... args)
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
2020-04-01 21:04:51 +02:00
|
|
|
auto interpreter = adopt_own(*new Interpreter);
|
|
|
|
interpreter->m_global_object = interpreter->heap().allocate<GlobalObjectType>(forward<Args>(args)...);
|
|
|
|
return interpreter;
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-01 21:04:51 +02:00
|
|
|
~Interpreter();
|
|
|
|
|
2020-04-06 19:22:12 +02:00
|
|
|
Value run(const Statement&, ArgumentVector = {}, ScopeType = ScopeType::Block);
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-04-08 11:05:38 +02:00
|
|
|
GlobalObject& global_object();
|
|
|
|
const GlobalObject& global_object() const;
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
Heap& heap() { return m_heap; }
|
|
|
|
|
2020-03-23 19:19:03 +01:00
|
|
|
void unwind(ScopeType type) { m_unwind_until = type; }
|
2020-04-05 00:09:48 +02:00
|
|
|
void stop_unwind() { m_unwind_until = ScopeType::None; }
|
|
|
|
bool should_unwind_until(ScopeType type) const { return m_unwind_until == type; }
|
2020-03-29 14:34:25 +02:00
|
|
|
bool should_unwind() const { return m_unwind_until != ScopeType::None; }
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-03-27 12:54:18 +01:00
|
|
|
Optional<Value> get_variable(const FlyString& name);
|
2020-03-22 11:07:55 +01:00
|
|
|
void set_variable(const FlyString& name, Value, bool first_assignment = false);
|
2020-03-09 21:13:55 +01:00
|
|
|
|
2020-03-15 15:12:34 +01:00
|
|
|
void gather_roots(Badge<Heap>, HashTable<Cell*>&);
|
2020-03-09 21:29:22 +01:00
|
|
|
|
2020-04-06 19:22:12 +02:00
|
|
|
void enter_scope(const ScopeNode&, ArgumentVector, ScopeType);
|
2020-03-07 19:42:11 +01:00
|
|
|
void exit_scope(const ScopeNode&);
|
|
|
|
|
2020-03-29 00:45:53 +01:00
|
|
|
Value call(Function*, Value this_value = {}, const Vector<Value>& arguments = {});
|
2020-03-18 15:20:49 +01:00
|
|
|
|
2020-03-22 11:07:55 +01:00
|
|
|
CallFrame& push_call_frame()
|
|
|
|
{
|
2020-04-11 12:56:20 +01:00
|
|
|
m_call_stack.append({ {}, js_undefined(), {} });
|
2020-03-22 11:07:55 +01:00
|
|
|
return m_call_stack.last();
|
|
|
|
}
|
2020-03-17 11:00:09 +01:00
|
|
|
void pop_call_frame() { m_call_stack.take_last(); }
|
2020-03-28 22:48:35 +01:00
|
|
|
const CallFrame& call_frame() { return m_call_stack.last(); }
|
2020-04-11 12:57:13 +01:00
|
|
|
const Vector<CallFrame> call_stack() { return m_call_stack; }
|
2020-03-28 22:48:35 +01:00
|
|
|
|
2020-04-01 22:38:59 +02:00
|
|
|
size_t argument_count() const
|
|
|
|
{
|
|
|
|
if (m_call_stack.is_empty())
|
|
|
|
return 0;
|
|
|
|
return m_call_stack.last().arguments.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
Value argument(size_t index) const
|
|
|
|
{
|
|
|
|
if (m_call_stack.is_empty())
|
|
|
|
return {};
|
|
|
|
auto& arguments = m_call_stack.last().arguments;
|
|
|
|
return index < arguments.size() ? arguments[index] : js_undefined();
|
|
|
|
}
|
|
|
|
|
2020-03-15 15:01:10 +01:00
|
|
|
Value this_value() const
|
|
|
|
{
|
2020-03-17 11:00:09 +01:00
|
|
|
if (m_call_stack.is_empty())
|
2020-03-15 15:01:10 +01:00
|
|
|
return m_global_object;
|
2020-03-17 11:00:09 +01:00
|
|
|
return m_call_stack.last().this_value;
|
2020-03-15 15:01:10 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 19:32:21 +02:00
|
|
|
Shape* empty_object_shape() { return m_empty_object_shape; }
|
|
|
|
|
2020-04-10 14:06:52 +02:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
|
|
|
|
Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
|
|
|
|
JS_ENUMERATE_BUILTIN_TYPES
|
|
|
|
#undef __JS_ENUMERATE
|
|
|
|
|
|
|
|
Exception* exception()
|
|
|
|
{
|
|
|
|
return m_exception;
|
|
|
|
}
|
2020-03-24 14:37:39 +01:00
|
|
|
void clear_exception() { m_exception = nullptr; }
|
2020-03-24 22:03:50 +01:00
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
Value throw_exception(Args&&... args)
|
|
|
|
{
|
|
|
|
return throw_exception(heap().allocate<T>(forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
Value throw_exception(Exception*);
|
|
|
|
Value throw_exception(Value value)
|
|
|
|
{
|
|
|
|
return throw_exception(heap().allocate<Exception>(value));
|
|
|
|
}
|
2020-03-15 15:11:13 +01:00
|
|
|
|
2020-04-04 23:45:13 +02:00
|
|
|
Value last_value() const { return m_last_value; }
|
|
|
|
|
2020-03-14 13:56:49 +02:00
|
|
|
private:
|
2020-04-01 21:04:51 +02:00
|
|
|
Interpreter();
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
Heap m_heap;
|
|
|
|
|
2020-04-04 23:45:13 +02:00
|
|
|
Value m_last_value;
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
Vector<ScopeFrame> m_scope_stack;
|
2020-03-17 11:00:09 +01:00
|
|
|
Vector<CallFrame> m_call_stack;
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-04-02 19:32:21 +02:00
|
|
|
Shape* m_empty_object_shape { nullptr };
|
|
|
|
|
2020-04-10 14:06:52 +02:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
|
|
|
|
Object* m_##snake_name##_prototype { nullptr };
|
|
|
|
JS_ENUMERATE_BUILTIN_TYPES
|
|
|
|
#undef __JS_ENUMERATE
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
Object* m_global_object { nullptr };
|
2020-04-10 12:42:33 +02:00
|
|
|
|
2020-03-24 22:03:50 +01:00
|
|
|
Exception* m_exception { nullptr };
|
2020-03-23 19:19:03 +01:00
|
|
|
|
|
|
|
ScopeType m_unwind_until { ScopeType::None };
|
2020-03-07 19:42:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|