AST.h 66 KB

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