2021-10-13 21:08:48 +01:00
/*
2022-01-02 21:37:50 +01:00
* Copyright ( c ) 2021 - 2022 , Linus Groh < linusg @ serenityos . org >
2021-10-13 21:08:48 +01:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2023-06-22 15:59:18 +02:00
# include <LibJS/Bytecode/Executable.h>
# include <LibJS/Bytecode/Interpreter.h>
2021-10-13 21:40:33 +01:00
# include <LibJS/Lexer.h>
# include <LibJS/Parser.h>
# include <LibJS/Runtime/AbstractOperations.h>
# include <LibJS/Runtime/DeclarativeEnvironment.h>
2022-02-07 18:51:58 +01:00
# include <LibJS/Runtime/GlobalEnvironment.h>
2022-01-19 10:26:38 +01:00
# include <LibJS/Runtime/ModuleNamespaceObject.h>
2021-10-15 01:10:05 +01:00
# include <LibJS/Runtime/NativeFunction.h>
2022-10-02 10:59:22 +01:00
# include <LibJS/Runtime/PromiseCapability.h>
2021-10-15 01:10:05 +01:00
# include <LibJS/Runtime/PromiseConstructor.h>
2021-10-13 21:08:48 +01:00
# include <LibJS/Runtime/ShadowRealm.h>
# include <LibJS/Runtime/WrappedFunction.h>
namespace JS {
2023-11-19 09:45:05 +01:00
JS_DEFINE_ALLOCATOR ( ShadowRealm ) ;
2023-11-27 16:45:45 +01:00
ShadowRealm : : ShadowRealm ( Realm & shadow_realm , NonnullOwnPtr < ExecutionContext > execution_context , Object & prototype )
2022-12-14 12:17:58 +01:00
: Object ( ConstructWithPrototypeTag : : Tag , prototype )
2021-10-13 21:09:45 +01:00
, m_shadow_realm ( shadow_realm )
, m_execution_context ( move ( execution_context ) )
{
}
void ShadowRealm : : visit_edges ( Visitor & visitor )
{
Base : : visit_edges ( visitor ) ;
2023-02-26 16:09:02 -07:00
visitor . visit ( m_shadow_realm ) ;
2021-10-13 21:09:45 +01:00
}
2022-02-12 16:06:37 +00:00
// 3.1.2 CopyNameAndLength ( F: a function object, Target: a function object, optional prefix: a String, optional argCount: a Number, ), https://tc39.es/proposal-shadowrealm/#sec-copynameandlength
2022-08-21 17:58:23 +01:00
ThrowCompletionOr < void > copy_name_and_length ( VM & vm , FunctionObject & function , FunctionObject & target , Optional < StringView > prefix , Optional < unsigned > arg_count )
2022-01-24 19:14:04 +00:00
{
// 1. If argCount is undefined, then set argCount to 0.
if ( ! arg_count . has_value ( ) )
arg_count = 0 ;
// 2. Let L be 0.
double length = 0 ;
// 3. Let targetHasLength be ? HasOwnProperty(Target, "length").
auto target_has_length = TRY ( target . has_own_property ( vm . names . length ) ) ;
// 4. If targetHasLength is true, then
if ( target_has_length ) {
// a. Let targetLen be ? Get(Target, "length").
auto target_length = TRY ( target . get ( vm . names . length ) ) ;
// b. If Type(targetLen) is Number, then
if ( target_length . is_number ( ) ) {
// i. If targetLen is +∞𝔽, set L to +∞.
if ( target_length . is_positive_infinity ( ) ) {
length = target_length . as_double ( ) ;
}
// ii. Else if targetLen is -∞𝔽, set L to 0.
else if ( target_length . is_negative_infinity ( ) ) {
length = 0 ;
}
// iii. Else,
else {
// 1. Let targetLenAsInt be ! ToIntegerOrInfinity(targetLen).
2022-08-21 14:00:56 +01:00
auto target_length_as_int = MUST ( target_length . to_integer_or_infinity ( vm ) ) ;
2022-01-24 19:14:04 +00:00
// 2. Assert: targetLenAsInt is finite.
VERIFY ( ! isinf ( target_length_as_int ) ) ;
// 3. Set L to max(targetLenAsInt - argCount, 0).
length = max ( target_length_as_int - * arg_count , 0 ) ;
}
}
}
2022-05-02 20:54:39 +02:00
// 5. Perform SetFunctionLength(F, L).
2022-01-24 19:14:04 +00:00
function . set_function_length ( length ) ;
// 6. Let targetName be ? Get(Target, "name").
auto target_name = TRY ( target . get ( vm . names . name ) ) ;
// 7. If Type(targetName) is not String, set targetName to the empty String.
if ( ! target_name . is_string ( ) )
2023-01-21 10:42:46 -05:00
target_name = PrimitiveString : : create ( vm , String { } ) ;
2022-01-24 19:14:04 +00:00
2022-05-02 20:54:39 +02:00
// 8. Perform SetFunctionName(F, targetName, prefix).
2023-12-16 17:49:34 +03:30
function . set_function_name ( { target_name . as_string ( ) . byte_string ( ) } , move ( prefix ) ) ;
2022-01-24 19:14:04 +00:00
return { } ;
}
2022-01-24 19:22:46 +00:00
// 3.1.3 PerformShadowRealmEval ( sourceText: a String, callerRealm: a Realm Record, evalRealm: a Realm Record, ), https://tc39.es/proposal-shadowrealm/#sec-performshadowrealmeval
2022-08-21 17:58:23 +01:00
ThrowCompletionOr < Value > perform_shadow_realm_eval ( VM & vm , StringView source_text , Realm & caller_realm , Realm & eval_realm )
2021-10-13 21:40:33 +01:00
{
2022-07-31 12:20:17 +02:00
// FIXME: Needs to be updated to latest ECMA-262. See: https://github.com/tc39/proposal-shadowrealm/issues/367
2022-01-24 19:22:46 +00:00
// 1. Perform ? HostEnsureCanCompileStrings(callerRealm, evalRealm).
2022-07-31 12:20:17 +02:00
TRY ( vm . host_ensure_can_compile_strings ( eval_realm ) ) ;
2021-10-13 21:40:33 +01:00
2022-01-24 19:22:46 +00:00
// 2. Perform the following substeps in an implementation-defined order, possibly interleaving parsing and error detection:
2021-10-13 21:40:33 +01:00
2022-05-02 20:54:39 +02:00
// a. Let script be ParseText(StringToCodePoints(sourceText), Script).
2023-07-12 04:02:27 +02:00
auto parser = Parser ( Lexer ( source_text ) , Program : : Type : : Script , Parser : : EvalInitialState { } ) ;
2021-10-13 21:40:33 +01:00
auto program = parser . parse_program ( ) ;
// b. If script is a List of errors, throw a SyntaxError exception.
if ( parser . has_errors ( ) ) {
auto & error = parser . errors ( ) [ 0 ] ;
2024-04-05 00:39:53 +02:00
return vm . throw_completion < SyntaxError > ( error . to_string ( ) ) ;
2021-10-13 21:40:33 +01:00
}
// c. If script Contains ScriptBody is false, return undefined.
if ( program - > children ( ) . is_empty ( ) )
return js_undefined ( ) ;
// d. Let body be the ScriptBody of script.
// e. If body Contains NewTarget is true, throw a SyntaxError exception.
// f. If body Contains SuperProperty is true, throw a SyntaxError exception.
// g. If body Contains SuperCall is true, throw a SyntaxError exception.
// FIXME: Implement these, we probably need a generic way of scanning the AST for certain nodes.
2022-01-24 19:22:46 +00:00
// 3. Let strictEval be IsStrict of script.
2021-10-13 21:40:33 +01:00
auto strict_eval = program - > is_strict_mode ( ) ;
2022-01-24 19:22:46 +00:00
// 4. Let runningContext be the running execution context.
2021-10-13 21:40:33 +01:00
// NOTE: This would be unused due to step 11 and is omitted for that reason.
2022-01-24 19:22:46 +00:00
// 5. Let lexEnv be NewDeclarativeEnvironment(evalRealm.[[GlobalEnv]]).
2022-12-15 19:59:47 +00:00
Environment * lexical_environment = new_declarative_environment ( eval_realm . global_environment ( ) ) . ptr ( ) ;
2021-10-13 21:40:33 +01:00
2022-01-24 19:22:46 +00:00
// 6. Let varEnv be evalRealm.[[GlobalEnv]].
2021-10-13 21:40:33 +01:00
Environment * variable_environment = & eval_realm . global_environment ( ) ;
2022-01-24 19:22:46 +00:00
// 7. If strictEval is true, set varEnv to lexEnv.
2021-10-13 21:40:33 +01:00
if ( strict_eval )
variable_environment = lexical_environment ;
2022-01-24 19:22:46 +00:00
// 8. If runningContext is not already suspended, suspend runningContext.
2021-10-13 21:40:33 +01:00
// NOTE: We don't support this concept yet.
2022-01-24 19:22:46 +00:00
// 9. Let evalContext be a new ECMAScript code execution context.
2024-05-31 15:12:33 +02:00
auto eval_context = ExecutionContext : : create ( ) ;
2021-10-13 21:40:33 +01:00
2022-01-24 19:22:46 +00:00
// 10. Set evalContext's Function to null.
2023-11-27 16:45:45 +01:00
eval_context - > function = nullptr ;
2021-10-13 21:40:33 +01:00
2022-01-24 19:22:46 +00:00
// 11. Set evalContext's Realm to evalRealm.
2023-11-27 16:45:45 +01:00
eval_context - > realm = & eval_realm ;
2021-10-13 21:40:33 +01:00
2022-01-24 19:22:46 +00:00
// 12. Set evalContext's ScriptOrModule to null.
2022-01-17 14:48:22 +01:00
// Note: This is already the default value.
2021-10-13 21:40:33 +01:00
2022-01-24 19:22:46 +00:00
// 13. Set evalContext's VariableEnvironment to varEnv.
2023-11-27 16:45:45 +01:00
eval_context - > variable_environment = variable_environment ;
2021-10-13 21:40:33 +01:00
2022-01-24 19:22:46 +00:00
// 14. Set evalContext's LexicalEnvironment to lexEnv.
2023-11-27 16:45:45 +01:00
eval_context - > lexical_environment = lexical_environment ;
2021-10-13 21:40:33 +01:00
// Non-standard
2023-11-27 16:45:45 +01:00
eval_context - > is_strict_mode = strict_eval ;
2021-10-13 21:40:33 +01:00
2022-01-24 19:22:46 +00:00
// 15. Push evalContext onto the execution context stack; evalContext is now the running execution context.
2023-11-27 16:45:45 +01:00
TRY ( vm . push_execution_context ( * eval_context , { } ) ) ;
2021-10-13 21:40:33 +01:00
2022-05-02 20:54:39 +02:00
// 16. Let result be Completion(EvalDeclarationInstantiation(body, varEnv, lexEnv, null, strictEval)).
2022-08-21 19:24:32 +01:00
auto eval_result = eval_declaration_instantiation ( vm , program , variable_environment , lexical_environment , nullptr , strict_eval ) ;
2021-10-13 21:40:33 +01:00
Completion result ;
2022-01-24 19:22:46 +00:00
// 17. If result.[[Type]] is normal, then
2021-10-13 21:40:33 +01:00
if ( ! eval_result . is_throw_completion ( ) ) {
// a. Set result to the result of evaluating body.
2024-05-05 22:06:55 +02:00
auto maybe_executable = Bytecode : : compile ( vm , program , FunctionKind : : Normal , " ShadowRealmEval " sv ) ;
2023-08-07 19:59:00 +02:00
if ( maybe_executable . is_error ( ) )
result = maybe_executable . release_error ( ) ;
else {
auto executable = maybe_executable . release_value ( ) ;
2024-05-06 06:44:08 +02:00
auto result_and_return_register = vm . bytecode_interpreter ( ) . run_executable ( * executable , { } ) ;
2024-05-01 19:33:49 +02:00
if ( result_and_return_register . value . is_error ( ) ) {
result = result_and_return_register . value . release_error ( ) ;
2023-08-07 19:59:00 +02:00
} else {
// Resulting value is in the accumulator.
2024-05-01 19:33:49 +02:00
result = result_and_return_register . return_register_value . value_or ( js_undefined ( ) ) ;
2023-06-22 16:07:46 +02:00
}
}
2021-10-13 21:40:33 +01:00
}
2022-01-24 19:22:46 +00:00
// 18. If result.[[Type]] is normal and result.[[Value]] is empty, then
2021-12-28 17:42:14 +01:00
if ( result . type ( ) = = Completion : : Type : : Normal & & ! result . value ( ) . has_value ( ) ) {
2021-10-13 21:40:33 +01:00
// a. Set result to NormalCompletion(undefined).
result = normal_completion ( js_undefined ( ) ) ;
}
2022-01-24 19:22:46 +00:00
// 19. Suspend evalContext and remove it from the execution context stack.
2021-10-13 21:40:33 +01:00
// NOTE: We don't support this concept yet.
vm . pop_execution_context ( ) ;
2022-01-24 19:22:46 +00:00
// 20. Resume the context that is now on the top of the execution context stack as the running execution context.
2021-10-13 21:40:33 +01:00
// NOTE: We don't support this concept yet.
2022-01-24 19:22:46 +00:00
// 21. If result.[[Type]] is not normal, throw a TypeError exception.
2021-10-13 21:40:33 +01:00
if ( result . type ( ) ! = Completion : : Type : : Normal )
2022-08-16 20:33:17 +01:00
return vm . throw_completion < TypeError > ( ErrorType : : ShadowRealmEvaluateAbruptCompletion ) ;
2021-10-13 21:40:33 +01:00
2022-01-24 19:22:46 +00:00
// 22. Return ? GetWrappedValue(callerRealm, result.[[Value]]).
2022-08-21 17:58:23 +01:00
return get_wrapped_value ( vm , caller_realm , * result . value ( ) ) ;
2021-10-13 21:40:33 +01:00
// NOTE: Also see "Editor's Note" in the spec regarding the TypeError above.
}
2022-01-24 19:22:46 +00:00
// 3.1.4 ShadowRealmImportValue ( specifierString: a String, exportNameString: a String, callerRealm: a Realm Record, evalRealm: a Realm Record, evalContext: an execution context, ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealmimportvalue
2023-12-16 17:49:34 +03:30
ThrowCompletionOr < Value > shadow_realm_import_value ( VM & vm , ByteString specifier_string , ByteString export_name_string , Realm & caller_realm , Realm & eval_realm , ExecutionContext & eval_context )
2021-10-15 01:10:05 +01:00
{
2022-08-21 20:38:35 +01:00
// FIXME: evalRealm isn't being used anywhere in this AO (spec issue)
( void ) eval_realm ;
2022-08-21 17:58:23 +01:00
auto & realm = * vm . current_realm ( ) ;
2021-10-15 01:10:05 +01:00
2022-01-24 19:22:46 +00:00
// 1. Assert: evalContext is an execution context associated to a ShadowRealm instance's [[ExecutionContext]].
2021-10-15 01:10:05 +01:00
2022-01-24 19:22:46 +00:00
// 2. Let innerCapability be ! NewPromiseCapability(%Promise%).
2022-08-27 00:54:55 +01:00
auto inner_capability = MUST ( new_promise_capability ( vm , realm . intrinsics ( ) . promise_constructor ( ) ) ) ;
2021-10-15 01:10:05 +01:00
2022-01-24 19:22:46 +00:00
// 3. Let runningContext be the running execution context.
// 4. If runningContext is not already suspended, suspend runningContext.
2021-10-15 01:10:05 +01:00
// NOTE: We don't support this concept yet.
2022-01-24 19:22:46 +00:00
// 5. Push evalContext onto the execution context stack; evalContext is now the running execution context.
2022-08-21 20:38:35 +01:00
TRY ( vm . push_execution_context ( eval_context , { } ) ) ;
2021-10-15 01:10:05 +01:00
2023-12-02 16:20:01 +01:00
// 6. Let referrer be the Realm component of evalContext.
2023-12-02 22:56:47 +01:00
auto referrer = JS : : NonnullGCPtr { * eval_context . realm } ;
// 7. Perform HostLoadImportedModule(referrer, specifierString, empty, innerCapability).
vm . host_load_imported_module ( referrer , ModuleRequest { specifier_string } , nullptr , inner_capability ) ;
2021-10-15 01:10:05 +01:00
2022-01-24 19:22:46 +00:00
// 7. Suspend evalContext and remove it from the execution context stack.
2021-10-15 01:10:05 +01:00
// NOTE: We don't support this concept yet.
vm . pop_execution_context ( ) ;
2022-01-24 19:22:46 +00:00
// 8. Resume the context that is now on the top of the execution context stack as the running execution context.
2021-10-15 01:10:05 +01:00
// NOTE: We don't support this concept yet.
2022-01-24 19:22:46 +00:00
// 9. Let steps be the steps of an ExportGetter function as described below.
2022-08-22 11:48:08 +01:00
auto steps = [ string = move ( export_name_string ) ] ( auto & vm ) - > ThrowCompletionOr < Value > {
2022-02-20 17:51:04 +00:00
// 1. Assert: exports is a module namespace exotic object.
VERIFY ( vm . argument ( 0 ) . is_object ( ) ) ;
auto & exports = vm . argument ( 0 ) . as_object ( ) ;
VERIFY ( is < ModuleNamespaceObject > ( exports ) ) ;
// 2. Let f be the active function object.
2023-02-26 16:09:02 -07:00
auto function = vm . running_execution_context ( ) . function ;
2022-02-20 17:51:04 +00:00
// 3. Let string be f.[[ExportNameString]].
// 4. Assert: Type(string) is String.
// 5. Let hasOwn be ? HasOwnProperty(exports, string).
auto has_own = TRY ( exports . has_own_property ( string ) ) ;
// 6. If hasOwn is false, throw a TypeError exception.
if ( ! has_own )
2022-08-16 20:33:17 +01:00
return vm . template throw_completion < TypeError > ( ErrorType : : MissingRequiredProperty , string ) ;
2022-02-20 17:51:04 +00:00
// 7. Let value be ? Get(exports, string).
auto value = TRY ( exports . get ( string ) ) ;
// 8. Let realm be f.[[Realm]].
auto * realm = function - > realm ( ) ;
VERIFY ( realm ) ;
// 9. Return ? GetWrappedValue(realm, value).
2022-08-21 17:58:23 +01:00
return get_wrapped_value ( vm , * realm , value ) ;
2022-02-20 17:51:04 +00:00
} ;
2022-05-02 20:54:39 +02:00
// 10. Let onFulfilled be CreateBuiltinFunction(steps, 1, "", « [[ExportNameString]] », callerRealm).
2022-01-24 19:22:46 +00:00
// 11. Set onFulfilled.[[ExportNameString]] to exportNameString.
2022-12-13 20:49:50 +00:00
auto on_fulfilled = NativeFunction : : create ( realm , move ( steps ) , 1 , " " , & caller_realm ) ;
2021-10-15 01:10:05 +01:00
2022-01-24 19:22:46 +00:00
// 12. Let promiseCapability be ! NewPromiseCapability(%Promise%).
2022-08-27 00:54:55 +01:00
auto promise_capability = MUST ( new_promise_capability ( vm , realm . intrinsics ( ) . promise_constructor ( ) ) ) ;
2021-10-15 01:10:05 +01:00
// NOTE: Even though the spec tells us to use %ThrowTypeError%, it's not observable if we actually do.
// Throw a nicer TypeError forwarding the import error message instead (we know the argument is an Error object).
2022-12-13 20:49:50 +00:00
auto throw_type_error = NativeFunction : : create ( realm , { } , [ ] ( auto & vm ) - > ThrowCompletionOr < Value > {
2023-08-08 19:17:55 +02:00
return vm . template throw_completion < TypeError > ( vm . argument ( 0 ) . as_object ( ) . get_without_side_effects ( vm . names . message ) . as_string ( ) . utf8_string ( ) ) ;
2021-10-15 01:10:05 +01:00
} ) ;
2022-05-02 20:54:39 +02:00
// 13. Return PerformPromiseThen(innerCapability.[[Promise]], onFulfilled, callerRealm.[[Intrinsics]].[[%ThrowTypeError%]], promiseCapability).
2022-10-02 12:11:30 +01:00
return verify_cast < Promise > ( inner_capability - > promise ( ) . ptr ( ) ) - > perform_then ( on_fulfilled , throw_type_error , promise_capability ) ;
2021-10-15 01:10:05 +01:00
}
2022-01-24 19:22:46 +00:00
// 3.1.5 GetWrappedValue ( callerRealm: a Realm Record, value: unknown, ), https://tc39.es/proposal-shadowrealm/#sec-getwrappedvalue
2022-08-21 17:58:23 +01:00
ThrowCompletionOr < Value > get_wrapped_value ( VM & vm , Realm & caller_realm , Value value )
2021-10-13 21:08:48 +01:00
{
2022-08-21 17:58:23 +01:00
auto & realm = * vm . current_realm ( ) ;
2021-10-13 21:08:48 +01:00
2022-01-24 19:22:46 +00:00
// 1. If Type(value) is Object, then
2021-10-13 21:08:48 +01:00
if ( value . is_object ( ) ) {
// a. If IsCallable(value) is false, throw a TypeError exception.
if ( ! value . is_function ( ) )
2022-08-16 20:33:17 +01:00
return vm . throw_completion < TypeError > ( ErrorType : : ShadowRealmWrappedValueNonFunctionObject , value ) ;
2021-10-13 21:08:48 +01:00
2022-01-24 19:14:04 +00:00
// b. Return ? WrappedFunctionCreate(callerRealm, value).
2022-08-16 00:20:49 +01:00
return TRY ( WrappedFunction : : create ( realm , caller_realm , value . as_function ( ) ) ) ;
2021-10-13 21:08:48 +01:00
}
2022-01-24 19:22:46 +00:00
// 2. Return value.
2021-10-13 21:08:48 +01:00
return value ;
}
}