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

This commit is contained in:
Linus Groh 2020-04-04 14:13:53 +01:00 committed by Andreas Kling
parent b3c4514902
commit cd3e2690eb
Notes: sideshowbarker 2024-07-19 07:56:28 +09:00
14 changed files with 25 additions and 23 deletions

View file

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

View file

@ -16,7 +16,7 @@ namespace JS {
GlobalObject::GlobalObject() GlobalObject::GlobalObject()
{ {
put_native_function("gc", gc); 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 // FIXME: These are read-only in ES5
put("NaN", js_nan()); put("NaN", js_nan());

View file

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

View file

@ -155,9 +155,11 @@ void Object::put(const FlyString& property_name, Value value)
put_own_property(*this, property_name, 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) void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter)

View file

@ -49,7 +49,7 @@ public:
virtual Optional<Value> get_own_property(const Object& this_object, const FlyString& property_name) const; 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); 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); 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; } virtual bool is_array() const { return false; }

View file

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

View file

@ -37,7 +37,7 @@ ObjectPrototype::ObjectPrototype()
{ {
set_prototype(nullptr); 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("toString", to_string);
put_native_function("valueOf", value_of); put_native_function("valueOf", value_of);
} }

View file

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

View file

@ -44,7 +44,7 @@ CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRendering
: m_impl(impl) : m_impl(impl)
{ {
put_native_property("fillStyle", fill_style_getter, fill_style_setter); 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() CanvasRenderingContext2DWrapper::~CanvasRenderingContext2DWrapper()

View file

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

View file

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

View file

@ -40,7 +40,7 @@ namespace Bindings {
HTMLCanvasElementWrapper::HTMLCanvasElementWrapper(HTMLCanvasElement& element) HTMLCanvasElementWrapper::HTMLCanvasElementWrapper(HTMLCanvasElement& element)
: ElementWrapper(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("width", width_getter, nullptr);
put_native_property("height", height_getter, nullptr); put_native_property("height", height_getter, nullptr);

View file

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

View file

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