From 7b54845c8e7645c3d67e000e50b18a9352f93f3d Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 3 Mar 2022 14:20:45 -0500 Subject: LibWeb: Allow calling invoke-a-callback with an existing argument list Some callers, e.g. setTimeout / setInterval, will want to invoke this AO with an arguments list retrieved from the JS VM (as opposed to invoking it with a variadic list at the call site). --- .../LibWeb/Bindings/IDLAbstractOperations.cpp | 59 ++++++++++++++++++++++ .../LibWeb/Bindings/IDLAbstractOperations.h | 55 ++------------------ 2 files changed, 64 insertions(+), 50 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.cpp b/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.cpp index 8c75a1c2dd..ac0efb5db3 100644 --- a/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.cpp @@ -83,4 +83,63 @@ Optional get_buffer_source_copy(JS::Object const& buffer_source) return bytes.release_value(); } +// https://webidl.spec.whatwg.org/#invoke-a-callback-function +JS::Completion invoke_callback(Bindings::CallbackType& callback, Optional this_argument, JS::MarkedVector 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 F be the ECMAScript object corresponding to callable. + auto* function_object = callback.callback.cell(); + VERIFY(function_object); + + // 4. If ! IsCallable(F) is false: + if (!function_object->is_function()) { + // 1. Note: This is only possible when the callback function came from an attribute marked with [LegacyTreatNonObjectAsNull]. + + // 2. Return the result of converting undefined to the callback function’s return type. + // FIXME: This does no conversion. + return { JS::js_undefined() }; + } + + // 5. Let realm be F’s associated Realm. + // See the comment about associated realm on step 4 of call_user_object_operation. + auto& global_object = function_object->global_object(); + auto& realm = *global_object.associated_realm(); + + // 6. Let relevant settings be realm’s settings object. + auto& relevant_settings = verify_cast(*realm.host_defined()); + + // 7. Let stored settings be value’s callback context. + auto& stored_settings = callback.callback_context; + + // 8. Prepare to run script with relevant settings. + relevant_settings.prepare_to_run_script(); + + // 9. Prepare to run a callback with stored settings. + stored_settings.prepare_to_run_callback(); + + // FIXME: 10. 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. + + // 11. Let callResult be Call(F, thisArg, esArgs). + auto call_result = JS::call(global_object, verify_cast(*function_object), this_argument.value(), move(args)); + + // 12. 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); + } + + // 13. 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); +} + } diff --git a/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.h b/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.h index 9804363a0a..439283ae5c 100644 --- a/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.h +++ b/Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.h @@ -120,64 +120,19 @@ JS::Completion call_user_object_operation(Bindings::CallbackType& callback, Stri return clean_up_on_return(stored_settings, relevant_settings, completion); } +JS::Completion invoke_callback(Bindings::CallbackType& callback, Optional this_argument, JS::MarkedVector args); + // https://webidl.spec.whatwg.org/#invoke-a-callback-function template JS::Completion invoke_callback(Bindings::CallbackType& callback, Optional 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 F be the ECMAScript object corresponding to callable. auto* function_object = callback.callback.cell(); VERIFY(function_object); - // 4. If ! IsCallable(F) is false: - if (!function_object->is_function()) { - // 1. Note: This is only possible when the callback function came from an attribute marked with [LegacyTreatNonObjectAsNull]. - - // 2. Return the result of converting undefined to the callback function’s return type. - // FIXME: This does no conversion. - return { JS::js_undefined() }; - } - - // 5. Let realm be F’s associated Realm. - // See the comment about associated realm on step 4 of call_user_object_operation. - auto& global_object = function_object->global_object(); - auto& realm = *global_object.associated_realm(); - - // 6. Let relevant settings be realm’s settings object. - auto& relevant_settings = verify_cast(*realm.host_defined()); - - // 7. Let stored settings be value’s callback context. - auto& stored_settings = callback.callback_context; - - // 8. Prepare to run script with relevant settings. - relevant_settings.prepare_to_run_script(); - - // 9. Prepare to run a callback with stored settings. - stored_settings.prepare_to_run_callback(); - - // FIXME: 10. 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. - - // 11. Let callResult be Call(F, thisArg, esArgs). - auto call_result = JS::call(global_object, verify_cast(*function_object), this_argument.value(), forward(args)...); - - // 12. 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); - } + JS::MarkedVector arguments_list { function_object->global_object().heap() }; + (arguments_list.append(forward(args)), ...); - // 13. 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); + return invoke_callback(callback, move(this_argument), move(arguments_list)); } } -- cgit v1.2.3