AST.h 72 KB

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