Op.h 49 KB

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