Interpreter.cpp 6.6 KB

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