Interpreter.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // FIXME: 5. Set the ScriptOrModule of scriptContext to scriptRecord.
  53. // 6. Set the VariableEnvironment of scriptContext to globalEnv.
  54. script_context.variable_environment = &global_environment;
  55. // 7. Set the LexicalEnvironment of scriptContext to globalEnv.
  56. script_context.lexical_environment = &global_environment;
  57. // 8. Set the PrivateEnvironment of scriptContext to null.
  58. // NOTE: This isn't in the spec, but we require it.
  59. script_context.is_strict_mode = script_record.parse_node().is_strict_mode();
  60. // FIXME: 9. Suspend the currently running execution context.
  61. // 10. Push scriptContext onto the execution context stack; scriptContext is now the running execution context.
  62. vm.push_execution_context(script_context, global_object);
  63. // 11. Let scriptBody be scriptRecord.[[ECMAScriptCode]].
  64. auto& script_body = script_record.parse_node();
  65. // 12. Let result be GlobalDeclarationInstantiation(scriptBody, globalEnv).
  66. auto instantiation_result = script_body.global_declaration_instantiation(*this, global_object, global_environment);
  67. Completion result = instantiation_result.is_throw_completion() ? instantiation_result.throw_completion() : normal_completion({});
  68. // 13. If result.[[Type]] is normal, then
  69. if (result.type() == Completion::Type::Normal) {
  70. // a. Set result to the result of evaluating scriptBody.
  71. result = script_body.execute(*this, global_object);
  72. }
  73. // 14. If result.[[Type]] is normal and result.[[Value]] is empty, then
  74. if (result.type() == Completion::Type::Normal && !result.value().has_value()) {
  75. // a. Set result to NormalCompletion(undefined).
  76. result = normal_completion(js_undefined());
  77. }
  78. // FIXME: 15. Suspend scriptContext and remove it from the execution context stack.
  79. vm.pop_execution_context();
  80. // 16. Assert: The execution context stack is not empty.
  81. VERIFY(!vm.execution_context_stack().is_empty());
  82. // FIXME: 17. Resume the context that is now on the top of the execution context stack as the running execution context.
  83. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  84. // in which case this is a no-op.
  85. // 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.
  86. // https://tc39.es/ecma262/#sec-jobs for jobs and https://tc39.es/ecma262/#_ref_3508 for ClearKeptObjects
  87. // finish_execution_generation is particularly an issue for LibWeb, as the HTML spec wants to run it specifically after performing a microtask checkpoint.
  88. // The promise and registry cleanup queues don't cause LibWeb an issue, as LibWeb overrides the hooks that push onto these queues.
  89. vm.run_queued_promise_jobs();
  90. vm.run_queued_finalization_registry_cleanup_jobs();
  91. vm.finish_execution_generation();
  92. // 18. Return Completion(result).
  93. if (result.is_abrupt()) {
  94. VERIFY(result.type() == Completion::Type::Throw);
  95. return result.release_error();
  96. }
  97. VERIFY(result.value().has_value());
  98. return *result.value();
  99. }
  100. ThrowCompletionOr<Value> Interpreter::run(SourceTextModule&)
  101. {
  102. auto* error = InternalError::create(global_object(), "Can't run modules directly yet :^(");
  103. vm().throw_exception(global_object(), Value { error });
  104. return throw_completion(error);
  105. }
  106. GlobalObject& Interpreter::global_object()
  107. {
  108. return static_cast<GlobalObject&>(*m_global_object.cell());
  109. }
  110. const GlobalObject& Interpreter::global_object() const
  111. {
  112. return static_cast<const GlobalObject&>(*m_global_object.cell());
  113. }
  114. Realm& Interpreter::realm()
  115. {
  116. return static_cast<Realm&>(*m_realm.cell());
  117. }
  118. const Realm& Interpreter::realm() const
  119. {
  120. return static_cast<const Realm&>(*m_realm.cell());
  121. }
  122. }