mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
LibWeb: Change backup imcumbent stack to hold Realm instead of Settings
This is a bit of a chonkier commit as it results in both: clean_up_after_running_callback and prepare_to_run_callback being changed to accept a realm instead of an environment settings object, which has a bunch of fallout, particuarly for IDL abstract operations.
This commit is contained in:
parent
8dffd8e7d6
commit
d7023f5f45
Notes:
github-actions[bot]
2024-11-01 19:16:12 +00:00
Author: https://github.com/shannonbooth Commit: https://github.com/LadybirdBrowser/ladybird/commit/d7023f5f450 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1932 Reviewed-by: https://github.com/ADKaster ✅
10 changed files with 114 additions and 117 deletions
|
@ -199,14 +199,15 @@ ErrorOr<void> initialize_main_thread_vm(HTML::EventLoop::Type type)
|
|||
};
|
||||
|
||||
// 8.1.5.4.1 HostCallJobCallback(callback, V, argumentsList), https://html.spec.whatwg.org/multipage/webappapis.html#hostcalljobcallback
|
||||
// https://whatpr.org/html/9893/webappapis.html#hostcalljobcallback
|
||||
s_main_thread_vm->host_call_job_callback = [](JS::JobCallback& callback, JS::Value this_value, ReadonlySpan<JS::Value> arguments_list) {
|
||||
auto& callback_host_defined = verify_cast<WebEngineCustomJobCallbackData>(*callback.custom_data());
|
||||
|
||||
// 1. Let incumbent settings be callback.[[HostDefined]].[[IncumbentSettings]]. (NOTE: Not necessary)
|
||||
// 1. Let incumbent realm be callback.[[HostDefined]].[[IncumbentRealm]]. (NOTE: Not necessary)
|
||||
// 2. Let script execution context be callback.[[HostDefined]].[[ActiveScriptContext]]. (NOTE: Not necessary)
|
||||
|
||||
// 3. Prepare to run a callback with incumbent settings.
|
||||
callback_host_defined.incumbent_settings->prepare_to_run_callback();
|
||||
// 3. Prepare to run a callback with incumbent realm.
|
||||
HTML::prepare_to_run_callback(callback_host_defined.incumbent_settings->realm());
|
||||
|
||||
// 4. If script execution context is not null, then push script execution context onto the JavaScript execution context stack.
|
||||
if (callback_host_defined.active_script_context)
|
||||
|
@ -221,8 +222,8 @@ ErrorOr<void> initialize_main_thread_vm(HTML::EventLoop::Type type)
|
|||
s_main_thread_vm->pop_execution_context();
|
||||
}
|
||||
|
||||
// 7. Clean up after running a callback with incumbent settings.
|
||||
callback_host_defined.incumbent_settings->clean_up_after_running_callback();
|
||||
// 7. Clean up after running a callback with incumbent realm.
|
||||
HTML::clean_up_after_running_callback(callback_host_defined.incumbent_settings->realm());
|
||||
|
||||
// 8. Return result.
|
||||
return result;
|
||||
|
@ -291,7 +292,7 @@ ErrorOr<void> initialize_main_thread_vm(HTML::EventLoop::Type type)
|
|||
// IMPLEMENTATION DEFINED: Additionally to preparing to run a script, we also prepare to run a callback here. This matches WebIDL's
|
||||
// invoke_callback() / call_user_object_operation() functions, and prevents a crash in host_make_job_callback()
|
||||
// when getting the incumbent settings object.
|
||||
job_settings->prepare_to_run_callback();
|
||||
HTML::prepare_to_run_callback(*realm);
|
||||
|
||||
// IMPLEMENTATION DEFINED: Per the previous "implementation defined" comment, we must now make the script or module the active script or module.
|
||||
// Since the only active execution context currently is the realm execution context of job settings, lets attach it here.
|
||||
|
@ -314,7 +315,7 @@ ErrorOr<void> initialize_main_thread_vm(HTML::EventLoop::Type type)
|
|||
job_settings->realm_execution_context().script_or_module = Empty {};
|
||||
|
||||
// IMPLEMENTATION DEFINED: See comment above, we need to clean up the non-standard prepare_to_run_callback() call.
|
||||
job_settings->clean_up_after_running_callback();
|
||||
HTML::clean_up_after_running_callback(*realm);
|
||||
|
||||
HTML::clean_up_after_running_script(*realm);
|
||||
} else {
|
||||
|
|
|
@ -335,17 +335,17 @@ JS::NonnullGCPtr<Streams::ReadableStream> Blob::get_stream()
|
|||
|
||||
// 2. Queue a global task on the file reading task source given blob’s relevant global object to perform the following steps:
|
||||
HTML::queue_global_task(HTML::Task::Source::FileReading, realm.global_object(), JS::create_heap_function(heap(), [stream, bytes = move(bytes)]() {
|
||||
// NOTE: Using an TemporaryExecutionContext here results in a crash in the method HTML::incumbent_settings_object()
|
||||
// NOTE: Using an TemporaryExecutionContext here results in a crash in the method HTML::incumbent_realm()
|
||||
// since we end up in a state where we have no execution context + an event loop with an empty incumbent
|
||||
// settings object stack. We still need an execution context therefore we push the realm's execution context
|
||||
// onto the realm's VM, and we need an incumbent settings object which is pushed onto the incumbent settings
|
||||
// object stack by EnvironmentSettings::prepare_to_run_callback().
|
||||
// realm stack. We still need an execution context therefore we push the realm's execution context
|
||||
// onto the realm's VM, and we need an incumbent realm which is pushed onto the incumbent realm stack
|
||||
// by HTML::prepare_to_run_callback().
|
||||
auto& realm = stream->realm();
|
||||
auto& environment_settings = Bindings::host_defined_environment_settings_object(realm);
|
||||
realm.vm().push_execution_context(environment_settings.realm_execution_context());
|
||||
environment_settings.prepare_to_run_callback();
|
||||
ScopeGuard const guard = [&environment_settings, &realm] {
|
||||
environment_settings.clean_up_after_running_callback();
|
||||
HTML::prepare_to_run_callback(realm);
|
||||
ScopeGuard const guard = [&realm] {
|
||||
HTML::clean_up_after_running_callback(realm);
|
||||
realm.vm().pop_execution_context();
|
||||
};
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ void EventLoop::visit_edges(Visitor& visitor)
|
|||
visitor.visit(m_task_queue);
|
||||
visitor.visit(m_microtask_queue);
|
||||
visitor.visit(m_currently_running_task);
|
||||
visitor.visit(m_backup_incumbent_settings_object_stack);
|
||||
visitor.visit(m_backup_incumbent_realm_stack);
|
||||
visitor.visit(m_rendering_task_function);
|
||||
visitor.visit(m_system_event_loop_timer);
|
||||
}
|
||||
|
@ -538,19 +538,19 @@ void EventLoop::unregister_document(Badge<DOM::Document>, DOM::Document& documen
|
|||
VERIFY(did_remove);
|
||||
}
|
||||
|
||||
void EventLoop::push_onto_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object)
|
||||
void EventLoop::push_onto_backup_incumbent_realm_stack(JS::Realm& realm)
|
||||
{
|
||||
m_backup_incumbent_settings_object_stack.append(environment_settings_object);
|
||||
m_backup_incumbent_realm_stack.append(realm);
|
||||
}
|
||||
|
||||
void EventLoop::pop_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>)
|
||||
void EventLoop::pop_backup_incumbent_realm_stack()
|
||||
{
|
||||
m_backup_incumbent_settings_object_stack.take_last();
|
||||
m_backup_incumbent_realm_stack.take_last();
|
||||
}
|
||||
|
||||
EnvironmentSettingsObject& EventLoop::top_of_backup_incumbent_settings_object_stack()
|
||||
JS::Realm& EventLoop::top_of_backup_incumbent_realm_stack()
|
||||
{
|
||||
return m_backup_incumbent_settings_object_stack.last();
|
||||
return m_backup_incumbent_realm_stack.last();
|
||||
}
|
||||
|
||||
void EventLoop::register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object)
|
||||
|
|
|
@ -61,10 +61,10 @@ public:
|
|||
|
||||
Vector<JS::Handle<HTML::Window>> same_loop_windows() const;
|
||||
|
||||
void push_onto_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object);
|
||||
void pop_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>);
|
||||
EnvironmentSettingsObject& top_of_backup_incumbent_settings_object_stack();
|
||||
bool is_backup_incumbent_settings_object_stack_empty() const { return m_backup_incumbent_settings_object_stack.is_empty(); }
|
||||
void push_onto_backup_incumbent_realm_stack(JS::Realm&);
|
||||
void pop_backup_incumbent_realm_stack();
|
||||
JS::Realm& top_of_backup_incumbent_realm_stack();
|
||||
bool is_backup_incumbent_realm_stack_empty() const { return m_backup_incumbent_realm_stack.is_empty(); }
|
||||
|
||||
void register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);
|
||||
void unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);
|
||||
|
@ -107,7 +107,8 @@ private:
|
|||
Vector<RawPtr<EnvironmentSettingsObject>> m_related_environment_settings_objects;
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stack
|
||||
Vector<JS::NonnullGCPtr<EnvironmentSettingsObject>> m_backup_incumbent_settings_object_stack;
|
||||
// https://whatpr.org/html/9893/webappapis.html#backup-incumbent-realm-stack
|
||||
Vector<JS::NonnullGCPtr<JS::Realm>> m_backup_incumbent_realm_stack;
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
|
||||
size_t m_termination_nesting_level { 0 };
|
||||
|
|
|
@ -175,14 +175,14 @@ static JS::ExecutionContext* top_most_script_having_execution_context(JS::VM& vm
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#prepare-to-run-a-callback
|
||||
void EnvironmentSettingsObject::prepare_to_run_callback()
|
||||
void prepare_to_run_callback(JS::Realm& realm)
|
||||
{
|
||||
auto& vm = global_object().vm();
|
||||
auto& vm = realm.global_object().vm();
|
||||
|
||||
// 1. Push settings onto the backup incumbent settings object stack.
|
||||
// 1. Push realm onto the backup incumbent settings object stack.
|
||||
// NOTE: The spec doesn't say which event loop's stack to put this on. However, all the examples of the incumbent settings object use iframes and cross browsing context communication to demonstrate the concept.
|
||||
// This means that it must rely on some global state that can be accessed by all browsing contexts, which is the main thread event loop.
|
||||
HTML::main_thread_event_loop().push_onto_backup_incumbent_settings_object_stack({}, *this);
|
||||
HTML::main_thread_event_loop().push_onto_backup_incumbent_realm_stack(realm);
|
||||
|
||||
// 2. Let context be the topmost script-having execution context.
|
||||
auto* context = top_most_script_having_execution_context(vm);
|
||||
|
@ -209,9 +209,10 @@ URL::URL EnvironmentSettingsObject::parse_url(StringView url)
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#clean-up-after-running-a-callback
|
||||
void EnvironmentSettingsObject::clean_up_after_running_callback()
|
||||
// https://whatpr.org/html/9893/b8ea975...df5706b/webappapis.html#clean-up-after-running-a-callback
|
||||
void clean_up_after_running_callback(JS::Realm const& realm)
|
||||
{
|
||||
auto& vm = global_object().vm();
|
||||
auto& vm = realm.global_object().vm();
|
||||
|
||||
// 1. Let context be the topmost script-having execution context.
|
||||
auto* context = top_most_script_having_execution_context(vm);
|
||||
|
@ -220,12 +221,12 @@ void EnvironmentSettingsObject::clean_up_after_running_callback()
|
|||
if (context)
|
||||
context->skip_when_determining_incumbent_counter--;
|
||||
|
||||
// 3. Assert: the topmost entry of the backup incumbent settings object stack is settings.
|
||||
// 3. Assert: the topmost entry of the backup incumbent realm stack is realm.
|
||||
auto& event_loop = HTML::main_thread_event_loop();
|
||||
VERIFY(&event_loop.top_of_backup_incumbent_settings_object_stack() == this);
|
||||
VERIFY(&event_loop.top_of_backup_incumbent_realm_stack() == &realm);
|
||||
|
||||
// 4. Remove settings from the backup incumbent settings object stack.
|
||||
event_loop.pop_backup_incumbent_settings_object_stack({});
|
||||
// 4. Remove realm from the backup incumbent realm stack.
|
||||
event_loop.pop_backup_incumbent_realm_stack();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-script
|
||||
|
@ -285,8 +286,9 @@ void EnvironmentSettingsObject::disallow_further_import_maps()
|
|||
verify_cast<Window>(global).set_import_maps_allowed(false);
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#incumbent-settings-object
|
||||
EnvironmentSettingsObject& incumbent_settings_object()
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-incumbent-realm
|
||||
// https://whatpr.org/html/9893/b8ea975...df5706b/webappapis.html#concept-incumbent-realm
|
||||
JS::Realm& incumbent_realm()
|
||||
{
|
||||
auto& event_loop = HTML::main_thread_event_loop();
|
||||
auto& vm = event_loop.vm();
|
||||
|
@ -297,22 +299,24 @@ EnvironmentSettingsObject& incumbent_settings_object()
|
|||
// 2. If context is null, or if context's skip-when-determining-incumbent counter is greater than zero, then:
|
||||
if (!context || context->skip_when_determining_incumbent_counter > 0) {
|
||||
// 1. Assert: the backup incumbent settings object stack is not empty.
|
||||
// NOTE: If this assertion fails, it's because the incumbent settings object was used with no involvement of JavaScript.
|
||||
VERIFY(!event_loop.is_backup_incumbent_settings_object_stack_empty());
|
||||
// 1. Assert: the backup incumbent realm stack is not empty.
|
||||
// NOTE: If this assertion fails, it's because the incumbent realm was used with no involvement of JavaScript.
|
||||
VERIFY(!event_loop.is_backup_incumbent_realm_stack_empty());
|
||||
|
||||
// 2. Return the topmost entry of the backup incumbent settings object stack.
|
||||
return event_loop.top_of_backup_incumbent_settings_object_stack();
|
||||
// 2. Return the topmost entry of the backup incumbent realm stack.
|
||||
return event_loop.top_of_backup_incumbent_realm_stack();
|
||||
}
|
||||
|
||||
// 3. Return context's Realm component's settings object.
|
||||
return Bindings::host_defined_environment_settings_object(*context->realm);
|
||||
// 3. Return context's Realm component.
|
||||
return *context->realm;
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-incumbent-realm
|
||||
JS::Realm& incumbent_realm()
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#incumbent-settings-object
|
||||
// https://whatpr.org/html/9893/b8ea975...df5706b/webappapis.html#incumbent-settings-object
|
||||
EnvironmentSettingsObject& incumbent_settings_object()
|
||||
{
|
||||
// Then, the incumbent Realm is the Realm of the incumbent settings object.
|
||||
return incumbent_settings_object().realm();
|
||||
// FIXME: Then, the incumbent settings object is the incumbent realm's principal realm settings object.
|
||||
return Bindings::host_defined_environment_settings_object(incumbent_realm());
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-incumbent-global
|
||||
|
|
|
@ -96,9 +96,6 @@ public:
|
|||
// https://fetch.spec.whatwg.org/#concept-fetch-group
|
||||
Vector<JS::NonnullGCPtr<Fetch::Infrastructure::FetchRecord>>& fetch_group() { return m_fetch_group; }
|
||||
|
||||
void prepare_to_run_callback();
|
||||
void clean_up_after_running_callback();
|
||||
|
||||
bool module_type_allowed(StringView module_type) const;
|
||||
|
||||
void disallow_further_import_maps();
|
||||
|
@ -142,6 +139,8 @@ bool is_scripting_enabled(JS::Realm const&);
|
|||
bool is_scripting_disabled(JS::Realm const&);
|
||||
void prepare_to_run_script(JS::Realm&);
|
||||
void clean_up_after_running_script(JS::Realm const&);
|
||||
void prepare_to_run_callback(JS::Realm&);
|
||||
void clean_up_after_running_callback(JS::Realm const&);
|
||||
|
||||
EnvironmentSettingsObject& incumbent_settings_object();
|
||||
JS::Realm& incumbent_realm();
|
||||
|
|
|
@ -958,7 +958,7 @@ void fetch_descendants_of_and_link_a_module_script(JS::Realm& realm,
|
|||
// resulting in the event loop hanging forever awaiting for the script to be ready for parser
|
||||
// execution.
|
||||
realm.vm().push_execution_context(fetch_client.realm_execution_context());
|
||||
fetch_client.prepare_to_run_callback();
|
||||
prepare_to_run_callback(realm);
|
||||
|
||||
// 5. Let loadingPromise be record.LoadRequestedModules(state).
|
||||
auto& loading_promise = record->load_requested_modules(state);
|
||||
|
@ -995,7 +995,8 @@ void fetch_descendants_of_and_link_a_module_script(JS::Realm& realm,
|
|||
return JS::js_undefined();
|
||||
}));
|
||||
|
||||
fetch_client.clean_up_after_running_callback();
|
||||
clean_up_after_running_callback(realm);
|
||||
|
||||
realm.vm().pop_execution_context();
|
||||
}
|
||||
|
||||
|
|
|
@ -15,14 +15,14 @@ TemporaryExecutionContext::TemporaryExecutionContext(EnvironmentSettingsObject&
|
|||
{
|
||||
prepare_to_run_script(m_environment_settings->realm());
|
||||
if (m_callbacks_enabled == CallbacksEnabled::Yes)
|
||||
m_environment_settings->prepare_to_run_callback();
|
||||
prepare_to_run_callback(m_environment_settings->realm());
|
||||
}
|
||||
|
||||
TemporaryExecutionContext::~TemporaryExecutionContext()
|
||||
{
|
||||
clean_up_after_running_script(m_environment_settings->realm());
|
||||
if (m_callbacks_enabled == CallbacksEnabled::Yes)
|
||||
m_environment_settings->clean_up_after_running_callback();
|
||||
clean_up_after_running_callback(m_environment_settings->realm());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -283,7 +283,7 @@ JS::ThrowCompletionOr<JS::Value> execute_a_function_body(HTML::Window const& win
|
|||
HTML::prepare_to_run_script(realm);
|
||||
|
||||
// 7. Prepare to run a callback with environment settings.
|
||||
environment_settings.prepare_to_run_callback();
|
||||
HTML::prepare_to_run_callback(realm);
|
||||
|
||||
// 8. Let function be the result of calling FunctionCreate, with arguments:
|
||||
// kind
|
||||
|
@ -304,7 +304,7 @@ JS::ThrowCompletionOr<JS::Value> execute_a_function_body(HTML::Window const& win
|
|||
auto completion = function->internal_call(&window, parameters);
|
||||
|
||||
// 10. Clean up after running a callback with environment settings.
|
||||
environment_settings.clean_up_after_running_callback();
|
||||
HTML::clean_up_after_running_callback(realm);
|
||||
|
||||
// 11. Clean up after running a script with realm.
|
||||
HTML::clean_up_after_running_script(realm);
|
||||
|
|
|
@ -103,17 +103,15 @@ ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source)
|
|||
|
||||
// https://webidl.spec.whatwg.org/#call-user-object-operation-return
|
||||
// https://whatpr.org/webidl/1437.html#call-user-object-operation-return
|
||||
inline JS::Completion clean_up_on_return(HTML::EnvironmentSettingsObject& stored_settings, HTML::EnvironmentSettingsObject& relevant_settings, JS::Completion& completion, OperationReturnsPromise operation_returns_promise)
|
||||
inline JS::Completion clean_up_on_return(JS::Realm& stored_realm, JS::Realm& relevant_realm, JS::Completion& completion, OperationReturnsPromise operation_returns_promise)
|
||||
{
|
||||
auto& realm = stored_settings.realm();
|
||||
|
||||
// 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();
|
||||
// 1. Clean up after running a callback with stored realm.
|
||||
HTML::clean_up_after_running_callback(stored_realm);
|
||||
|
||||
// 2. Clean up after running script with relevant realm.
|
||||
HTML::clean_up_after_running_script(relevant_settings.realm());
|
||||
HTML::clean_up_after_running_script(relevant_realm);
|
||||
|
||||
// 3. If completion is a normal completion, return completion.
|
||||
if (completion.type() == JS::Completion::Type::Normal)
|
||||
|
@ -124,7 +122,7 @@ inline JS::Completion clean_up_on_return(HTML::EnvironmentSettingsObject& stored
|
|||
return completion;
|
||||
|
||||
// 5. Let rejectedPromise be ! Call(%Promise.reject%, %Promise%, «completion.[[Value]]»).
|
||||
auto rejected_promise = create_rejected_promise(realm, *completion.release_value());
|
||||
auto rejected_promise = create_rejected_promise(relevant_realm, *completion.release_value());
|
||||
|
||||
// 6. Return the result of converting rejectedPromise to the operation’s return type.
|
||||
// Note: The operation must return a promise, so no conversion is necessary
|
||||
|
@ -148,22 +146,20 @@ JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, String
|
|||
// 4. Let relevant realm be O’s associated Realm.
|
||||
auto& relevant_realm = object->shape().realm();
|
||||
|
||||
// 5. Let relevant settings be relvant realm’s settings object.
|
||||
auto& relevant_settings = Bindings::host_defined_environment_settings_object(relevant_realm);
|
||||
// FIXME: We should get the realm directly from the callback context.
|
||||
// 5. Let stored realm be value’s callback context.
|
||||
auto& stored_realm = callback.callback_context->realm();
|
||||
|
||||
// 6. Let stored settings be value’s callback context.
|
||||
auto& stored_settings = callback.callback_context;
|
||||
|
||||
// 7. Prepare to run script with relevant realm.
|
||||
// 6. Prepare to run script with relevant realm.
|
||||
HTML::prepare_to_run_script(relevant_realm);
|
||||
|
||||
// 8. Prepare to run a callback with stored settings.
|
||||
stored_settings->prepare_to_run_callback();
|
||||
// 7. Prepare to run a callback with stored realm.
|
||||
HTML::prepare_to_run_callback(stored_realm);
|
||||
|
||||
// 9. Let X be O.
|
||||
// 8. Let X be O.
|
||||
auto actual_function_object = object;
|
||||
|
||||
// 10. If ! IsCallable(O) is false, then:
|
||||
// 9. If ! IsCallable(O) is false, then:
|
||||
if (!object->is_function()) {
|
||||
// 1. Let getResult be Get(O, opName).
|
||||
auto get_result = object->get(operation_name.to_byte_string());
|
||||
|
@ -171,13 +167,13 @@ JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, String
|
|||
// 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, callback.operation_returns_promise);
|
||||
return clean_up_on_return(stored_realm, relevant_realm, completion, callback.operation_returns_promise);
|
||||
}
|
||||
|
||||
// 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 = relevant_realm.vm().template throw_completion<JS::TypeError>(JS::ErrorType::NotAFunction, get_result.value().to_string_without_side_effects());
|
||||
return clean_up_on_return(stored_settings, relevant_settings, completion, callback.operation_returns_promise);
|
||||
return clean_up_on_return(stored_realm, relevant_realm, completion, callback.operation_returns_promise);
|
||||
}
|
||||
|
||||
// 3. Set X to getResult.[[Value]].
|
||||
|
@ -188,28 +184,29 @@ JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, String
|
|||
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.
|
||||
// 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.
|
||||
|
||||
// 12. Let callResult be Call(X, thisArg, esArgs).
|
||||
// 11. 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(), args.span());
|
||||
|
||||
// 13. If callResult is an abrupt completion, set completion to callResult and jump to the step labeled return.
|
||||
// 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, callback.operation_returns_promise);
|
||||
return clean_up_on_return(stored_realm, relevant_realm, completion, callback.operation_returns_promise);
|
||||
}
|
||||
|
||||
// 14. Set completion to the result of converting callResult.[[Value]] to an IDL value of the same type as the operation’s return type.
|
||||
// 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, callback.operation_returns_promise);
|
||||
return clean_up_on_return(stored_realm, relevant_realm, completion, callback.operation_returns_promise);
|
||||
}
|
||||
|
||||
// https://webidl.spec.whatwg.org/#invoke-a-callback-function
|
||||
// https://whatpr.org/webidl/1437.html#invoke-a-callback-function
|
||||
JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Value> this_argument, JS::MarkedVector<JS::Value> args)
|
||||
{
|
||||
// 1. Let completion be an uninitialized variable.
|
||||
|
@ -231,21 +228,18 @@ JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Valu
|
|||
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& realm = function_object->shape().realm();
|
||||
// 5. Let relevant realm be F’s associated Realm.
|
||||
auto& relevant_realm = function_object->shape().realm();
|
||||
|
||||
// 6. Let relevant settings be realm’s settings object.
|
||||
auto& relevant_settings = Bindings::host_defined_environment_settings_object(realm);
|
||||
// FIXME: We should get the realm directly from the callback context.
|
||||
// 6. Let stored realm be value’s callback context.
|
||||
auto& stored_realm = callback.callback_context->realm();
|
||||
|
||||
// 7. Let stored settings be value’s callback context.
|
||||
auto& stored_settings = callback.callback_context;
|
||||
// 8. Prepare to run script with relevant realm.
|
||||
HTML::prepare_to_run_script(relevant_realm);
|
||||
|
||||
// 8. Prepare to run script with relevant settings.
|
||||
HTML::prepare_to_run_script(realm);
|
||||
|
||||
// 9. Prepare to run a callback with stored settings.
|
||||
stored_settings->prepare_to_run_callback();
|
||||
// 9. Prepare to run a callback with stored realm.
|
||||
HTML::prepare_to_run_callback(stored_realm);
|
||||
|
||||
// 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.
|
||||
|
@ -257,14 +251,14 @@ JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Valu
|
|||
// 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, callback.operation_returns_promise);
|
||||
return clean_up_on_return(stored_realm, relevant_realm, completion, callback.operation_returns_promise);
|
||||
}
|
||||
|
||||
// 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, callback.operation_returns_promise);
|
||||
return clean_up_on_return(stored_realm, relevant_realm, completion, callback.operation_returns_promise);
|
||||
}
|
||||
|
||||
JS::Completion construct(WebIDL::CallbackType& callback, JS::MarkedVector<JS::Value> args)
|
||||
|
@ -275,49 +269,46 @@ JS::Completion construct(WebIDL::CallbackType& callback, JS::MarkedVector<JS::Va
|
|||
// 2. Let F be the ECMAScript object corresponding to callable.
|
||||
auto& function_object = callback.callback;
|
||||
|
||||
// 4. Let realm be F’s associated Realm.
|
||||
auto& realm = function_object->shape().realm();
|
||||
// 4. Let relevant realm be F’s associated Realm.
|
||||
auto& relevant_realm = function_object->shape().realm();
|
||||
|
||||
// 3. If IsConstructor(F) is false, throw a TypeError exception.
|
||||
if (!JS::Value(function_object).is_constructor())
|
||||
return realm.vm().template throw_completion<JS::TypeError>(JS::ErrorType::NotAConstructor, JS::Value(function_object).to_string_without_side_effects());
|
||||
return relevant_realm.vm().template throw_completion<JS::TypeError>(JS::ErrorType::NotAConstructor, JS::Value(function_object).to_string_without_side_effects());
|
||||
|
||||
// 5. Let relevant settings be realm’s settings object.
|
||||
// NOTE: Not needed after ShadowRealm implementation.
|
||||
// FIXME: We should get the realm directly from the callback context.
|
||||
// 4. Let stored realm be callable’s callback context.
|
||||
auto& stored_realm = callback.callback_context->realm();
|
||||
|
||||
// 6. Let stored settings be callable’s callback context.
|
||||
auto& stored_settings = callback.callback_context;
|
||||
// 5. Prepare to run script with relevant realm.
|
||||
HTML::prepare_to_run_script(relevant_realm);
|
||||
|
||||
// 7. Prepare to run script with relevant settings.
|
||||
HTML::prepare_to_run_script(realm);
|
||||
// 6. Prepare to run a callback with stored realm.
|
||||
HTML::prepare_to_run_callback(stored_realm);
|
||||
|
||||
// 8. Prepare to run a callback with stored settings.
|
||||
stored_settings->prepare_to_run_callback();
|
||||
|
||||
// FIXME: 9. 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.
|
||||
// FIXME: 7. 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.
|
||||
|
||||
// 10. Let callResult be Completion(Construct(F, esArgs)).
|
||||
// 8. Let callResult be Completion(Construct(F, esArgs)).
|
||||
auto& vm = function_object->vm();
|
||||
auto call_result = JS::construct(vm, verify_cast<JS::FunctionObject>(*function_object), args.span());
|
||||
|
||||
// 11. If callResult is an abrupt completion, set completion to callResult and jump to the step labeled return.
|
||||
// 9. 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();
|
||||
}
|
||||
|
||||
// 12. Set completion to the result of converting callResult.[[Value]] to an IDL value of the same type as the operation’s return type.
|
||||
// 10. Set completion to the result of converting callResult.[[Value]] to an IDL value of the same type as the operation’s return type.
|
||||
else {
|
||||
// FIXME: This does no conversion.
|
||||
completion = JS::Value(call_result.value());
|
||||
}
|
||||
|
||||
// 13. 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();
|
||||
// 11. Return: at this point completion will be set to an ECMAScript completion value.
|
||||
// 1. Clean up after running a callback with stored realm.
|
||||
HTML::clean_up_after_running_callback(stored_realm);
|
||||
|
||||
// 2. Clean up after running script with relevant realm.
|
||||
HTML::clean_up_after_running_script(realm);
|
||||
HTML::clean_up_after_running_script(relevant_realm);
|
||||
|
||||
// 3. Return completion.
|
||||
return completion;
|
||||
|
|
Loading…
Reference in a new issue