Interpreter.cpp 5.5 KB

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