Op.h 49 KB

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