mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +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
Notes:
sideshowbarker
2024-07-19 08:00:41 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/9d5d0261e10
3 changed files with 16 additions and 17 deletions
|
@ -68,16 +68,16 @@ struct Argument {
|
|||
|
||||
class Interpreter {
|
||||
public:
|
||||
Interpreter();
|
||||
~Interpreter();
|
||||
|
||||
template<typename T, typename... Args>
|
||||
void initialize_global_object(Args&&... args)
|
||||
template<typename GlobalObjectType, typename... Args>
|
||||
static NonnullOwnPtr<Interpreter> create(Args&&... args)
|
||||
{
|
||||
ASSERT(!m_global_object);
|
||||
m_global_object = heap().allocate<T>(forward<Args>(args)...);
|
||||
auto interpreter = adopt_own(*new Interpreter);
|
||||
interpreter->m_global_object = interpreter->heap().allocate<GlobalObjectType>(forward<Args>(args)...);
|
||||
return interpreter;
|
||||
}
|
||||
|
||||
~Interpreter();
|
||||
|
||||
Value run(const Statement&, Vector<Argument> = {}, ScopeType = ScopeType::Block);
|
||||
|
||||
Object& global_object() { return *m_global_object; }
|
||||
|
@ -136,6 +136,8 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
Interpreter();
|
||||
|
||||
Heap m_heap;
|
||||
|
||||
Vector<ScopeFrame> m_scope_stack;
|
||||
|
|
|
@ -360,10 +360,8 @@ Color Document::visited_link_color() const
|
|||
|
||||
JS::Interpreter& Document::interpreter()
|
||||
{
|
||||
if (!m_interpreter) {
|
||||
m_interpreter = make<JS::Interpreter>();
|
||||
m_interpreter->initialize_global_object<Bindings::WindowObject>(*m_window);
|
||||
}
|
||||
if (!m_interpreter)
|
||||
m_interpreter = JS::Interpreter::create<Bindings::WindowObject>(*m_window);
|
||||
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.parse(argc, argv);
|
||||
|
||||
JS::Interpreter interpreter;
|
||||
interpreter.initialize_global_object<JS::GlobalObject>();
|
||||
interpreter.heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
||||
auto interpreter = JS::Interpreter::create<JS::GlobalObject>();
|
||||
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) {
|
||||
editor = make<Line::Editor>();
|
||||
editor->initialize();
|
||||
repl(interpreter);
|
||||
repl(*interpreter);
|
||||
} else {
|
||||
auto file = Core::File::construct(script_path);
|
||||
if (!file->open(Core::IODevice::ReadOnly)) {
|
||||
|
@ -232,7 +231,7 @@ int main(int argc, char** argv)
|
|||
if (dump_ast)
|
||||
program->dump(0);
|
||||
|
||||
auto result = interpreter.run(*program);
|
||||
auto result = interpreter->run(*program);
|
||||
|
||||
if (print_last_result)
|
||||
printf("%s\n", result.to_string().characters());
|
||||
|
|
Loading…
Add table
Reference in a new issue