Interpreter.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/ScopeGuard.h>
  9. #include <LibJS/AST.h>
  10. #include <LibJS/Interpreter.h>
  11. #include <LibJS/Runtime/AbstractOperations.h>
  12. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  13. #include <LibJS/Runtime/FunctionEnvironment.h>
  14. #include <LibJS/Runtime/GlobalEnvironment.h>
  15. #include <LibJS/Runtime/GlobalObject.h>
  16. #include <LibJS/Runtime/Reference.h>
  17. #include <LibJS/Runtime/Shape.h>
  18. #include <LibJS/Runtime/Value.h>
  19. namespace JS {
  20. NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_realm(Realm& realm)
  21. {
  22. auto& vm = realm.vm();
  23. DeferGC defer_gc(vm.heap());
  24. auto interpreter = adopt_own(*new Interpreter(vm));
  25. interpreter->m_realm = make_handle(&realm);
  26. return interpreter;
  27. }
  28. Interpreter::Interpreter(VM& vm)
  29. : m_vm(vm)
  30. {
  31. }
  32. // 16.1.6 ScriptEvaluation ( scriptRecord ), https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation
  33. ThrowCompletionOr<Value> Interpreter::run(Script& script_record, JS::GCPtr<Environment> lexical_environment_override)
  34. {
  35. auto& vm = this->vm();
  36. VM::InterpreterExecutionScope scope(*this);
  37. // 1. Let globalEnv be scriptRecord.[[Realm]].[[GlobalEnv]].
  38. auto& global_environment = script_record.realm().global_environment();
  39. // 2. Let scriptContext be a new ECMAScript code execution context.
  40. ExecutionContext script_context(vm.heap());
  41. // 3. Set the Function of scriptContext to null.
  42. // NOTE: This was done during execution context construction.
  43. // 4. Set the Realm of scriptContext to scriptRecord.[[Realm]].
  44. script_context.realm = &script_record.realm();
  45. // 5. Set the ScriptOrModule of scriptContext to scriptRecord.
  46. script_context.script_or_module = NonnullGCPtr<Script>(script_record);
  47. // 6. Set the VariableEnvironment of scriptContext to globalEnv.
  48. script_context.variable_environment = &global_environment;
  49. // 7. Set the LexicalEnvironment of scriptContext to globalEnv.
  50. script_context.lexical_environment = &global_environment;
  51. // Non-standard: Override the lexical environment if requested.
  52. if (lexical_environment_override)
  53. script_context.lexical_environment = lexical_environment_override;
  54. // 8. Set the PrivateEnvironment of scriptContext to null.
  55. // NOTE: This isn't in the spec, but we require it.
  56. script_context.is_strict_mode = script_record.parse_node().is_strict_mode();
  57. // FIXME: 9. Suspend the currently running execution context.
  58. // 10. Push scriptContext onto the execution context stack; scriptContext is now the running execution context.
  59. TRY(vm.push_execution_context(script_context, {}));
  60. // 11. Let script be scriptRecord.[[ECMAScriptCode]].
  61. auto& script = script_record.parse_node();
  62. // 12. Let result be Completion(GlobalDeclarationInstantiation(script, globalEnv)).
  63. auto instantiation_result = script.global_declaration_instantiation(vm, global_environment);
  64. Completion result = instantiation_result.is_throw_completion() ? instantiation_result.throw_completion() : normal_completion({});
  65. // 13. If result.[[Type]] is normal, then
  66. if (result.type() == Completion::Type::Normal) {
  67. // a. Set result to the result of evaluating script.
  68. result = script.execute(*this);
  69. }
  70. // 14. If result.[[Type]] is normal and result.[[Value]] is empty, then
  71. if (result.type() == Completion::Type::Normal && !result.value().has_value()) {
  72. // a. Set result to NormalCompletion(undefined).
  73. result = normal_completion(js_undefined());
  74. }
  75. // FIXME: 15. Suspend scriptContext and remove it from the execution context stack.
  76. vm.pop_execution_context();
  77. // 16. Assert: The execution context stack is not empty.
  78. VERIFY(!vm.execution_context_stack().is_empty());
  79. // FIXME: 17. Resume the context that is now on the top of the execution context stack as the running execution context.
  80. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  81. // in which case this is a no-op.
  82. // 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.
  83. // https://tc39.es/ecma262/#sec-jobs for jobs and https://tc39.es/ecma262/#_ref_3508 for ClearKeptObjects
  84. // finish_execution_generation is particularly an issue for LibWeb, as the HTML spec wants to run it specifically after performing a microtask checkpoint.
  85. // The promise and registry cleanup queues don't cause LibWeb an issue, as LibWeb overrides the hooks that push onto these queues.
  86. vm.run_queued_promise_jobs();
  87. vm.run_queued_finalization_registry_cleanup_jobs();
  88. vm.finish_execution_generation();
  89. // 18. Return ? result.
  90. if (result.is_abrupt()) {
  91. VERIFY(result.type() == Completion::Type::Throw);
  92. return result.release_error();
  93. }
  94. VERIFY(result.value().has_value());
  95. return *result.value();
  96. }
  97. ThrowCompletionOr<Value> Interpreter::run(SourceTextModule& module)
  98. {
  99. // FIXME: This is not a entry point as defined in the spec, but is convenient.
  100. // To avoid work we use link_and_eval_module however that can already be
  101. // dangerous if the vm loaded other modules.
  102. auto& vm = this->vm();
  103. VM::InterpreterExecutionScope scope(*this);
  104. TRY(vm.link_and_eval_module(Badge<JS::Interpreter> {}, module));
  105. vm.run_queued_promise_jobs();
  106. vm.run_queued_finalization_registry_cleanup_jobs();
  107. return js_undefined();
  108. }
  109. Realm& Interpreter::realm()
  110. {
  111. return static_cast<Realm&>(*m_realm.cell());
  112. }
  113. Realm const& Interpreter::realm() const
  114. {
  115. return static_cast<Realm const&>(*m_realm.cell());
  116. }
  117. }