Op.h 49 KB

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