Op.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /*
  2. * Copyright (c) 2021-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/StdLibExtras.h>
  10. #include <LibCrypto/BigInt/SignedBigInteger.h>
  11. #include <LibJS/Bytecode/IdentifierTable.h>
  12. #include <LibJS/Bytecode/Instruction.h>
  13. #include <LibJS/Bytecode/Label.h>
  14. #include <LibJS/Bytecode/RegexTable.h>
  15. #include <LibJS/Bytecode/Register.h>
  16. #include <LibJS/Bytecode/StringTable.h>
  17. #include <LibJS/Heap/Cell.h>
  18. #include <LibJS/Runtime/Environment.h>
  19. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  20. #include <LibJS/Runtime/Iterator.h>
  21. #include <LibJS/Runtime/Value.h>
  22. #include <LibJS/Runtime/ValueTraits.h>
  23. namespace JS {
  24. class FunctionExpression;
  25. }
  26. namespace JS::Bytecode::Op {
  27. class Load final : public Instruction {
  28. public:
  29. explicit Load(Register src)
  30. : Instruction(Type::Load)
  31. , m_src(src)
  32. {
  33. }
  34. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  35. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  36. private:
  37. Register m_src;
  38. };
  39. class LoadImmediate final : public Instruction {
  40. public:
  41. explicit LoadImmediate(Value value)
  42. : Instruction(Type::LoadImmediate)
  43. , m_value(value)
  44. {
  45. }
  46. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  47. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  48. private:
  49. Value m_value;
  50. };
  51. class Store final : public Instruction {
  52. public:
  53. explicit Store(Register dst)
  54. : Instruction(Type::Store)
  55. , m_dst(dst)
  56. {
  57. }
  58. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  59. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  60. Register dst() const { return m_dst; }
  61. private:
  62. Register m_dst;
  63. };
  64. #define JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  65. O(Add, add) \
  66. O(Sub, sub) \
  67. O(Mul, mul) \
  68. O(Div, div) \
  69. O(Exp, exp) \
  70. O(Mod, mod) \
  71. O(In, in) \
  72. O(InstanceOf, instance_of) \
  73. O(GreaterThan, greater_than) \
  74. O(GreaterThanEquals, greater_than_equals) \
  75. O(LessThan, less_than) \
  76. O(LessThanEquals, less_than_equals) \
  77. O(LooselyInequals, abstract_inequals) \
  78. O(LooselyEquals, abstract_equals) \
  79. O(StrictlyInequals, typed_inequals) \
  80. O(StrictlyEquals, typed_equals) \
  81. O(BitwiseAnd, bitwise_and) \
  82. O(BitwiseOr, bitwise_or) \
  83. O(BitwiseXor, bitwise_xor) \
  84. O(LeftShift, left_shift) \
  85. O(RightShift, right_shift) \
  86. O(UnsignedRightShift, unsigned_right_shift)
  87. #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  88. class OpTitleCase final : public Instruction { \
  89. public: \
  90. explicit OpTitleCase(Register lhs_reg) \
  91. : Instruction(Type::OpTitleCase) \
  92. , m_lhs_reg(lhs_reg) \
  93. { \
  94. } \
  95. \
  96. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  97. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  98. \
  99. private: \
  100. Register m_lhs_reg; \
  101. };
  102. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DECLARE_COMMON_BINARY_OP)
  103. #undef JS_DECLARE_COMMON_BINARY_OP
  104. #define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  105. O(BitwiseNot, bitwise_not) \
  106. O(Not, not_) \
  107. O(UnaryPlus, unary_plus) \
  108. O(UnaryMinus, unary_minus) \
  109. O(Typeof, typeof_)
  110. #define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  111. class OpTitleCase final : public Instruction { \
  112. public: \
  113. OpTitleCase() \
  114. : Instruction(Type::OpTitleCase) \
  115. { \
  116. } \
  117. \
  118. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  119. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  120. };
  121. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
  122. #undef JS_DECLARE_COMMON_UNARY_OP
  123. class NewString final : public Instruction {
  124. public:
  125. explicit NewString(StringTableIndex string)
  126. : Instruction(Type::NewString)
  127. , m_string(string)
  128. {
  129. }
  130. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  131. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  132. private:
  133. StringTableIndex m_string;
  134. };
  135. class NewObject final : public Instruction {
  136. public:
  137. NewObject()
  138. : Instruction(Type::NewObject)
  139. {
  140. }
  141. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  142. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  143. };
  144. class NewRegExp final : public Instruction {
  145. public:
  146. NewRegExp(StringTableIndex source_index, StringTableIndex flags_index, RegexTableIndex regex_index)
  147. : Instruction(Type::NewRegExp)
  148. , m_source_index(source_index)
  149. , m_flags_index(flags_index)
  150. , m_regex_index(regex_index)
  151. {
  152. }
  153. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  154. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  155. private:
  156. StringTableIndex m_source_index;
  157. StringTableIndex m_flags_index;
  158. RegexTableIndex m_regex_index;
  159. };
  160. #define JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(O) \
  161. O(TypeError)
  162. #define JS_DECLARE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  163. class New##ErrorName final : public Instruction { \
  164. public: \
  165. explicit New##ErrorName(StringTableIndex error_string) \
  166. : Instruction(Type::New##ErrorName) \
  167. , m_error_string(error_string) \
  168. { \
  169. } \
  170. \
  171. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  172. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  173. \
  174. private: \
  175. StringTableIndex m_error_string; \
  176. };
  177. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DECLARE_NEW_BUILTIN_ERROR_OP)
  178. #undef JS_DECLARE_NEW_BUILTIN_ERROR_OP
  179. // NOTE: This instruction is variable-width depending on the number of excluded names
  180. class CopyObjectExcludingProperties final : public Instruction {
  181. public:
  182. CopyObjectExcludingProperties(Register from_object, Vector<Register> const& excluded_names)
  183. : Instruction(Type::CopyObjectExcludingProperties)
  184. , m_from_object(from_object)
  185. , m_excluded_names_count(excluded_names.size())
  186. {
  187. for (size_t i = 0; i < m_excluded_names_count; i++)
  188. m_excluded_names[i] = excluded_names[i];
  189. }
  190. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  191. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  192. size_t length_impl() const { return sizeof(*this) + sizeof(Register) * m_excluded_names_count; }
  193. private:
  194. Register m_from_object;
  195. size_t m_excluded_names_count { 0 };
  196. Register m_excluded_names[];
  197. };
  198. class NewBigInt final : public Instruction {
  199. public:
  200. explicit NewBigInt(Crypto::SignedBigInteger bigint)
  201. : Instruction(Type::NewBigInt)
  202. , m_bigint(move(bigint))
  203. {
  204. }
  205. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  206. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  207. private:
  208. Crypto::SignedBigInteger m_bigint;
  209. };
  210. // NOTE: This instruction is variable-width depending on the number of elements!
  211. class NewArray final : public Instruction {
  212. public:
  213. NewArray()
  214. : Instruction(Type::NewArray)
  215. , m_element_count(0)
  216. {
  217. }
  218. explicit NewArray(AK::Array<Register, 2> const& elements_range)
  219. : Instruction(Type::NewArray)
  220. , m_element_count(elements_range[1].index() - elements_range[0].index() + 1)
  221. {
  222. m_elements[0] = elements_range[0];
  223. m_elements[1] = elements_range[1];
  224. }
  225. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  226. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  227. size_t length_impl() const
  228. {
  229. return sizeof(*this) + sizeof(Register) * (m_element_count == 0 ? 0 : 2);
  230. }
  231. Register start() const
  232. {
  233. VERIFY(m_element_count);
  234. return m_elements[0];
  235. }
  236. Register end() const
  237. {
  238. VERIFY(m_element_count);
  239. return m_elements[1];
  240. }
  241. size_t element_count() const { return m_element_count; }
  242. private:
  243. size_t m_element_count { 0 };
  244. Register m_elements[];
  245. };
  246. class Append final : public Instruction {
  247. public:
  248. Append(Register lhs, bool is_spread)
  249. : Instruction(Type::Append)
  250. , m_lhs(lhs)
  251. , m_is_spread(is_spread)
  252. {
  253. }
  254. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  255. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  256. private:
  257. Register m_lhs;
  258. bool m_is_spread = false;
  259. };
  260. class ImportCall final : public Instruction {
  261. public:
  262. ImportCall(Register specifier, Register options)
  263. : Instruction(Type::ImportCall)
  264. , m_specifier(specifier)
  265. , m_options(options)
  266. {
  267. }
  268. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  269. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  270. private:
  271. Register m_specifier;
  272. Register m_options;
  273. };
  274. class IteratorToArray final : public Instruction {
  275. public:
  276. IteratorToArray()
  277. : Instruction(Type::IteratorToArray)
  278. {
  279. }
  280. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  281. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  282. };
  283. class ConcatString final : public Instruction {
  284. public:
  285. explicit ConcatString(Register lhs)
  286. : Instruction(Type::ConcatString)
  287. , m_lhs(lhs)
  288. {
  289. }
  290. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  291. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  292. private:
  293. Register m_lhs;
  294. };
  295. enum class EnvironmentMode {
  296. Lexical,
  297. Var,
  298. };
  299. class CreateLexicalEnvironment final : public Instruction {
  300. public:
  301. explicit CreateLexicalEnvironment()
  302. : Instruction(Type::CreateLexicalEnvironment)
  303. {
  304. }
  305. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  306. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  307. };
  308. class EnterObjectEnvironment final : public Instruction {
  309. public:
  310. explicit EnterObjectEnvironment()
  311. : Instruction(Type::EnterObjectEnvironment)
  312. {
  313. }
  314. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  315. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  316. };
  317. class CreateVariable final : public Instruction {
  318. public:
  319. explicit CreateVariable(IdentifierTableIndex identifier, EnvironmentMode mode, bool is_immutable, bool is_global = false)
  320. : Instruction(Type::CreateVariable)
  321. , m_identifier(identifier)
  322. , m_mode(mode)
  323. , m_is_immutable(is_immutable)
  324. , m_is_global(is_global)
  325. {
  326. }
  327. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  328. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  329. private:
  330. IdentifierTableIndex m_identifier;
  331. EnvironmentMode m_mode;
  332. bool m_is_immutable : 4 { false };
  333. bool m_is_global : 4 { false };
  334. };
  335. class SetVariable final : public Instruction {
  336. public:
  337. enum class InitializationMode {
  338. Initialize,
  339. Set,
  340. InitializeOrSet,
  341. };
  342. explicit SetVariable(IdentifierTableIndex identifier, InitializationMode initialization_mode = InitializationMode::Set, EnvironmentMode mode = EnvironmentMode::Lexical)
  343. : Instruction(Type::SetVariable)
  344. , m_identifier(identifier)
  345. , m_mode(mode)
  346. , m_initialization_mode(initialization_mode)
  347. {
  348. }
  349. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  350. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  351. IdentifierTableIndex identifier() const { return m_identifier; }
  352. private:
  353. IdentifierTableIndex m_identifier;
  354. EnvironmentMode m_mode;
  355. InitializationMode m_initialization_mode { InitializationMode::Set };
  356. };
  357. class SetLocal final : public Instruction {
  358. public:
  359. explicit SetLocal(size_t index)
  360. : Instruction(Type::SetLocal)
  361. , m_index(index)
  362. {
  363. }
  364. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  365. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  366. size_t index() const { return m_index; }
  367. private:
  368. size_t m_index;
  369. };
  370. class GetCalleeAndThisFromEnvironment final : public Instruction {
  371. public:
  372. explicit GetCalleeAndThisFromEnvironment(IdentifierTableIndex identifier, Register callee_reg, Register this_reg)
  373. : Instruction(Type::GetCalleeAndThisFromEnvironment)
  374. , m_identifier(identifier)
  375. , m_callee_reg(callee_reg)
  376. , m_this_reg(this_reg)
  377. {
  378. }
  379. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  380. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  381. IdentifierTableIndex identifier() const { return m_identifier; }
  382. private:
  383. IdentifierTableIndex m_identifier;
  384. Register m_callee_reg;
  385. Register m_this_reg;
  386. Optional<EnvironmentCoordinate> mutable m_cached_environment_coordinate;
  387. };
  388. class GetVariable final : public Instruction {
  389. public:
  390. explicit GetVariable(IdentifierTableIndex identifier)
  391. : Instruction(Type::GetVariable)
  392. , m_identifier(identifier)
  393. {
  394. }
  395. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  396. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  397. IdentifierTableIndex identifier() const { return m_identifier; }
  398. private:
  399. IdentifierTableIndex m_identifier;
  400. Optional<EnvironmentCoordinate> mutable m_cached_environment_coordinate;
  401. };
  402. class GetGlobal final : public Instruction {
  403. public:
  404. explicit GetGlobal(IdentifierTableIndex identifier, u32 cache_index)
  405. : Instruction(Type::GetGlobal)
  406. , m_identifier(identifier)
  407. , m_cache_index(cache_index)
  408. {
  409. }
  410. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  411. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  412. private:
  413. IdentifierTableIndex m_identifier;
  414. u32 m_cache_index { 0 };
  415. };
  416. class GetLocal final : public Instruction {
  417. public:
  418. explicit GetLocal(size_t index)
  419. : Instruction(Type::GetLocal)
  420. , m_index(index)
  421. {
  422. }
  423. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  424. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  425. size_t index() const { return m_index; }
  426. private:
  427. size_t m_index;
  428. };
  429. class DeleteVariable final : public Instruction {
  430. public:
  431. explicit DeleteVariable(IdentifierTableIndex identifier)
  432. : Instruction(Type::DeleteVariable)
  433. , m_identifier(identifier)
  434. {
  435. }
  436. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  437. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  438. IdentifierTableIndex identifier() const { return m_identifier; }
  439. private:
  440. IdentifierTableIndex m_identifier;
  441. };
  442. class GetById final : public Instruction {
  443. public:
  444. GetById(IdentifierTableIndex property, u32 cache_index)
  445. : Instruction(Type::GetById)
  446. , m_property(property)
  447. , m_cache_index(cache_index)
  448. {
  449. }
  450. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  451. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  452. private:
  453. IdentifierTableIndex m_property;
  454. u32 m_cache_index { 0 };
  455. };
  456. class GetByIdWithThis final : public Instruction {
  457. public:
  458. GetByIdWithThis(IdentifierTableIndex property, Register this_value, u32 cache_index)
  459. : Instruction(Type::GetByIdWithThis)
  460. , m_property(property)
  461. , m_this_value(this_value)
  462. , m_cache_index(cache_index)
  463. {
  464. }
  465. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  466. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  467. private:
  468. IdentifierTableIndex m_property;
  469. Register m_this_value;
  470. u32 m_cache_index { 0 };
  471. };
  472. class GetPrivateById final : public Instruction {
  473. public:
  474. explicit GetPrivateById(IdentifierTableIndex property)
  475. : Instruction(Type::GetPrivateById)
  476. , m_property(property)
  477. {
  478. }
  479. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  480. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  481. private:
  482. IdentifierTableIndex m_property;
  483. };
  484. class HasPrivateId final : public Instruction {
  485. public:
  486. explicit HasPrivateId(IdentifierTableIndex property)
  487. : Instruction(Type::HasPrivateId)
  488. , m_property(property)
  489. {
  490. }
  491. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  492. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  493. private:
  494. IdentifierTableIndex m_property;
  495. };
  496. enum class PropertyKind {
  497. Getter,
  498. Setter,
  499. KeyValue,
  500. DirectKeyValue, // Used for Object expressions. Always sets an own property, never calls a setter.
  501. Spread,
  502. ProtoSetter,
  503. };
  504. class PutById final : public Instruction {
  505. public:
  506. explicit PutById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  507. : Instruction(Type::PutById)
  508. , m_base(base)
  509. , m_property(property)
  510. , m_kind(kind)
  511. {
  512. }
  513. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  514. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  515. private:
  516. Register m_base;
  517. IdentifierTableIndex m_property;
  518. PropertyKind m_kind;
  519. };
  520. class PutByIdWithThis final : public Instruction {
  521. public:
  522. PutByIdWithThis(Register base, Register this_value, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  523. : Instruction(Type::PutByIdWithThis)
  524. , m_base(base)
  525. , m_this_value(this_value)
  526. , m_property(property)
  527. , m_kind(kind)
  528. {
  529. }
  530. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  531. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  532. private:
  533. Register m_base;
  534. Register m_this_value;
  535. IdentifierTableIndex m_property;
  536. PropertyKind m_kind;
  537. };
  538. class PutPrivateById final : public Instruction {
  539. public:
  540. explicit PutPrivateById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  541. : Instruction(Type::PutPrivateById)
  542. , m_base(base)
  543. , m_property(property)
  544. , m_kind(kind)
  545. {
  546. }
  547. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  548. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  549. private:
  550. Register m_base;
  551. IdentifierTableIndex m_property;
  552. PropertyKind m_kind;
  553. };
  554. class DeleteById final : public Instruction {
  555. public:
  556. explicit DeleteById(IdentifierTableIndex property)
  557. : Instruction(Type::DeleteById)
  558. , m_property(property)
  559. {
  560. }
  561. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  562. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  563. private:
  564. IdentifierTableIndex m_property;
  565. };
  566. class DeleteByIdWithThis final : public Instruction {
  567. public:
  568. DeleteByIdWithThis(Register this_value, IdentifierTableIndex property)
  569. : Instruction(Type::DeleteByIdWithThis)
  570. , m_this_value(this_value)
  571. , m_property(property)
  572. {
  573. }
  574. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  575. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  576. private:
  577. Register m_this_value;
  578. IdentifierTableIndex m_property;
  579. };
  580. class GetByValue final : public Instruction {
  581. public:
  582. explicit GetByValue(Register base)
  583. : Instruction(Type::GetByValue)
  584. , m_base(base)
  585. {
  586. }
  587. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  588. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  589. private:
  590. Register m_base;
  591. };
  592. class GetByValueWithThis final : public Instruction {
  593. public:
  594. GetByValueWithThis(Register base, Register this_value)
  595. : Instruction(Type::GetByValueWithThis)
  596. , m_base(base)
  597. , m_this_value(this_value)
  598. {
  599. }
  600. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  601. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  602. private:
  603. Register m_base;
  604. Register m_this_value;
  605. };
  606. class PutByValue final : public Instruction {
  607. public:
  608. PutByValue(Register base, Register property, PropertyKind kind = PropertyKind::KeyValue)
  609. : Instruction(Type::PutByValue)
  610. , m_base(base)
  611. , m_property(property)
  612. , m_kind(kind)
  613. {
  614. }
  615. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  616. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  617. private:
  618. Register m_base;
  619. Register m_property;
  620. PropertyKind m_kind;
  621. };
  622. class PutByValueWithThis final : public Instruction {
  623. public:
  624. PutByValueWithThis(Register base, Register property, Register this_value, PropertyKind kind = PropertyKind::KeyValue)
  625. : Instruction(Type::PutByValueWithThis)
  626. , m_base(base)
  627. , m_property(property)
  628. , m_this_value(this_value)
  629. , m_kind(kind)
  630. {
  631. }
  632. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  633. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  634. private:
  635. Register m_base;
  636. Register m_property;
  637. Register m_this_value;
  638. PropertyKind m_kind;
  639. };
  640. class DeleteByValue final : public Instruction {
  641. public:
  642. DeleteByValue(Register base)
  643. : Instruction(Type::DeleteByValue)
  644. , m_base(base)
  645. {
  646. }
  647. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  648. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  649. private:
  650. Register m_base;
  651. };
  652. class DeleteByValueWithThis final : public Instruction {
  653. public:
  654. DeleteByValueWithThis(Register base, Register this_value)
  655. : Instruction(Type::DeleteByValueWithThis)
  656. , m_base(base)
  657. , m_this_value(this_value)
  658. {
  659. }
  660. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  661. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  662. private:
  663. Register m_base;
  664. Register m_this_value;
  665. };
  666. class Jump : public Instruction {
  667. public:
  668. constexpr static bool IsTerminator = true;
  669. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  670. : Instruction(type)
  671. , m_true_target(move(taken_target))
  672. , m_false_target(move(nontaken_target))
  673. {
  674. }
  675. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  676. : Instruction(Type::Jump)
  677. , m_true_target(move(taken_target))
  678. , m_false_target(move(nontaken_target))
  679. {
  680. }
  681. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  682. {
  683. m_true_target = move(true_target);
  684. m_false_target = move(false_target);
  685. }
  686. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  687. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  688. auto& true_target() const { return m_true_target; }
  689. auto& false_target() const { return m_false_target; }
  690. protected:
  691. Optional<Label> m_true_target;
  692. Optional<Label> m_false_target;
  693. };
  694. class JumpConditional final : public Jump {
  695. public:
  696. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  697. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  698. {
  699. }
  700. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  701. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  702. };
  703. class JumpNullish final : public Jump {
  704. public:
  705. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  706. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  707. {
  708. }
  709. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  710. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  711. };
  712. class JumpUndefined final : public Jump {
  713. public:
  714. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  715. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  716. {
  717. }
  718. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  719. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  720. };
  721. enum class CallType {
  722. Call,
  723. Construct,
  724. DirectEval,
  725. };
  726. class Call final : public Instruction {
  727. public:
  728. Call(CallType type, Register callee, Register this_value, Register first_argument, u32 argument_count, Optional<StringTableIndex> expression_string = {})
  729. : Instruction(Type::Call)
  730. , m_callee(callee)
  731. , m_this_value(this_value)
  732. , m_first_argument(first_argument)
  733. , m_argument_count(argument_count)
  734. , m_type(type)
  735. , m_expression_string(expression_string)
  736. {
  737. }
  738. CallType call_type() const { return m_type; }
  739. Register callee() const { return m_callee; }
  740. Register this_value() const { return m_this_value; }
  741. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  742. Register first_argument() const { return m_first_argument; }
  743. u32 argument_count() const { return m_argument_count; }
  744. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  745. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  746. private:
  747. Register m_callee;
  748. Register m_this_value;
  749. Register m_first_argument;
  750. u32 m_argument_count { 0 };
  751. CallType m_type;
  752. Optional<StringTableIndex> m_expression_string;
  753. };
  754. class CallWithArgumentArray final : public Instruction {
  755. public:
  756. CallWithArgumentArray(CallType type, Register callee, Register this_value, Optional<StringTableIndex> expression_string = {})
  757. : Instruction(Type::CallWithArgumentArray)
  758. , m_callee(callee)
  759. , m_this_value(this_value)
  760. , m_type(type)
  761. , m_expression_string(expression_string)
  762. {
  763. }
  764. CallType call_type() const { return m_type; }
  765. Register callee() const { return m_callee; }
  766. Register this_value() const { return m_this_value; }
  767. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  768. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  769. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  770. private:
  771. Register m_callee;
  772. Register m_this_value;
  773. CallType m_type;
  774. Optional<StringTableIndex> m_expression_string;
  775. };
  776. class SuperCallWithArgumentArray : public Instruction {
  777. public:
  778. explicit SuperCallWithArgumentArray(bool is_synthetic)
  779. : Instruction(Type::SuperCallWithArgumentArray)
  780. , m_is_synthetic(is_synthetic)
  781. {
  782. }
  783. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  784. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  785. private:
  786. bool m_is_synthetic;
  787. };
  788. class NewClass final : public Instruction {
  789. public:
  790. explicit NewClass(ClassExpression const& class_expression, Optional<IdentifierTableIndex> lhs_name)
  791. : Instruction(Type::NewClass)
  792. , m_class_expression(class_expression)
  793. , m_lhs_name(lhs_name)
  794. {
  795. }
  796. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  797. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  798. private:
  799. ClassExpression const& m_class_expression;
  800. Optional<IdentifierTableIndex> m_lhs_name;
  801. };
  802. class NewFunction final : public Instruction {
  803. public:
  804. explicit NewFunction(FunctionExpression const& function_node, Optional<IdentifierTableIndex> lhs_name, Optional<Register> home_object = {})
  805. : Instruction(Type::NewFunction)
  806. , m_function_node(function_node)
  807. , m_lhs_name(lhs_name)
  808. , m_home_object(move(home_object))
  809. {
  810. }
  811. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  812. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  813. private:
  814. FunctionExpression const& m_function_node;
  815. Optional<IdentifierTableIndex> m_lhs_name;
  816. Optional<Register> m_home_object;
  817. };
  818. class BlockDeclarationInstantiation final : public Instruction {
  819. public:
  820. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  821. : Instruction(Type::BlockDeclarationInstantiation)
  822. , m_scope_node(scope_node)
  823. {
  824. }
  825. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  826. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  827. private:
  828. ScopeNode const& m_scope_node;
  829. };
  830. class Return final : public Instruction {
  831. public:
  832. constexpr static bool IsTerminator = true;
  833. Return()
  834. : Instruction(Type::Return)
  835. {
  836. }
  837. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  838. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  839. };
  840. class Increment final : public Instruction {
  841. public:
  842. Increment()
  843. : Instruction(Type::Increment)
  844. {
  845. }
  846. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  847. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  848. };
  849. class Decrement final : public Instruction {
  850. public:
  851. Decrement()
  852. : Instruction(Type::Decrement)
  853. {
  854. }
  855. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  856. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  857. };
  858. class ToNumeric final : public Instruction {
  859. public:
  860. ToNumeric()
  861. : Instruction(Type::ToNumeric)
  862. {
  863. }
  864. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  865. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  866. };
  867. class Throw final : public Instruction {
  868. public:
  869. constexpr static bool IsTerminator = true;
  870. Throw()
  871. : Instruction(Type::Throw)
  872. {
  873. }
  874. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  875. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  876. };
  877. class ThrowIfNotObject final : public Instruction {
  878. public:
  879. ThrowIfNotObject()
  880. : Instruction(Type::ThrowIfNotObject)
  881. {
  882. }
  883. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  884. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  885. };
  886. class ThrowIfNullish final : public Instruction {
  887. public:
  888. ThrowIfNullish()
  889. : Instruction(Type::ThrowIfNullish)
  890. {
  891. }
  892. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  893. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  894. };
  895. class EnterUnwindContext final : public Instruction {
  896. public:
  897. constexpr static bool IsTerminator = true;
  898. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  899. : Instruction(Type::EnterUnwindContext)
  900. , m_entry_point(move(entry_point))
  901. , m_handler_target(move(handler_target))
  902. , m_finalizer_target(move(finalizer_target))
  903. {
  904. }
  905. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  906. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  907. auto& entry_point() const { return m_entry_point; }
  908. auto& handler_target() const { return m_handler_target; }
  909. auto& finalizer_target() const { return m_finalizer_target; }
  910. private:
  911. Label m_entry_point;
  912. Optional<Label> m_handler_target;
  913. Optional<Label> m_finalizer_target;
  914. };
  915. class ScheduleJump final : public Instruction {
  916. public:
  917. // Note: We use this instruction to tell the next `finally` block to
  918. // continue execution with a specific break/continue target;
  919. // FIXME: We currently don't clear the interpreter internal flag, when we change
  920. // the control-flow (`break`, `continue`) in a finally-block,
  921. // FIXME: .NET on x86_64 uses a call to the finally instead, which could make this
  922. // easier, at the cost of making control-flow changes (`break`, `continue`, `return`)
  923. // in the finally-block more difficult, but as stated above, those
  924. // aren't handled 100% correctly at the moment anyway
  925. // It might be worth investigating a similar mechanism
  926. constexpr static bool IsTerminator = true;
  927. ScheduleJump(Label target)
  928. : Instruction(Type::ScheduleJump)
  929. , m_target(target)
  930. {
  931. }
  932. Label target() const { return m_target; }
  933. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  934. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  935. private:
  936. Label m_target;
  937. };
  938. class LeaveLexicalEnvironment final : public Instruction {
  939. public:
  940. LeaveLexicalEnvironment()
  941. : Instruction(Type::LeaveLexicalEnvironment)
  942. {
  943. }
  944. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  945. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  946. };
  947. class LeaveUnwindContext final : public Instruction {
  948. public:
  949. LeaveUnwindContext()
  950. : Instruction(Type::LeaveUnwindContext)
  951. {
  952. }
  953. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  954. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  955. };
  956. class ContinuePendingUnwind final : public Instruction {
  957. public:
  958. constexpr static bool IsTerminator = true;
  959. explicit ContinuePendingUnwind(Label resume_target)
  960. : Instruction(Type::ContinuePendingUnwind)
  961. , m_resume_target(resume_target)
  962. {
  963. }
  964. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  965. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  966. auto& resume_target() const { return m_resume_target; }
  967. private:
  968. Label m_resume_target;
  969. };
  970. class Yield final : public Instruction {
  971. public:
  972. constexpr static bool IsTerminator = true;
  973. explicit Yield(Label continuation_label)
  974. : Instruction(Type::Yield)
  975. , m_continuation_label(continuation_label)
  976. {
  977. }
  978. explicit Yield(nullptr_t)
  979. : Instruction(Type::Yield)
  980. {
  981. }
  982. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  983. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  984. auto& continuation() const { return m_continuation_label; }
  985. private:
  986. Optional<Label> m_continuation_label;
  987. };
  988. class Await final : public Instruction {
  989. public:
  990. constexpr static bool IsTerminator = true;
  991. explicit Await(Label continuation_label)
  992. : Instruction(Type::Await)
  993. , m_continuation_label(continuation_label)
  994. {
  995. }
  996. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  997. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  998. auto& continuation() const { return m_continuation_label; }
  999. private:
  1000. Label m_continuation_label;
  1001. };
  1002. class PushDeclarativeEnvironment final : public Instruction {
  1003. public:
  1004. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  1005. : Instruction(Type::PushDeclarativeEnvironment)
  1006. , m_variables(move(variables))
  1007. {
  1008. }
  1009. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1010. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1011. private:
  1012. HashMap<u32, Variable> m_variables;
  1013. };
  1014. class GetIterator final : public Instruction {
  1015. public:
  1016. GetIterator(IteratorHint hint = IteratorHint::Sync)
  1017. : Instruction(Type::GetIterator)
  1018. , m_hint(hint)
  1019. {
  1020. }
  1021. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1022. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1023. private:
  1024. IteratorHint m_hint { IteratorHint::Sync };
  1025. };
  1026. class GetMethod final : public Instruction {
  1027. public:
  1028. GetMethod(IdentifierTableIndex property)
  1029. : Instruction(Type::GetMethod)
  1030. , m_property(property)
  1031. {
  1032. }
  1033. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1034. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1035. private:
  1036. IdentifierTableIndex m_property;
  1037. };
  1038. class GetObjectPropertyIterator final : public Instruction {
  1039. public:
  1040. GetObjectPropertyIterator()
  1041. : Instruction(Type::GetObjectPropertyIterator)
  1042. {
  1043. }
  1044. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1045. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1046. };
  1047. class IteratorClose final : public Instruction {
  1048. public:
  1049. IteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  1050. : Instruction(Type::IteratorClose)
  1051. , m_completion_type(completion_type)
  1052. , m_completion_value(completion_value)
  1053. {
  1054. }
  1055. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1056. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1057. private:
  1058. Completion::Type m_completion_type { Completion::Type::Normal };
  1059. Optional<Value> m_completion_value;
  1060. };
  1061. class AsyncIteratorClose final : public Instruction {
  1062. public:
  1063. AsyncIteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  1064. : Instruction(Type::AsyncIteratorClose)
  1065. , m_completion_type(completion_type)
  1066. , m_completion_value(completion_value)
  1067. {
  1068. }
  1069. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1070. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1071. private:
  1072. Completion::Type m_completion_type { Completion::Type::Normal };
  1073. Optional<Value> m_completion_value;
  1074. };
  1075. class IteratorNext final : public Instruction {
  1076. public:
  1077. IteratorNext()
  1078. : Instruction(Type::IteratorNext)
  1079. {
  1080. }
  1081. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1082. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1083. };
  1084. class IteratorResultDone final : public Instruction {
  1085. public:
  1086. IteratorResultDone()
  1087. : Instruction(Type::IteratorResultDone)
  1088. {
  1089. }
  1090. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1091. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1092. };
  1093. class IteratorResultValue final : public Instruction {
  1094. public:
  1095. IteratorResultValue()
  1096. : Instruction(Type::IteratorResultValue)
  1097. {
  1098. }
  1099. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1100. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1101. };
  1102. class ResolveThisBinding final : public Instruction {
  1103. public:
  1104. explicit ResolveThisBinding()
  1105. : Instruction(Type::ResolveThisBinding)
  1106. {
  1107. }
  1108. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1109. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1110. };
  1111. class ResolveSuperBase final : public Instruction {
  1112. public:
  1113. explicit ResolveSuperBase()
  1114. : Instruction(Type::ResolveSuperBase)
  1115. {
  1116. }
  1117. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1118. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1119. };
  1120. class GetNewTarget final : public Instruction {
  1121. public:
  1122. explicit GetNewTarget()
  1123. : Instruction(Type::GetNewTarget)
  1124. {
  1125. }
  1126. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1127. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1128. };
  1129. class GetImportMeta final : public Instruction {
  1130. public:
  1131. explicit GetImportMeta()
  1132. : Instruction(Type::GetImportMeta)
  1133. {
  1134. }
  1135. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1136. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1137. };
  1138. class TypeofVariable final : public Instruction {
  1139. public:
  1140. explicit TypeofVariable(IdentifierTableIndex identifier)
  1141. : Instruction(Type::TypeofVariable)
  1142. , m_identifier(identifier)
  1143. {
  1144. }
  1145. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1146. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1147. private:
  1148. IdentifierTableIndex m_identifier;
  1149. };
  1150. class TypeofLocal final : public Instruction {
  1151. public:
  1152. explicit TypeofLocal(size_t index)
  1153. : Instruction(Type::TypeofLocal)
  1154. , m_index(index)
  1155. {
  1156. }
  1157. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1158. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1159. private:
  1160. size_t m_index;
  1161. };
  1162. }
  1163. namespace JS::Bytecode {
  1164. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1165. {
  1166. #define __BYTECODE_OP(op) \
  1167. case Instruction::Type::op: \
  1168. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1169. switch (type()) {
  1170. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1171. default:
  1172. VERIFY_NOT_REACHED();
  1173. }
  1174. #undef __BYTECODE_OP
  1175. }
  1176. ALWAYS_INLINE size_t Instruction::length() const
  1177. {
  1178. if (type() == Type::NewArray)
  1179. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  1180. if (type() == Type::CopyObjectExcludingProperties)
  1181. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  1182. #define __BYTECODE_OP(op) \
  1183. case Type::op: \
  1184. return sizeof(Op::op);
  1185. switch (type()) {
  1186. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1187. default:
  1188. VERIFY_NOT_REACHED();
  1189. }
  1190. #undef __BYTECODE_OP
  1191. }
  1192. ALWAYS_INLINE bool Instruction::is_terminator() const
  1193. {
  1194. #define __BYTECODE_OP(op) \
  1195. case Type::op: \
  1196. return Op::op::IsTerminator;
  1197. switch (type()) {
  1198. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1199. default:
  1200. VERIFY_NOT_REACHED();
  1201. }
  1202. #undef __BYTECODE_OP
  1203. }
  1204. }