diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2020-05-26 21:33:37 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-27 13:17:35 +0200 |
commit | dd08c992e8d884f646bdf9e9e75b2236ad5d38c8 (patch) | |
tree | 695ac51fb004e0a41d9374c1f1646dc51dbefeaf /Libraries/LibWeb | |
parent | 59a32f29b05857d7928789f0374930b8f5db4c62 (diff) | |
download | serenity-dd08c992e8d884f646bdf9e9e75b2236ad5d38c8.zip |
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
Diffstat (limited to 'Libraries/LibWeb')
-rw-r--r-- | Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp | 38 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/DocumentWrapper.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/ElementWrapper.cpp | 8 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/EventTargetWrapper.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/ImageDataWrapper.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/LocationObject.cpp | 18 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/MouseEventWrapper.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/NavigatorObject.cpp | 16 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/WindowObject.cpp | 24 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp | 12 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp | 18 |
12 files changed, 79 insertions, 79 deletions
diff --git a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp index 197d0166b7..9b89801a89 100644 --- a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp +++ b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp @@ -51,25 +51,25 @@ CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRendering : Wrapper(*interpreter().global_object().object_prototype()) , m_impl(impl) { - put_native_function("fillRect", fill_rect, 4); - put_native_function("scale", scale, 2); - put_native_function("translate", translate, 2); - put_native_function("strokeRect", stroke_rect, 4); - put_native_function("drawImage", draw_image, 3); - put_native_function("beginPath", begin_path, 0); - put_native_function("closePath", close_path, 0); - put_native_function("stroke", stroke, 0); - put_native_function("fill", fill, 0); - put_native_function("moveTo", move_to, 2); - put_native_function("lineTo", line_to, 2); - put_native_function("quadraticCurveTo", quadratic_curve_to, 4); - put_native_function("createImageData", create_image_data, 1); - put_native_function("putImageData", put_image_data, 3); - - put_native_property("fillStyle", fill_style_getter, fill_style_setter); - put_native_property("strokeStyle", stroke_style_getter, stroke_style_setter); - put_native_property("lineWidth", line_width_getter, line_width_setter); - put_native_property("canvas", canvas_getter, nullptr); + define_native_function("fillRect", fill_rect, 4); + define_native_function("scale", scale, 2); + define_native_function("translate", translate, 2); + define_native_function("strokeRect", stroke_rect, 4); + define_native_function("drawImage", draw_image, 3); + define_native_function("beginPath", begin_path, 0); + define_native_function("closePath", close_path, 0); + define_native_function("stroke", stroke, 0); + define_native_function("fill", fill, 0); + define_native_function("moveTo", move_to, 2); + define_native_function("lineTo", line_to, 2); + define_native_function("quadraticCurveTo", quadratic_curve_to, 4); + define_native_function("createImageData", create_image_data, 1); + define_native_function("putImageData", put_image_data, 3); + + define_native_property("fillStyle", fill_style_getter, fill_style_setter); + define_native_property("strokeStyle", stroke_style_getter, stroke_style_setter); + define_native_property("lineWidth", line_width_getter, line_width_setter); + define_native_property("canvas", canvas_getter, nullptr); } CanvasRenderingContext2DWrapper::~CanvasRenderingContext2DWrapper() diff --git a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp index a74fa06b01..d9bf59f45d 100644 --- a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp +++ b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp @@ -40,9 +40,9 @@ namespace Bindings { DocumentWrapper::DocumentWrapper(Document& document) : NodeWrapper(document) { - put_native_function("getElementById", get_element_by_id, 1); - put_native_function("querySelector", query_selector, 1); - put_native_function("querySelectorAll", query_selector_all, 1); + define_native_function("getElementById", get_element_by_id, 1); + define_native_function("querySelector", query_selector, 1); + define_native_function("querySelectorAll", query_selector_all, 1); } DocumentWrapper::~DocumentWrapper() diff --git a/Libraries/LibWeb/Bindings/ElementWrapper.cpp b/Libraries/LibWeb/Bindings/ElementWrapper.cpp index 8e53ef46c9..3028d44a77 100644 --- a/Libraries/LibWeb/Bindings/ElementWrapper.cpp +++ b/Libraries/LibWeb/Bindings/ElementWrapper.cpp @@ -40,12 +40,12 @@ namespace Bindings { ElementWrapper::ElementWrapper(Element& element) : NodeWrapper(element) { - put_native_property("innerHTML", inner_html_getter, inner_html_setter); - put_native_property("id", id_getter, id_setter); + define_native_property("innerHTML", inner_html_getter, inner_html_setter); + define_native_property("id", id_getter, id_setter); u8 attributes = JS::Attribute::Configurable | JS::Attribute::Enumerable | JS::Attribute::Writable; - put_native_function("getAttribute", get_attribute, 1, attributes); - put_native_function("setAttribute", set_attribute, 2, attributes); + define_native_function("getAttribute", get_attribute, 1, attributes); + define_native_function("setAttribute", set_attribute, 2, attributes); } ElementWrapper::~ElementWrapper() diff --git a/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp b/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp index 9157e19cb3..79711295d2 100644 --- a/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp +++ b/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp @@ -42,7 +42,7 @@ EventTargetWrapper::EventTargetWrapper(EventTarget& impl) : Wrapper(*interpreter().global_object().object_prototype()) , m_impl(impl) { - put_native_function("addEventListener", add_event_listener, 2); + define_native_function("addEventListener", add_event_listener, 2); } EventTargetWrapper::~EventTargetWrapper() diff --git a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp index 8237c5fdc3..12f14ff46c 100644 --- a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp +++ b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp @@ -40,10 +40,10 @@ namespace Bindings { HTMLCanvasElementWrapper::HTMLCanvasElementWrapper(HTMLCanvasElement& element) : ElementWrapper(element) { - put_native_function("getContext", get_context, 1); + define_native_function("getContext", get_context, 1); - put_native_property("width", width_getter, nullptr); - put_native_property("height", height_getter, nullptr); + define_native_property("width", width_getter, nullptr); + define_native_property("height", height_getter, nullptr); } HTMLCanvasElementWrapper::~HTMLCanvasElementWrapper() diff --git a/Libraries/LibWeb/Bindings/ImageDataWrapper.cpp b/Libraries/LibWeb/Bindings/ImageDataWrapper.cpp index fe7bb3708b..f3e7cbad3e 100644 --- a/Libraries/LibWeb/Bindings/ImageDataWrapper.cpp +++ b/Libraries/LibWeb/Bindings/ImageDataWrapper.cpp @@ -44,9 +44,9 @@ ImageDataWrapper::ImageDataWrapper(ImageData& impl) : Wrapper(*interpreter().global_object().object_prototype()) , m_impl(impl) { - put_native_property("width", width_getter, nullptr); - put_native_property("height", height_getter, nullptr); - put_native_property("data", data_getter, nullptr); + define_native_property("width", width_getter, nullptr); + define_native_property("height", height_getter, nullptr); + define_native_property("data", data_getter, nullptr); } ImageDataWrapper::~ImageDataWrapper() diff --git a/Libraries/LibWeb/Bindings/LocationObject.cpp b/Libraries/LibWeb/Bindings/LocationObject.cpp index 6a26c01b10..8acce54dfa 100644 --- a/Libraries/LibWeb/Bindings/LocationObject.cpp +++ b/Libraries/LibWeb/Bindings/LocationObject.cpp @@ -39,15 +39,15 @@ LocationObject::LocationObject() : Object(interpreter().global_object().object_prototype()) { u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable; - put_native_property("href", href_getter, href_setter, attr); - put_native_property("host", host_getter, nullptr, attr); - put_native_property("hostname", hostname_getter, nullptr, attr); - put_native_property("pathname", pathname_getter, nullptr, attr); - put_native_property("hash", hash_getter, nullptr, attr); - put_native_property("search", search_getter, nullptr, attr); - put_native_property("protocol", protocol_getter, nullptr, attr); - - put_native_function("reload", reload, JS::Attribute::Enumerable); + define_native_property("href", href_getter, href_setter, attr); + define_native_property("host", host_getter, nullptr, attr); + define_native_property("hostname", hostname_getter, nullptr, attr); + define_native_property("pathname", pathname_getter, nullptr, attr); + define_native_property("hash", hash_getter, nullptr, attr); + define_native_property("search", search_getter, nullptr, attr); + define_native_property("protocol", protocol_getter, nullptr, attr); + + define_native_function("reload", reload, JS::Attribute::Enumerable); } LocationObject::~LocationObject() diff --git a/Libraries/LibWeb/Bindings/MouseEventWrapper.cpp b/Libraries/LibWeb/Bindings/MouseEventWrapper.cpp index c369c26d52..8cabc9ee76 100644 --- a/Libraries/LibWeb/Bindings/MouseEventWrapper.cpp +++ b/Libraries/LibWeb/Bindings/MouseEventWrapper.cpp @@ -37,8 +37,8 @@ namespace Bindings { MouseEventWrapper::MouseEventWrapper(MouseEvent& event) : EventWrapper(event) { - put_native_property("offsetX", offset_x_getter, nullptr); - put_native_property("offsetY", offset_y_getter, nullptr); + define_native_property("offsetX", offset_x_getter, nullptr); + define_native_property("offsetY", offset_y_getter, nullptr); } MouseEventWrapper::~MouseEventWrapper() diff --git a/Libraries/LibWeb/Bindings/NavigatorObject.cpp b/Libraries/LibWeb/Bindings/NavigatorObject.cpp index 83c79ed953..54d6077b06 100644 --- a/Libraries/LibWeb/Bindings/NavigatorObject.cpp +++ b/Libraries/LibWeb/Bindings/NavigatorObject.cpp @@ -40,15 +40,15 @@ NavigatorObject::NavigatorObject() auto* languages = JS::Array::create(interpreter().global_object()); languages->elements().append(js_string(heap(), "en-US")); - put("appCodeName", js_string(heap(), "Mozilla")); - put("appName", js_string(heap(), "Netscape")); - put("appVersion", js_string(heap(), "4.0")); - put("language", languages->elements().first()); - put("languages", languages); - put("platform", js_string(heap(), "SerenityOS")); - put("product", js_string(heap(), "Gecko")); + define_property("appCodeName", js_string(heap(), "Mozilla")); + define_property("appName", js_string(heap(), "Netscape")); + define_property("appVersion", js_string(heap(), "4.0")); + define_property("language", languages->elements().first()); + define_property("languages", languages); + define_property("platform", js_string(heap(), "SerenityOS")); + define_property("product", js_string(heap(), "Gecko")); - put_native_property("userAgent", user_agent_getter, nullptr); + define_native_property("userAgent", user_agent_getter, nullptr); } NavigatorObject::~NavigatorObject() diff --git a/Libraries/LibWeb/Bindings/WindowObject.cpp b/Libraries/LibWeb/Bindings/WindowObject.cpp index dc816d50ac..6c6adaa4c9 100644 --- a/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -51,21 +51,21 @@ void WindowObject::initialize() { GlobalObject::initialize(); - put("window", this, JS::Attribute::Enumerable); - put_native_property("document", document_getter, document_setter, JS::Attribute::Enumerable); - put_native_function("alert", alert); - put_native_function("confirm", confirm); - put_native_function("setInterval", set_interval, 1); - put_native_function("setTimeout", set_timeout, 1); - put_native_function("requestAnimationFrame", request_animation_frame, 1); - put_native_function("cancelAnimationFrame", cancel_animation_frame, 1); - - put("navigator", heap().allocate<NavigatorObject>(), JS::Attribute::Enumerable | JS::Attribute::Configurable); - put("location", heap().allocate<LocationObject>(), JS::Attribute::Enumerable | JS::Attribute::Configurable); + define_property("window", this, JS::Attribute::Enumerable); + define_native_property("document", document_getter, document_setter, JS::Attribute::Enumerable); + define_native_function("alert", alert); + define_native_function("confirm", confirm); + define_native_function("setInterval", set_interval, 1); + define_native_function("setTimeout", set_timeout, 1); + define_native_function("requestAnimationFrame", request_animation_frame, 1); + define_native_function("cancelAnimationFrame", cancel_animation_frame, 1); + + define_property("navigator", heap().allocate<NavigatorObject>(), JS::Attribute::Enumerable | JS::Attribute::Configurable); + define_property("location", heap().allocate<LocationObject>(), JS::Attribute::Enumerable | JS::Attribute::Configurable); m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>(); m_xhr_constructor = heap().allocate<XMLHttpRequestConstructor>(); - m_xhr_constructor->put("prototype", m_xhr_prototype, 0); + m_xhr_constructor->define_property("prototype", m_xhr_prototype, 0); add_constructor("XMLHttpRequest", m_xhr_constructor, *m_xhr_prototype); } diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp index 2a2acb829c..3ad3150412 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp @@ -39,13 +39,13 @@ namespace Bindings { XMLHttpRequestConstructor::XMLHttpRequestConstructor() : NativeFunction(*interpreter().global_object().function_prototype()) { - put("length", JS::Value(1), JS::Attribute::Configurable); + define_property("length", JS::Value(1), JS::Attribute::Configurable); - put("UNSENT", JS::Value((i32)XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable); - put("OPENED", JS::Value((i32)XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable); - put("HEADERS_RECEIVED", JS::Value((i32)XMLHttpRequest::ReadyState::HeadersReceived), JS::Attribute::Enumerable); - put("LOADING", JS::Value((i32)XMLHttpRequest::ReadyState::Loading), JS::Attribute::Enumerable); - put("DONE", JS::Value((i32)XMLHttpRequest::ReadyState::Done), JS::Attribute::Enumerable); + define_property("UNSENT", JS::Value((i32)XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable); + define_property("OPENED", JS::Value((i32)XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable); + define_property("HEADERS_RECEIVED", JS::Value((i32)XMLHttpRequest::ReadyState::HeadersReceived), JS::Attribute::Enumerable); + define_property("LOADING", JS::Value((i32)XMLHttpRequest::ReadyState::Loading), JS::Attribute::Enumerable); + define_property("DONE", JS::Value((i32)XMLHttpRequest::ReadyState::Done), JS::Attribute::Enumerable); } XMLHttpRequestConstructor::~XMLHttpRequestConstructor() diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp index 43d9cdae1c..aefe1a4abb 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp @@ -38,16 +38,16 @@ namespace Bindings { XMLHttpRequestPrototype::XMLHttpRequestPrototype() : Object(interpreter().global_object().object_prototype()) { - put_native_function("open", open, 2); - put_native_function("send", send, 0); - put_native_property("readyState", ready_state_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable); - put_native_property("responseText", response_text_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable); + define_native_function("open", open, 2); + define_native_function("send", send, 0); + define_native_property("readyState", ready_state_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable); + define_native_property("responseText", response_text_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable); - put("UNSENT", JS::Value((i32)XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable); - put("OPENED", JS::Value((i32)XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable); - put("HEADERS_RECEIVED", JS::Value((i32)XMLHttpRequest::ReadyState::HeadersReceived), JS::Attribute::Enumerable); - put("LOADING", JS::Value((i32)XMLHttpRequest::ReadyState::Loading), JS::Attribute::Enumerable); - put("DONE", JS::Value((i32)XMLHttpRequest::ReadyState::Done), JS::Attribute::Enumerable); + define_property("UNSENT", JS::Value((i32)XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable); + define_property("OPENED", JS::Value((i32)XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable); + define_property("HEADERS_RECEIVED", JS::Value((i32)XMLHttpRequest::ReadyState::HeadersReceived), JS::Attribute::Enumerable); + define_property("LOADING", JS::Value((i32)XMLHttpRequest::ReadyState::Loading), JS::Attribute::Enumerable); + define_property("DONE", JS::Value((i32)XMLHttpRequest::ReadyState::Done), JS::Attribute::Enumerable); } XMLHttpRequestPrototype::~XMLHttpRequestPrototype() |