Generator.cpp 23 KB

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