Interpreter.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ScopeGuard.h>
  8. #include <LibJS/AST.h>
  9. #include <LibJS/Interpreter.h>
  10. #include <LibJS/Runtime/AbstractOperations.h>
  11. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  12. #include <LibJS/Runtime/FunctionEnvironment.h>
  13. #include <LibJS/Runtime/GlobalEnvironment.h>
  14. #include <LibJS/Runtime/GlobalObject.h>
  15. #include <LibJS/Runtime/Reference.h>
  16. #include <LibJS/Runtime/Shape.h>
  17. #include <LibJS/Runtime/Value.h>
  18. namespace JS {
  19. NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_realm(Realm& realm)
  20. {
  21. auto& global_object = realm.global_object();
  22. DeferGC defer_gc(global_object.heap());
  23. auto interpreter = adopt_own(*new Interpreter(global_object.vm()));
  24. interpreter->m_global_object = make_handle(&global_object);
  25. interpreter->m_realm = make_handle(&realm);
  26. return interpreter;
  27. }
  28. Interpreter::Interpreter(VM& vm)
  29. : m_vm(vm)
  30. {
  31. }
  32. Interpreter::~Interpreter()
  33. {
  34. }
  35. void Interpreter::run(GlobalObject& global_object, const Program& program)
  36. {
  37. // FIXME: Why does this receive a GlobalObject? Interpreter has one already, and this might not be in sync with the Realm's GlobalObject.
  38. auto& vm = this->vm();
  39. VERIFY(!vm.exception());
  40. VM::InterpreterExecutionScope scope(*this);
  41. vm.set_last_value(Badge<Interpreter> {}, {});
  42. ExecutionContext execution_context(heap());
  43. execution_context.current_node = &program;
  44. execution_context.this_value = &global_object;
  45. static FlyString global_execution_context_name = "(global execution context)";
  46. execution_context.function_name = global_execution_context_name;
  47. execution_context.lexical_environment = &realm().global_environment();
  48. execution_context.variable_environment = &realm().global_environment();
  49. execution_context.realm = &realm();
  50. execution_context.is_strict_mode = program.is_strict_mode();
  51. vm.push_execution_context(execution_context, global_object);
  52. VERIFY(!vm.exception());
  53. auto value = program.execute(*this, global_object);
  54. vm.set_last_value(Badge<Interpreter> {}, value.value_or(js_undefined()));
  55. // FIXME: We unconditionally stop the unwind here this should be done using completions leaving
  56. // the VM in a cleaner state after executing. For example it does still store the exception.
  57. vm.stop_unwind();
  58. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  59. // in which case this is a no-op.
  60. vm.run_queued_promise_jobs();
  61. vm.run_queued_finalization_registry_cleanup_jobs();
  62. vm.pop_execution_context();
  63. vm.finish_execution_generation();
  64. }
  65. GlobalObject& Interpreter::global_object()
  66. {
  67. return static_cast<GlobalObject&>(*m_global_object.cell());
  68. }
  69. const GlobalObject& Interpreter::global_object() const
  70. {
  71. return static_cast<const GlobalObject&>(*m_global_object.cell());
  72. }
  73. Realm& Interpreter::realm()
  74. {
  75. return static_cast<Realm&>(*m_realm.cell());
  76. }
  77. const Realm& Interpreter::realm() const
  78. {
  79. return static_cast<const Realm&>(*m_realm.cell());
  80. }
  81. }