diff options
author | Luke Wilde <lukew@serenityos.org> | 2023-03-29 23:39:59 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-04-06 11:36:56 +0200 |
commit | 9b8b363445bd1ae83613ab514f3c9e2de1d70d89 (patch) | |
tree | aa7f13d0b6c509ae5f7ab3e6daf61c42a211aaff | |
parent | 686e3a0d94f48dd3df98b09458f9916f5b0dddf8 (diff) | |
download | serenity-9b8b363445bd1ae83613ab514f3c9e2de1d70d89.zip |
LibWeb/WebIDL: Move call_user_object_operation out of line
This makes it in line with `invoke_callback`.
-rw-r--r-- | Userland/Libraries/LibWeb/WebIDL/AbstractOperations.cpp | 100 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/WebIDL/AbstractOperations.h | 100 |
2 files changed, 106 insertions, 94 deletions
diff --git a/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.cpp b/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.cpp index e26b250608..ef35952681 100644 --- a/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.cpp @@ -81,6 +81,106 @@ ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source) return bytes; } +// https://webidl.spec.whatwg.org/#call-user-object-operation-return +inline JS::Completion clean_up_on_return(HTML::EnvironmentSettingsObject& stored_settings, HTML::EnvironmentSettingsObject& relevant_settings, JS::Completion& completion) +{ + // Return: at this point completion will be set to an ECMAScript completion value. + + // 1. Clean up after running a callback with stored settings. + stored_settings.clean_up_after_running_callback(); + + // 2. Clean up after running script with relevant settings. + relevant_settings.clean_up_after_running_script(); + + // 3. If completion is a normal completion, return completion. + if (completion.type() == JS::Completion::Type::Normal) + return completion; + + // 4. If completion is an abrupt completion and the operation has a return type that is not a promise type, return completion. + // FIXME: This does not handle promises and thus always returns completion at this point. + return completion; + + // FIXME: 5. Let rejectedPromise be ! Call(%Promise.reject%, %Promise%, Ā«completion.[[Value]]Ā»). + + // FIXME: 6. Return the result of converting rejectedPromise to the operationās return type. +} + +JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, DeprecatedString const& operation_name, Optional<JS::Value> this_argument, JS::MarkedVector<JS::Value> args) +{ + // 1. Let completion be an uninitialized variable. + JS::Completion completion; + + // 2. If thisArg was not given, let thisArg be undefined. + if (!this_argument.has_value()) + this_argument = JS::js_undefined(); + + // 3. Let O be the ECMAScript object corresponding to value. + auto& object = callback.callback; + + // 4. Let realm be Oās associated Realm. + auto& realm = object->shape().realm(); + + // 5. Let relevant settings be realmās settings object. + auto& relevant_settings = Bindings::host_defined_environment_settings_object(realm); + + // 6. Let stored settings be valueās callback context. + auto& stored_settings = callback.callback_context; + + // 7. Prepare to run script with relevant settings. + relevant_settings.prepare_to_run_script(); + + // 8. Prepare to run a callback with stored settings. + stored_settings->prepare_to_run_callback(); + + // 9. Let X be O. + auto actual_function_object = object; + + // 10. If ! IsCallable(O) is false, then: + if (!object->is_function()) { + // 1. Let getResult be Get(O, opName). + auto get_result = object->get(operation_name); + + // 2. If getResult is an abrupt completion, set completion to getResult and jump to the step labeled return. + if (get_result.is_throw_completion()) { + completion = get_result.throw_completion(); + return clean_up_on_return(stored_settings, relevant_settings, completion); + } + + // 4. If ! IsCallable(X) is false, then set completion to a new Completion{[[Type]]: throw, [[Value]]: a newly created TypeError object, [[Target]]: empty}, and jump to the step labeled return. + if (!get_result.value().is_function()) { + completion = realm.vm().template throw_completion<JS::TypeError>(JS::ErrorType::NotAFunction, TRY_OR_THROW_OOM(realm.vm(), get_result.value().to_string_without_side_effects())); + return clean_up_on_return(stored_settings, relevant_settings, completion); + } + + // 3. Set X to getResult.[[Value]]. + // NOTE: This is done out of order because `actual_function_object` is of type JS::Object and we cannot assign to it until we know for sure getResult.[[Value]] is a JS::Object. + actual_function_object = get_result.release_value().as_object(); + + // 5. Set thisArg to O (overriding the provided value). + this_argument = object; + } + + // FIXME: 11. Let esArgs be the result of converting args to an ECMAScript arguments list. If this throws an exception, set completion to the completion value representing the thrown exception and jump to the step labeled return. + // For simplicity, we currently make the caller do this. However, this means we can't throw exceptions at this point like the spec wants us to. + + // 12. Let callResult be Call(X, thisArg, esArgs). + VERIFY(actual_function_object); + auto& vm = object->vm(); + auto call_result = JS::call(vm, verify_cast<JS::FunctionObject>(*actual_function_object), this_argument.value(), move(args)); + + // 13. If callResult is an abrupt completion, set completion to callResult and jump to the step labeled return. + if (call_result.is_throw_completion()) { + completion = call_result.throw_completion(); + return clean_up_on_return(stored_settings, relevant_settings, completion); + } + + // 14. Set completion to the result of converting callResult.[[Value]] to an IDL value of the same type as the operationās return type. + // FIXME: This does no conversion. + completion = call_result.value(); + + return clean_up_on_return(stored_settings, relevant_settings, completion); +} + // https://webidl.spec.whatwg.org/#invoke-a-callback-function JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Value> this_argument, JS::MarkedVector<JS::Value> args) { diff --git a/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.h b/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.h index 41682a85ca..9239bc4940 100644 --- a/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/WebIDL/AbstractOperations.h @@ -19,106 +19,18 @@ namespace Web::WebIDL { ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source); -// https://webidl.spec.whatwg.org/#call-user-object-operation-return -inline JS::Completion clean_up_on_return(HTML::EnvironmentSettingsObject& stored_settings, HTML::EnvironmentSettingsObject& relevant_settings, JS::Completion& completion) -{ - // Return: at this point completion will be set to an ECMAScript completion value. - - // 1. Clean up after running a callback with stored settings. - stored_settings.clean_up_after_running_callback(); - - // 2. Clean up after running script with relevant settings. - relevant_settings.clean_up_after_running_script(); - - // 3. If completion is a normal completion, return completion. - if (completion.type() == JS::Completion::Type::Normal) - return completion; - - // 4. If completion is an abrupt completion and the operation has a return type that is not a promise type, return completion. - // FIXME: This does not handle promises and thus always returns completion at this point. - return completion; - - // FIXME: 5. Let rejectedPromise be ! Call(%Promise.reject%, %Promise%, Ā«completion.[[Value]]Ā»). - - // FIXME: 6. Return the result of converting rejectedPromise to the operationās return type. -} +JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, DeprecatedString const& operation_name, Optional<JS::Value> this_argument, JS::MarkedVector<JS::Value> args); // https://webidl.spec.whatwg.org/#call-a-user-objects-operation template<typename... Args> JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, DeprecatedString const& operation_name, Optional<JS::Value> this_argument, Args&&... args) { - // 1. Let completion be an uninitialized variable. - JS::Completion completion; - - // 2. If thisArg was not given, let thisArg be undefined. - if (!this_argument.has_value()) - this_argument = JS::js_undefined(); - - // 3. Let O be the ECMAScript object corresponding to value. - auto& object = callback.callback; - - // 4. Let realm be Oās associated Realm. - auto& realm = object->shape().realm(); - - // 5. Let relevant settings be realmās settings object. - auto& relevant_settings = Bindings::host_defined_environment_settings_object(realm); - - // 6. Let stored settings be valueās callback context. - auto& stored_settings = callback.callback_context; - - // 7. Prepare to run script with relevant settings. - relevant_settings.prepare_to_run_script(); - - // 8. Prepare to run a callback with stored settings. - stored_settings->prepare_to_run_callback(); - - // 9. Let X be O. - auto actual_function_object = object; - - // 10. If ! IsCallable(O) is false, then: - if (!object->is_function()) { - // 1. Let getResult be Get(O, opName). - auto get_result = object->get(operation_name); - - // 2. If getResult is an abrupt completion, set completion to getResult and jump to the step labeled return. - if (get_result.is_throw_completion()) { - completion = get_result.throw_completion(); - return clean_up_on_return(stored_settings, relevant_settings, completion); - } - - // 4. If ! IsCallable(X) is false, then set completion to a new Completion{[[Type]]: throw, [[Value]]: a newly created TypeError object, [[Target]]: empty}, and jump to the step labeled return. - if (!get_result.value().is_function()) { - completion = realm.vm().template throw_completion<JS::TypeError>(JS::ErrorType::NotAFunction, TRY_OR_THROW_OOM(realm.vm(), get_result.value().to_string_without_side_effects())); - return clean_up_on_return(stored_settings, relevant_settings, completion); - } - - // 3. Set X to getResult.[[Value]]. - // NOTE: This is done out of order because `actual_function_object` is of type JS::Object and we cannot assign to it until we know for sure getResult.[[Value]] is a JS::Object. - actual_function_object = get_result.release_value().as_object(); - - // 5. Set thisArg to O (overriding the provided value). - this_argument = object; - } - - // FIXME: 11. Let esArgs be the result of converting args to an ECMAScript arguments list. If this throws an exception, set completion to the completion value representing the thrown exception and jump to the step labeled return. - // For simplicity, we currently make the caller do this. However, this means we can't throw exceptions at this point like the spec wants us to. - - // 12. Let callResult be Call(X, thisArg, esArgs). - VERIFY(actual_function_object); - auto& vm = object->vm(); - auto call_result = JS::call(vm, verify_cast<JS::FunctionObject>(*actual_function_object), this_argument.value(), forward<Args>(args)...); - - // 13. If callResult is an abrupt completion, set completion to callResult and jump to the step labeled return. - if (call_result.is_throw_completion()) { - completion = call_result.throw_completion(); - return clean_up_on_return(stored_settings, relevant_settings, completion); - } + auto& function_object = callback.callback; - // 14. Set completion to the result of converting callResult.[[Value]] to an IDL value of the same type as the operationās return type. - // FIXME: This does no conversion. - completion = call_result.value(); + JS::MarkedVector<JS::Value> arguments_list { function_object->heap() }; + (arguments_list.append(forward<Args>(args)), ...); - return clean_up_on_return(stored_settings, relevant_settings, completion); + return call_user_object_operation(callback, operation_name, move(this_argument), move(arguments_list)); } JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Value> this_argument, JS::MarkedVector<JS::Value> args); @@ -129,7 +41,7 @@ JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Valu { auto& function_object = callback.callback; - JS::MarkedVector<JS::Value> arguments_list { function_object->vm().heap() }; + JS::MarkedVector<JS::Value> arguments_list { function_object->heap() }; (arguments_list.append(forward<Args>(args)), ...); return invoke_callback(callback, move(this_argument), move(arguments_list)); |