Generator.cpp 24 KB

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