AST.h 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021-2022, David Tuin <davidot@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/DeprecatedFlyString.h>
  10. #include <AK/DeprecatedString.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/OwnPtr.h>
  13. #include <AK/RefPtr.h>
  14. #include <AK/Variant.h>
  15. #include <AK/Vector.h>
  16. #include <LibJS/Bytecode/CodeGenerationError.h>
  17. #include <LibJS/Forward.h>
  18. #include <LibJS/Heap/Handle.h>
  19. #include <LibJS/Runtime/ClassFieldDefinition.h>
  20. #include <LibJS/Runtime/Completion.h>
  21. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  22. #include <LibJS/Runtime/FunctionKind.h>
  23. #include <LibJS/Runtime/ModuleRequest.h>
  24. #include <LibJS/Runtime/PropertyKey.h>
  25. #include <LibJS/Runtime/Reference.h>
  26. #include <LibJS/Runtime/Value.h>
  27. #include <LibJS/SourceRange.h>
  28. #include <LibRegex/Regex.h>
  29. namespace JS {
  30. class Declaration;
  31. class ClassDeclaration;
  32. class FunctionDeclaration;
  33. class Identifier;
  34. class MemberExpression;
  35. class VariableDeclaration;
  36. template<class T, class... Args>
  37. static inline NonnullRefPtr<T>
  38. create_ast_node(SourceRange range, Args&&... args)
  39. {
  40. return adopt_ref(*new T(move(range), forward<Args>(args)...));
  41. }
  42. class ASTNode : public RefCounted<ASTNode> {
  43. public:
  44. virtual ~ASTNode() = default;
  45. // NOTE: This is here to stop ASAN complaining about mismatch between new/delete sizes in ASTNodeWithTailArray.
  46. void operator delete(void* ptr) { ::operator delete(ptr); }
  47. virtual Completion execute(Interpreter&) const = 0;
  48. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const;
  49. virtual void dump(int indent) const;
  50. [[nodiscard]] SourceRange source_range() const;
  51. u32 start_offset() const { return m_start_offset; }
  52. u32 end_offset() const { return m_end_offset; }
  53. SourceCode const& source_code() const { return *m_source_code; }
  54. void set_end_offset(Badge<Parser>, u32 end_offset) { m_end_offset = end_offset; }
  55. DeprecatedString class_name() const;
  56. template<typename T>
  57. bool fast_is() const = delete;
  58. virtual bool is_new_expression() const { return false; }
  59. virtual bool is_member_expression() const { return false; }
  60. virtual bool is_super_expression() const { return false; }
  61. virtual bool is_function_expression() const { return false; }
  62. virtual bool is_class_expression() const { return false; }
  63. virtual bool is_expression_statement() const { return false; }
  64. virtual bool is_identifier() const { return false; }
  65. virtual bool is_private_identifier() const { return false; }
  66. virtual bool is_scope_node() const { return false; }
  67. virtual bool is_program() const { return false; }
  68. virtual bool is_class_declaration() const { return false; }
  69. virtual bool is_function_declaration() const { return false; }
  70. virtual bool is_variable_declaration() const { return false; }
  71. virtual bool is_import_call() const { return false; }
  72. virtual bool is_array_expression() const { return false; }
  73. virtual bool is_object_expression() const { return false; }
  74. virtual bool is_string_literal() const { return false; }
  75. virtual bool is_update_expression() const { return false; }
  76. virtual bool is_call_expression() const { return false; }
  77. virtual bool is_labelled_statement() const { return false; }
  78. virtual bool is_iteration_statement() const { return false; }
  79. virtual bool is_class_method() const { return false; }
  80. protected:
  81. explicit ASTNode(SourceRange);
  82. private:
  83. // NOTE: These members are carefully ordered so that `m_start_offset` is packed with the padding after RefCounted::m_ref_count.
  84. // This creates a 4-byte padding hole after `m_end_offset` which is used to pack subclasses better.
  85. u32 m_start_offset { 0 };
  86. RefPtr<SourceCode const> m_source_code;
  87. u32 m_end_offset { 0 };
  88. };
  89. // This is a helper class that packs an array of T after the AST node, all in the same allocation.
  90. template<typename Derived, typename Base, typename T>
  91. class ASTNodeWithTailArray : public Base {
  92. public:
  93. virtual ~ASTNodeWithTailArray() override
  94. {
  95. for (auto& value : tail_span())
  96. value.~T();
  97. }
  98. ReadonlySpan<T> tail_span() const { return { tail_data(), tail_size() }; }
  99. T const* tail_data() const { return reinterpret_cast<T const*>(reinterpret_cast<uintptr_t>(this) + sizeof(Derived)); }
  100. size_t tail_size() const { return m_tail_size; }
  101. protected:
  102. template<typename ActualDerived = Derived, typename... Args>
  103. static NonnullRefPtr<ActualDerived> create(size_t tail_size, SourceRange source_range, Args&&... args)
  104. {
  105. static_assert(sizeof(ActualDerived) == sizeof(Derived), "This leaf class cannot add more members");
  106. static_assert(alignof(ActualDerived) % alignof(T) == 0, "Need padding for tail array");
  107. auto memory = ::operator new(sizeof(ActualDerived) + tail_size * sizeof(T));
  108. return adopt_ref(*::new (memory) ActualDerived(move(source_range), forward<Args>(args)...));
  109. }
  110. ASTNodeWithTailArray(SourceRange source_range, ReadonlySpan<T> values)
  111. : Base(move(source_range))
  112. , m_tail_size(values.size())
  113. {
  114. VERIFY(values.size() <= NumericLimits<u32>::max());
  115. for (size_t i = 0; i < values.size(); ++i)
  116. new (&tail_data()[i]) T(values[i]);
  117. }
  118. private:
  119. T* tail_data() { return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(this) + sizeof(Derived)); }
  120. u32 m_tail_size { 0 };
  121. };
  122. class Statement : public ASTNode {
  123. public:
  124. explicit Statement(SourceRange source_range)
  125. : ASTNode(source_range)
  126. {
  127. }
  128. };
  129. // 14.13 Labelled Statements, https://tc39.es/ecma262/#sec-labelled-statements
  130. class LabelledStatement : public Statement {
  131. public:
  132. LabelledStatement(SourceRange source_range, DeprecatedFlyString label, NonnullRefPtr<Statement const> labelled_item)
  133. : Statement(source_range)
  134. , m_label(move(label))
  135. , m_labelled_item(move(labelled_item))
  136. {
  137. }
  138. virtual Completion execute(Interpreter&) const override;
  139. virtual void dump(int indent) const override;
  140. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  141. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const;
  142. DeprecatedFlyString const& label() const { return m_label; }
  143. DeprecatedFlyString& label() { return m_label; }
  144. NonnullRefPtr<Statement const> const& labelled_item() const { return m_labelled_item; }
  145. private:
  146. virtual bool is_labelled_statement() const final { return true; }
  147. DeprecatedFlyString m_label;
  148. NonnullRefPtr<Statement const> m_labelled_item;
  149. };
  150. class LabelableStatement : public Statement {
  151. public:
  152. using Statement::Statement;
  153. Vector<DeprecatedFlyString> const& labels() const { return m_labels; }
  154. virtual void add_label(DeprecatedFlyString string) { m_labels.append(move(string)); }
  155. protected:
  156. Vector<DeprecatedFlyString> m_labels;
  157. };
  158. class IterationStatement : public Statement {
  159. public:
  160. using Statement::Statement;
  161. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const = 0;
  162. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const;
  163. private:
  164. virtual bool is_iteration_statement() const final { return true; }
  165. };
  166. class EmptyStatement final : public Statement {
  167. public:
  168. explicit EmptyStatement(SourceRange source_range)
  169. : Statement(source_range)
  170. {
  171. }
  172. Completion execute(Interpreter&) const override;
  173. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  174. };
  175. class ErrorStatement final : public Statement {
  176. public:
  177. explicit ErrorStatement(SourceRange source_range)
  178. : Statement(source_range)
  179. {
  180. }
  181. Completion execute(Interpreter&) const override { return {}; }
  182. };
  183. class ExpressionStatement final : public Statement {
  184. public:
  185. ExpressionStatement(SourceRange source_range, NonnullRefPtr<Expression const> expression)
  186. : Statement(source_range)
  187. , m_expression(move(expression))
  188. {
  189. }
  190. virtual Completion execute(Interpreter&) const override;
  191. virtual void dump(int indent) const override;
  192. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  193. Expression const& expression() const { return m_expression; };
  194. private:
  195. virtual bool is_expression_statement() const override { return true; }
  196. NonnullRefPtr<Expression const> m_expression;
  197. };
  198. template<typename Func, typename... Args>
  199. concept ThrowCompletionOrVoidFunction = requires(Func func, Args... args) {
  200. {
  201. func(args...)
  202. }
  203. -> SameAs<ThrowCompletionOr<void>>;
  204. };
  205. template<typename... Args>
  206. class ThrowCompletionOrVoidCallback : public Function<ThrowCompletionOr<void>(Args...)> {
  207. public:
  208. template<typename CallableType>
  209. ThrowCompletionOrVoidCallback(CallableType&& callable)
  210. requires(VoidFunction<CallableType, Args...>)
  211. : Function<ThrowCompletionOr<void>(Args...)>([callable = forward<CallableType>(callable)](Args... args) {
  212. callable(args...);
  213. return ThrowCompletionOr<void> {};
  214. })
  215. {
  216. }
  217. template<typename CallableType>
  218. ThrowCompletionOrVoidCallback(CallableType&& callable)
  219. requires(ThrowCompletionOrVoidFunction<CallableType, Args...>)
  220. : Function<ThrowCompletionOr<void>(Args...)>(forward<CallableType>(callable))
  221. {
  222. }
  223. };
  224. class ScopeNode : public Statement {
  225. public:
  226. template<typename T, typename... Args>
  227. T& append(SourceRange range, Args&&... args)
  228. {
  229. auto child = create_ast_node<T>(range, forward<Args>(args)...);
  230. m_children.append(move(child));
  231. return static_cast<T&>(*m_children.last());
  232. }
  233. void append(NonnullRefPtr<Statement const> child)
  234. {
  235. m_children.append(move(child));
  236. }
  237. void shrink_to_fit()
  238. {
  239. m_children.shrink_to_fit();
  240. m_lexical_declarations.shrink_to_fit();
  241. m_var_declarations.shrink_to_fit();
  242. m_functions_hoistable_with_annexB_extension.shrink_to_fit();
  243. }
  244. Vector<NonnullRefPtr<Statement const>> const& children() const { return m_children; }
  245. virtual void dump(int indent) const override;
  246. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  247. Completion evaluate_statements(Interpreter&) const;
  248. void add_var_scoped_declaration(NonnullRefPtr<Declaration const> variables);
  249. void add_lexical_declaration(NonnullRefPtr<Declaration const> variables);
  250. void add_hoisted_function(NonnullRefPtr<FunctionDeclaration const> declaration);
  251. [[nodiscard]] bool has_lexical_declarations() const { return !m_lexical_declarations.is_empty(); }
  252. [[nodiscard]] bool has_var_declarations() const { return !m_var_declarations.is_empty(); }
  253. [[nodiscard]] size_t var_declaration_count() const { return m_var_declarations.size(); }
  254. [[nodiscard]] size_t lexical_declaration_count() const { return m_lexical_declarations.size(); }
  255. ThrowCompletionOr<void> for_each_lexically_scoped_declaration(ThrowCompletionOrVoidCallback<Declaration const&>&& callback) const;
  256. ThrowCompletionOr<void> for_each_lexically_declared_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const;
  257. ThrowCompletionOr<void> for_each_var_declared_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const;
  258. ThrowCompletionOr<void> for_each_var_function_declaration_in_reverse_order(ThrowCompletionOrVoidCallback<FunctionDeclaration const&>&& callback) const;
  259. ThrowCompletionOr<void> for_each_var_scoped_variable_declaration(ThrowCompletionOrVoidCallback<VariableDeclaration const&>&& callback) const;
  260. void block_declaration_instantiation(VM&, Environment*) const;
  261. ThrowCompletionOr<void> for_each_function_hoistable_with_annexB_extension(ThrowCompletionOrVoidCallback<FunctionDeclaration&>&& callback) const;
  262. protected:
  263. explicit ScopeNode(SourceRange source_range)
  264. : Statement(source_range)
  265. {
  266. }
  267. private:
  268. virtual bool is_scope_node() const final { return true; }
  269. Vector<NonnullRefPtr<Statement const>> m_children;
  270. Vector<NonnullRefPtr<Declaration const>> m_lexical_declarations;
  271. Vector<NonnullRefPtr<Declaration const>> m_var_declarations;
  272. Vector<NonnullRefPtr<FunctionDeclaration const>> m_functions_hoistable_with_annexB_extension;
  273. };
  274. // ImportEntry Record, https://tc39.es/ecma262/#table-importentry-record-fields
  275. struct ImportEntry {
  276. DeprecatedFlyString import_name; // [[ImportName]] if a String
  277. DeprecatedFlyString local_name; // [[LocalName]]
  278. bool is_namespace { false }; // [[ImportName]] if `namespace-object`
  279. ImportEntry(DeprecatedFlyString import_name_, DeprecatedFlyString local_name_, bool is_namespace_ = false)
  280. : import_name(move(import_name_))
  281. , local_name(move(local_name_))
  282. , is_namespace(is_namespace_)
  283. {
  284. VERIFY(!is_namespace || import_name.is_null());
  285. }
  286. ModuleRequest const& module_request() const
  287. {
  288. VERIFY(m_module_request);
  289. return *m_module_request;
  290. }
  291. private:
  292. friend class ImportStatement;
  293. ModuleRequest* m_module_request; // [[ModuleRequest]]
  294. };
  295. class ImportStatement final : public Statement {
  296. public:
  297. explicit ImportStatement(SourceRange source_range, ModuleRequest from_module, Vector<ImportEntry> entries = {})
  298. : Statement(source_range)
  299. , m_module_request(move(from_module))
  300. , m_entries(move(entries))
  301. {
  302. for (auto& entry : m_entries)
  303. entry.m_module_request = &m_module_request;
  304. }
  305. virtual Completion execute(Interpreter&) const override;
  306. virtual void dump(int indent) const override;
  307. bool has_bound_name(DeprecatedFlyString const& name) const;
  308. Vector<ImportEntry> const& entries() const { return m_entries; }
  309. ModuleRequest const& module_request() const { return m_module_request; }
  310. private:
  311. ModuleRequest m_module_request;
  312. Vector<ImportEntry> m_entries;
  313. };
  314. // ExportEntry Record, https://tc39.es/ecma262/#table-exportentry-records
  315. struct ExportEntry {
  316. enum class Kind {
  317. NamedExport,
  318. ModuleRequestAll,
  319. ModuleRequestAllButDefault,
  320. // EmptyNamedExport is a special type for export {} from "module",
  321. // which should import the module without getting any of the exports
  322. // however we don't want give it a fake export name which may get
  323. // duplicates
  324. EmptyNamedExport,
  325. } kind;
  326. DeprecatedFlyString export_name; // [[ExportName]]
  327. DeprecatedFlyString local_or_import_name; // Either [[ImportName]] or [[LocalName]]
  328. ExportEntry(Kind export_kind, DeprecatedFlyString export_name_, DeprecatedFlyString local_or_import_name_)
  329. : kind(export_kind)
  330. , export_name(move(export_name_))
  331. , local_or_import_name(move(local_or_import_name_))
  332. {
  333. }
  334. bool is_module_request() const
  335. {
  336. return m_module_request != nullptr;
  337. }
  338. static ExportEntry indirect_export_entry(ModuleRequest const& module_request, DeprecatedFlyString export_name, DeprecatedFlyString import_name)
  339. {
  340. ExportEntry entry { Kind::NamedExport, move(export_name), move(import_name) };
  341. entry.m_module_request = &module_request;
  342. return entry;
  343. }
  344. ModuleRequest const& module_request() const
  345. {
  346. VERIFY(m_module_request);
  347. return *m_module_request;
  348. }
  349. private:
  350. ModuleRequest const* m_module_request { nullptr }; // [[ModuleRequest]]
  351. friend class ExportStatement;
  352. public:
  353. static ExportEntry named_export(DeprecatedFlyString export_name, DeprecatedFlyString local_name)
  354. {
  355. return ExportEntry { Kind::NamedExport, move(export_name), move(local_name) };
  356. }
  357. static ExportEntry all_but_default_entry()
  358. {
  359. return ExportEntry { Kind::ModuleRequestAllButDefault, {}, {} };
  360. }
  361. static ExportEntry all_module_request(DeprecatedFlyString export_name)
  362. {
  363. return ExportEntry { Kind::ModuleRequestAll, move(export_name), {} };
  364. }
  365. static ExportEntry empty_named_export()
  366. {
  367. return ExportEntry { Kind::EmptyNamedExport, {}, {} };
  368. }
  369. };
  370. class ExportStatement final : public Statement {
  371. public:
  372. static DeprecatedFlyString local_name_for_default;
  373. ExportStatement(SourceRange source_range, RefPtr<ASTNode const> statement, Vector<ExportEntry> entries, bool is_default_export, ModuleRequest module_request)
  374. : Statement(source_range)
  375. , m_statement(move(statement))
  376. , m_entries(move(entries))
  377. , m_is_default_export(is_default_export)
  378. , m_module_request(move(module_request))
  379. {
  380. if (!m_module_request.module_specifier.is_null()) {
  381. for (auto& entry : m_entries)
  382. entry.m_module_request = &m_module_request;
  383. }
  384. }
  385. virtual Completion execute(Interpreter&) const override;
  386. virtual void dump(int indent) const override;
  387. bool has_export(DeprecatedFlyString const& export_name) const;
  388. bool has_statement() const { return m_statement; }
  389. Vector<ExportEntry> const& entries() const { return m_entries; }
  390. bool is_default_export() const { return m_is_default_export; }
  391. ASTNode const& statement() const
  392. {
  393. VERIFY(m_statement);
  394. return *m_statement;
  395. }
  396. ModuleRequest const& module_request() const
  397. {
  398. VERIFY(!m_module_request.module_specifier.is_null());
  399. return m_module_request;
  400. }
  401. private:
  402. RefPtr<ASTNode const> m_statement;
  403. Vector<ExportEntry> m_entries;
  404. bool m_is_default_export { false };
  405. ModuleRequest m_module_request;
  406. };
  407. class Program final : public ScopeNode {
  408. public:
  409. enum class Type {
  410. Script,
  411. Module
  412. };
  413. explicit Program(SourceRange source_range, Type program_type)
  414. : ScopeNode(source_range)
  415. , m_type(program_type)
  416. {
  417. }
  418. virtual Completion execute(Interpreter&) const override;
  419. bool is_strict_mode() const { return m_is_strict_mode; }
  420. void set_strict_mode() { m_is_strict_mode = true; }
  421. Type type() const { return m_type; }
  422. void append_import(NonnullRefPtr<ImportStatement const> import_statement)
  423. {
  424. m_imports.append(import_statement);
  425. append(import_statement);
  426. }
  427. void append_export(NonnullRefPtr<ExportStatement const> export_statement)
  428. {
  429. m_exports.append(export_statement);
  430. append(export_statement);
  431. }
  432. Vector<NonnullRefPtr<ImportStatement const>> const& imports() const { return m_imports; }
  433. Vector<NonnullRefPtr<ExportStatement const>> const& exports() const { return m_exports; }
  434. Vector<NonnullRefPtr<ImportStatement const>>& imports() { return m_imports; }
  435. Vector<NonnullRefPtr<ExportStatement const>>& exports() { return m_exports; }
  436. bool has_top_level_await() const { return m_has_top_level_await; }
  437. void set_has_top_level_await() { m_has_top_level_await = true; }
  438. ThrowCompletionOr<void> global_declaration_instantiation(VM&, GlobalEnvironment&) const;
  439. private:
  440. virtual bool is_program() const override { return true; }
  441. bool m_is_strict_mode { false };
  442. Type m_type { Type::Script };
  443. Vector<NonnullRefPtr<ImportStatement const>> m_imports;
  444. Vector<NonnullRefPtr<ExportStatement const>> m_exports;
  445. bool m_has_top_level_await { false };
  446. };
  447. class BlockStatement final : public ScopeNode {
  448. public:
  449. explicit BlockStatement(SourceRange source_range)
  450. : ScopeNode(source_range)
  451. {
  452. }
  453. Completion execute(Interpreter&) const override;
  454. };
  455. class FunctionBody final : public ScopeNode {
  456. public:
  457. explicit FunctionBody(SourceRange source_range)
  458. : ScopeNode(source_range)
  459. {
  460. }
  461. void set_strict_mode() { m_in_strict_mode = true; }
  462. bool in_strict_mode() const { return m_in_strict_mode; }
  463. virtual Completion execute(Interpreter&) const override;
  464. private:
  465. bool m_in_strict_mode { false };
  466. };
  467. class Expression : public ASTNode {
  468. public:
  469. explicit Expression(SourceRange source_range)
  470. : ASTNode(source_range)
  471. {
  472. }
  473. virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const;
  474. };
  475. class Declaration : public Statement {
  476. public:
  477. explicit Declaration(SourceRange source_range)
  478. : Statement(source_range)
  479. {
  480. }
  481. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const = 0;
  482. // 8.1.3 Static Semantics: IsConstantDeclaration, https://tc39.es/ecma262/#sec-static-semantics-isconstantdeclaration
  483. virtual bool is_constant_declaration() const { return false; }
  484. virtual bool is_lexical_declaration() const { return false; }
  485. };
  486. class ErrorDeclaration final : public Declaration {
  487. public:
  488. explicit ErrorDeclaration(SourceRange source_range)
  489. : Declaration(source_range)
  490. {
  491. }
  492. Completion execute(Interpreter&) const override { return {}; }
  493. ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&&) const override
  494. {
  495. VERIFY_NOT_REACHED();
  496. }
  497. };
  498. struct BindingPattern : RefCounted<BindingPattern> {
  499. // This covers both BindingProperty and BindingElement, hence the more generic name
  500. struct BindingEntry {
  501. // If this entry represents a BindingElement, then name will be Empty
  502. Variant<NonnullRefPtr<Identifier const>, NonnullRefPtr<Expression const>, Empty> name {};
  503. Variant<NonnullRefPtr<Identifier const>, NonnullRefPtr<BindingPattern const>, NonnullRefPtr<MemberExpression const>, Empty> alias {};
  504. RefPtr<Expression const> initializer {};
  505. bool is_rest { false };
  506. bool is_elision() const { return name.has<Empty>() && alias.has<Empty>(); }
  507. };
  508. enum class Kind {
  509. Array,
  510. Object,
  511. };
  512. void dump(int indent) const;
  513. ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const;
  514. bool contains_expression() const;
  515. Vector<BindingEntry> entries;
  516. Kind kind { Kind::Object };
  517. };
  518. struct FunctionParameter {
  519. Variant<DeprecatedFlyString, NonnullRefPtr<BindingPattern const>> binding;
  520. RefPtr<Expression const> default_value;
  521. bool is_rest { false };
  522. };
  523. class FunctionNode {
  524. public:
  525. DeprecatedFlyString const& name() const { return m_name; }
  526. DeprecatedString const& source_text() const { return m_source_text; }
  527. Statement const& body() const { return *m_body; }
  528. Vector<FunctionParameter> const& parameters() const { return m_parameters; };
  529. i32 function_length() const { return m_function_length; }
  530. bool is_strict_mode() const { return m_is_strict_mode; }
  531. bool might_need_arguments_object() const { return m_might_need_arguments_object; }
  532. bool contains_direct_call_to_eval() const { return m_contains_direct_call_to_eval; }
  533. bool is_arrow_function() const { return m_is_arrow_function; }
  534. FunctionKind kind() const { return m_kind; }
  535. protected:
  536. FunctionNode(DeprecatedFlyString name, DeprecatedString source_text, NonnullRefPtr<Statement const> body, Vector<FunctionParameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function)
  537. : m_name(move(name))
  538. , m_source_text(move(source_text))
  539. , m_body(move(body))
  540. , m_parameters(move(parameters))
  541. , m_function_length(function_length)
  542. , m_kind(kind)
  543. , m_is_strict_mode(is_strict_mode)
  544. , m_might_need_arguments_object(might_need_arguments_object)
  545. , m_contains_direct_call_to_eval(contains_direct_call_to_eval)
  546. , m_is_arrow_function(is_arrow_function)
  547. {
  548. if (m_is_arrow_function)
  549. VERIFY(!m_might_need_arguments_object);
  550. }
  551. void dump(int indent, DeprecatedString const& class_name) const;
  552. private:
  553. DeprecatedFlyString m_name;
  554. DeprecatedString m_source_text;
  555. NonnullRefPtr<Statement const> m_body;
  556. Vector<FunctionParameter> const m_parameters;
  557. const i32 m_function_length;
  558. FunctionKind m_kind;
  559. bool m_is_strict_mode : 1 { false };
  560. bool m_might_need_arguments_object : 1 { false };
  561. bool m_contains_direct_call_to_eval : 1 { false };
  562. bool m_is_arrow_function : 1 { false };
  563. };
  564. class FunctionDeclaration final
  565. : public Declaration
  566. , public FunctionNode {
  567. public:
  568. static bool must_have_name() { return true; }
  569. FunctionDeclaration(SourceRange source_range, DeprecatedFlyString const& name, DeprecatedString source_text, NonnullRefPtr<Statement const> body, Vector<FunctionParameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval)
  570. : Declaration(source_range)
  571. , FunctionNode(name, move(source_text), move(body), move(parameters), function_length, kind, is_strict_mode, might_need_arguments_object, contains_direct_call_to_eval, false)
  572. {
  573. }
  574. virtual Completion execute(Interpreter&) const override;
  575. virtual void dump(int indent) const override;
  576. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  577. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
  578. virtual bool is_function_declaration() const override { return true; }
  579. void set_should_do_additional_annexB_steps() { m_is_hoisted = true; }
  580. private:
  581. bool m_is_hoisted { false };
  582. };
  583. class FunctionExpression final
  584. : public Expression
  585. , public FunctionNode {
  586. public:
  587. static bool must_have_name() { return false; }
  588. FunctionExpression(SourceRange source_range, DeprecatedFlyString const& name, DeprecatedString source_text, NonnullRefPtr<Statement const> body, Vector<FunctionParameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function = false)
  589. : Expression(source_range)
  590. , FunctionNode(name, move(source_text), move(body), move(parameters), function_length, kind, is_strict_mode, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function)
  591. {
  592. }
  593. virtual Completion execute(Interpreter&) const override;
  594. virtual void dump(int indent) const override;
  595. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  596. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode_with_lhs_name(Bytecode::Generator&, Optional<DeprecatedFlyString const&> lhs_name) const;
  597. bool has_name() const { return !name().is_empty(); }
  598. Value instantiate_ordinary_function_expression(VM&, DeprecatedFlyString given_name) const;
  599. private:
  600. virtual bool is_function_expression() const override { return true; }
  601. };
  602. class ErrorExpression final : public Expression {
  603. public:
  604. explicit ErrorExpression(SourceRange source_range)
  605. : Expression(source_range)
  606. {
  607. }
  608. Completion execute(Interpreter&) const override { return {}; }
  609. };
  610. class YieldExpression final : public Expression {
  611. public:
  612. explicit YieldExpression(SourceRange source_range, RefPtr<Expression const> argument, bool is_yield_from)
  613. : Expression(source_range)
  614. , m_argument(move(argument))
  615. , m_is_yield_from(is_yield_from)
  616. {
  617. }
  618. Expression const* argument() const { return m_argument; }
  619. bool is_yield_from() const { return m_is_yield_from; }
  620. virtual Completion execute(Interpreter&) const override;
  621. virtual void dump(int indent) const override;
  622. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  623. private:
  624. RefPtr<Expression const> m_argument;
  625. bool m_is_yield_from { false };
  626. };
  627. class AwaitExpression final : public Expression {
  628. public:
  629. explicit AwaitExpression(SourceRange source_range, NonnullRefPtr<Expression const> argument)
  630. : Expression(source_range)
  631. , m_argument(move(argument))
  632. {
  633. }
  634. virtual Completion execute(Interpreter&) const override;
  635. virtual void dump(int indent) const override;
  636. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  637. private:
  638. NonnullRefPtr<Expression const> m_argument;
  639. };
  640. class ReturnStatement final : public Statement {
  641. public:
  642. explicit ReturnStatement(SourceRange source_range, RefPtr<Expression const> argument)
  643. : Statement(source_range)
  644. , m_argument(move(argument))
  645. {
  646. }
  647. Expression const* argument() const { return m_argument; }
  648. virtual Completion execute(Interpreter&) const override;
  649. virtual void dump(int indent) const override;
  650. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  651. private:
  652. RefPtr<Expression const> m_argument;
  653. };
  654. class IfStatement final : public Statement {
  655. public:
  656. IfStatement(SourceRange source_range, NonnullRefPtr<Expression const> predicate, NonnullRefPtr<Statement const> consequent, RefPtr<Statement const> alternate)
  657. : Statement(source_range)
  658. , m_predicate(move(predicate))
  659. , m_consequent(move(consequent))
  660. , m_alternate(move(alternate))
  661. {
  662. }
  663. Expression const& predicate() const { return *m_predicate; }
  664. Statement const& consequent() const { return *m_consequent; }
  665. Statement const* alternate() const { return m_alternate; }
  666. virtual Completion execute(Interpreter&) const override;
  667. virtual void dump(int indent) const override;
  668. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  669. private:
  670. NonnullRefPtr<Expression const> m_predicate;
  671. NonnullRefPtr<Statement const> m_consequent;
  672. RefPtr<Statement const> m_alternate;
  673. };
  674. class WhileStatement final : public IterationStatement {
  675. public:
  676. WhileStatement(SourceRange source_range, NonnullRefPtr<Expression const> test, NonnullRefPtr<Statement const> body)
  677. : IterationStatement(source_range)
  678. , m_test(move(test))
  679. , m_body(move(body))
  680. {
  681. }
  682. Expression const& test() const { return *m_test; }
  683. Statement const& body() const { return *m_body; }
  684. virtual Completion execute(Interpreter&) const override;
  685. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  686. virtual void dump(int indent) const override;
  687. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  688. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  689. private:
  690. NonnullRefPtr<Expression const> m_test;
  691. NonnullRefPtr<Statement const> m_body;
  692. };
  693. class DoWhileStatement final : public IterationStatement {
  694. public:
  695. DoWhileStatement(SourceRange source_range, NonnullRefPtr<Expression const> test, NonnullRefPtr<Statement const> body)
  696. : IterationStatement(source_range)
  697. , m_test(move(test))
  698. , m_body(move(body))
  699. {
  700. }
  701. Expression const& test() const { return *m_test; }
  702. Statement const& body() const { return *m_body; }
  703. virtual Completion execute(Interpreter&) const override;
  704. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  705. virtual void dump(int indent) const override;
  706. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  707. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  708. private:
  709. NonnullRefPtr<Expression const> m_test;
  710. NonnullRefPtr<Statement const> m_body;
  711. };
  712. class WithStatement final : public Statement {
  713. public:
  714. WithStatement(SourceRange source_range, NonnullRefPtr<Expression const> object, NonnullRefPtr<Statement const> body)
  715. : Statement(source_range)
  716. , m_object(move(object))
  717. , m_body(move(body))
  718. {
  719. }
  720. Expression const& object() const { return *m_object; }
  721. Statement const& body() const { return *m_body; }
  722. virtual Completion execute(Interpreter&) const override;
  723. virtual void dump(int indent) const override;
  724. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  725. private:
  726. NonnullRefPtr<Expression const> m_object;
  727. NonnullRefPtr<Statement const> m_body;
  728. };
  729. class ForStatement final : public IterationStatement {
  730. public:
  731. ForStatement(SourceRange source_range, RefPtr<ASTNode const> init, RefPtr<Expression const> test, RefPtr<Expression const> update, NonnullRefPtr<Statement const> body)
  732. : IterationStatement(source_range)
  733. , m_init(move(init))
  734. , m_test(move(test))
  735. , m_update(move(update))
  736. , m_body(move(body))
  737. {
  738. }
  739. ASTNode const* init() const { return m_init; }
  740. Expression const* test() const { return m_test; }
  741. Expression const* update() const { return m_update; }
  742. Statement const& body() const { return *m_body; }
  743. virtual Completion execute(Interpreter&) const override;
  744. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  745. virtual void dump(int indent) const override;
  746. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  747. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  748. private:
  749. Completion for_body_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&, size_t per_iteration_bindings_size) const;
  750. RefPtr<ASTNode const> m_init;
  751. RefPtr<Expression const> m_test;
  752. RefPtr<Expression const> m_update;
  753. NonnullRefPtr<Statement const> m_body;
  754. };
  755. class ForInStatement final : public IterationStatement {
  756. public:
  757. ForInStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> lhs, NonnullRefPtr<Expression const> rhs, NonnullRefPtr<Statement const> body)
  758. : IterationStatement(source_range)
  759. , m_lhs(move(lhs))
  760. , m_rhs(move(rhs))
  761. , m_body(move(body))
  762. {
  763. }
  764. auto const& lhs() const { return m_lhs; }
  765. Expression const& rhs() const { return *m_rhs; }
  766. Statement const& body() const { return *m_body; }
  767. virtual Completion execute(Interpreter&) const override;
  768. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  769. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  770. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  771. virtual void dump(int indent) const override;
  772. private:
  773. Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> m_lhs;
  774. NonnullRefPtr<Expression const> m_rhs;
  775. NonnullRefPtr<Statement const> m_body;
  776. };
  777. class ForOfStatement final : public IterationStatement {
  778. public:
  779. ForOfStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> lhs, NonnullRefPtr<Expression const> rhs, NonnullRefPtr<Statement const> body)
  780. : IterationStatement(source_range)
  781. , m_lhs(move(lhs))
  782. , m_rhs(move(rhs))
  783. , m_body(move(body))
  784. {
  785. }
  786. auto const& lhs() const { return m_lhs; }
  787. Expression const& rhs() const { return *m_rhs; }
  788. Statement const& body() const { return *m_body; }
  789. virtual Completion execute(Interpreter&) const override;
  790. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  791. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  792. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  793. virtual void dump(int indent) const override;
  794. private:
  795. Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> m_lhs;
  796. NonnullRefPtr<Expression const> m_rhs;
  797. NonnullRefPtr<Statement const> m_body;
  798. };
  799. class ForAwaitOfStatement final : public IterationStatement {
  800. public:
  801. ForAwaitOfStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> lhs, NonnullRefPtr<Expression const> rhs, NonnullRefPtr<Statement const> body)
  802. : IterationStatement(source_range)
  803. , m_lhs(move(lhs))
  804. , m_rhs(move(rhs))
  805. , m_body(move(body))
  806. {
  807. }
  808. virtual Completion execute(Interpreter&) const override;
  809. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  810. virtual void dump(int indent) const override;
  811. private:
  812. Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> m_lhs;
  813. NonnullRefPtr<Expression const> m_rhs;
  814. NonnullRefPtr<Statement const> m_body;
  815. };
  816. enum class BinaryOp {
  817. Addition,
  818. Subtraction,
  819. Multiplication,
  820. Division,
  821. Modulo,
  822. Exponentiation,
  823. StrictlyEquals,
  824. StrictlyInequals,
  825. LooselyEquals,
  826. LooselyInequals,
  827. GreaterThan,
  828. GreaterThanEquals,
  829. LessThan,
  830. LessThanEquals,
  831. BitwiseAnd,
  832. BitwiseOr,
  833. BitwiseXor,
  834. LeftShift,
  835. RightShift,
  836. UnsignedRightShift,
  837. In,
  838. InstanceOf,
  839. };
  840. class BinaryExpression final : public Expression {
  841. public:
  842. BinaryExpression(SourceRange source_range, BinaryOp op, NonnullRefPtr<Expression const> lhs, NonnullRefPtr<Expression const> rhs)
  843. : Expression(source_range)
  844. , m_op(op)
  845. , m_lhs(move(lhs))
  846. , m_rhs(move(rhs))
  847. {
  848. }
  849. virtual Completion execute(Interpreter&) const override;
  850. virtual void dump(int indent) const override;
  851. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  852. private:
  853. BinaryOp m_op;
  854. NonnullRefPtr<Expression const> m_lhs;
  855. NonnullRefPtr<Expression const> m_rhs;
  856. };
  857. enum class LogicalOp {
  858. And,
  859. Or,
  860. NullishCoalescing,
  861. };
  862. class LogicalExpression final : public Expression {
  863. public:
  864. LogicalExpression(SourceRange source_range, LogicalOp op, NonnullRefPtr<Expression const> lhs, NonnullRefPtr<Expression const> rhs)
  865. : Expression(source_range)
  866. , m_op(op)
  867. , m_lhs(move(lhs))
  868. , m_rhs(move(rhs))
  869. {
  870. }
  871. virtual Completion execute(Interpreter&) const override;
  872. virtual void dump(int indent) const override;
  873. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  874. private:
  875. LogicalOp m_op;
  876. NonnullRefPtr<Expression const> m_lhs;
  877. NonnullRefPtr<Expression const> m_rhs;
  878. };
  879. enum class UnaryOp {
  880. BitwiseNot,
  881. Not,
  882. Plus,
  883. Minus,
  884. Typeof,
  885. Void,
  886. Delete,
  887. };
  888. class UnaryExpression final : public Expression {
  889. public:
  890. UnaryExpression(SourceRange source_range, UnaryOp op, NonnullRefPtr<Expression const> lhs)
  891. : Expression(source_range)
  892. , m_op(op)
  893. , m_lhs(move(lhs))
  894. {
  895. }
  896. virtual Completion execute(Interpreter&) const override;
  897. virtual void dump(int indent) const override;
  898. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  899. private:
  900. UnaryOp m_op;
  901. NonnullRefPtr<Expression const> m_lhs;
  902. };
  903. class SequenceExpression final : public Expression {
  904. public:
  905. SequenceExpression(SourceRange source_range, Vector<NonnullRefPtr<Expression const>> expressions)
  906. : Expression(source_range)
  907. , m_expressions(move(expressions))
  908. {
  909. VERIFY(m_expressions.size() >= 2);
  910. }
  911. virtual void dump(int indent) const override;
  912. virtual Completion execute(Interpreter&) const override;
  913. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  914. private:
  915. Vector<NonnullRefPtr<Expression const>> m_expressions;
  916. };
  917. class Literal : public Expression {
  918. protected:
  919. explicit Literal(SourceRange source_range)
  920. : Expression(source_range)
  921. {
  922. }
  923. };
  924. class BooleanLiteral final : public Literal {
  925. public:
  926. explicit BooleanLiteral(SourceRange source_range, bool value)
  927. : Literal(source_range)
  928. , m_value(value)
  929. {
  930. }
  931. virtual Completion execute(Interpreter&) const override;
  932. virtual void dump(int indent) const override;
  933. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  934. private:
  935. bool m_value { false };
  936. };
  937. class NumericLiteral final : public Literal {
  938. public:
  939. explicit NumericLiteral(SourceRange source_range, double value)
  940. : Literal(source_range)
  941. , m_value(value)
  942. {
  943. }
  944. virtual Completion execute(Interpreter&) const override;
  945. virtual void dump(int indent) const override;
  946. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  947. private:
  948. Value m_value;
  949. };
  950. class BigIntLiteral final : public Literal {
  951. public:
  952. explicit BigIntLiteral(SourceRange source_range, DeprecatedString value)
  953. : Literal(source_range)
  954. , m_value(move(value))
  955. {
  956. }
  957. virtual Completion execute(Interpreter&) const override;
  958. virtual void dump(int indent) const override;
  959. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  960. private:
  961. DeprecatedString m_value;
  962. };
  963. class StringLiteral final : public Literal {
  964. public:
  965. explicit StringLiteral(SourceRange source_range, DeprecatedString value)
  966. : Literal(source_range)
  967. , m_value(move(value))
  968. {
  969. }
  970. virtual Completion execute(Interpreter&) const override;
  971. virtual void dump(int indent) const override;
  972. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  973. StringView value() const { return m_value; }
  974. private:
  975. virtual bool is_string_literal() const override { return true; }
  976. DeprecatedString m_value;
  977. };
  978. class NullLiteral final : public Literal {
  979. public:
  980. explicit NullLiteral(SourceRange source_range)
  981. : Literal(source_range)
  982. {
  983. }
  984. virtual Completion execute(Interpreter&) const override;
  985. virtual void dump(int indent) const override;
  986. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  987. };
  988. class RegExpLiteral final : public Literal {
  989. public:
  990. RegExpLiteral(SourceRange source_range, regex::Parser::Result parsed_regex, DeprecatedString parsed_pattern, regex::RegexOptions<ECMAScriptFlags> parsed_flags, DeprecatedString pattern, DeprecatedString flags)
  991. : Literal(source_range)
  992. , m_parsed_regex(move(parsed_regex))
  993. , m_parsed_pattern(move(parsed_pattern))
  994. , m_parsed_flags(move(parsed_flags))
  995. , m_pattern(move(pattern))
  996. , m_flags(move(flags))
  997. {
  998. }
  999. virtual Completion execute(Interpreter&) const override;
  1000. virtual void dump(int indent) const override;
  1001. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1002. regex::Parser::Result const& parsed_regex() const { return m_parsed_regex; }
  1003. DeprecatedString const& parsed_pattern() const { return m_parsed_pattern; }
  1004. regex::RegexOptions<ECMAScriptFlags> const& parsed_flags() const { return m_parsed_flags; }
  1005. DeprecatedString const& pattern() const { return m_pattern; }
  1006. DeprecatedString const& flags() const { return m_flags; }
  1007. private:
  1008. regex::Parser::Result m_parsed_regex;
  1009. DeprecatedString m_parsed_pattern;
  1010. regex::RegexOptions<ECMAScriptFlags> m_parsed_flags;
  1011. DeprecatedString m_pattern;
  1012. DeprecatedString m_flags;
  1013. };
  1014. class Identifier final : public Expression {
  1015. public:
  1016. explicit Identifier(SourceRange source_range, DeprecatedFlyString string)
  1017. : Expression(source_range)
  1018. , m_string(move(string))
  1019. {
  1020. }
  1021. DeprecatedFlyString const& string() const { return m_string; }
  1022. virtual Completion execute(Interpreter&) const override;
  1023. virtual void dump(int indent) const override;
  1024. virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const override;
  1025. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1026. private:
  1027. virtual bool is_identifier() const override { return true; }
  1028. DeprecatedFlyString m_string;
  1029. mutable EnvironmentCoordinate m_cached_environment_coordinate;
  1030. };
  1031. class PrivateIdentifier final : public Expression {
  1032. public:
  1033. explicit PrivateIdentifier(SourceRange source_range, DeprecatedFlyString string)
  1034. : Expression(source_range)
  1035. , m_string(move(string))
  1036. {
  1037. }
  1038. DeprecatedFlyString const& string() const { return m_string; }
  1039. virtual Completion execute(Interpreter&) const override;
  1040. virtual void dump(int indent) const override;
  1041. virtual bool is_private_identifier() const override { return true; }
  1042. private:
  1043. DeprecatedFlyString m_string;
  1044. };
  1045. class ClassElement : public ASTNode {
  1046. public:
  1047. ClassElement(SourceRange source_range, bool is_static)
  1048. : ASTNode(source_range)
  1049. , m_is_static(is_static)
  1050. {
  1051. }
  1052. virtual Completion execute(Interpreter&) const override;
  1053. enum class ElementKind {
  1054. Method,
  1055. Field,
  1056. StaticInitializer,
  1057. };
  1058. virtual ElementKind class_element_kind() const = 0;
  1059. bool is_static() const { return m_is_static; }
  1060. // We use the Completion also as a ClassStaticBlockDefinition Record.
  1061. using ClassValue = Variant<ClassFieldDefinition, Completion, PrivateElement>;
  1062. virtual ThrowCompletionOr<ClassValue> class_element_evaluation(Interpreter&, Object& home_object) const = 0;
  1063. virtual Optional<DeprecatedFlyString> private_bound_identifier() const { return {}; };
  1064. private:
  1065. bool m_is_static { false };
  1066. };
  1067. class ClassMethod final : public ClassElement {
  1068. public:
  1069. enum class Kind {
  1070. Method,
  1071. Getter,
  1072. Setter,
  1073. };
  1074. ClassMethod(SourceRange source_range, NonnullRefPtr<Expression const> key, NonnullRefPtr<FunctionExpression const> function, Kind kind, bool is_static)
  1075. : ClassElement(source_range, is_static)
  1076. , m_key(move(key))
  1077. , m_function(move(function))
  1078. , m_kind(kind)
  1079. {
  1080. }
  1081. Expression const& key() const { return *m_key; }
  1082. Kind kind() const { return m_kind; }
  1083. virtual ElementKind class_element_kind() const override { return ElementKind::Method; }
  1084. virtual void dump(int indent) const override;
  1085. virtual ThrowCompletionOr<ClassValue> class_element_evaluation(Interpreter&, Object& home_object) const override;
  1086. virtual Optional<DeprecatedFlyString> private_bound_identifier() const override;
  1087. private:
  1088. virtual bool is_class_method() const override { return true; }
  1089. NonnullRefPtr<Expression const> m_key;
  1090. NonnullRefPtr<FunctionExpression const> m_function;
  1091. Kind m_kind;
  1092. };
  1093. class ClassField final : public ClassElement {
  1094. public:
  1095. ClassField(SourceRange source_range, NonnullRefPtr<Expression const> key, RefPtr<Expression const> init, bool contains_direct_call_to_eval, bool is_static)
  1096. : ClassElement(source_range, is_static)
  1097. , m_key(move(key))
  1098. , m_initializer(move(init))
  1099. , m_contains_direct_call_to_eval(contains_direct_call_to_eval)
  1100. {
  1101. }
  1102. Expression const& key() const { return *m_key; }
  1103. RefPtr<Expression const> const& initializer() const { return m_initializer; }
  1104. RefPtr<Expression const>& initializer() { return m_initializer; }
  1105. virtual ElementKind class_element_kind() const override { return ElementKind::Field; }
  1106. virtual void dump(int indent) const override;
  1107. virtual ThrowCompletionOr<ClassValue> class_element_evaluation(Interpreter&, Object& home_object) const override;
  1108. virtual Optional<DeprecatedFlyString> private_bound_identifier() const override;
  1109. private:
  1110. NonnullRefPtr<Expression const> m_key;
  1111. RefPtr<Expression const> m_initializer;
  1112. bool m_contains_direct_call_to_eval { false };
  1113. };
  1114. class StaticInitializer final : public ClassElement {
  1115. public:
  1116. StaticInitializer(SourceRange source_range, NonnullRefPtr<FunctionBody> function_body, bool contains_direct_call_to_eval)
  1117. : ClassElement(source_range, true)
  1118. , m_function_body(move(function_body))
  1119. , m_contains_direct_call_to_eval(contains_direct_call_to_eval)
  1120. {
  1121. }
  1122. virtual ElementKind class_element_kind() const override { return ElementKind::StaticInitializer; }
  1123. virtual ThrowCompletionOr<ClassValue> class_element_evaluation(Interpreter&, Object& home_object) const override;
  1124. virtual void dump(int indent) const override;
  1125. private:
  1126. NonnullRefPtr<FunctionBody> m_function_body;
  1127. bool m_contains_direct_call_to_eval { false };
  1128. };
  1129. class SuperExpression final : public Expression {
  1130. public:
  1131. explicit SuperExpression(SourceRange source_range)
  1132. : Expression(source_range)
  1133. {
  1134. }
  1135. virtual Completion execute(Interpreter&) const override;
  1136. virtual void dump(int indent) const override;
  1137. virtual bool is_super_expression() const override { return true; }
  1138. };
  1139. class ClassExpression final : public Expression {
  1140. public:
  1141. ClassExpression(SourceRange source_range, DeprecatedString name, DeprecatedString source_text, RefPtr<FunctionExpression const> constructor, RefPtr<Expression const> super_class, Vector<NonnullRefPtr<ClassElement const>> elements)
  1142. : Expression(source_range)
  1143. , m_name(move(name))
  1144. , m_source_text(move(source_text))
  1145. , m_constructor(move(constructor))
  1146. , m_super_class(move(super_class))
  1147. , m_elements(move(elements))
  1148. {
  1149. }
  1150. StringView name() const { return m_name; }
  1151. DeprecatedString const& source_text() const { return m_source_text; }
  1152. RefPtr<FunctionExpression const> constructor() const { return m_constructor; }
  1153. virtual Completion execute(Interpreter&) const override;
  1154. virtual void dump(int indent) const override;
  1155. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1156. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode_with_lhs_name(Bytecode::Generator&, Optional<DeprecatedFlyString const&> lhs_name) const;
  1157. bool has_name() const { return !m_name.is_empty(); }
  1158. ThrowCompletionOr<ECMAScriptFunctionObject*> class_definition_evaluation(Interpreter&, DeprecatedFlyString const& binding_name = {}, DeprecatedFlyString const& class_name = {}) const;
  1159. private:
  1160. virtual bool is_class_expression() const override { return true; }
  1161. DeprecatedString m_name;
  1162. DeprecatedString m_source_text;
  1163. RefPtr<FunctionExpression const> m_constructor;
  1164. RefPtr<Expression const> m_super_class;
  1165. Vector<NonnullRefPtr<ClassElement const>> m_elements;
  1166. };
  1167. class ClassDeclaration final : public Declaration {
  1168. public:
  1169. ClassDeclaration(SourceRange source_range, NonnullRefPtr<ClassExpression const> class_expression)
  1170. : Declaration(source_range)
  1171. , m_class_expression(move(class_expression))
  1172. {
  1173. }
  1174. virtual Completion execute(Interpreter&) const override;
  1175. virtual void dump(int indent) const override;
  1176. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1177. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
  1178. virtual bool is_lexical_declaration() const override { return true; }
  1179. StringView name() const { return m_class_expression->name(); }
  1180. private:
  1181. virtual bool is_class_declaration() const override { return true; }
  1182. friend ExportStatement;
  1183. NonnullRefPtr<ClassExpression const> m_class_expression;
  1184. };
  1185. // We use this class to mimic Initializer : = AssignmentExpression of
  1186. // 10.2.1.3 Runtime Semantics: EvaluateBody, https://tc39.es/ecma262/#sec-runtime-semantics-evaluatebody
  1187. class ClassFieldInitializerStatement final : public Statement {
  1188. public:
  1189. ClassFieldInitializerStatement(SourceRange source_range, NonnullRefPtr<Expression const> expression, DeprecatedFlyString field_name)
  1190. : Statement(move(source_range))
  1191. , m_expression(move(expression))
  1192. , m_class_field_identifier_name(move(field_name))
  1193. {
  1194. }
  1195. virtual Completion execute(Interpreter& interpreter) const override;
  1196. virtual void dump(int) const override;
  1197. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1198. private:
  1199. NonnullRefPtr<Expression const> m_expression;
  1200. DeprecatedFlyString m_class_field_identifier_name; // [[ClassFieldIdentifierName]]
  1201. };
  1202. class SpreadExpression final : public Expression {
  1203. public:
  1204. explicit SpreadExpression(SourceRange source_range, NonnullRefPtr<Expression const> target)
  1205. : Expression(source_range)
  1206. , m_target(move(target))
  1207. {
  1208. }
  1209. virtual Completion execute(Interpreter&) const override;
  1210. virtual void dump(int indent) const override;
  1211. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1212. private:
  1213. NonnullRefPtr<Expression const> m_target;
  1214. };
  1215. class ThisExpression final : public Expression {
  1216. public:
  1217. explicit ThisExpression(SourceRange source_range)
  1218. : Expression(source_range)
  1219. {
  1220. }
  1221. virtual Completion execute(Interpreter&) const override;
  1222. virtual void dump(int indent) const override;
  1223. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1224. };
  1225. struct CallExpressionArgument {
  1226. NonnullRefPtr<Expression const> value;
  1227. bool is_spread;
  1228. };
  1229. enum InvocationStyleEnum {
  1230. Parenthesized,
  1231. NotParenthesized,
  1232. };
  1233. enum InsideParenthesesEnum {
  1234. InsideParentheses,
  1235. NotInsideParentheses,
  1236. };
  1237. class CallExpression : public ASTNodeWithTailArray<CallExpression, Expression, CallExpressionArgument> {
  1238. friend class ASTNodeWithTailArray;
  1239. InvocationStyleEnum m_invocation_style;
  1240. InsideParenthesesEnum m_inside_parentheses;
  1241. public:
  1242. using Argument = CallExpressionArgument;
  1243. static NonnullRefPtr<CallExpression> create(SourceRange, NonnullRefPtr<Expression const> callee, ReadonlySpan<Argument> arguments, InvocationStyleEnum invocation_style, InsideParenthesesEnum inside_parens);
  1244. virtual Completion execute(Interpreter&) const override;
  1245. virtual void dump(int indent) const override;
  1246. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1247. Expression const& callee() const { return m_callee; }
  1248. ReadonlySpan<Argument> arguments() const { return tail_span(); }
  1249. bool is_parenthesized() const { return m_invocation_style == InvocationStyleEnum::Parenthesized; }
  1250. bool is_inside_parens() const { return m_inside_parentheses == InsideParenthesesEnum::InsideParentheses; }
  1251. void set_inside_parens() { m_inside_parentheses = InsideParenthesesEnum::InsideParentheses; }
  1252. protected:
  1253. CallExpression(SourceRange source_range, NonnullRefPtr<Expression const> callee, ReadonlySpan<Argument> arguments, InvocationStyleEnum invocation_style, InsideParenthesesEnum inside_parens = InsideParenthesesEnum::NotInsideParentheses)
  1254. : ASTNodeWithTailArray(move(source_range), arguments)
  1255. , m_invocation_style(invocation_style)
  1256. , m_inside_parentheses(inside_parens)
  1257. , m_callee(move(callee))
  1258. {
  1259. }
  1260. private:
  1261. struct ThisAndCallee {
  1262. Value this_value;
  1263. Value callee;
  1264. };
  1265. ThrowCompletionOr<ThisAndCallee> compute_this_and_callee(Interpreter&, Reference const&) const;
  1266. protected:
  1267. virtual bool is_call_expression() const override { return true; }
  1268. Completion throw_type_error_for_callee(Interpreter&, Value callee_value, StringView call_type) const;
  1269. Optional<DeprecatedString> expression_string() const;
  1270. NonnullRefPtr<Expression const> m_callee;
  1271. };
  1272. class NewExpression final : public CallExpression {
  1273. friend class ASTNodeWithTailArray;
  1274. public:
  1275. static NonnullRefPtr<NewExpression> create(SourceRange, NonnullRefPtr<Expression const> callee, ReadonlySpan<Argument> arguments, InvocationStyleEnum invocation_style, InsideParenthesesEnum inside_parens);
  1276. virtual Completion execute(Interpreter&) const override;
  1277. virtual bool is_new_expression() const override { return true; }
  1278. private:
  1279. NewExpression(SourceRange source_range, NonnullRefPtr<Expression const> callee, ReadonlySpan<Argument> arguments, InvocationStyleEnum invocation_style, InsideParenthesesEnum inside_parens)
  1280. : CallExpression(move(source_range), move(callee), arguments, invocation_style, inside_parens)
  1281. {
  1282. }
  1283. };
  1284. static_assert(sizeof(NewExpression) == sizeof(CallExpression), "Adding members to NewExpression will break CallExpression memory layout");
  1285. class SuperCall final : public Expression {
  1286. public:
  1287. // This is here to be able to make a constructor like
  1288. // constructor(...args) { super(...args); } which does not use @@iterator of %Array.prototype%.
  1289. enum class IsPartOfSyntheticConstructor {
  1290. No,
  1291. Yes,
  1292. };
  1293. SuperCall(SourceRange source_range, Vector<CallExpression::Argument> arguments)
  1294. : Expression(source_range)
  1295. , m_arguments(move(arguments))
  1296. , m_is_synthetic(IsPartOfSyntheticConstructor::No)
  1297. {
  1298. }
  1299. SuperCall(SourceRange source_range, IsPartOfSyntheticConstructor is_part_of_synthetic_constructor, CallExpression::Argument constructor_argument)
  1300. : Expression(source_range)
  1301. , m_arguments({ move(constructor_argument) })
  1302. , m_is_synthetic(IsPartOfSyntheticConstructor::Yes)
  1303. {
  1304. VERIFY(is_part_of_synthetic_constructor == IsPartOfSyntheticConstructor::Yes);
  1305. }
  1306. virtual Completion execute(Interpreter&) const override;
  1307. virtual void dump(int indent) const override;
  1308. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1309. private:
  1310. Vector<CallExpression::Argument> const m_arguments;
  1311. IsPartOfSyntheticConstructor const m_is_synthetic;
  1312. };
  1313. enum class AssignmentOp {
  1314. Assignment,
  1315. AdditionAssignment,
  1316. SubtractionAssignment,
  1317. MultiplicationAssignment,
  1318. DivisionAssignment,
  1319. ModuloAssignment,
  1320. ExponentiationAssignment,
  1321. BitwiseAndAssignment,
  1322. BitwiseOrAssignment,
  1323. BitwiseXorAssignment,
  1324. LeftShiftAssignment,
  1325. RightShiftAssignment,
  1326. UnsignedRightShiftAssignment,
  1327. AndAssignment,
  1328. OrAssignment,
  1329. NullishAssignment,
  1330. };
  1331. class AssignmentExpression final : public Expression {
  1332. public:
  1333. AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr<Expression const> lhs, NonnullRefPtr<Expression const> rhs)
  1334. : Expression(source_range)
  1335. , m_op(op)
  1336. , m_lhs(move(lhs))
  1337. , m_rhs(move(rhs))
  1338. {
  1339. }
  1340. AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr<BindingPattern const> lhs, NonnullRefPtr<Expression const> rhs)
  1341. : Expression(source_range)
  1342. , m_op(op)
  1343. , m_lhs(move(lhs))
  1344. , m_rhs(move(rhs))
  1345. {
  1346. }
  1347. virtual Completion execute(Interpreter&) const override;
  1348. virtual void dump(int indent) const override;
  1349. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1350. private:
  1351. AssignmentOp m_op;
  1352. Variant<NonnullRefPtr<Expression const>, NonnullRefPtr<BindingPattern const>> m_lhs;
  1353. NonnullRefPtr<Expression const> m_rhs;
  1354. };
  1355. enum class UpdateOp {
  1356. Increment,
  1357. Decrement,
  1358. };
  1359. class UpdateExpression final : public Expression {
  1360. public:
  1361. UpdateExpression(SourceRange source_range, UpdateOp op, NonnullRefPtr<Expression const> argument, bool prefixed = false)
  1362. : Expression(source_range)
  1363. , m_op(op)
  1364. , m_argument(move(argument))
  1365. , m_prefixed(prefixed)
  1366. {
  1367. }
  1368. virtual Completion execute(Interpreter&) const override;
  1369. virtual void dump(int indent) const override;
  1370. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1371. private:
  1372. virtual bool is_update_expression() const override { return true; }
  1373. UpdateOp m_op;
  1374. NonnullRefPtr<Expression const> m_argument;
  1375. bool m_prefixed;
  1376. };
  1377. enum class DeclarationKind {
  1378. Var,
  1379. Let,
  1380. Const,
  1381. };
  1382. class VariableDeclarator final : public ASTNode {
  1383. public:
  1384. VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier const> id)
  1385. : ASTNode(source_range)
  1386. , m_target(move(id))
  1387. {
  1388. }
  1389. VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier const> target, RefPtr<Expression const> init)
  1390. : ASTNode(source_range)
  1391. , m_target(move(target))
  1392. , m_init(move(init))
  1393. {
  1394. }
  1395. VariableDeclarator(SourceRange source_range, Variant<NonnullRefPtr<Identifier const>, NonnullRefPtr<BindingPattern const>> target, RefPtr<Expression const> init)
  1396. : ASTNode(source_range)
  1397. , m_target(move(target))
  1398. , m_init(move(init))
  1399. {
  1400. }
  1401. auto& target() const { return m_target; }
  1402. Expression const* init() const { return m_init; }
  1403. virtual Completion execute(Interpreter&) const override;
  1404. virtual void dump(int indent) const override;
  1405. private:
  1406. Variant<NonnullRefPtr<Identifier const>, NonnullRefPtr<BindingPattern const>> m_target;
  1407. RefPtr<Expression const> m_init;
  1408. };
  1409. class VariableDeclaration final : public Declaration {
  1410. public:
  1411. VariableDeclaration(SourceRange source_range, DeclarationKind declaration_kind, Vector<NonnullRefPtr<VariableDeclarator const>> declarations)
  1412. : Declaration(source_range)
  1413. , m_declaration_kind(declaration_kind)
  1414. , m_declarations(move(declarations))
  1415. {
  1416. }
  1417. DeclarationKind declaration_kind() const { return m_declaration_kind; }
  1418. virtual Completion execute(Interpreter&) const override;
  1419. virtual void dump(int indent) const override;
  1420. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1421. Vector<NonnullRefPtr<VariableDeclarator const>> const& declarations() const { return m_declarations; }
  1422. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
  1423. virtual bool is_constant_declaration() const override { return m_declaration_kind == DeclarationKind::Const; };
  1424. virtual bool is_lexical_declaration() const override { return m_declaration_kind != DeclarationKind::Var; }
  1425. private:
  1426. virtual bool is_variable_declaration() const override { return true; }
  1427. DeclarationKind m_declaration_kind;
  1428. Vector<NonnullRefPtr<VariableDeclarator const>> m_declarations;
  1429. };
  1430. class UsingDeclaration final : public Declaration {
  1431. public:
  1432. UsingDeclaration(SourceRange source_range, Vector<NonnullRefPtr<VariableDeclarator const>> declarations)
  1433. : Declaration(move(source_range))
  1434. , m_declarations(move(declarations))
  1435. {
  1436. }
  1437. virtual Completion execute(Interpreter&) const override;
  1438. virtual void dump(int indent) const override;
  1439. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
  1440. virtual bool is_constant_declaration() const override { return true; };
  1441. virtual bool is_lexical_declaration() const override { return true; }
  1442. Vector<NonnullRefPtr<VariableDeclarator const>> const& declarations() const { return m_declarations; }
  1443. private:
  1444. Vector<NonnullRefPtr<VariableDeclarator const>> m_declarations;
  1445. };
  1446. class ObjectProperty final : public ASTNode {
  1447. public:
  1448. enum class Type : u8 {
  1449. KeyValue,
  1450. Getter,
  1451. Setter,
  1452. Spread,
  1453. ProtoSetter,
  1454. };
  1455. ObjectProperty(SourceRange source_range, NonnullRefPtr<Expression const> key, RefPtr<Expression const> value, Type property_type, bool is_method)
  1456. : ASTNode(source_range)
  1457. , m_property_type(property_type)
  1458. , m_is_method(is_method)
  1459. , m_key(move(key))
  1460. , m_value(move(value))
  1461. {
  1462. }
  1463. Expression const& key() const { return m_key; }
  1464. Expression const& value() const
  1465. {
  1466. VERIFY(m_value);
  1467. return *m_value;
  1468. }
  1469. Type type() const { return m_property_type; }
  1470. bool is_method() const { return m_is_method; }
  1471. virtual void dump(int indent) const override;
  1472. virtual Completion execute(Interpreter&) const override;
  1473. private:
  1474. Type m_property_type;
  1475. bool m_is_method { false };
  1476. NonnullRefPtr<Expression const> m_key;
  1477. RefPtr<Expression const> m_value;
  1478. };
  1479. class ObjectExpression final : public Expression {
  1480. public:
  1481. explicit ObjectExpression(SourceRange source_range, Vector<NonnullRefPtr<ObjectProperty>> properties = {})
  1482. : Expression(source_range)
  1483. , m_properties(move(properties))
  1484. {
  1485. }
  1486. virtual Completion execute(Interpreter&) const override;
  1487. virtual void dump(int indent) const override;
  1488. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1489. private:
  1490. virtual bool is_object_expression() const override { return true; }
  1491. Vector<NonnullRefPtr<ObjectProperty>> m_properties;
  1492. };
  1493. class ArrayExpression final : public Expression {
  1494. public:
  1495. ArrayExpression(SourceRange source_range, Vector<RefPtr<Expression const>> elements)
  1496. : Expression(source_range)
  1497. , m_elements(move(elements))
  1498. {
  1499. }
  1500. Vector<RefPtr<Expression const>> const& elements() const { return m_elements; }
  1501. virtual Completion execute(Interpreter&) const override;
  1502. virtual void dump(int indent) const override;
  1503. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1504. private:
  1505. virtual bool is_array_expression() const override { return true; }
  1506. Vector<RefPtr<Expression const>> m_elements;
  1507. };
  1508. class TemplateLiteral final : public Expression {
  1509. public:
  1510. TemplateLiteral(SourceRange source_range, Vector<NonnullRefPtr<Expression const>> expressions)
  1511. : Expression(source_range)
  1512. , m_expressions(move(expressions))
  1513. {
  1514. }
  1515. TemplateLiteral(SourceRange source_range, Vector<NonnullRefPtr<Expression const>> expressions, Vector<NonnullRefPtr<Expression const>> raw_strings)
  1516. : Expression(source_range)
  1517. , m_expressions(move(expressions))
  1518. , m_raw_strings(move(raw_strings))
  1519. {
  1520. }
  1521. virtual Completion execute(Interpreter&) const override;
  1522. virtual void dump(int indent) const override;
  1523. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1524. Vector<NonnullRefPtr<Expression const>> const& expressions() const { return m_expressions; }
  1525. Vector<NonnullRefPtr<Expression const>> const& raw_strings() const { return m_raw_strings; }
  1526. private:
  1527. Vector<NonnullRefPtr<Expression const>> const m_expressions;
  1528. Vector<NonnullRefPtr<Expression const>> const m_raw_strings;
  1529. };
  1530. class TaggedTemplateLiteral final : public Expression {
  1531. public:
  1532. TaggedTemplateLiteral(SourceRange source_range, NonnullRefPtr<Expression const> tag, NonnullRefPtr<TemplateLiteral const> template_literal)
  1533. : Expression(source_range)
  1534. , m_tag(move(tag))
  1535. , m_template_literal(move(template_literal))
  1536. {
  1537. }
  1538. virtual Completion execute(Interpreter&) const override;
  1539. virtual void dump(int indent) const override;
  1540. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1541. ThrowCompletionOr<Value> get_template_object(Interpreter&) const;
  1542. private:
  1543. NonnullRefPtr<Expression const> const m_tag;
  1544. NonnullRefPtr<TemplateLiteral const> const m_template_literal;
  1545. mutable HashMap<GCPtr<Realm>, Handle<Array>> m_cached_values;
  1546. };
  1547. class MemberExpression final : public Expression {
  1548. public:
  1549. MemberExpression(SourceRange source_range, NonnullRefPtr<Expression const> object, NonnullRefPtr<Expression const> property, bool computed = false)
  1550. : Expression(source_range)
  1551. , m_computed(computed)
  1552. , m_object(move(object))
  1553. , m_property(move(property))
  1554. {
  1555. }
  1556. virtual Completion execute(Interpreter&) const override;
  1557. virtual void dump(int indent) const override;
  1558. virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const override;
  1559. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1560. bool is_computed() const { return m_computed; }
  1561. Expression const& object() const { return *m_object; }
  1562. Expression const& property() const { return *m_property; }
  1563. DeprecatedString to_string_approximation() const;
  1564. bool ends_in_private_name() const;
  1565. private:
  1566. virtual bool is_member_expression() const override { return true; }
  1567. bool m_computed { false };
  1568. NonnullRefPtr<Expression const> m_object;
  1569. NonnullRefPtr<Expression const> m_property;
  1570. };
  1571. class OptionalChain final : public Expression {
  1572. public:
  1573. enum class Mode {
  1574. Optional,
  1575. NotOptional,
  1576. };
  1577. struct Call {
  1578. Vector<CallExpression::Argument> arguments;
  1579. Mode mode;
  1580. };
  1581. struct ComputedReference {
  1582. NonnullRefPtr<Expression const> expression;
  1583. Mode mode;
  1584. };
  1585. struct MemberReference {
  1586. NonnullRefPtr<Identifier const> identifier;
  1587. Mode mode;
  1588. };
  1589. struct PrivateMemberReference {
  1590. NonnullRefPtr<PrivateIdentifier const> private_identifier;
  1591. Mode mode;
  1592. };
  1593. using Reference = Variant<Call, ComputedReference, MemberReference, PrivateMemberReference>;
  1594. OptionalChain(SourceRange source_range, NonnullRefPtr<Expression const> base, Vector<Reference> references)
  1595. : Expression(source_range)
  1596. , m_base(move(base))
  1597. , m_references(move(references))
  1598. {
  1599. }
  1600. virtual Completion execute(Interpreter&) const override;
  1601. virtual ThrowCompletionOr<JS::Reference> to_reference(Interpreter&) const override;
  1602. virtual void dump(int indent) const override;
  1603. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1604. Expression const& base() const { return *m_base; }
  1605. Vector<Reference> const& references() const { return m_references; }
  1606. private:
  1607. struct ReferenceAndValue {
  1608. JS::Reference reference;
  1609. Value value;
  1610. };
  1611. ThrowCompletionOr<ReferenceAndValue> to_reference_and_value(Interpreter&) const;
  1612. NonnullRefPtr<Expression const> m_base;
  1613. Vector<Reference> m_references;
  1614. };
  1615. class MetaProperty final : public Expression {
  1616. public:
  1617. enum class Type {
  1618. NewTarget,
  1619. ImportMeta,
  1620. };
  1621. MetaProperty(SourceRange source_range, Type type)
  1622. : Expression(source_range)
  1623. , m_type(type)
  1624. {
  1625. }
  1626. virtual Completion execute(Interpreter&) const override;
  1627. virtual void dump(int indent) const override;
  1628. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1629. private:
  1630. Type m_type;
  1631. };
  1632. class ImportCall final : public Expression {
  1633. public:
  1634. ImportCall(SourceRange source_range, NonnullRefPtr<Expression const> specifier, RefPtr<Expression const> options)
  1635. : Expression(source_range)
  1636. , m_specifier(move(specifier))
  1637. , m_options(move(options))
  1638. {
  1639. }
  1640. virtual void dump(int indent) const override;
  1641. virtual Completion execute(Interpreter&) const override;
  1642. private:
  1643. virtual bool is_import_call() const override { return true; }
  1644. NonnullRefPtr<Expression const> m_specifier;
  1645. RefPtr<Expression const> m_options;
  1646. };
  1647. class ConditionalExpression final : public Expression {
  1648. public:
  1649. ConditionalExpression(SourceRange source_range, NonnullRefPtr<Expression const> test, NonnullRefPtr<Expression const> consequent, NonnullRefPtr<Expression const> alternate)
  1650. : Expression(source_range)
  1651. , m_test(move(test))
  1652. , m_consequent(move(consequent))
  1653. , m_alternate(move(alternate))
  1654. {
  1655. }
  1656. virtual void dump(int indent) const override;
  1657. virtual Completion execute(Interpreter&) const override;
  1658. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1659. private:
  1660. NonnullRefPtr<Expression const> m_test;
  1661. NonnullRefPtr<Expression const> m_consequent;
  1662. NonnullRefPtr<Expression const> m_alternate;
  1663. };
  1664. class CatchClause final : public ASTNode {
  1665. public:
  1666. CatchClause(SourceRange source_range, DeprecatedFlyString parameter, NonnullRefPtr<BlockStatement const> body)
  1667. : ASTNode(source_range)
  1668. , m_parameter(move(parameter))
  1669. , m_body(move(body))
  1670. {
  1671. }
  1672. CatchClause(SourceRange source_range, NonnullRefPtr<BindingPattern const> parameter, NonnullRefPtr<BlockStatement const> body)
  1673. : ASTNode(source_range)
  1674. , m_parameter(move(parameter))
  1675. , m_body(move(body))
  1676. {
  1677. }
  1678. auto& parameter() const { return m_parameter; }
  1679. BlockStatement const& body() const { return m_body; }
  1680. virtual void dump(int indent) const override;
  1681. virtual Completion execute(Interpreter&) const override;
  1682. private:
  1683. Variant<DeprecatedFlyString, NonnullRefPtr<BindingPattern const>> m_parameter;
  1684. NonnullRefPtr<BlockStatement const> m_body;
  1685. };
  1686. class TryStatement final : public Statement {
  1687. public:
  1688. TryStatement(SourceRange source_range, NonnullRefPtr<BlockStatement const> block, RefPtr<CatchClause const> handler, RefPtr<BlockStatement const> finalizer)
  1689. : Statement(source_range)
  1690. , m_block(move(block))
  1691. , m_handler(move(handler))
  1692. , m_finalizer(move(finalizer))
  1693. {
  1694. }
  1695. BlockStatement const& block() const { return m_block; }
  1696. CatchClause const* handler() const { return m_handler; }
  1697. BlockStatement const* finalizer() const { return m_finalizer; }
  1698. virtual void dump(int indent) const override;
  1699. virtual Completion execute(Interpreter&) const override;
  1700. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1701. private:
  1702. NonnullRefPtr<BlockStatement const> m_block;
  1703. RefPtr<CatchClause const> m_handler;
  1704. RefPtr<BlockStatement const> m_finalizer;
  1705. };
  1706. class ThrowStatement final : public Statement {
  1707. public:
  1708. explicit ThrowStatement(SourceRange source_range, NonnullRefPtr<Expression const> argument)
  1709. : Statement(source_range)
  1710. , m_argument(move(argument))
  1711. {
  1712. }
  1713. Expression const& argument() const { return m_argument; }
  1714. virtual void dump(int indent) const override;
  1715. virtual Completion execute(Interpreter&) const override;
  1716. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1717. private:
  1718. NonnullRefPtr<Expression const> m_argument;
  1719. };
  1720. class SwitchCase final : public ScopeNode {
  1721. public:
  1722. SwitchCase(SourceRange source_range, RefPtr<Expression const> test)
  1723. : ScopeNode(source_range)
  1724. , m_test(move(test))
  1725. {
  1726. }
  1727. Expression const* test() const { return m_test; }
  1728. virtual void dump(int indent) const override;
  1729. virtual Completion execute(Interpreter&) const override;
  1730. private:
  1731. RefPtr<Expression const> m_test;
  1732. };
  1733. class SwitchStatement final : public ScopeNode {
  1734. public:
  1735. SwitchStatement(SourceRange source_range, NonnullRefPtr<Expression const> discriminant)
  1736. : ScopeNode(source_range)
  1737. , m_discriminant(move(discriminant))
  1738. {
  1739. }
  1740. virtual void dump(int indent) const override;
  1741. virtual Completion execute(Interpreter&) const override;
  1742. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1743. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const;
  1744. Completion execute_impl(Interpreter&) const;
  1745. void add_case(NonnullRefPtr<SwitchCase const> switch_case) { m_cases.append(move(switch_case)); }
  1746. private:
  1747. NonnullRefPtr<Expression const> m_discriminant;
  1748. Vector<NonnullRefPtr<SwitchCase const>> m_cases;
  1749. };
  1750. class BreakStatement final : public Statement {
  1751. public:
  1752. BreakStatement(SourceRange source_range, DeprecatedFlyString target_label)
  1753. : Statement(source_range)
  1754. , m_target_label(move(target_label))
  1755. {
  1756. }
  1757. virtual Completion execute(Interpreter&) const override;
  1758. DeprecatedFlyString const& target_label() const { return m_target_label; }
  1759. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1760. private:
  1761. DeprecatedFlyString m_target_label;
  1762. };
  1763. class ContinueStatement final : public Statement {
  1764. public:
  1765. ContinueStatement(SourceRange source_range, DeprecatedFlyString target_label)
  1766. : Statement(source_range)
  1767. , m_target_label(move(target_label))
  1768. {
  1769. }
  1770. virtual Completion execute(Interpreter&) const override;
  1771. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1772. DeprecatedFlyString const& target_label() const { return m_target_label; }
  1773. private:
  1774. DeprecatedFlyString m_target_label;
  1775. };
  1776. class DebuggerStatement final : public Statement {
  1777. public:
  1778. explicit DebuggerStatement(SourceRange source_range)
  1779. : Statement(source_range)
  1780. {
  1781. }
  1782. virtual Completion execute(Interpreter&) const override;
  1783. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1784. };
  1785. class SyntheticReferenceExpression final : public Expression {
  1786. public:
  1787. explicit SyntheticReferenceExpression(SourceRange source_range, Reference reference, Value value)
  1788. : Expression(source_range)
  1789. , m_reference(move(reference))
  1790. , m_value(value)
  1791. {
  1792. }
  1793. virtual Completion execute(Interpreter&) const override { return m_value; }
  1794. virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const override { return m_reference; }
  1795. private:
  1796. Reference m_reference;
  1797. Value m_value;
  1798. };
  1799. template<>
  1800. inline bool ASTNode::fast_is<NewExpression>() const { return is_new_expression(); }
  1801. template<>
  1802. inline bool ASTNode::fast_is<MemberExpression>() const { return is_member_expression(); }
  1803. template<>
  1804. inline bool ASTNode::fast_is<SuperExpression>() const { return is_super_expression(); }
  1805. template<>
  1806. inline bool ASTNode::fast_is<FunctionExpression>() const { return is_function_expression(); }
  1807. template<>
  1808. inline bool ASTNode::fast_is<ClassExpression>() const { return is_class_expression(); }
  1809. template<>
  1810. inline bool ASTNode::fast_is<Identifier>() const { return is_identifier(); }
  1811. template<>
  1812. inline bool ASTNode::fast_is<PrivateIdentifier>() const { return is_private_identifier(); }
  1813. template<>
  1814. inline bool ASTNode::fast_is<ExpressionStatement>() const { return is_expression_statement(); }
  1815. template<>
  1816. inline bool ASTNode::fast_is<ScopeNode>() const { return is_scope_node(); }
  1817. template<>
  1818. inline bool ASTNode::fast_is<Program>() const { return is_program(); }
  1819. template<>
  1820. inline bool ASTNode::fast_is<ClassDeclaration>() const { return is_class_declaration(); }
  1821. template<>
  1822. inline bool ASTNode::fast_is<FunctionDeclaration>() const { return is_function_declaration(); }
  1823. template<>
  1824. inline bool ASTNode::fast_is<VariableDeclaration>() const { return is_variable_declaration(); }
  1825. template<>
  1826. inline bool ASTNode::fast_is<ArrayExpression>() const { return is_array_expression(); }
  1827. template<>
  1828. inline bool ASTNode::fast_is<ObjectExpression>() const { return is_object_expression(); }
  1829. template<>
  1830. inline bool ASTNode::fast_is<ImportCall>() const { return is_import_call(); }
  1831. template<>
  1832. inline bool ASTNode::fast_is<StringLiteral>() const { return is_string_literal(); }
  1833. template<>
  1834. inline bool ASTNode::fast_is<UpdateExpression>() const { return is_update_expression(); }
  1835. template<>
  1836. inline bool ASTNode::fast_is<CallExpression>() const { return is_call_expression(); }
  1837. template<>
  1838. inline bool ASTNode::fast_is<LabelledStatement>() const { return is_labelled_statement(); }
  1839. template<>
  1840. inline bool ASTNode::fast_is<IterationStatement>() const { return is_iteration_statement(); }
  1841. template<>
  1842. inline bool ASTNode::fast_is<ClassMethod>() const { return is_class_method(); }
  1843. }