From d1d136b4e5c935ec659fe49fc5e81ca08a969f9e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 12 Mar 2020 20:04:54 +0100 Subject: [PATCH] LibJS: Replace $gc() hack with a NativeFunction on the global object To make this work, also start passing Interpreter& to native functions. --- Base/home/anon/js/forced-gc.js | 2 +- Libraries/LibJS/AST.cpp | 7 +------ Libraries/LibJS/Interpreter.cpp | 7 ++++++- Libraries/LibJS/NativeFunction.cpp | 2 +- Libraries/LibJS/NativeFunction.h | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Base/home/anon/js/forced-gc.js b/Base/home/anon/js/forced-gc.js index 70a1da3a43..5ea20bcddd 100644 --- a/Base/home/anon/js/forced-gc.js +++ b/Base/home/anon/js/forced-gc.js @@ -1,5 +1,5 @@ function foo() { var x = {}; - $gc(); + gc(); } foo(); diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index af0f21aef3..dce5c0092c 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -55,11 +55,6 @@ Value ExpressionStatement::execute(Interpreter& interpreter) const Value CallExpression::execute(Interpreter& interpreter) const { - if (name() == "$gc") { - interpreter.heap().collect_garbage(); - return js_undefined(); - } - auto callee = interpreter.get_variable(name()); ASSERT(callee.is_object()); auto* callee_object = callee.as_object(); @@ -78,7 +73,7 @@ Value CallExpression::execute(Interpreter& interpreter) const return interpreter.run(static_cast(*callee_object).body(), move(passed_arguments), ScopeType::Function); if (callee_object->is_native_function()) { - return static_cast(*callee_object).native_function()(move(passed_arguments)); + return static_cast(*callee_object).native_function()(interpreter, move(passed_arguments)); } ASSERT_NOT_REACHED(); diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 637736b97e..a1983025d8 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -38,11 +38,16 @@ Interpreter::Interpreter() : m_heap(*this) { m_global_object = heap().allocate(); - m_global_object->put("print", heap().allocate([](Vector arguments) -> Value { + m_global_object->put("print", heap().allocate([](Interpreter&, Vector arguments) -> Value { for (auto& argument : arguments) printf("%s ", argument.value.to_string().characters()); return js_undefined(); })); + m_global_object->put("gc", heap().allocate([](Interpreter& interpreter, Vector) -> Value { + dbg() << "Forced garbage collection requested!"; + interpreter.heap().collect_garbage(); + return js_undefined(); + })); } Interpreter::~Interpreter() diff --git a/Libraries/LibJS/NativeFunction.cpp b/Libraries/LibJS/NativeFunction.cpp index 92ad88b544..dd62039a0e 100644 --- a/Libraries/LibJS/NativeFunction.cpp +++ b/Libraries/LibJS/NativeFunction.cpp @@ -30,7 +30,7 @@ namespace JS { -NativeFunction::NativeFunction(AK::Function)> native_function) +NativeFunction::NativeFunction(AK::Function)> native_function) : m_native_function(move(native_function)) { } diff --git a/Libraries/LibJS/NativeFunction.h b/Libraries/LibJS/NativeFunction.h index 6d68148461..1b3b3be81e 100644 --- a/Libraries/LibJS/NativeFunction.h +++ b/Libraries/LibJS/NativeFunction.h @@ -33,16 +33,16 @@ namespace JS { class NativeFunction final : public Object { public: - explicit NativeFunction(AK::Function)>); + explicit NativeFunction(AK::Function)>); virtual ~NativeFunction() override; - AK::Function)>& native_function() { return m_native_function; } + AK::Function)>& native_function() { return m_native_function; } private: virtual bool is_native_function() const override { return true; } virtual const char* class_name() const override { return "NativeFunction"; } - AK::Function)> m_native_function; + AK::Function)> m_native_function; }; }