1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:27:34 +00:00

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.
This commit is contained in:
Andreas Kling 2020-05-15 13:39:24 +02:00
parent d8aa2a6997
commit c6ddbd1f3e
25 changed files with 285 additions and 112 deletions

View file

@ -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)

View file

@ -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());

View file

@ -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);
}
}
}

View file

@ -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()));

View file

@ -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();

View file

@ -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));
}

View file

@ -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();
}