diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-15 13:39:24 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-15 13:50:42 +0200 |
commit | c6ddbd1f3ec30ccf7dc150f5a0eff982bd411293 (patch) | |
tree | 58897d645f1adcbbebd14569b9be555305f8382f /Libraries/LibWeb/Bindings | |
parent | d8aa2a6997800779cd2dc4303cb2256a32ffc2f3 (diff) | |
download | serenity-c6ddbd1f3ec30ccf7dc150f5a0eff982bd411293.zip |
LibJS: Add side-effect-free version of Value::to_string()
There are now two API's on Value:
- Value::to_string(Interpreter&) -- may throw.
- Value::to_string_without_side_effects() -- will never throw.
These are some pretty big sweeping changes, so it's possible that I did
some part the wrong way. We'll work it out as we go. :^)
Fixes #2123.
Diffstat (limited to 'Libraries/LibWeb/Bindings')
7 files changed, 54 insertions, 17 deletions
diff --git a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp index 9ad5e8e89d..4fc1ac7238 100644 --- a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp +++ b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp @@ -161,8 +161,12 @@ JS::Value CanvasRenderingContext2DWrapper::fill_style_getter(JS::Interpreter& in void CanvasRenderingContext2DWrapper::fill_style_setter(JS::Interpreter& interpreter, JS::Value value) { - if (auto* impl = impl_from(interpreter)) - impl->set_fill_style(value.to_string()); + if (auto* impl = impl_from(interpreter)) { + auto string = value.to_string(interpreter); + if (interpreter.exception()) + return; + impl->set_fill_style(string); + } } JS::Value CanvasRenderingContext2DWrapper::stroke_style_getter(JS::Interpreter& interpreter) @@ -175,8 +179,12 @@ JS::Value CanvasRenderingContext2DWrapper::stroke_style_getter(JS::Interpreter& void CanvasRenderingContext2DWrapper::stroke_style_setter(JS::Interpreter& interpreter, JS::Value value) { - if (auto* impl = impl_from(interpreter)) - impl->set_stroke_style(value.to_string()); + if (auto* impl = impl_from(interpreter)){ + auto string = value.to_string(interpreter); + if (interpreter.exception()) + return; + impl->set_stroke_style(string); + } } JS::Value CanvasRenderingContext2DWrapper::line_width_getter(JS::Interpreter& interpreter) diff --git a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp index a9439e3593..aab6b105bd 100644 --- a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp +++ b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp @@ -78,7 +78,9 @@ JS::Value DocumentWrapper::get_element_by_id(JS::Interpreter& interpreter) auto& arguments = interpreter.call_frame().arguments; if (arguments.is_empty()) return JS::js_null(); - auto id = arguments[0].to_string(); + auto id = arguments[0].to_string(interpreter); + if (interpreter.exception()) + return {}; auto* element = document->get_element_by_id(id); if (!element) return JS::js_null(); @@ -93,7 +95,9 @@ JS::Value DocumentWrapper::query_selector_all(JS::Interpreter& interpreter) auto& arguments = interpreter.call_frame().arguments; if (arguments.is_empty()) return JS::js_null(); - auto selector = arguments[0].to_string(); + auto selector = arguments[0].to_string(interpreter); + if (interpreter.exception()) + return {}; auto elements = document->query_selector_all(selector); // FIXME: This should be a static NodeList, not a plain JS::Array. auto* node_list = JS::Array::create(interpreter.global_object()); diff --git a/Libraries/LibWeb/Bindings/ElementWrapper.cpp b/Libraries/LibWeb/Bindings/ElementWrapper.cpp index 5e0bac9e23..7907619ad4 100644 --- a/Libraries/LibWeb/Bindings/ElementWrapper.cpp +++ b/Libraries/LibWeb/Bindings/ElementWrapper.cpp @@ -74,8 +74,12 @@ JS::Value ElementWrapper::inner_html_getter(JS::Interpreter& interpreter) void ElementWrapper::inner_html_setter(JS::Interpreter& interpreter, JS::Value value) { - if (auto* impl = impl_from(interpreter)) - impl->set_inner_html(value.to_string()); + if (auto* impl = impl_from(interpreter)) { + auto string = value.to_string(interpreter); + if (interpreter.exception()) + return; + impl->set_inner_html(string); + } } JS::Value ElementWrapper::id_getter(JS::Interpreter& interpreter) @@ -87,8 +91,12 @@ JS::Value ElementWrapper::id_getter(JS::Interpreter& interpreter) void ElementWrapper::id_setter(JS::Interpreter& interpreter, JS::Value value) { - if (auto* impl = impl_from(interpreter)) - impl->set_attribute("id", value.to_string()); + if (auto* impl = impl_from(interpreter)) { + auto string = value.to_string(interpreter); + if (interpreter.exception()) + return; + impl->set_attribute("id", string); + } } } diff --git a/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp b/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp index d2c3b2289e..50c91d7622 100644 --- a/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp +++ b/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp @@ -56,7 +56,9 @@ JS::Value EventTargetWrapper::add_event_listener(JS::Interpreter& interpreter) auto& arguments = interpreter.call_frame().arguments; if (arguments.size() < 2) return JS::js_undefined(); - auto event_name = arguments[0].to_string(); + auto event_name = arguments[0].to_string(interpreter); + if (interpreter.exception()) + return {}; ASSERT(arguments[1].is_object()); ASSERT(arguments[1].as_object().is_function()); auto& function = static_cast<JS::Function&>(const_cast<Object&>(arguments[1].as_object())); diff --git a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp index 193ec65901..74f196b645 100644 --- a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp +++ b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp @@ -76,7 +76,10 @@ JS::Value HTMLCanvasElementWrapper::get_context(JS::Interpreter& interpreter) return {}; auto& arguments = interpreter.call_frame().arguments; if (arguments.size() >= 1) { - auto* context = impl->get_context(arguments[0].to_string()); + auto string = arguments[0].to_string(interpreter); + if (interpreter.exception()) + return {}; + auto* context = impl->get_context(string); return wrap(interpreter.heap(), *context); } return JS::js_undefined(); diff --git a/Libraries/LibWeb/Bindings/WindowObject.cpp b/Libraries/LibWeb/Bindings/WindowObject.cpp index bab0a17316..1c1dcedce4 100644 --- a/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -98,8 +98,11 @@ JS::Value WindowObject::alert(JS::Interpreter& interpreter) if (!impl) return {}; String message = ""; - if (interpreter.argument_count()) - message = interpreter.argument(0).to_string(); + if (interpreter.argument_count()) { + message = interpreter.argument(0).to_string(interpreter); + if (interpreter.exception()) + return {}; + } impl->alert(message); return JS::js_undefined(); } @@ -110,8 +113,11 @@ JS::Value WindowObject::confirm(JS::Interpreter& interpreter) if (!impl) return {}; String message = ""; - if (interpreter.argument_count()) - message = interpreter.argument(0).to_string(); + if (interpreter.argument_count()) { + message = interpreter.argument(0).to_string(interpreter); + if (interpreter.exception()) + return {}; + } return JS::Value(impl->confirm(message)); } diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp index a5d95c302e..48e82fac1a 100644 --- a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp +++ b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp @@ -71,7 +71,13 @@ JS::Value XMLHttpRequestPrototype::open(JS::Interpreter& interpreter) auto* impl = impl_from(interpreter); if (!impl) return {}; - impl->open(interpreter.argument(0).to_string(), interpreter.argument(1).to_string()); + auto arg0 = interpreter.argument(0).to_string(interpreter); + if (interpreter.exception()) + return {}; + auto arg1 = interpreter.argument(1).to_string(interpreter); + if (interpreter.exception()) + return {}; + impl->open(arg0, arg1); return JS::js_undefined(); } |