Browse Source

LibJS: Replace $gc() hack with a NativeFunction on the global object

To make this work, also start passing Interpreter& to native functions.
Andreas Kling 5 năm trước cách đây
mục cha
commit
d1d136b4e5

+ 1 - 1
Base/home/anon/js/forced-gc.js

@@ -1,5 +1,5 @@
 function foo() {
     var x = {};
-    $gc();
+    gc();
 }
 foo();

+ 1 - 6
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<Function&>(*callee_object).body(), move(passed_arguments), ScopeType::Function);
 
     if (callee_object->is_native_function()) {
-        return static_cast<NativeFunction&>(*callee_object).native_function()(move(passed_arguments));
+        return static_cast<NativeFunction&>(*callee_object).native_function()(interpreter, move(passed_arguments));
     }
 
     ASSERT_NOT_REACHED();

+ 6 - 1
Libraries/LibJS/Interpreter.cpp

@@ -38,11 +38,16 @@ Interpreter::Interpreter()
     : m_heap(*this)
 {
     m_global_object = heap().allocate<Object>();
-    m_global_object->put("print", heap().allocate<NativeFunction>([](Vector<Argument> arguments) -> Value {
+    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();
+    }));
 }
 
 Interpreter::~Interpreter()

+ 1 - 1
Libraries/LibJS/NativeFunction.cpp

@@ -30,7 +30,7 @@
 
 namespace JS {
 
-NativeFunction::NativeFunction(AK::Function<Value(Vector<Argument>)> native_function)
+NativeFunction::NativeFunction(AK::Function<Value(Interpreter&, Vector<Argument>)> native_function)
     : m_native_function(move(native_function))
 {
 }

+ 3 - 3
Libraries/LibJS/NativeFunction.h

@@ -33,16 +33,16 @@ namespace JS {
 
 class NativeFunction final : public Object {
 public:
-    explicit NativeFunction(AK::Function<Value(Vector<Argument>)>);
+    explicit NativeFunction(AK::Function<Value(Interpreter&, Vector<Argument>)>);
     virtual ~NativeFunction() override;
 
-    AK::Function<Value(Vector<Argument>)>& native_function() { return m_native_function; }
+    AK::Function<Value(Interpreter&, Vector<Argument>)>& 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<Value(Vector<Argument>)> m_native_function;
+    AK::Function<Value(Interpreter&, Vector<Argument>)> m_native_function;
 };
 
 }