AST.h 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  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. Optional<String> expression_string() const;
  1204. NonnullRefPtr<Expression> m_callee;
  1205. Vector<Argument> const m_arguments;
  1206. private:
  1207. struct ThisAndCallee {
  1208. Value this_value;
  1209. Value callee;
  1210. };
  1211. ThrowCompletionOr<ThisAndCallee> compute_this_and_callee(Interpreter&, Reference const&) const;
  1212. };
  1213. class NewExpression final : public CallExpression {
  1214. public:
  1215. NewExpression(SourceRange source_range, NonnullRefPtr<Expression> callee, Vector<Argument> arguments = {})
  1216. : CallExpression(source_range, move(callee), move(arguments))
  1217. {
  1218. }
  1219. virtual Completion execute(Interpreter&) const override;
  1220. virtual bool is_new_expression() const override { return true; }
  1221. };
  1222. class SuperCall final : public Expression {
  1223. public:
  1224. // This is here to be able to make a constructor like
  1225. // constructor(...args) { super(...args); } which does not use @@iterator of %Array.prototype%.
  1226. enum class IsPartOfSyntheticConstructor {
  1227. No,
  1228. Yes,
  1229. };
  1230. SuperCall(SourceRange source_range, Vector<CallExpression::Argument> arguments)
  1231. : Expression(source_range)
  1232. , m_arguments(move(arguments))
  1233. , m_is_synthetic(IsPartOfSyntheticConstructor::No)
  1234. {
  1235. }
  1236. SuperCall(SourceRange source_range, IsPartOfSyntheticConstructor is_part_of_synthetic_constructor, CallExpression::Argument constructor_argument)
  1237. : Expression(source_range)
  1238. , m_arguments({ move(constructor_argument) })
  1239. , m_is_synthetic(IsPartOfSyntheticConstructor::Yes)
  1240. {
  1241. VERIFY(is_part_of_synthetic_constructor == IsPartOfSyntheticConstructor::Yes);
  1242. }
  1243. virtual Completion execute(Interpreter&) const override;
  1244. virtual void dump(int indent) const override;
  1245. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1246. private:
  1247. Vector<CallExpression::Argument> const m_arguments;
  1248. IsPartOfSyntheticConstructor const m_is_synthetic;
  1249. };
  1250. enum class AssignmentOp {
  1251. Assignment,
  1252. AdditionAssignment,
  1253. SubtractionAssignment,
  1254. MultiplicationAssignment,
  1255. DivisionAssignment,
  1256. ModuloAssignment,
  1257. ExponentiationAssignment,
  1258. BitwiseAndAssignment,
  1259. BitwiseOrAssignment,
  1260. BitwiseXorAssignment,
  1261. LeftShiftAssignment,
  1262. RightShiftAssignment,
  1263. UnsignedRightShiftAssignment,
  1264. AndAssignment,
  1265. OrAssignment,
  1266. NullishAssignment,
  1267. };
  1268. class AssignmentExpression final : public Expression {
  1269. public:
  1270. AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  1271. : Expression(source_range)
  1272. , m_op(op)
  1273. , m_lhs(move(lhs))
  1274. , m_rhs(move(rhs))
  1275. {
  1276. }
  1277. AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr<BindingPattern> lhs, NonnullRefPtr<Expression> rhs)
  1278. : Expression(source_range)
  1279. , m_op(op)
  1280. , m_lhs(move(lhs))
  1281. , m_rhs(move(rhs))
  1282. {
  1283. }
  1284. virtual Completion execute(Interpreter&) const override;
  1285. virtual void dump(int indent) const override;
  1286. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1287. private:
  1288. AssignmentOp m_op;
  1289. Variant<NonnullRefPtr<Expression>, NonnullRefPtr<BindingPattern>> m_lhs;
  1290. NonnullRefPtr<Expression> m_rhs;
  1291. };
  1292. enum class UpdateOp {
  1293. Increment,
  1294. Decrement,
  1295. };
  1296. class UpdateExpression final : public Expression {
  1297. public:
  1298. UpdateExpression(SourceRange source_range, UpdateOp op, NonnullRefPtr<Expression> argument, bool prefixed = false)
  1299. : Expression(source_range)
  1300. , m_op(op)
  1301. , m_argument(move(argument))
  1302. , m_prefixed(prefixed)
  1303. {
  1304. }
  1305. virtual Completion execute(Interpreter&) const override;
  1306. virtual void dump(int indent) const override;
  1307. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1308. private:
  1309. virtual bool is_update_expression() const override { return true; }
  1310. UpdateOp m_op;
  1311. NonnullRefPtr<Expression> m_argument;
  1312. bool m_prefixed;
  1313. };
  1314. enum class DeclarationKind {
  1315. Var,
  1316. Let,
  1317. Const,
  1318. };
  1319. class VariableDeclarator final : public ASTNode {
  1320. public:
  1321. VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier> id)
  1322. : ASTNode(source_range)
  1323. , m_target(move(id))
  1324. {
  1325. }
  1326. VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier> target, RefPtr<Expression> init)
  1327. : ASTNode(source_range)
  1328. , m_target(move(target))
  1329. , m_init(move(init))
  1330. {
  1331. }
  1332. VariableDeclarator(SourceRange source_range, Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>> target, RefPtr<Expression> init)
  1333. : ASTNode(source_range)
  1334. , m_target(move(target))
  1335. , m_init(move(init))
  1336. {
  1337. }
  1338. auto& target() const { return m_target; }
  1339. Expression const* init() const { return m_init; }
  1340. virtual Completion execute(Interpreter&) const override;
  1341. virtual void dump(int indent) const override;
  1342. private:
  1343. Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>> m_target;
  1344. RefPtr<Expression> m_init;
  1345. };
  1346. class VariableDeclaration final : public Declaration {
  1347. public:
  1348. VariableDeclaration(SourceRange source_range, DeclarationKind declaration_kind, NonnullRefPtrVector<VariableDeclarator> declarations)
  1349. : Declaration(source_range)
  1350. , m_declaration_kind(declaration_kind)
  1351. , m_declarations(move(declarations))
  1352. {
  1353. }
  1354. DeclarationKind declaration_kind() const { return m_declaration_kind; }
  1355. virtual Completion execute(Interpreter&) const override;
  1356. virtual void dump(int indent) const override;
  1357. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1358. NonnullRefPtrVector<VariableDeclarator> const& declarations() const { return m_declarations; }
  1359. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<FlyString const&>&& callback) const override;
  1360. virtual bool is_constant_declaration() const override { return m_declaration_kind == DeclarationKind::Const; };
  1361. virtual bool is_lexical_declaration() const override { return m_declaration_kind != DeclarationKind::Var; }
  1362. private:
  1363. virtual bool is_variable_declaration() const override { return true; }
  1364. DeclarationKind m_declaration_kind;
  1365. NonnullRefPtrVector<VariableDeclarator> m_declarations;
  1366. };
  1367. class ObjectProperty final : public ASTNode {
  1368. public:
  1369. enum class Type {
  1370. KeyValue,
  1371. Getter,
  1372. Setter,
  1373. Spread,
  1374. ProtoSetter,
  1375. };
  1376. ObjectProperty(SourceRange source_range, NonnullRefPtr<Expression> key, RefPtr<Expression> value, Type property_type, bool is_method)
  1377. : ASTNode(source_range)
  1378. , m_key(move(key))
  1379. , m_value(move(value))
  1380. , m_property_type(property_type)
  1381. , m_is_method(is_method)
  1382. {
  1383. }
  1384. Expression const& key() const { return m_key; }
  1385. Expression const& value() const
  1386. {
  1387. VERIFY(m_value);
  1388. return *m_value;
  1389. }
  1390. Type type() const { return m_property_type; }
  1391. bool is_method() const { return m_is_method; }
  1392. virtual void dump(int indent) const override;
  1393. virtual Completion execute(Interpreter&) const override;
  1394. private:
  1395. NonnullRefPtr<Expression> m_key;
  1396. RefPtr<Expression> m_value;
  1397. Type m_property_type;
  1398. bool m_is_method { false };
  1399. };
  1400. class ObjectExpression final : public Expression {
  1401. public:
  1402. explicit ObjectExpression(SourceRange source_range, NonnullRefPtrVector<ObjectProperty> properties = {}, Optional<SourceRange> first_invalid_property_range = {})
  1403. : Expression(source_range)
  1404. , m_properties(move(properties))
  1405. , m_first_invalid_property_range(move(first_invalid_property_range))
  1406. {
  1407. }
  1408. virtual Completion execute(Interpreter&) const override;
  1409. virtual void dump(int indent) const override;
  1410. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1411. Optional<SourceRange> const& invalid_property_range() const { return m_first_invalid_property_range; }
  1412. private:
  1413. virtual bool is_object_expression() const override { return true; }
  1414. NonnullRefPtrVector<ObjectProperty> m_properties;
  1415. Optional<SourceRange> m_first_invalid_property_range;
  1416. };
  1417. class ArrayExpression final : public Expression {
  1418. public:
  1419. ArrayExpression(SourceRange source_range, Vector<RefPtr<Expression>> elements)
  1420. : Expression(source_range)
  1421. , m_elements(move(elements))
  1422. {
  1423. }
  1424. Vector<RefPtr<Expression>> const& elements() const { return m_elements; }
  1425. virtual Completion execute(Interpreter&) const override;
  1426. virtual void dump(int indent) const override;
  1427. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1428. private:
  1429. virtual bool is_array_expression() const override { return true; }
  1430. Vector<RefPtr<Expression>> m_elements;
  1431. };
  1432. class TemplateLiteral final : public Expression {
  1433. public:
  1434. TemplateLiteral(SourceRange source_range, NonnullRefPtrVector<Expression> expressions)
  1435. : Expression(source_range)
  1436. , m_expressions(move(expressions))
  1437. {
  1438. }
  1439. TemplateLiteral(SourceRange source_range, NonnullRefPtrVector<Expression> expressions, NonnullRefPtrVector<Expression> raw_strings)
  1440. : Expression(source_range)
  1441. , m_expressions(move(expressions))
  1442. , m_raw_strings(move(raw_strings))
  1443. {
  1444. }
  1445. virtual Completion execute(Interpreter&) const override;
  1446. virtual void dump(int indent) const override;
  1447. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1448. NonnullRefPtrVector<Expression> const& expressions() const { return m_expressions; }
  1449. NonnullRefPtrVector<Expression> const& raw_strings() const { return m_raw_strings; }
  1450. private:
  1451. NonnullRefPtrVector<Expression> const m_expressions;
  1452. NonnullRefPtrVector<Expression> const m_raw_strings;
  1453. };
  1454. class TaggedTemplateLiteral final : public Expression {
  1455. public:
  1456. TaggedTemplateLiteral(SourceRange source_range, NonnullRefPtr<Expression> tag, NonnullRefPtr<TemplateLiteral> template_literal)
  1457. : Expression(source_range)
  1458. , m_tag(move(tag))
  1459. , m_template_literal(move(template_literal))
  1460. {
  1461. }
  1462. virtual Completion execute(Interpreter&) const override;
  1463. virtual void dump(int indent) const override;
  1464. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1465. ThrowCompletionOr<Value> get_template_object(Interpreter&) const;
  1466. private:
  1467. NonnullRefPtr<Expression> const m_tag;
  1468. NonnullRefPtr<TemplateLiteral> const m_template_literal;
  1469. mutable HashMap<Realm*, Handle<Array>> m_cached_values;
  1470. };
  1471. class MemberExpression final : public Expression {
  1472. public:
  1473. MemberExpression(SourceRange source_range, NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property, bool computed = false)
  1474. : Expression(source_range)
  1475. , m_object(move(object))
  1476. , m_property(move(property))
  1477. , m_computed(computed)
  1478. {
  1479. }
  1480. virtual Completion execute(Interpreter&) const override;
  1481. virtual void dump(int indent) const override;
  1482. virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const override;
  1483. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1484. bool is_computed() const { return m_computed; }
  1485. Expression const& object() const { return *m_object; }
  1486. Expression const& property() const { return *m_property; }
  1487. String to_string_approximation() const;
  1488. bool ends_in_private_name() const;
  1489. private:
  1490. virtual bool is_member_expression() const override { return true; }
  1491. NonnullRefPtr<Expression> m_object;
  1492. NonnullRefPtr<Expression> m_property;
  1493. bool m_computed { false };
  1494. };
  1495. class OptionalChain final : public Expression {
  1496. public:
  1497. enum class Mode {
  1498. Optional,
  1499. NotOptional,
  1500. };
  1501. struct Call {
  1502. Vector<CallExpression::Argument> arguments;
  1503. Mode mode;
  1504. };
  1505. struct ComputedReference {
  1506. NonnullRefPtr<Expression> expression;
  1507. Mode mode;
  1508. };
  1509. struct MemberReference {
  1510. NonnullRefPtr<Identifier> identifier;
  1511. Mode mode;
  1512. };
  1513. struct PrivateMemberReference {
  1514. NonnullRefPtr<PrivateIdentifier> private_identifier;
  1515. Mode mode;
  1516. };
  1517. using Reference = Variant<Call, ComputedReference, MemberReference, PrivateMemberReference>;
  1518. OptionalChain(SourceRange source_range, NonnullRefPtr<Expression> base, Vector<Reference> references)
  1519. : Expression(source_range)
  1520. , m_base(move(base))
  1521. , m_references(move(references))
  1522. {
  1523. }
  1524. virtual Completion execute(Interpreter&) const override;
  1525. virtual ThrowCompletionOr<JS::Reference> to_reference(Interpreter&) const override;
  1526. virtual void dump(int indent) const override;
  1527. private:
  1528. struct ReferenceAndValue {
  1529. JS::Reference reference;
  1530. Value value;
  1531. };
  1532. ThrowCompletionOr<ReferenceAndValue> to_reference_and_value(Interpreter&) const;
  1533. NonnullRefPtr<Expression> m_base;
  1534. Vector<Reference> m_references;
  1535. };
  1536. class MetaProperty final : public Expression {
  1537. public:
  1538. enum class Type {
  1539. NewTarget,
  1540. ImportMeta,
  1541. };
  1542. MetaProperty(SourceRange source_range, Type type)
  1543. : Expression(source_range)
  1544. , m_type(type)
  1545. {
  1546. }
  1547. virtual Completion execute(Interpreter&) const override;
  1548. virtual void dump(int indent) const override;
  1549. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1550. private:
  1551. Type m_type;
  1552. };
  1553. class ImportCall final : public Expression {
  1554. public:
  1555. ImportCall(SourceRange source_range, NonnullRefPtr<Expression> specifier, RefPtr<Expression> options)
  1556. : Expression(source_range)
  1557. , m_specifier(move(specifier))
  1558. , m_options(move(options))
  1559. {
  1560. }
  1561. virtual void dump(int indent) const override;
  1562. virtual Completion execute(Interpreter&) const override;
  1563. private:
  1564. virtual bool is_import_call() const override { return true; }
  1565. NonnullRefPtr<Expression> m_specifier;
  1566. RefPtr<Expression> m_options;
  1567. };
  1568. class ConditionalExpression final : public Expression {
  1569. public:
  1570. ConditionalExpression(SourceRange source_range, NonnullRefPtr<Expression> test, NonnullRefPtr<Expression> consequent, NonnullRefPtr<Expression> alternate)
  1571. : Expression(source_range)
  1572. , m_test(move(test))
  1573. , m_consequent(move(consequent))
  1574. , m_alternate(move(alternate))
  1575. {
  1576. }
  1577. virtual void dump(int indent) const override;
  1578. virtual Completion execute(Interpreter&) const override;
  1579. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1580. private:
  1581. NonnullRefPtr<Expression> m_test;
  1582. NonnullRefPtr<Expression> m_consequent;
  1583. NonnullRefPtr<Expression> m_alternate;
  1584. };
  1585. class CatchClause final : public ASTNode {
  1586. public:
  1587. CatchClause(SourceRange source_range, FlyString parameter, NonnullRefPtr<BlockStatement> body)
  1588. : ASTNode(source_range)
  1589. , m_parameter(move(parameter))
  1590. , m_body(move(body))
  1591. {
  1592. }
  1593. CatchClause(SourceRange source_range, NonnullRefPtr<BindingPattern> parameter, NonnullRefPtr<BlockStatement> body)
  1594. : ASTNode(source_range)
  1595. , m_parameter(move(parameter))
  1596. , m_body(move(body))
  1597. {
  1598. }
  1599. auto& parameter() const { return m_parameter; }
  1600. BlockStatement const& body() const { return m_body; }
  1601. virtual void dump(int indent) const override;
  1602. virtual Completion execute(Interpreter&) const override;
  1603. private:
  1604. Variant<FlyString, NonnullRefPtr<BindingPattern>> m_parameter;
  1605. NonnullRefPtr<BlockStatement> m_body;
  1606. };
  1607. class TryStatement final : public Statement {
  1608. public:
  1609. TryStatement(SourceRange source_range, NonnullRefPtr<BlockStatement> block, RefPtr<CatchClause> handler, RefPtr<BlockStatement> finalizer)
  1610. : Statement(source_range)
  1611. , m_block(move(block))
  1612. , m_handler(move(handler))
  1613. , m_finalizer(move(finalizer))
  1614. {
  1615. }
  1616. BlockStatement const& block() const { return m_block; }
  1617. CatchClause const* handler() const { return m_handler; }
  1618. BlockStatement const* finalizer() const { return m_finalizer; }
  1619. virtual void dump(int indent) const override;
  1620. virtual Completion execute(Interpreter&) const override;
  1621. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1622. private:
  1623. NonnullRefPtr<BlockStatement> m_block;
  1624. RefPtr<CatchClause> m_handler;
  1625. RefPtr<BlockStatement> m_finalizer;
  1626. };
  1627. class ThrowStatement final : public Statement {
  1628. public:
  1629. explicit ThrowStatement(SourceRange source_range, NonnullRefPtr<Expression> argument)
  1630. : Statement(source_range)
  1631. , m_argument(move(argument))
  1632. {
  1633. }
  1634. Expression const& argument() const { return m_argument; }
  1635. virtual void dump(int indent) const override;
  1636. virtual Completion execute(Interpreter&) const override;
  1637. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1638. private:
  1639. NonnullRefPtr<Expression> m_argument;
  1640. };
  1641. class SwitchCase final : public ScopeNode {
  1642. public:
  1643. SwitchCase(SourceRange source_range, RefPtr<Expression> test)
  1644. : ScopeNode(source_range)
  1645. , m_test(move(test))
  1646. {
  1647. }
  1648. Expression const* test() const { return m_test; }
  1649. virtual void dump(int indent) const override;
  1650. virtual Completion execute(Interpreter&) const override;
  1651. private:
  1652. RefPtr<Expression> m_test;
  1653. };
  1654. class SwitchStatement final : public ScopeNode {
  1655. public:
  1656. SwitchStatement(SourceRange source_range, NonnullRefPtr<Expression> discriminant)
  1657. : ScopeNode(source_range)
  1658. , m_discriminant(move(discriminant))
  1659. {
  1660. }
  1661. virtual void dump(int indent) const override;
  1662. virtual Completion execute(Interpreter&) const override;
  1663. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1664. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<FlyString> const&) const;
  1665. Completion execute_impl(Interpreter&) const;
  1666. void add_case(NonnullRefPtr<SwitchCase> switch_case) { m_cases.append(move(switch_case)); }
  1667. private:
  1668. NonnullRefPtr<Expression> m_discriminant;
  1669. NonnullRefPtrVector<SwitchCase> m_cases;
  1670. };
  1671. class BreakStatement final : public Statement {
  1672. public:
  1673. BreakStatement(SourceRange source_range, FlyString target_label)
  1674. : Statement(source_range)
  1675. , m_target_label(move(target_label))
  1676. {
  1677. }
  1678. virtual Completion execute(Interpreter&) const override;
  1679. FlyString const& target_label() const { return m_target_label; }
  1680. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1681. private:
  1682. FlyString m_target_label;
  1683. };
  1684. class ContinueStatement final : public Statement {
  1685. public:
  1686. ContinueStatement(SourceRange source_range, FlyString target_label)
  1687. : Statement(source_range)
  1688. , m_target_label(move(target_label))
  1689. {
  1690. }
  1691. virtual Completion execute(Interpreter&) const override;
  1692. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1693. FlyString const& target_label() const { return m_target_label; }
  1694. private:
  1695. FlyString m_target_label;
  1696. };
  1697. class DebuggerStatement final : public Statement {
  1698. public:
  1699. explicit DebuggerStatement(SourceRange source_range)
  1700. : Statement(source_range)
  1701. {
  1702. }
  1703. virtual Completion execute(Interpreter&) const override;
  1704. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1705. };
  1706. class SyntheticReferenceExpression final : public Expression {
  1707. public:
  1708. explicit SyntheticReferenceExpression(SourceRange source_range, Reference reference, Value value)
  1709. : Expression(source_range)
  1710. , m_reference(move(reference))
  1711. , m_value(value)
  1712. {
  1713. }
  1714. virtual Completion execute(Interpreter&) const override { return m_value; }
  1715. virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const override { return m_reference; }
  1716. private:
  1717. Reference m_reference;
  1718. Value m_value;
  1719. };
  1720. template<>
  1721. inline bool ASTNode::fast_is<NewExpression>() const { return is_new_expression(); }
  1722. template<>
  1723. inline bool ASTNode::fast_is<MemberExpression>() const { return is_member_expression(); }
  1724. template<>
  1725. inline bool ASTNode::fast_is<SuperExpression>() const { return is_super_expression(); }
  1726. template<>
  1727. inline bool ASTNode::fast_is<FunctionExpression>() const { return is_function_expression(); }
  1728. template<>
  1729. inline bool ASTNode::fast_is<ClassExpression>() const { return is_class_expression(); }
  1730. template<>
  1731. inline bool ASTNode::fast_is<Identifier>() const { return is_identifier(); }
  1732. template<>
  1733. inline bool ASTNode::fast_is<PrivateIdentifier>() const { return is_private_identifier(); }
  1734. template<>
  1735. inline bool ASTNode::fast_is<ExpressionStatement>() const { return is_expression_statement(); }
  1736. template<>
  1737. inline bool ASTNode::fast_is<ScopeNode>() const { return is_scope_node(); }
  1738. template<>
  1739. inline bool ASTNode::fast_is<Program>() const { return is_program(); }
  1740. template<>
  1741. inline bool ASTNode::fast_is<ClassDeclaration>() const { return is_class_declaration(); }
  1742. template<>
  1743. inline bool ASTNode::fast_is<FunctionDeclaration>() const { return is_function_declaration(); }
  1744. template<>
  1745. inline bool ASTNode::fast_is<VariableDeclaration>() const { return is_variable_declaration(); }
  1746. template<>
  1747. inline bool ASTNode::fast_is<ArrayExpression>() const { return is_array_expression(); }
  1748. template<>
  1749. inline bool ASTNode::fast_is<ObjectExpression>() const { return is_object_expression(); }
  1750. template<>
  1751. inline bool ASTNode::fast_is<ImportCall>() const { return is_import_call(); }
  1752. template<>
  1753. inline bool ASTNode::fast_is<StringLiteral>() const { return is_string_literal(); }
  1754. template<>
  1755. inline bool ASTNode::fast_is<UpdateExpression>() const { return is_update_expression(); }
  1756. template<>
  1757. inline bool ASTNode::fast_is<CallExpression>() const { return is_call_expression(); }
  1758. template<>
  1759. inline bool ASTNode::fast_is<LabelledStatement>() const { return is_labelled_statement(); }
  1760. template<>
  1761. inline bool ASTNode::fast_is<IterationStatement>() const { return is_iteration_statement(); }
  1762. template<>
  1763. inline bool ASTNode::fast_is<ClassMethod>() const { return is_class_method(); }
  1764. }