AST.h 65 KB

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