Op.h 47 KB

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