AST.h 71 KB

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