mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibJS: Move GlobalObject to its own Object subclass
This is mostly for tidiness at the moment.
This commit is contained in:
parent
d1d136b4e5
commit
1448f4384d
Notes:
sideshowbarker
2024-07-19 08:20:15 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/1448f4384da
4 changed files with 49 additions and 12 deletions
30
Libraries/LibJS/GlobalObject.cpp
Normal file
30
Libraries/LibJS/GlobalObject.cpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include <AK/LogStream.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibJS/GlobalObject.h>
|
||||
#include <LibJS/Heap.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/NativeFunction.h>
|
||||
#include <LibJS/Value.h>
|
||||
#include <stdio.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
GlobalObject::GlobalObject(Heap& heap)
|
||||
{
|
||||
put("print", heap.allocate<NativeFunction>([](Interpreter&, Vector<Argument> arguments) -> Value {
|
||||
for (auto& argument : arguments)
|
||||
printf("%s ", argument.value.to_string().characters());
|
||||
return js_undefined();
|
||||
}));
|
||||
put("gc", heap.allocate<NativeFunction>([](Interpreter& interpreter, Vector<Argument>) -> Value {
|
||||
dbg() << "Forced garbage collection requested!";
|
||||
interpreter.heap().collect_garbage();
|
||||
return js_undefined();
|
||||
}));
|
||||
}
|
||||
|
||||
GlobalObject::~GlobalObject()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
16
Libraries/LibJS/GlobalObject.h
Normal file
16
Libraries/LibJS/GlobalObject.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#include <LibJS/Object.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
class GlobalObject final : public Object {
|
||||
public:
|
||||
explicit GlobalObject(Heap&);
|
||||
virtual ~GlobalObject() override;
|
||||
|
||||
private:
|
||||
virtual const char* class_name() const override { return "GlobalObject"; }
|
||||
};
|
||||
|
||||
}
|
|
@ -26,28 +26,18 @@
|
|||
|
||||
#include <AK/Badge.h>
|
||||
#include <LibJS/AST.h>
|
||||
#include <LibJS/GlobalObject.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/NativeFunction.h>
|
||||
#include <LibJS/Object.h>
|
||||
#include <LibJS/Value.h>
|
||||
#include <stdio.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
Interpreter::Interpreter()
|
||||
: m_heap(*this)
|
||||
{
|
||||
m_global_object = heap().allocate<Object>();
|
||||
m_global_object->put("print", heap().allocate<NativeFunction>([](Interpreter&, Vector<Argument> arguments) -> Value {
|
||||
for (auto& argument : arguments)
|
||||
printf("%s ", argument.value.to_string().characters());
|
||||
return js_undefined();
|
||||
}));
|
||||
m_global_object->put("gc", heap().allocate<NativeFunction>([](Interpreter& interpreter, Vector<Argument>) -> Value {
|
||||
dbg() << "Forced garbage collection requested!";
|
||||
interpreter.heap().collect_garbage();
|
||||
return js_undefined();
|
||||
}));
|
||||
m_global_object = heap().allocate<GlobalObject>(heap());
|
||||
}
|
||||
|
||||
Interpreter::~Interpreter()
|
||||
|
|
|
@ -2,6 +2,7 @@ OBJS = \
|
|||
AST.o \
|
||||
Cell.o \
|
||||
Function.o \
|
||||
GlobalObject.o \
|
||||
Heap.o \
|
||||
HeapBlock.o \
|
||||
Interpreter.o \
|
||||
|
|
Loading…
Add table
Reference in a new issue