2020-03-07 19:42:11 +01:00
/*
* Copyright ( c ) 2020 , Andreas Kling < kling @ serenityos . org >
2021-05-13 23:58:45 +01:00
* Copyright ( c ) 2020 - 2021 , Linus Groh < linusg @ serenityos . org >
2022-01-16 13:16:04 +01:00
* Copyright ( c ) 2022 , Luke Wilde < lukew @ serenityos . org >
2020-03-07 19:42:11 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-03-07 19:42:11 +01:00
*/
2021-05-13 23:58:45 +01:00
# include <AK/ScopeGuard.h>
2020-03-07 19:42:11 +01:00
# include <LibJS/AST.h>
# include <LibJS/Interpreter.h>
2021-09-22 12:44:56 +02:00
# include <LibJS/Runtime/AbstractOperations.h>
2021-09-24 22:40:38 +02:00
# include <LibJS/Runtime/ECMAScriptFunctionObject.h>
2021-07-01 12:24:46 +02:00
# include <LibJS/Runtime/FunctionEnvironment.h>
# include <LibJS/Runtime/GlobalEnvironment.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Runtime/GlobalObject.h>
2020-04-27 12:37:27 +02:00
# include <LibJS/Runtime/Reference.h>
2020-04-02 19:32:21 +02:00
# include <LibJS/Runtime/Shape.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Runtime/Value.h>
2020-03-07 19:42:11 +01:00
namespace JS {
2021-09-12 12:33:54 +01:00
NonnullOwnPtr < Interpreter > Interpreter : : create_with_existing_realm ( Realm & realm )
2020-09-27 17:24:14 +02:00
{
2021-09-12 12:33:54 +01:00
auto & global_object = realm . global_object ( ) ;
2020-09-27 17:24:14 +02:00
DeferGC defer_gc ( global_object . heap ( ) ) ;
auto interpreter = adopt_own ( * new Interpreter ( global_object . vm ( ) ) ) ;
2021-09-11 19:36:25 +01:00
interpreter - > m_global_object = make_handle ( & global_object ) ;
2021-09-12 12:33:54 +01:00
interpreter - > m_realm = make_handle ( & realm ) ;
2020-09-27 17:24:14 +02:00
return interpreter ;
}
2020-09-20 19:24:44 +02:00
Interpreter : : Interpreter ( VM & vm )
: m_vm ( vm )
2022-01-16 13:16:04 +01:00
, m_global_execution_context ( vm . heap ( ) )
2020-03-07 19:42:11 +01:00
{
}
2022-01-16 13:16:04 +01:00
// 16.1.6 ScriptEvaluation ( scriptRecord ), https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation
ThrowCompletionOr < Value > Interpreter : : run ( Script & script_record )
2020-03-07 19:42:11 +01:00
{
2020-11-08 12:54:52 +00:00
auto & vm = this - > vm ( ) ;
2020-04-13 16:42:54 +02:00
2020-09-27 15:18:55 +02:00
VM : : InterpreterExecutionScope scope ( * this ) ;
2020-03-09 21:13:55 +01:00
2022-01-16 13:16:04 +01:00
// 1. Let globalEnv be scriptRecord.[[Realm]].[[GlobalEnv]].
auto & global_environment = script_record . realm ( ) . global_environment ( ) ;
// NOTE: This isn't in the spec but we require it.
auto & global_object = script_record . realm ( ) . global_object ( ) ;
// 2. Let scriptContext be a new ECMAScript code execution context.
ExecutionContext script_context ( vm . heap ( ) ) ;
// 3. Set the Function of scriptContext to null. (This was done in the construction of script_context)
// 4. Set the Realm of scriptContext to scriptRecord.[[Realm]].
script_context . realm = & script_record . realm ( ) ;
2022-01-17 14:48:22 +01:00
// 5. Set the ScriptOrModule of scriptContext to scriptRecord.
2022-02-07 16:28:39 +01:00
script_context . script_or_module = script_record . make_weak_ptr ( ) ;
2022-01-16 13:16:04 +01:00
// 6. Set the VariableEnvironment of scriptContext to globalEnv.
script_context . variable_environment = & global_environment ;
// 7. Set the LexicalEnvironment of scriptContext to globalEnv.
script_context . lexical_environment = & global_environment ;
// 8. Set the PrivateEnvironment of scriptContext to null.
// NOTE: This isn't in the spec, but we require it.
script_context . is_strict_mode = script_record . parse_node ( ) . is_strict_mode ( ) ;
// FIXME: 9. Suspend the currently running execution context.
// 10. Push scriptContext onto the execution context stack; scriptContext is now the running execution context.
2022-03-17 22:42:41 +00:00
TRY ( vm . push_execution_context ( script_context , global_object ) ) ;
2022-01-16 13:16:04 +01:00
// 11. Let scriptBody be scriptRecord.[[ECMAScriptCode]].
auto & script_body = script_record . parse_node ( ) ;
// 12. Let result be GlobalDeclarationInstantiation(scriptBody, globalEnv).
auto instantiation_result = script_body . global_declaration_instantiation ( * this , global_object , global_environment ) ;
Completion result = instantiation_result . is_throw_completion ( ) ? instantiation_result . throw_completion ( ) : normal_completion ( { } ) ;
// 13. If result.[[Type]] is normal, then
if ( result . type ( ) = = Completion : : Type : : Normal ) {
// a. Set result to the result of evaluating scriptBody.
result = script_body . execute ( * this , global_object ) ;
}
// 14. If result.[[Type]] is normal and result.[[Value]] is empty, then
if ( result . type ( ) = = Completion : : Type : : Normal & & ! result . value ( ) . has_value ( ) ) {
// a. Set result to NormalCompletion(undefined).
result = normal_completion ( js_undefined ( ) ) ;
}
// FIXME: 15. Suspend scriptContext and remove it from the execution context stack.
vm . pop_execution_context ( ) ;
// 16. Assert: The execution context stack is not empty.
VERIFY ( ! vm . execution_context_stack ( ) . is_empty ( ) ) ;
// FIXME: 17. Resume the context that is now on the top of the execution context stack as the running execution context.
2021-04-24 18:21:02 +02:00
// At this point we may have already run any queued promise jobs via on_call_stack_emptied,
// in which case this is a no-op.
2022-01-16 13:16:04 +01:00
// FIXME: These three should be moved out of Interpreter::run and give the host an option to run these, as it's up to the host when these get run.
// https://tc39.es/ecma262/#sec-jobs for jobs and https://tc39.es/ecma262/#_ref_3508 for ClearKeptObjects
// finish_execution_generation is particularly an issue for LibWeb, as the HTML spec wants to run it specifically after performing a microtask checkpoint.
// The promise and registry cleanup queues don't cause LibWeb an issue, as LibWeb overrides the hooks that push onto these queues.
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
2021-04-01 22:13:29 +02:00
vm . run_queued_promise_jobs ( ) ;
2021-06-12 17:32:54 +03:00
2021-06-15 22:16:17 +03:00
vm . run_queued_finalization_registry_cleanup_jobs ( ) ;
2021-06-12 17:32:54 +03:00
vm . finish_execution_generation ( ) ;
2022-01-08 21:28:27 +01:00
2022-01-16 13:16:04 +01:00
// 18. Return Completion(result).
if ( result . is_abrupt ( ) ) {
VERIFY ( result . type ( ) = = Completion : : Type : : Throw ) ;
return result . release_error ( ) ;
2022-01-08 21:28:27 +01:00
}
2022-01-16 13:16:04 +01:00
VERIFY ( result . value ( ) . has_value ( ) ) ;
return * result . value ( ) ;
}
2022-01-19 01:22:58 +01:00
ThrowCompletionOr < Value > Interpreter : : run ( SourceTextModule & module )
2022-01-16 13:16:04 +01:00
{
2022-01-19 01:22:58 +01:00
// FIXME: This is not a entry point as defined in the spec, but is convenient.
// To avoid work we use link_and_eval_module however that can already be
// dangerous if the vm loaded other modules.
auto & vm = this - > vm ( ) ;
VM : : InterpreterExecutionScope scope ( * this ) ;
2022-02-07 15:12:41 +01:00
TRY ( vm . link_and_eval_module ( { } , module ) ) ;
2022-01-19 01:22:58 +01:00
vm . run_queued_promise_jobs ( ) ;
vm . run_queued_finalization_registry_cleanup_jobs ( ) ;
return js_undefined ( ) ;
2020-03-09 21:13:55 +01:00
}
2020-09-27 15:18:55 +02:00
GlobalObject & Interpreter : : global_object ( )
2020-04-27 12:37:27 +02:00
{
2020-09-27 15:18:55 +02:00
return static_cast < GlobalObject & > ( * m_global_object . cell ( ) ) ;
2020-04-27 12:37:27 +02:00
}
2020-09-27 15:18:55 +02:00
const GlobalObject & Interpreter : : global_object ( ) const
2020-03-09 21:29:22 +01:00
{
2020-09-27 15:18:55 +02:00
return static_cast < const GlobalObject & > ( * m_global_object . cell ( ) ) ;
2020-03-09 21:29:22 +01:00
}
2021-09-11 19:36:25 +01:00
Realm & Interpreter : : realm ( )
{
return static_cast < Realm & > ( * m_realm . cell ( ) ) ;
}
const Realm & Interpreter : : realm ( ) const
{
return static_cast < const Realm & > ( * m_realm . cell ( ) ) ;
}
2020-03-07 19:42:11 +01:00
}