AST.h 72 KB

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