AST.h 59 KB

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