Generator.cpp 24 KB

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