Generator.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Copyright (c) 2021-2024, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TemporaryChange.h>
  7. #include <LibJS/AST.h>
  8. #include <LibJS/Bytecode/BasicBlock.h>
  9. #include <LibJS/Bytecode/Generator.h>
  10. #include <LibJS/Bytecode/Instruction.h>
  11. #include <LibJS/Bytecode/Op.h>
  12. #include <LibJS/Bytecode/Register.h>
  13. #include <LibJS/Runtime/VM.h>
  14. namespace JS::Bytecode {
  15. Generator::Generator(VM& vm)
  16. : m_vm(vm)
  17. , m_string_table(make<StringTable>())
  18. , m_identifier_table(make<IdentifierTable>())
  19. , m_regex_table(make<RegexTable>())
  20. , m_constants(vm.heap())
  21. {
  22. }
  23. CodeGenerationErrorOr<NonnullGCPtr<Executable>> Generator::generate(VM& vm, ASTNode const& node, ReadonlySpan<FunctionParameter> parameters, FunctionKind enclosing_function_kind)
  24. {
  25. Generator generator(vm);
  26. for (auto const& parameter : parameters) {
  27. if (auto const* identifier = parameter.binding.get_pointer<NonnullRefPtr<Identifier const>>();
  28. identifier && (*identifier)->is_local()) {
  29. generator.set_local_initialized((*identifier)->local_variable_index());
  30. }
  31. }
  32. generator.switch_to_basic_block(generator.make_block());
  33. SourceLocationScope scope(generator, node);
  34. generator.m_enclosing_function_kind = enclosing_function_kind;
  35. if (generator.is_in_generator_or_async_function()) {
  36. // Immediately yield with no value.
  37. auto& start_block = generator.make_block();
  38. generator.emit<Bytecode::Op::Yield>(Label { start_block }, generator.add_constant(js_undefined()));
  39. generator.switch_to_basic_block(start_block);
  40. // NOTE: This doesn't have to handle received throw/return completions, as GeneratorObject::resume_abrupt
  41. // will not enter the generator from the SuspendedStart state and immediately completes the generator.
  42. }
  43. auto last_value = TRY(node.generate_bytecode(generator));
  44. if (!generator.current_block().is_terminated() && last_value.has_value()) {
  45. generator.emit<Bytecode::Op::End>(last_value.value());
  46. }
  47. if (generator.is_in_generator_or_async_function()) {
  48. // Terminate all unterminated blocks with yield return
  49. for (auto& block : generator.m_root_basic_blocks) {
  50. if (block->is_terminated())
  51. continue;
  52. generator.switch_to_basic_block(*block);
  53. generator.emit<Bytecode::Op::Yield>(nullptr, generator.add_constant(js_undefined()));
  54. }
  55. }
  56. bool is_strict_mode = false;
  57. if (is<Program>(node))
  58. is_strict_mode = static_cast<Program const&>(node).is_strict_mode();
  59. else if (is<FunctionBody>(node))
  60. is_strict_mode = static_cast<FunctionBody const&>(node).in_strict_mode();
  61. else if (is<FunctionDeclaration>(node))
  62. is_strict_mode = static_cast<FunctionDeclaration const&>(node).is_strict_mode();
  63. else if (is<FunctionExpression>(node))
  64. is_strict_mode = static_cast<FunctionExpression const&>(node).is_strict_mode();
  65. auto executable = vm.heap().allocate_without_realm<Executable>(
  66. move(generator.m_identifier_table),
  67. move(generator.m_string_table),
  68. move(generator.m_regex_table),
  69. move(generator.m_constants),
  70. node.source_code(),
  71. generator.m_next_property_lookup_cache,
  72. generator.m_next_global_variable_cache,
  73. generator.m_next_environment_variable_cache,
  74. generator.m_next_register,
  75. move(generator.m_root_basic_blocks),
  76. is_strict_mode);
  77. return executable;
  78. }
  79. void Generator::grow(size_t additional_size)
  80. {
  81. VERIFY(m_current_basic_block);
  82. m_current_basic_block->grow(additional_size);
  83. }
  84. Register Generator::allocate_register()
  85. {
  86. VERIFY(m_next_register != NumericLimits<u32>::max());
  87. return Register { m_next_register++ };
  88. }
  89. Generator::SourceLocationScope::SourceLocationScope(Generator& generator, ASTNode const& node)
  90. : m_generator(generator)
  91. , m_previous_node(m_generator.m_current_ast_node)
  92. {
  93. m_generator.m_current_ast_node = &node;
  94. }
  95. Generator::SourceLocationScope::~SourceLocationScope()
  96. {
  97. m_generator.m_current_ast_node = m_previous_node;
  98. }
  99. Generator::UnwindContext::UnwindContext(Generator& generator, Optional<Label> finalizer)
  100. : m_generator(generator)
  101. , m_finalizer(finalizer)
  102. , m_previous_context(m_generator.m_current_unwind_context)
  103. {
  104. m_generator.m_current_unwind_context = this;
  105. }
  106. Generator::UnwindContext::~UnwindContext()
  107. {
  108. VERIFY(m_generator.m_current_unwind_context == this);
  109. m_generator.m_current_unwind_context = m_previous_context;
  110. }
  111. Label Generator::nearest_continuable_scope() const
  112. {
  113. return m_continuable_scopes.last().bytecode_target;
  114. }
  115. void Generator::block_declaration_instantiation(ScopeNode const& scope_node)
  116. {
  117. start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
  118. emit<Bytecode::Op::BlockDeclarationInstantiation>(scope_node);
  119. }
  120. void Generator::begin_variable_scope()
  121. {
  122. start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
  123. emit<Bytecode::Op::CreateLexicalEnvironment>();
  124. }
  125. void Generator::end_variable_scope()
  126. {
  127. end_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
  128. if (!m_current_basic_block->is_terminated()) {
  129. emit<Bytecode::Op::LeaveLexicalEnvironment>();
  130. }
  131. }
  132. void Generator::begin_continuable_scope(Label continue_target, Vector<DeprecatedFlyString> const& language_label_set)
  133. {
  134. m_continuable_scopes.append({ continue_target, language_label_set });
  135. start_boundary(BlockBoundaryType::Continue);
  136. }
  137. void Generator::end_continuable_scope()
  138. {
  139. m_continuable_scopes.take_last();
  140. end_boundary(BlockBoundaryType::Continue);
  141. }
  142. Label Generator::nearest_breakable_scope() const
  143. {
  144. return m_breakable_scopes.last().bytecode_target;
  145. }
  146. void Generator::begin_breakable_scope(Label breakable_target, Vector<DeprecatedFlyString> const& language_label_set)
  147. {
  148. m_breakable_scopes.append({ breakable_target, language_label_set });
  149. start_boundary(BlockBoundaryType::Break);
  150. }
  151. void Generator::end_breakable_scope()
  152. {
  153. m_breakable_scopes.take_last();
  154. end_boundary(BlockBoundaryType::Break);
  155. }
  156. CodeGenerationErrorOr<Generator::ReferenceOperands> Generator::emit_super_reference(MemberExpression const& expression)
  157. {
  158. VERIFY(is<SuperExpression>(expression.object()));
  159. // https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  160. // 1. Let env be GetThisEnvironment().
  161. // 2. Let actualThis be ? env.GetThisBinding().
  162. auto actual_this = Operand(allocate_register());
  163. emit<Bytecode::Op::ResolveThisBinding>(actual_this);
  164. Optional<Bytecode::Operand> computed_property_value;
  165. if (expression.is_computed()) {
  166. // SuperProperty : super [ Expression ]
  167. // 3. Let propertyNameReference be ? Evaluation of Expression.
  168. // 4. Let propertyNameValue be ? GetValue(propertyNameReference).
  169. computed_property_value = TRY(expression.property().generate_bytecode(*this)).value();
  170. }
  171. // 5/7. Return ? MakeSuperPropertyReference(actualThis, propertyKey, strict).
  172. // https://tc39.es/ecma262/#sec-makesuperpropertyreference
  173. // 1. Let env be GetThisEnvironment().
  174. // 2. Assert: env.HasSuperBinding() is true.
  175. // 3. Let baseValue be ? env.GetSuperBase().
  176. auto base_value = Operand(allocate_register());
  177. emit<Bytecode::Op::ResolveSuperBase>(base_value);
  178. // 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: propertyKey, [[Strict]]: strict, [[ThisValue]]: actualThis }.
  179. return ReferenceOperands {
  180. .base = base_value,
  181. .referenced_name = computed_property_value,
  182. .this_value = actual_this,
  183. };
  184. }
  185. CodeGenerationErrorOr<Generator::ReferenceOperands> Generator::emit_load_from_reference(JS::ASTNode const& node, Optional<Operand> preferred_dst)
  186. {
  187. if (is<Identifier>(node)) {
  188. auto& identifier = static_cast<Identifier const&>(node);
  189. auto loaded_value = TRY(identifier.generate_bytecode(*this, preferred_dst)).value();
  190. return ReferenceOperands {
  191. .loaded_value = loaded_value,
  192. };
  193. }
  194. if (!is<MemberExpression>(node)) {
  195. return CodeGenerationError {
  196. &node,
  197. "Unimplemented/invalid node used as a reference"sv
  198. };
  199. }
  200. auto& expression = static_cast<MemberExpression const&>(node);
  201. // https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  202. if (is<SuperExpression>(expression.object())) {
  203. auto super_reference = TRY(emit_super_reference(expression));
  204. auto dst = preferred_dst.has_value() ? preferred_dst.value() : Operand(allocate_register());
  205. if (super_reference.referenced_name.has_value()) {
  206. // 5. Let propertyKey be ? ToPropertyKey(propertyNameValue).
  207. // FIXME: This does ToPropertyKey out of order, which is observable by Symbol.toPrimitive!
  208. emit<Bytecode::Op::GetByValueWithThis>(dst, *super_reference.base, *super_reference.referenced_name, *super_reference.this_value);
  209. } else {
  210. // 3. Let propertyKey be StringValue of IdentifierName.
  211. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  212. emit_get_by_id_with_this(dst, *super_reference.base, identifier_table_ref, *super_reference.this_value);
  213. }
  214. super_reference.loaded_value = dst;
  215. return super_reference;
  216. }
  217. auto base = TRY(expression.object().generate_bytecode(*this)).value();
  218. if (expression.is_computed()) {
  219. auto property = TRY(expression.property().generate_bytecode(*this)).value();
  220. auto saved_property = Operand(allocate_register());
  221. emit<Bytecode::Op::Mov>(saved_property, property);
  222. auto dst = preferred_dst.has_value() ? preferred_dst.value() : Operand(allocate_register());
  223. emit<Bytecode::Op::GetByValue>(dst, base, property);
  224. return ReferenceOperands {
  225. .base = base,
  226. .referenced_name = saved_property,
  227. .this_value = base,
  228. .loaded_value = dst,
  229. };
  230. }
  231. if (expression.property().is_identifier()) {
  232. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  233. auto dst = preferred_dst.has_value() ? preferred_dst.value() : Operand(allocate_register());
  234. emit_get_by_id(dst, base, identifier_table_ref);
  235. return ReferenceOperands {
  236. .base = base,
  237. .referenced_identifier = identifier_table_ref,
  238. .this_value = base,
  239. .loaded_value = dst,
  240. };
  241. }
  242. if (expression.property().is_private_identifier()) {
  243. auto identifier_table_ref = intern_identifier(verify_cast<PrivateIdentifier>(expression.property()).string());
  244. auto dst = preferred_dst.has_value() ? preferred_dst.value() : Operand(allocate_register());
  245. emit<Bytecode::Op::GetPrivateById>(dst, base, identifier_table_ref);
  246. return ReferenceOperands {
  247. .base = base,
  248. .referenced_private_identifier = identifier_table_ref,
  249. .this_value = base,
  250. .loaded_value = dst,
  251. };
  252. }
  253. return CodeGenerationError {
  254. &expression,
  255. "Unimplemented non-computed member expression"sv
  256. };
  257. }
  258. CodeGenerationErrorOr<void> Generator::emit_store_to_reference(JS::ASTNode const& node, Operand value)
  259. {
  260. if (is<Identifier>(node)) {
  261. auto& identifier = static_cast<Identifier const&>(node);
  262. emit_set_variable(identifier, value);
  263. return {};
  264. }
  265. if (is<MemberExpression>(node)) {
  266. auto& expression = static_cast<MemberExpression const&>(node);
  267. // https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  268. if (is<SuperExpression>(expression.object())) {
  269. auto super_reference = TRY(emit_super_reference(expression));
  270. // 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: propertyKey, [[Strict]]: strict, [[ThisValue]]: actualThis }.
  271. if (super_reference.referenced_name.has_value()) {
  272. // 5. Let propertyKey be ? ToPropertyKey(propertyNameValue).
  273. // FIXME: This does ToPropertyKey out of order, which is observable by Symbol.toPrimitive!
  274. emit<Bytecode::Op::PutByValueWithThis>(*super_reference.base, *super_reference.referenced_name, *super_reference.this_value, value);
  275. } else {
  276. // 3. Let propertyKey be StringValue of IdentifierName.
  277. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  278. emit<Bytecode::Op::PutByIdWithThis>(*super_reference.base, *super_reference.this_value, identifier_table_ref, value, Bytecode::Op::PropertyKind::KeyValue, next_property_lookup_cache());
  279. }
  280. } else {
  281. auto object = TRY(expression.object().generate_bytecode(*this)).value();
  282. if (expression.is_computed()) {
  283. auto property = TRY(expression.property().generate_bytecode(*this)).value();
  284. emit<Bytecode::Op::PutByValue>(object, property, value);
  285. } else if (expression.property().is_identifier()) {
  286. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  287. emit<Bytecode::Op::PutById>(object, identifier_table_ref, value, Bytecode::Op::PropertyKind::KeyValue, next_property_lookup_cache());
  288. } else if (expression.property().is_private_identifier()) {
  289. auto identifier_table_ref = intern_identifier(verify_cast<PrivateIdentifier>(expression.property()).string());
  290. emit<Bytecode::Op::PutPrivateById>(object, identifier_table_ref, value);
  291. } else {
  292. return CodeGenerationError {
  293. &expression,
  294. "Unimplemented non-computed member expression"sv
  295. };
  296. }
  297. }
  298. return {};
  299. }
  300. return CodeGenerationError {
  301. &node,
  302. "Unimplemented/invalid node used a reference"sv
  303. };
  304. }
  305. CodeGenerationErrorOr<void> Generator::emit_store_to_reference(ReferenceOperands const& reference, Operand value)
  306. {
  307. if (reference.referenced_private_identifier.has_value()) {
  308. emit<Bytecode::Op::PutPrivateById>(*reference.base, *reference.referenced_private_identifier, value);
  309. return {};
  310. }
  311. if (reference.referenced_identifier.has_value()) {
  312. if (reference.base == reference.this_value)
  313. emit<Bytecode::Op::PutById>(*reference.base, *reference.referenced_identifier, value, Bytecode::Op::PropertyKind::KeyValue, next_property_lookup_cache());
  314. else
  315. emit<Bytecode::Op::PutByIdWithThis>(*reference.base, *reference.this_value, *reference.referenced_identifier, value, Bytecode::Op::PropertyKind::KeyValue, next_property_lookup_cache());
  316. return {};
  317. }
  318. if (reference.base == reference.this_value)
  319. emit<Bytecode::Op::PutByValue>(*reference.base, *reference.referenced_name, value);
  320. else
  321. emit<Bytecode::Op::PutByValueWithThis>(*reference.base, *reference.referenced_name, *reference.this_value, value);
  322. return {};
  323. }
  324. CodeGenerationErrorOr<Optional<Operand>> Generator::emit_delete_reference(JS::ASTNode const& node)
  325. {
  326. if (is<Identifier>(node)) {
  327. auto& identifier = static_cast<Identifier const&>(node);
  328. if (identifier.is_local()) {
  329. return add_constant(Value(false));
  330. }
  331. auto dst = Operand(allocate_register());
  332. emit<Bytecode::Op::DeleteVariable>(dst, intern_identifier(identifier.string()));
  333. return dst;
  334. }
  335. if (is<MemberExpression>(node)) {
  336. auto& expression = static_cast<MemberExpression const&>(node);
  337. // https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  338. if (is<SuperExpression>(expression.object())) {
  339. auto super_reference = TRY(emit_super_reference(expression));
  340. auto dst = Operand(allocate_register());
  341. if (super_reference.referenced_name.has_value()) {
  342. emit<Bytecode::Op::DeleteByValueWithThis>(dst, *super_reference.base, *super_reference.this_value, *super_reference.referenced_name);
  343. } else {
  344. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  345. emit<Bytecode::Op::DeleteByIdWithThis>(dst, *super_reference.base, *super_reference.this_value, identifier_table_ref);
  346. }
  347. return Optional<Operand> {};
  348. }
  349. auto object = TRY(expression.object().generate_bytecode(*this)).value();
  350. auto dst = Operand(allocate_register());
  351. if (expression.is_computed()) {
  352. auto property = TRY(expression.property().generate_bytecode(*this)).value();
  353. emit<Bytecode::Op::DeleteByValue>(dst, object, property);
  354. } else if (expression.property().is_identifier()) {
  355. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  356. emit<Bytecode::Op::DeleteById>(dst, object, identifier_table_ref);
  357. } else {
  358. // NOTE: Trying to delete a private field generates a SyntaxError in the parser.
  359. return CodeGenerationError {
  360. &expression,
  361. "Unimplemented non-computed member expression"sv
  362. };
  363. }
  364. return dst;
  365. }
  366. // Though this will have no deletion effect, we still have to evaluate the node as it can have side effects.
  367. // For example: delete a(); delete ++c.b; etc.
  368. // 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
  369. // 1. Let ref be the result of evaluating UnaryExpression.
  370. // 2. ReturnIfAbrupt(ref).
  371. (void)TRY(node.generate_bytecode(*this));
  372. // 3. If ref is not a Reference Record, return true.
  373. // NOTE: The rest of the steps are handled by Delete{Variable,ByValue,Id}.
  374. return add_constant(Value(true));
  375. }
  376. void Generator::emit_set_variable(JS::Identifier const& identifier, Operand value, Bytecode::Op::SetVariable::InitializationMode initialization_mode, Bytecode::Op::EnvironmentMode mode)
  377. {
  378. if (identifier.is_local()) {
  379. if (value.is_local() && value.index() == identifier.local_variable_index()) {
  380. // Moving a local to itself is a no-op.
  381. return;
  382. }
  383. emit<Bytecode::Op::SetLocal>(identifier.local_variable_index(), value);
  384. } else {
  385. emit<Bytecode::Op::SetVariable>(intern_identifier(identifier.string()), value, next_environment_variable_cache(), initialization_mode, mode);
  386. }
  387. }
  388. void Generator::generate_scoped_jump(JumpType type)
  389. {
  390. TemporaryChange temp { m_current_unwind_context, m_current_unwind_context };
  391. bool last_was_finally = false;
  392. for (size_t i = m_boundaries.size(); i > 0; --i) {
  393. auto boundary = m_boundaries[i - 1];
  394. using enum BlockBoundaryType;
  395. switch (boundary) {
  396. case Break:
  397. if (type == JumpType::Break) {
  398. emit<Op::Jump>(nearest_breakable_scope());
  399. return;
  400. }
  401. break;
  402. case Continue:
  403. if (type == JumpType::Continue) {
  404. emit<Op::Jump>(nearest_continuable_scope());
  405. return;
  406. }
  407. break;
  408. case Unwind:
  409. VERIFY(last_was_finally || !m_current_unwind_context->finalizer().has_value());
  410. if (!last_was_finally) {
  411. VERIFY(m_current_unwind_context && m_current_unwind_context->handler().has_value());
  412. emit<Bytecode::Op::LeaveUnwindContext>();
  413. m_current_unwind_context = m_current_unwind_context->previous();
  414. }
  415. last_was_finally = false;
  416. break;
  417. case LeaveLexicalEnvironment:
  418. emit<Bytecode::Op::LeaveLexicalEnvironment>();
  419. break;
  420. case ReturnToFinally: {
  421. VERIFY(m_current_unwind_context->finalizer().has_value());
  422. m_current_unwind_context = m_current_unwind_context->previous();
  423. auto jump_type_name = type == JumpType::Break ? "break"sv : "continue"sv;
  424. auto block_name = MUST(String::formatted("{}.{}", current_block().name(), jump_type_name));
  425. auto& block = make_block(block_name);
  426. emit<Op::ScheduleJump>(Label { block });
  427. switch_to_basic_block(block);
  428. last_was_finally = true;
  429. break;
  430. };
  431. }
  432. }
  433. VERIFY_NOT_REACHED();
  434. }
  435. void Generator::generate_labelled_jump(JumpType type, DeprecatedFlyString const& label)
  436. {
  437. TemporaryChange temp { m_current_unwind_context, m_current_unwind_context };
  438. size_t current_boundary = m_boundaries.size();
  439. bool last_was_finally = false;
  440. auto const& jumpable_scopes = type == JumpType::Continue ? m_continuable_scopes : m_breakable_scopes;
  441. for (auto const& jumpable_scope : jumpable_scopes.in_reverse()) {
  442. for (; current_boundary > 0; --current_boundary) {
  443. auto boundary = m_boundaries[current_boundary - 1];
  444. if (boundary == BlockBoundaryType::Unwind) {
  445. VERIFY(last_was_finally || !m_current_unwind_context->finalizer().has_value());
  446. if (!last_was_finally) {
  447. VERIFY(m_current_unwind_context && m_current_unwind_context->handler().has_value());
  448. emit<Bytecode::Op::LeaveUnwindContext>();
  449. m_current_unwind_context = m_current_unwind_context->previous();
  450. }
  451. last_was_finally = false;
  452. } else if (boundary == BlockBoundaryType::LeaveLexicalEnvironment) {
  453. emit<Bytecode::Op::LeaveLexicalEnvironment>();
  454. } else if (boundary == BlockBoundaryType::ReturnToFinally) {
  455. VERIFY(m_current_unwind_context->finalizer().has_value());
  456. m_current_unwind_context = m_current_unwind_context->previous();
  457. auto jump_type_name = type == JumpType::Break ? "break"sv : "continue"sv;
  458. auto block_name = MUST(String::formatted("{}.{}", current_block().name(), jump_type_name));
  459. auto& block = make_block(block_name);
  460. emit<Op::ScheduleJump>(Label { block });
  461. switch_to_basic_block(block);
  462. last_was_finally = true;
  463. } else if ((type == JumpType::Continue && boundary == BlockBoundaryType::Continue) || (type == JumpType::Break && boundary == BlockBoundaryType::Break)) {
  464. // Make sure we don't process this boundary twice if the current jumpable scope doesn't contain the target label.
  465. --current_boundary;
  466. break;
  467. }
  468. }
  469. if (jumpable_scope.language_label_set.contains_slow(label)) {
  470. emit<Op::Jump>(jumpable_scope.bytecode_target);
  471. return;
  472. }
  473. }
  474. // We must have a jumpable scope available that contains the label, as this should be enforced by the parser.
  475. VERIFY_NOT_REACHED();
  476. }
  477. void Generator::generate_break()
  478. {
  479. generate_scoped_jump(JumpType::Break);
  480. }
  481. void Generator::generate_break(DeprecatedFlyString const& break_label)
  482. {
  483. generate_labelled_jump(JumpType::Break, break_label);
  484. }
  485. void Generator::generate_continue()
  486. {
  487. generate_scoped_jump(JumpType::Continue);
  488. }
  489. void Generator::generate_continue(DeprecatedFlyString const& continue_label)
  490. {
  491. generate_labelled_jump(JumpType::Continue, continue_label);
  492. }
  493. void Generator::push_home_object(Operand object)
  494. {
  495. m_home_objects.append(object);
  496. }
  497. void Generator::pop_home_object()
  498. {
  499. m_home_objects.take_last();
  500. }
  501. void Generator::emit_new_function(Operand dst, FunctionExpression const& function_node, Optional<IdentifierTableIndex> lhs_name)
  502. {
  503. if (m_home_objects.is_empty()) {
  504. emit<Op::NewFunction>(dst, function_node, lhs_name);
  505. } else {
  506. emit<Op::NewFunction>(dst, function_node, lhs_name, m_home_objects.last());
  507. }
  508. }
  509. CodeGenerationErrorOr<Optional<Operand>> Generator::emit_named_evaluation_if_anonymous_function(Expression const& expression, Optional<IdentifierTableIndex> lhs_name, Optional<Operand> preferred_dst)
  510. {
  511. if (is<FunctionExpression>(expression)) {
  512. auto const& function_expression = static_cast<FunctionExpression const&>(expression);
  513. if (!function_expression.has_name()) {
  514. return TRY(function_expression.generate_bytecode_with_lhs_name(*this, move(lhs_name), preferred_dst)).value();
  515. }
  516. }
  517. if (is<ClassExpression>(expression)) {
  518. auto const& class_expression = static_cast<ClassExpression const&>(expression);
  519. if (!class_expression.has_name()) {
  520. return TRY(class_expression.generate_bytecode_with_lhs_name(*this, move(lhs_name), preferred_dst)).value();
  521. }
  522. }
  523. return expression.generate_bytecode(*this, preferred_dst);
  524. }
  525. void Generator::emit_get_by_id(Operand dst, Operand base, IdentifierTableIndex id)
  526. {
  527. emit<Op::GetById>(dst, base, id, m_next_property_lookup_cache++);
  528. }
  529. void Generator::emit_get_by_id_with_this(Operand dst, Operand base, IdentifierTableIndex id, Operand this_value)
  530. {
  531. emit<Op::GetByIdWithThis>(dst, base, id, this_value, m_next_property_lookup_cache++);
  532. }
  533. void Generator::emit_iterator_value(Operand dst, Operand result)
  534. {
  535. emit_get_by_id(dst, result, intern_identifier("value"sv));
  536. }
  537. void Generator::emit_iterator_complete(Operand dst, Operand result)
  538. {
  539. emit_get_by_id(dst, result, intern_identifier("done"sv));
  540. }
  541. bool Generator::is_local_initialized(u32 local_index) const
  542. {
  543. return m_initialized_locals.find(local_index) != m_initialized_locals.end();
  544. }
  545. void Generator::set_local_initialized(u32 local_index)
  546. {
  547. m_initialized_locals.set(local_index);
  548. }
  549. }