Interpreter.cpp 6.1 KB

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