1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-17 15:15:06 +00:00
serenity/Userland/Libraries/LibJS/Runtime/JobCallback.h
Luke Wilde 4c1c6ef91c LibJS: Setup host hooks and have promise jobs work out the realm
This allows the host of LibJS (notably LibWeb in this case) to override
certain functions such as HostEnqueuePromiseJob, so it can do it's own
thing in certain situations. Notably, LibWeb will override
HostEnqueuePromiseJob to put promise jobs on the microtask queue.

This also makes promise jobs use AK::Function instead of
JS::NativeFunction. This removes the need to go through a JavaScript
function and it more closely matches the spec's idea of "abstract
closures"
2022-02-08 17:47:44 +00:00

42 lines
1.3 KiB
C++

/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/FunctionObject.h>
namespace JS {
// 9.5.1 JobCallback Records, https://tc39.es/ecma262/#sec-jobcallback-records
struct JobCallback {
struct CustomData {
virtual ~CustomData() = default;
};
Handle<FunctionObject> callback;
OwnPtr<CustomData> custom_data { nullptr };
};
// 9.5.2 HostMakeJobCallback ( callback ), https://tc39.es/ecma262/#sec-hostmakejobcallback
inline JobCallback make_job_callback(FunctionObject& callback)
{
// 1. Return the JobCallback Record { [[Callback]]: callback, [[HostDefined]]: empty }.
return { make_handle(&callback) };
}
// 9.5.3 HostCallJobCallback ( jobCallback, V, argumentsList ), https://tc39.es/ecma262/#sec-hostcalljobcallback
inline ThrowCompletionOr<Value> call_job_callback(GlobalObject& global_object, JobCallback& job_callback, Value this_value, MarkedValueList arguments_list)
{
// 1. Assert: IsCallable(jobCallback.[[Callback]]) is true.
VERIFY(!job_callback.callback.is_null());
// 2. Return ? Call(jobCallback.[[Callback]], V, argumentsList).
return call(global_object, job_callback.callback.cell(), this_value, move(arguments_list));
}
}