mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:07:44 +00:00
LibJS: Add initial support for Promises
Almost a year after first working on this, it's finally done: an implementation of Promises for LibJS! :^) The core functionality is working and closely following the spec [1]. I mostly took the pseudo code and transformed it into C++ - if you read and understand it, you will know how the spec implements Promises; and if you read the spec first, the code will look very familiar. Implemented functions are: - Promise() constructor - Promise.prototype.then() - Promise.prototype.catch() - Promise.prototype.finally() - Promise.resolve() - Promise.reject() For the tests I added a new function to test-js's global object, runQueuedPromiseJobs(), which calls vm.run_queued_promise_jobs(). By design, queued jobs normally only run after the script was fully executed, making it improssible to test handlers in individual test() calls by default [2]. Subsequent commits include integrations into LibWeb and js(1) - pretty-printing, running queued promise jobs when necessary. This has an unusual amount of dbgln() statements, all hidden behind the PROMISE_DEBUG flag - I'm leaving them in for now as they've been very useful while debugging this, things can get quite complex with so many asynchronously executed functions. I've not extensively explored use of these APIs for promise-based functionality in LibWeb (fetch(), Notification.requestPermission() etc.), but we'll get there in due time. [1]: https://tc39.es/ecma262/#sec-promise-objects [2]: https://tc39.es/ecma262/#sec-jobs-and-job-queues
This commit is contained in:
parent
563712abce
commit
f418115f1b
32 changed files with 1810 additions and 10 deletions
|
@ -80,6 +80,7 @@ public:
|
|||
private:
|
||||
JS_DECLARE_NATIVE_FUNCTION(is_strict_mode);
|
||||
JS_DECLARE_NATIVE_FUNCTION(can_parse_source);
|
||||
JS_DECLARE_NATIVE_FUNCTION(run_queued_promise_jobs);
|
||||
};
|
||||
|
||||
class TestRunner {
|
||||
|
@ -138,9 +139,11 @@ void TestRunnerGlobalObject::initialize_global_object()
|
|||
static FlyString global_property_name { "global" };
|
||||
static FlyString is_strict_mode_property_name { "isStrictMode" };
|
||||
static FlyString can_parse_source_property_name { "canParseSource" };
|
||||
static FlyString run_queued_promise_jobs_property_name { "runQueuedPromiseJobs" };
|
||||
define_property(global_property_name, this, JS::Attribute::Enumerable);
|
||||
define_native_function(is_strict_mode_property_name, is_strict_mode);
|
||||
define_native_function(can_parse_source_property_name, can_parse_source);
|
||||
define_native_function(run_queued_promise_jobs_property_name, run_queued_promise_jobs);
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(TestRunnerGlobalObject::is_strict_mode)
|
||||
|
@ -158,6 +161,12 @@ JS_DEFINE_NATIVE_FUNCTION(TestRunnerGlobalObject::can_parse_source)
|
|||
return JS::Value(!parser.has_errors());
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(TestRunnerGlobalObject::run_queued_promise_jobs)
|
||||
{
|
||||
vm.run_queued_promise_jobs();
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
static void cleanup_and_exit()
|
||||
{
|
||||
// Clear the taskbar progress.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue