瀏覽代碼

LibJS: Set length property in Object::put_native_function()

Linus Groh 5 年之前
父節點
當前提交
cd3e2690eb

+ 1 - 1
Libraries/LibJS/Runtime/ArrayPrototype.cpp

@@ -37,7 +37,7 @@ ArrayPrototype::ArrayPrototype()
 {
     put_native_function("shift", shift);
     put_native_function("pop", pop);
-    put_native_function("push", push);
+    put_native_function("push", push, 1);
 }
 
 ArrayPrototype::~ArrayPrototype()

+ 1 - 1
Libraries/LibJS/Runtime/GlobalObject.cpp

@@ -16,7 +16,7 @@ namespace JS {
 GlobalObject::GlobalObject()
 {
     put_native_function("gc", gc);
-    put_native_function("isNaN", is_nan);
+    put_native_function("isNaN", is_nan, 1);
 
     // FIXME: These are read-only in ES5
     put("NaN", js_nan());

+ 1 - 1
Libraries/LibJS/Runtime/MathObject.cpp

@@ -34,7 +34,7 @@ namespace JS {
 
 MathObject::MathObject()
 {
-    put_native_function("abs", abs);
+    put_native_function("abs", abs, 1);
     put_native_function("random", random);
 
     put("E", Value(M_E));

+ 4 - 2
Libraries/LibJS/Runtime/Object.cpp

@@ -155,9 +155,11 @@ void Object::put(const FlyString& property_name, Value value)
     put_own_property(*this, property_name, value);
 }
 
-void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function)
+void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length)
 {
-    put(property_name, heap().allocate<NativeFunction>(move(native_function)));
+    auto* function = heap().allocate<NativeFunction>(move(native_function));
+    function->put("length", Value(length));
+    put(property_name, function);
 }
 
 void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter)

+ 1 - 1
Libraries/LibJS/Runtime/Object.h

@@ -49,7 +49,7 @@ public:
     virtual Optional<Value> get_own_property(const Object& this_object, const FlyString& property_name) const;
     virtual bool put_own_property(Object& this_object, const FlyString& property_name, Value);
 
-    void put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>);
+    void put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)>, i32 length = 0);
     void put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter);
 
     virtual bool is_array() const { return false; }

+ 3 - 3
Libraries/LibJS/Runtime/ObjectConstructor.cpp

@@ -37,9 +37,9 @@ ObjectConstructor::ObjectConstructor()
 {
     put("prototype", interpreter().object_prototype());
 
-    put_native_function("getOwnPropertyNames", get_own_property_names);
-    put_native_function("getPrototypeOf", get_prototype_of);
-    put_native_function("setPrototypeOf", set_prototype_of);
+    put_native_function("getOwnPropertyNames", get_own_property_names, 1);
+    put_native_function("getPrototypeOf", get_prototype_of, 1);
+    put_native_function("setPrototypeOf", set_prototype_of, 2);
 }
 
 ObjectConstructor::~ObjectConstructor()

+ 1 - 1
Libraries/LibJS/Runtime/ObjectPrototype.cpp

@@ -37,7 +37,7 @@ ObjectPrototype::ObjectPrototype()
 {
     set_prototype(nullptr);
 
-    put_native_function("hasOwnProperty", has_own_property);
+    put_native_function("hasOwnProperty", has_own_property, 1);
     put_native_function("toString", to_string);
     put_native_function("valueOf", value_of);
 }

+ 3 - 3
Libraries/LibJS/Runtime/StringPrototype.cpp

@@ -39,9 +39,9 @@ namespace JS {
 StringPrototype::StringPrototype()
 {
     put_native_property("length", length_getter, nullptr);
-    put_native_function("charAt", char_at);
-    put_native_function("repeat", repeat);
-    put_native_function("startsWith", starts_with);
+    put_native_function("charAt", char_at, 1);
+    put_native_function("repeat", repeat, 1);
+    put_native_function("startsWith", starts_with, 1);
 }
 
 StringPrototype::~StringPrototype()

+ 1 - 1
Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp

@@ -44,7 +44,7 @@ CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRendering
     : m_impl(impl)
 {
     put_native_property("fillStyle", fill_style_getter, fill_style_setter);
-    put_native_function("fillRect", fill_rect);
+    put_native_function("fillRect", fill_rect, 4);
 }
 
 CanvasRenderingContext2DWrapper::~CanvasRenderingContext2DWrapper()

+ 2 - 2
Libraries/LibWeb/Bindings/DocumentWrapper.cpp

@@ -40,8 +40,8 @@ namespace Bindings {
 DocumentWrapper::DocumentWrapper(Document& document)
     : NodeWrapper(document)
 {
-    put_native_function("getElementById", get_element_by_id);
-    put_native_function("querySelectorAll", query_selector_all);
+    put_native_function("getElementById", get_element_by_id, 1);
+    put_native_function("querySelectorAll", query_selector_all, 1);
 }
 
 DocumentWrapper::~DocumentWrapper()

+ 1 - 1
Libraries/LibWeb/Bindings/EventTargetWrapper.cpp

@@ -39,7 +39,7 @@ namespace Bindings {
 EventTargetWrapper::EventTargetWrapper(EventTarget& impl)
     : m_impl(impl)
 {
-    put_native_function("addEventListener", add_event_listener);
+    put_native_function("addEventListener", add_event_listener, 2);
 }
 
 EventTargetWrapper::~EventTargetWrapper()

+ 1 - 1
Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp

@@ -40,7 +40,7 @@ namespace Bindings {
 HTMLCanvasElementWrapper::HTMLCanvasElementWrapper(HTMLCanvasElement& element)
     : ElementWrapper(element)
 {
-    put_native_function("getContext", get_context);
+    put_native_function("getContext", get_context, 1);
 
     put_native_property("width", width_getter, nullptr);
     put_native_property("height", height_getter, nullptr);

+ 3 - 3
Libraries/LibWeb/Bindings/WindowObject.cpp

@@ -43,9 +43,9 @@ WindowObject::WindowObject(Window& impl)
 {
     put_native_property("document", document_getter, document_setter);
     put_native_function("alert", alert);
-    put_native_function("setInterval", set_interval);
-    put_native_function("requestAnimationFrame", request_animation_frame);
-    put_native_function("cancelAnimationFrame", cancel_animation_frame);
+    put_native_function("setInterval", set_interval, 1);
+    put_native_function("requestAnimationFrame", request_animation_frame, 1);
+    put_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
 
     put("navigator", heap().allocate<NavigatorObject>());
 }

+ 2 - 2
Userland/js.cpp

@@ -243,8 +243,8 @@ ReplObject::ReplObject()
 {
     put_native_function("exit", exit_interpreter);
     put_native_function("help", repl_help);
-    put_native_function("load", load_file);
-    put_native_function("save", save_to_file);
+    put_native_function("load", load_file, 1);
+    put_native_function("save", save_to_file, 1);
 }
 
 ReplObject::~ReplObject()