ShadowRealm.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Lexer.h>
  7. #include <LibJS/Parser.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  10. #include <LibJS/Runtime/ShadowRealm.h>
  11. #include <LibJS/Runtime/WrappedFunction.h>
  12. namespace JS {
  13. ShadowRealm::ShadowRealm(Realm& shadow_realm, ExecutionContext execution_context, Object& prototype)
  14. : Object(prototype)
  15. , m_shadow_realm(shadow_realm)
  16. , m_execution_context(move(execution_context))
  17. {
  18. }
  19. void ShadowRealm::visit_edges(Visitor& visitor)
  20. {
  21. Base::visit_edges(visitor);
  22. visitor.visit(&m_shadow_realm);
  23. }
  24. // 3.1.1 PerformShadowRealmEval ( sourceText, callerRealm, evalRealm ), https://tc39.es/proposal-shadowrealm/#sec-performshadowrealmeval
  25. ThrowCompletionOr<Value> perform_shadow_realm_eval(GlobalObject& global_object, StringView source_text, Realm& caller_realm, Realm& eval_realm)
  26. {
  27. auto& vm = global_object.vm();
  28. // 1. Assert: Type(sourceText) is String.
  29. // 2. Assert: callerRealm is a Realm Record.
  30. // 3. Assert: evalRealm is a Realm Record.
  31. // 4. Perform ? HostEnsureCanCompileStrings(callerRealm, evalRealm).
  32. // FIXME: We don't have this host-defined abstract operation yet.
  33. // 5. Perform the following substeps in an implementation-defined order, possibly interleaving parsing and error detection:
  34. // a. Let script be ParseText(! StringToCodePoints(sourceText), Script).
  35. auto parser = Parser(Lexer(source_text));
  36. auto program = parser.parse_program();
  37. // b. If script is a List of errors, throw a SyntaxError exception.
  38. if (parser.has_errors()) {
  39. auto& error = parser.errors()[0];
  40. return vm.throw_completion<JS::SyntaxError>(global_object, error.to_string());
  41. }
  42. // c. If script Contains ScriptBody is false, return undefined.
  43. if (program->children().is_empty())
  44. return js_undefined();
  45. // d. Let body be the ScriptBody of script.
  46. // e. If body Contains NewTarget is true, throw a SyntaxError exception.
  47. // f. If body Contains SuperProperty is true, throw a SyntaxError exception.
  48. // g. If body Contains SuperCall is true, throw a SyntaxError exception.
  49. // FIXME: Implement these, we probably need a generic way of scanning the AST for certain nodes.
  50. // 6. Let strictEval be IsStrict of script.
  51. auto strict_eval = program->is_strict_mode();
  52. // 7. Let runningContext be the running execution context.
  53. // NOTE: This would be unused due to step 11 and is omitted for that reason.
  54. // 8. Let lexEnv be NewDeclarativeEnvironment(evalRealm.[[GlobalEnv]]).
  55. Environment* lexical_environment = new_declarative_environment(eval_realm.global_environment());
  56. // 9. Let varEnv be evalRealm.[[GlobalEnv]].
  57. Environment* variable_environment = &eval_realm.global_environment();
  58. // 10. If strictEval is true, set varEnv to lexEnv.
  59. if (strict_eval)
  60. variable_environment = lexical_environment;
  61. // 11. If runningContext is not already suspended, suspend runningContext.
  62. // NOTE: We don't support this concept yet.
  63. // 12. Let evalContext be a new ECMAScript code execution context.
  64. auto eval_context = ExecutionContext { vm.heap() };
  65. // 13. Set evalContext's Function to null.
  66. eval_context.function = nullptr;
  67. // 14. Set evalContext's Realm to evalRealm.
  68. eval_context.realm = &eval_realm;
  69. // 15. Set evalContext's ScriptOrModule to null.
  70. // FIXME: Our execution context struct currently does not track this item.
  71. // 16. Set evalContext's VariableEnvironment to varEnv.
  72. eval_context.variable_environment = variable_environment;
  73. // 17. Set evalContext's LexicalEnvironment to lexEnv.
  74. eval_context.lexical_environment = lexical_environment;
  75. // Non-standard
  76. eval_context.is_strict_mode = strict_eval;
  77. // 18. Push evalContext onto the execution context stack; evalContext is now the running execution context.
  78. vm.push_execution_context(eval_context, eval_realm.global_object());
  79. // 19. Let result be EvalDeclarationInstantiation(body, varEnv, lexEnv, null, strictEval).
  80. auto eval_result = eval_declaration_instantiation(vm, eval_realm.global_object(), program, variable_environment, lexical_environment, strict_eval);
  81. Completion result;
  82. // 20. If result.[[Type]] is normal, then
  83. if (!eval_result.is_throw_completion()) {
  84. // TODO: Optionally use bytecode interpreter?
  85. // FIXME: We need to use evaluate_statements() here because Program::execute() calls global_declaration_instantiation() when it shouldn't
  86. // a. Set result to the result of evaluating body.
  87. auto result_value = program->evaluate_statements(vm.interpreter(), eval_realm.global_object());
  88. if (auto* exception = vm.exception())
  89. result = throw_completion(exception->value());
  90. else if (!result_value.is_empty())
  91. result = normal_completion(result_value);
  92. else
  93. result = Completion {}; // Normal completion with no value
  94. }
  95. // 21. If result.[[Type]] is normal and result.[[Value]] is empty, then
  96. if (result.type() == Completion::Type::Normal && !result.has_value()) {
  97. // a. Set result to NormalCompletion(undefined).
  98. result = normal_completion(js_undefined());
  99. }
  100. // 22. Suspend evalContext and remove it from the execution context stack.
  101. // NOTE: We don't support this concept yet.
  102. vm.pop_execution_context();
  103. // 23. Resume the context that is now on the top of the execution context stack as the running execution context.
  104. // NOTE: We don't support this concept yet.
  105. // 24. If result.[[Type]] is not normal, throw a TypeError exception.
  106. if (result.type() != Completion::Type::Normal)
  107. return vm.throw_completion<TypeError>(global_object, ErrorType::ShadowRealmEvaluateAbruptCompletion);
  108. // 25. Return ? GetWrappedValue(callerRealm, result.[[Value]]).
  109. return get_wrapped_value(global_object, caller_realm, result.value());
  110. // NOTE: Also see "Editor's Note" in the spec regarding the TypeError above.
  111. }
  112. // 3.1.3 GetWrappedValue ( callerRealm, value ), https://tc39.es/proposal-shadowrealm/#sec-getwrappedvalue
  113. ThrowCompletionOr<Value> get_wrapped_value(GlobalObject& global_object, Realm& caller_realm, Value value)
  114. {
  115. auto& vm = global_object.vm();
  116. // 1. Assert: callerRealm is a Realm Record.
  117. // 2. If Type(value) is Object, then
  118. if (value.is_object()) {
  119. // a. If IsCallable(value) is false, throw a TypeError exception.
  120. if (!value.is_function())
  121. return vm.throw_completion<TypeError>(global_object, ErrorType::ShadowRealmWrappedValueNonFunctionObject, value);
  122. // b. Return ! WrappedFunctionCreate(callerRealm, value).
  123. return { WrappedFunction::create(global_object, caller_realm, value.as_function()) };
  124. }
  125. // 3. Return value.
  126. return value;
  127. }
  128. }