mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:07:46 +00:00
LibJS: Add Interpreter::create<GlobalObjectType>()
Force Interpreter construction to go via a create() helper that takes the global object type as a template parameter.
This commit is contained in:
parent
aee4c1f583
commit
9d5d0261e1
3 changed files with 16 additions and 17 deletions
|
@ -68,16 +68,16 @@ struct Argument {
|
||||||
|
|
||||||
class Interpreter {
|
class Interpreter {
|
||||||
public:
|
public:
|
||||||
Interpreter();
|
template<typename GlobalObjectType, typename... Args>
|
||||||
~Interpreter();
|
static NonnullOwnPtr<Interpreter> create(Args&&... args)
|
||||||
|
|
||||||
template<typename T, typename... Args>
|
|
||||||
void initialize_global_object(Args&&... args)
|
|
||||||
{
|
{
|
||||||
ASSERT(!m_global_object);
|
auto interpreter = adopt_own(*new Interpreter);
|
||||||
m_global_object = heap().allocate<T>(forward<Args>(args)...);
|
interpreter->m_global_object = interpreter->heap().allocate<GlobalObjectType>(forward<Args>(args)...);
|
||||||
|
return interpreter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
~Interpreter();
|
||||||
|
|
||||||
Value run(const Statement&, Vector<Argument> = {}, ScopeType = ScopeType::Block);
|
Value run(const Statement&, Vector<Argument> = {}, ScopeType = ScopeType::Block);
|
||||||
|
|
||||||
Object& global_object() { return *m_global_object; }
|
Object& global_object() { return *m_global_object; }
|
||||||
|
@ -136,6 +136,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Interpreter();
|
||||||
|
|
||||||
Heap m_heap;
|
Heap m_heap;
|
||||||
|
|
||||||
Vector<ScopeFrame> m_scope_stack;
|
Vector<ScopeFrame> m_scope_stack;
|
||||||
|
|
|
@ -360,10 +360,8 @@ Color Document::visited_link_color() const
|
||||||
|
|
||||||
JS::Interpreter& Document::interpreter()
|
JS::Interpreter& Document::interpreter()
|
||||||
{
|
{
|
||||||
if (!m_interpreter) {
|
if (!m_interpreter)
|
||||||
m_interpreter = make<JS::Interpreter>();
|
m_interpreter = JS::Interpreter::create<Bindings::WindowObject>(*m_window);
|
||||||
m_interpreter->initialize_global_object<Bindings::WindowObject>(*m_window);
|
|
||||||
}
|
|
||||||
return *m_interpreter;
|
return *m_interpreter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -198,16 +198,15 @@ int main(int argc, char** argv)
|
||||||
args_parser.add_positional_argument(script_path, "Path to script file", "script", Core::ArgsParser::Required::No);
|
args_parser.add_positional_argument(script_path, "Path to script file", "script", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
JS::Interpreter interpreter;
|
auto interpreter = JS::Interpreter::create<JS::GlobalObject>();
|
||||||
interpreter.initialize_global_object<JS::GlobalObject>();
|
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
||||||
interpreter.heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
|
||||||
|
|
||||||
interpreter.global_object().put("global", &interpreter.global_object());
|
interpreter->global_object().put("global", &interpreter->global_object());
|
||||||
|
|
||||||
if (script_path == nullptr) {
|
if (script_path == nullptr) {
|
||||||
editor = make<Line::Editor>();
|
editor = make<Line::Editor>();
|
||||||
editor->initialize();
|
editor->initialize();
|
||||||
repl(interpreter);
|
repl(*interpreter);
|
||||||
} else {
|
} else {
|
||||||
auto file = Core::File::construct(script_path);
|
auto file = Core::File::construct(script_path);
|
||||||
if (!file->open(Core::IODevice::ReadOnly)) {
|
if (!file->open(Core::IODevice::ReadOnly)) {
|
||||||
|
@ -232,7 +231,7 @@ int main(int argc, char** argv)
|
||||||
if (dump_ast)
|
if (dump_ast)
|
||||||
program->dump(0);
|
program->dump(0);
|
||||||
|
|
||||||
auto result = interpreter.run(*program);
|
auto result = interpreter->run(*program);
|
||||||
|
|
||||||
if (print_last_result)
|
if (print_last_result)
|
||||||
printf("%s\n", result.to_string().characters());
|
printf("%s\n", result.to_string().characters());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue