From 1639ed7e0a9bc063599ec193b7b6af48f7d5faba Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sat, 16 Oct 2021 22:11:08 +0300 Subject: [PATCH] LibJS: Convert to_double() to ThrowCompletionOr --- .../CodeGenerators/LibWeb/WrapperGenerator.cpp | 11 +++-------- Tests/LibWasm/test-wasm.cpp | 6 ++---- .../Applications/Spreadsheet/CellType/Date.cpp | 2 +- .../Spreadsheet/CellType/Numeric.cpp | 2 +- Userland/Libraries/LibJS/Runtime/ArrayBuffer.h | 4 ++-- .../Libraries/LibJS/Runtime/GeneratorObject.cpp | 6 ++++-- Userland/Libraries/LibJS/Runtime/Value.cpp | 6 +++--- Userland/Libraries/LibJS/Runtime/Value.h | 2 +- .../Libraries/LibWeb/Bindings/WindowObject.cpp | 16 ++++------------ .../LibWeb/WebAssembly/WebAssemblyObject.cpp | 8 ++------ 10 files changed, 23 insertions(+), 40 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp index 050bfef11b..a707ce13c2 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp @@ -1057,9 +1057,7 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter } else if (parameter.type->name == "double") { if (!optional) { scoped_generator.append(R"~~~( - double @cpp_name@ = @js_name@@js_suffix@.to_double(global_object); - if (vm.exception()) - @return_statement@ + double @cpp_name@ = TRY_OR_DISCARD(@js_name@@js_suffix@.to_double(global_object)); )~~~"); } else { if (optional_default_value.has_value()) { @@ -1072,11 +1070,8 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter )~~~"); } scoped_generator.append(R"~~~( - if (!@js_name@@js_suffix@.is_undefined()) { - @cpp_name@ = @js_name@@js_suffix@.to_double(global_object); - if (vm.exception()) - @return_statement@ - } + if (!@js_name@@js_suffix@.is_undefined()) + @cpp_name@ = TRY_OR_DISCARD(@js_name@@js_suffix@.to_double(global_object)); )~~~"); if (optional_default_value.has_value()) { scoped_generator.append(R"~~~( diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index 9bbb59e778..e7d6237fc6 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -195,9 +195,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export) JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke) { - auto address = static_cast(vm.argument(0).to_double(global_object)); - if (vm.exception()) - return {}; + auto address = static_cast(TRY_OR_DISCARD(vm.argument(0).to_double(global_object))); Wasm::FunctionAddress function_address { address }; auto function_instance = WebAssemblyModule::machine().store().get(function_address); if (!function_instance) { @@ -222,7 +220,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke) auto argument = vm.argument(index++); double double_value = 0; if (!argument.is_bigint()) - double_value = argument.to_double(global_object); + double_value = TRY_OR_DISCARD(argument.to_double(global_object)); switch (param.kind()) { case Wasm::ValueType::Kind::I32: arguments.append(Wasm::Value(param, static_cast(double_value))); diff --git a/Userland/Applications/Spreadsheet/CellType/Date.cpp b/Userland/Applications/Spreadsheet/CellType/Date.cpp index edc343b33e..5881c2030f 100644 --- a/Userland/Applications/Spreadsheet/CellType/Date.cpp +++ b/Userland/Applications/Spreadsheet/CellType/Date.cpp @@ -41,7 +41,7 @@ String DateCell::display(Cell& cell, const CellTypeMetadata& metadata) const JS::Value DateCell::js_value(Cell& cell, const CellTypeMetadata&) const { auto js_data = cell.js_data(); - auto value = js_data.to_double(cell.sheet().global_object()); + auto value = TRY_OR_DISCARD(js_data.to_double(cell.sheet().global_object())); return JS::Value(value / 1000); // Turn it to seconds } diff --git a/Userland/Applications/Spreadsheet/CellType/Numeric.cpp b/Userland/Applications/Spreadsheet/CellType/Numeric.cpp index 5d9a1b0112..4b526786d0 100644 --- a/Userland/Applications/Spreadsheet/CellType/Numeric.cpp +++ b/Userland/Applications/Spreadsheet/CellType/Numeric.cpp @@ -34,7 +34,7 @@ String NumericCell::display(Cell& cell, const CellTypeMetadata& metadata) const if (metadata.format.is_empty()) string = value.to_string_without_side_effects(); else - string = format_double(metadata.format.characters(), value.to_double(cell.sheet().global_object())); + string = format_double(metadata.format.characters(), TRY_OR_DISCARD(value.to_double(cell.sheet().global_object()))); if (metadata.length >= 0) return string.substring(0, metadata.length); diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h index 507a449bcd..94574ba837 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h +++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h @@ -141,13 +141,13 @@ static ByteBuffer numeric_to_raw_bytes(GlobalObject& global_object, Value value, swap(raw_bytes[i], raw_bytes[sizeof(UnderlyingBufferDataType) - 1 - i]); }; if constexpr (IsSame) { - float raw_value = value.to_double(global_object); + float raw_value = MUST(value.to_double(global_object)); ReadonlyBytes { &raw_value, sizeof(float) }.copy_to(raw_bytes); flip_if_needed(); return raw_bytes; } if constexpr (IsSame) { - double raw_value = value.to_double(global_object); + double raw_value = MUST(value.to_double(global_object)); ReadonlyBytes { &raw_value, sizeof(double) }.copy_to(raw_bytes); flip_if_needed(); return raw_bytes; diff --git a/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp b/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp index 6e5c4fe8ee..59308d9323 100644 --- a/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp @@ -59,8 +59,10 @@ Value GeneratorObject::next_impl(VM& vm, GlobalObject& global_object, Optional Bytecode::BasicBlock const* { - if (value.is_object()) - return reinterpret_cast(static_cast(TRY_OR_DISCARD(value.as_object().get("continuation")).to_double(global_object))); + if (value.is_object()) { + auto number_value = TRY_OR_DISCARD(value.as_object().get("continuation")); + return reinterpret_cast(static_cast(TRY_OR_DISCARD(number_value.to_double(global_object)))); + } return nullptr; }; diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 2fc03b5483..025b853c81 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -559,11 +559,11 @@ ThrowCompletionOr Value::to_bigint_uint64(GlobalObject& global_object) cons return bigint->big_integer().to_u64(); } -double Value::to_double(GlobalObject& global_object) const +ThrowCompletionOr Value::to_double(GlobalObject& global_object) const { auto number = to_number(global_object); - if (global_object.vm().exception()) - return {}; + if (auto* exception = global_object.vm().exception()) + return throw_completion(exception->value()); return number.as_double(); } diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index e155d34608..5cf3846741 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -313,7 +313,7 @@ public: ThrowCompletionOr to_bigint(GlobalObject&) const; ThrowCompletionOr to_bigint_int64(GlobalObject&) const; ThrowCompletionOr to_bigint_uint64(GlobalObject&) const; - double to_double(GlobalObject&) const; + ThrowCompletionOr to_double(GlobalObject&) const; StringOrSymbol to_property_key(GlobalObject&) const; i32 to_i32(GlobalObject& global_object) const { diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index 9be4e6f5aa..9eed2cebd5 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -641,14 +641,10 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll) ScrollBehavior behavior = (behavior_string == "smooth") ? ScrollBehavior::Smooth : ScrollBehavior::Auto; - double x = x_value.to_double(global_object); - if (vm.exception()) - return {}; + double x = TRY_OR_DISCARD(x_value.to_double(global_object)); x = JS::Value(x).is_finite_number() ? x : 0.0; - double y = y_value.to_double(global_object); - if (vm.exception()) - return {}; + double y = TRY_OR_DISCARD(y_value.to_double(global_object)); y = JS::Value(y).is_finite_number() ? y : 0.0; // FIXME: Are we calculating the viewport in the way this function expects? @@ -683,14 +679,10 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::scroll_by) } auto left_value = TRY_OR_DISCARD(options->get("left")); - auto left = left_value.to_double(global_object); - if (vm.exception()) - return {}; + auto left = TRY_OR_DISCARD(left_value.to_double(global_object)); auto top_value = TRY_OR_DISCARD(options->get("top")); - auto top = top_value.to_double(global_object); - if (vm.exception()) - return {}; + auto top = TRY_OR_DISCARD(top_value.to_double(global_object)); left = JS::Value(left).is_finite_number() ? left : 0.0; top = JS::Value(top).is_finite_number() ? top : 0.0; diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp index 4fbfddbec9..2b49e3b74d 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.cpp @@ -403,15 +403,11 @@ Optional to_webassembly_value(JS::Value value, const Wasm::ValueTyp return Wasm::Value { static_cast(_i32) }; } case Wasm::ValueType::F64: { - auto number = value.to_double(global_object); - if (vm.exception()) - return {}; + auto number = TRY_OR_DISCARD(value.to_double(global_object)); return Wasm::Value { static_cast(number) }; } case Wasm::ValueType::F32: { - auto number = value.to_double(global_object); - if (vm.exception()) - return {}; + auto number = TRY_OR_DISCARD(value.to_double(global_object)); return Wasm::Value { static_cast(number) }; } case Wasm::ValueType::FunctionReference: