AST.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Demangle.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/HashTable.h>
  10. #include <AK/ScopeGuard.h>
  11. #include <AK/StringBuilder.h>
  12. #include <AK/TemporaryChange.h>
  13. #include <LibCrypto/BigInt/SignedBigInteger.h>
  14. #include <LibJS/AST.h>
  15. #include <LibJS/Interpreter.h>
  16. #include <LibJS/Runtime/Accessor.h>
  17. #include <LibJS/Runtime/Array.h>
  18. #include <LibJS/Runtime/BigInt.h>
  19. #include <LibJS/Runtime/Error.h>
  20. #include <LibJS/Runtime/GlobalObject.h>
  21. #include <LibJS/Runtime/IteratorOperations.h>
  22. #include <LibJS/Runtime/MarkedValueList.h>
  23. #include <LibJS/Runtime/NativeFunction.h>
  24. #include <LibJS/Runtime/PrimitiveString.h>
  25. #include <LibJS/Runtime/Reference.h>
  26. #include <LibJS/Runtime/RegExpObject.h>
  27. #include <LibJS/Runtime/ScriptFunction.h>
  28. #include <LibJS/Runtime/Shape.h>
  29. #include <LibJS/Runtime/WithScope.h>
  30. #include <typeinfo>
  31. namespace JS {
  32. class InterpreterNodeScope {
  33. AK_MAKE_NONCOPYABLE(InterpreterNodeScope);
  34. AK_MAKE_NONMOVABLE(InterpreterNodeScope);
  35. public:
  36. InterpreterNodeScope(Interpreter& interpreter, ASTNode const& node)
  37. : m_interpreter(interpreter)
  38. , m_chain_node { nullptr, node }
  39. {
  40. m_interpreter.vm().call_frame().current_node = &node;
  41. m_interpreter.push_ast_node(m_chain_node);
  42. }
  43. ~InterpreterNodeScope()
  44. {
  45. m_interpreter.pop_ast_node();
  46. }
  47. private:
  48. Interpreter& m_interpreter;
  49. ExecutingASTNodeChain m_chain_node;
  50. };
  51. String ASTNode::class_name() const
  52. {
  53. // NOTE: We strip the "JS::" prefix.
  54. return demangle(typeid(*this).name()).substring(4);
  55. }
  56. static void update_function_name(Value value, FlyString const& name)
  57. {
  58. if (!value.is_function())
  59. return;
  60. auto& function = value.as_function();
  61. if (is<ScriptFunction>(function) && function.name().is_empty())
  62. static_cast<ScriptFunction&>(function).set_name(name);
  63. }
  64. static String get_function_name(GlobalObject& global_object, Value value)
  65. {
  66. if (value.is_symbol())
  67. return String::formatted("[{}]", value.as_symbol().description());
  68. if (value.is_string())
  69. return value.as_string().string();
  70. return value.to_string(global_object);
  71. }
  72. Value ScopeNode::execute(Interpreter& interpreter, GlobalObject& global_object) const
  73. {
  74. InterpreterNodeScope node_scope { interpreter, *this };
  75. return interpreter.execute_statement(global_object, *this);
  76. }
  77. Value Program::execute(Interpreter& interpreter, GlobalObject& global_object) const
  78. {
  79. InterpreterNodeScope node_scope { interpreter, *this };
  80. return interpreter.execute_statement(global_object, *this, ScopeType::Block);
  81. }
  82. Value FunctionDeclaration::execute(Interpreter& interpreter, GlobalObject&) const
  83. {
  84. InterpreterNodeScope node_scope { interpreter, *this };
  85. return {};
  86. }
  87. Value FunctionExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  88. {
  89. InterpreterNodeScope node_scope { interpreter, *this };
  90. return ScriptFunction::create(global_object, name(), body(), parameters(), function_length(), interpreter.current_scope(), is_strict_mode() || interpreter.vm().in_strict_mode(), m_is_arrow_function);
  91. }
  92. Value ExpressionStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  93. {
  94. InterpreterNodeScope node_scope { interpreter, *this };
  95. return m_expression->execute(interpreter, global_object);
  96. }
  97. CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter, GlobalObject& global_object) const
  98. {
  99. auto& vm = interpreter.vm();
  100. if (is<NewExpression>(*this)) {
  101. // Computing |this| is irrelevant for "new" expression.
  102. return { js_undefined(), m_callee->execute(interpreter, global_object) };
  103. }
  104. if (is<SuperExpression>(*m_callee)) {
  105. // If we are calling super, |this| has not been initialized yet, and would not be meaningful to provide.
  106. auto new_target = vm.get_new_target();
  107. VERIFY(new_target.is_function());
  108. return { js_undefined(), new_target };
  109. }
  110. if (is<MemberExpression>(*m_callee)) {
  111. auto& member_expression = static_cast<MemberExpression const&>(*m_callee);
  112. Value callee;
  113. Object* this_value = nullptr;
  114. if (is<SuperExpression>(member_expression.object())) {
  115. auto super_base = interpreter.current_environment()->get_super_base();
  116. if (super_base.is_nullish()) {
  117. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeNullOrUndefinedOnSuperPropertyAccess, super_base.to_string_without_side_effects());
  118. return {};
  119. }
  120. auto property_name = member_expression.computed_property_name(interpreter, global_object);
  121. if (!property_name.is_valid())
  122. return {};
  123. auto reference = Reference(super_base, property_name);
  124. callee = reference.get(global_object);
  125. if (vm.exception())
  126. return {};
  127. this_value = &vm.this_value(global_object).as_object();
  128. } else {
  129. auto reference = member_expression.to_reference(interpreter, global_object);
  130. if (vm.exception())
  131. return {};
  132. callee = reference.get(global_object);
  133. if (vm.exception())
  134. return {};
  135. this_value = reference.base().to_object(global_object);
  136. if (vm.exception())
  137. return {};
  138. }
  139. return { this_value, callee };
  140. }
  141. if (interpreter.vm().in_strict_mode()) {
  142. // If we are in strict mode, |this| should never be bound to global object by default.
  143. return { js_undefined(), m_callee->execute(interpreter, global_object) };
  144. }
  145. return { &global_object, m_callee->execute(interpreter, global_object) };
  146. }
  147. Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  148. {
  149. InterpreterNodeScope node_scope { interpreter, *this };
  150. auto& vm = interpreter.vm();
  151. auto [this_value, callee] = compute_this_and_callee(interpreter, global_object);
  152. if (vm.exception())
  153. return {};
  154. VERIFY(!callee.is_empty());
  155. if (!callee.is_function()
  156. || (is<NewExpression>(*this) && (is<NativeFunction>(callee.as_object()) && !static_cast<NativeFunction&>(callee.as_object()).has_constructor()))) {
  157. String error_message;
  158. auto call_type = is<NewExpression>(*this) ? "constructor" : "function";
  159. if (is<Identifier>(*m_callee) || is<MemberExpression>(*m_callee)) {
  160. String expression_string;
  161. if (is<Identifier>(*m_callee)) {
  162. expression_string = static_cast<Identifier const&>(*m_callee).string();
  163. } else {
  164. expression_string = static_cast<MemberExpression const&>(*m_callee).to_string_approximation();
  165. }
  166. vm.throw_exception<TypeError>(global_object, ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects(), call_type, expression_string);
  167. } else {
  168. vm.throw_exception<TypeError>(global_object, ErrorType::IsNotA, callee.to_string_without_side_effects(), call_type);
  169. }
  170. return {};
  171. }
  172. auto& function = callee.as_function();
  173. MarkedValueList arguments(vm.heap());
  174. arguments.ensure_capacity(m_arguments.size());
  175. for (auto& argument : m_arguments) {
  176. auto value = argument.value->execute(interpreter, global_object);
  177. if (vm.exception())
  178. return {};
  179. if (argument.is_spread) {
  180. get_iterator_values(global_object, value, [&](Value iterator_value) {
  181. if (vm.exception())
  182. return IterationDecision::Break;
  183. arguments.append(iterator_value);
  184. return IterationDecision::Continue;
  185. });
  186. if (vm.exception())
  187. return {};
  188. } else {
  189. arguments.append(value);
  190. }
  191. }
  192. vm.call_frame().current_node = interpreter.current_node();
  193. Object* new_object = nullptr;
  194. Value result;
  195. if (is<NewExpression>(*this)) {
  196. result = vm.construct(function, function, move(arguments));
  197. if (result.is_object())
  198. new_object = &result.as_object();
  199. } else if (is<SuperExpression>(*m_callee)) {
  200. // FIXME: This is merely a band-aid to make super() inside catch {} work (which constructs
  201. // a new LexicalEnvironment without current function). Implement GetSuperConstructor()
  202. // and subsequently GetThisEnvironment() instead.
  203. auto* function_environment = interpreter.current_environment();
  204. if (!function_environment->current_function())
  205. function_environment = static_cast<LexicalEnvironment*>(function_environment->parent());
  206. auto* super_constructor = function_environment->current_function()->prototype();
  207. // FIXME: Functions should track their constructor kind.
  208. if (!super_constructor || !super_constructor->is_function()) {
  209. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, "Super constructor");
  210. return {};
  211. }
  212. result = vm.construct(static_cast<Function&>(*super_constructor), function, move(arguments));
  213. if (vm.exception())
  214. return {};
  215. function_environment->bind_this_value(global_object, result);
  216. } else {
  217. result = vm.call(function, this_value, move(arguments));
  218. }
  219. if (vm.exception())
  220. return {};
  221. if (is<NewExpression>(*this)) {
  222. if (result.is_object())
  223. return result;
  224. return new_object;
  225. }
  226. return result;
  227. }
  228. Value YieldExpression::execute(Interpreter&, GlobalObject&) const
  229. {
  230. // This should be transformed to a return.
  231. VERIFY_NOT_REACHED();
  232. }
  233. Value ReturnStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  234. {
  235. InterpreterNodeScope node_scope { interpreter, *this };
  236. auto value = argument() ? argument()->execute(interpreter, global_object) : js_undefined();
  237. if (interpreter.exception())
  238. return {};
  239. interpreter.vm().unwind(ScopeType::Function);
  240. return value;
  241. }
  242. Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  243. {
  244. InterpreterNodeScope node_scope { interpreter, *this };
  245. auto predicate_result = m_predicate->execute(interpreter, global_object);
  246. if (interpreter.exception())
  247. return {};
  248. if (predicate_result.to_boolean())
  249. return interpreter.execute_statement(global_object, *m_consequent);
  250. if (m_alternate)
  251. return interpreter.execute_statement(global_object, *m_alternate);
  252. return js_undefined();
  253. }
  254. Value WithStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  255. {
  256. InterpreterNodeScope node_scope { interpreter, *this };
  257. auto object_value = m_object->execute(interpreter, global_object);
  258. if (interpreter.exception())
  259. return {};
  260. auto* object = object_value.to_object(global_object);
  261. if (interpreter.exception())
  262. return {};
  263. VERIFY(object);
  264. auto* with_scope = interpreter.heap().allocate<WithScope>(global_object, *object, interpreter.vm().call_frame().scope);
  265. TemporaryChange<ScopeObject*> scope_change(interpreter.vm().call_frame().scope, with_scope);
  266. return interpreter.execute_statement(global_object, m_body).value_or(js_undefined());
  267. }
  268. Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  269. {
  270. InterpreterNodeScope node_scope { interpreter, *this };
  271. auto last_value = js_undefined();
  272. for (;;) {
  273. auto test_result = m_test->execute(interpreter, global_object);
  274. if (interpreter.exception())
  275. return {};
  276. if (!test_result.to_boolean())
  277. break;
  278. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  279. if (interpreter.exception())
  280. return {};
  281. if (interpreter.vm().should_unwind()) {
  282. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  283. interpreter.vm().stop_unwind();
  284. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  285. interpreter.vm().stop_unwind();
  286. break;
  287. } else {
  288. return last_value;
  289. }
  290. }
  291. }
  292. return last_value;
  293. }
  294. Value DoWhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  295. {
  296. InterpreterNodeScope node_scope { interpreter, *this };
  297. auto last_value = js_undefined();
  298. for (;;) {
  299. if (interpreter.exception())
  300. return {};
  301. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  302. if (interpreter.exception())
  303. return {};
  304. if (interpreter.vm().should_unwind()) {
  305. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  306. interpreter.vm().stop_unwind();
  307. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  308. interpreter.vm().stop_unwind();
  309. break;
  310. } else {
  311. return last_value;
  312. }
  313. }
  314. auto test_result = m_test->execute(interpreter, global_object);
  315. if (interpreter.exception())
  316. return {};
  317. if (!test_result.to_boolean())
  318. break;
  319. }
  320. return last_value;
  321. }
  322. Value ForStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  323. {
  324. InterpreterNodeScope node_scope { interpreter, *this };
  325. RefPtr<BlockStatement> wrapper;
  326. if (m_init && is<VariableDeclaration>(*m_init) && static_cast<VariableDeclaration const&>(*m_init).declaration_kind() != DeclarationKind::Var) {
  327. wrapper = create_ast_node<BlockStatement>(source_range());
  328. NonnullRefPtrVector<VariableDeclaration> decls;
  329. decls.append(*static_cast<VariableDeclaration const*>(m_init.ptr()));
  330. wrapper->add_variables(decls);
  331. interpreter.enter_scope(*wrapper, ScopeType::Block, global_object);
  332. }
  333. auto wrapper_cleanup = ScopeGuard([&] {
  334. if (wrapper)
  335. interpreter.exit_scope(*wrapper);
  336. });
  337. auto last_value = js_undefined();
  338. if (m_init) {
  339. m_init->execute(interpreter, global_object);
  340. if (interpreter.exception())
  341. return {};
  342. }
  343. if (m_test) {
  344. while (true) {
  345. auto test_result = m_test->execute(interpreter, global_object);
  346. if (interpreter.exception())
  347. return {};
  348. if (!test_result.to_boolean())
  349. break;
  350. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  351. if (interpreter.exception())
  352. return {};
  353. if (interpreter.vm().should_unwind()) {
  354. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  355. interpreter.vm().stop_unwind();
  356. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  357. interpreter.vm().stop_unwind();
  358. break;
  359. } else {
  360. return last_value;
  361. }
  362. }
  363. if (m_update) {
  364. m_update->execute(interpreter, global_object);
  365. if (interpreter.exception())
  366. return {};
  367. }
  368. }
  369. } else {
  370. while (true) {
  371. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  372. if (interpreter.exception())
  373. return {};
  374. if (interpreter.vm().should_unwind()) {
  375. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  376. interpreter.vm().stop_unwind();
  377. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  378. interpreter.vm().stop_unwind();
  379. break;
  380. } else {
  381. return last_value;
  382. }
  383. }
  384. if (m_update) {
  385. m_update->execute(interpreter, global_object);
  386. if (interpreter.exception())
  387. return {};
  388. }
  389. }
  390. }
  391. return last_value;
  392. }
  393. static Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>> variable_from_for_declaration(Interpreter& interpreter, GlobalObject& global_object, ASTNode const& node, RefPtr<BlockStatement> wrapper)
  394. {
  395. if (is<VariableDeclaration>(node)) {
  396. auto& variable_declaration = static_cast<VariableDeclaration const&>(node);
  397. VERIFY(!variable_declaration.declarations().is_empty());
  398. if (variable_declaration.declaration_kind() != DeclarationKind::Var) {
  399. wrapper = create_ast_node<BlockStatement>(node.source_range());
  400. interpreter.enter_scope(*wrapper, ScopeType::Block, global_object);
  401. }
  402. variable_declaration.execute(interpreter, global_object);
  403. return variable_declaration.declarations().first().target();
  404. }
  405. if (is<Identifier>(node)) {
  406. return NonnullRefPtr(static_cast<Identifier const&>(node));
  407. }
  408. VERIFY_NOT_REACHED();
  409. }
  410. Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  411. {
  412. InterpreterNodeScope node_scope { interpreter, *this };
  413. bool has_declaration = is<VariableDeclaration>(*m_lhs);
  414. if (!has_declaration && !is<Identifier>(*m_lhs)) {
  415. // FIXME: Implement "for (foo.bar in baz)", "for (foo[0] in bar)"
  416. VERIFY_NOT_REACHED();
  417. }
  418. RefPtr<BlockStatement> wrapper;
  419. auto target = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  420. auto wrapper_cleanup = ScopeGuard([&] {
  421. if (wrapper)
  422. interpreter.exit_scope(*wrapper);
  423. });
  424. auto last_value = js_undefined();
  425. auto rhs_result = m_rhs->execute(interpreter, global_object);
  426. if (interpreter.exception())
  427. return {};
  428. if (rhs_result.is_nullish())
  429. return {};
  430. auto* object = rhs_result.to_object(global_object);
  431. while (object) {
  432. auto property_names = object->get_enumerable_own_property_names(Object::PropertyKind::Key);
  433. for (auto& value : property_names) {
  434. interpreter.vm().assign(target, value, global_object, has_declaration);
  435. if (interpreter.exception())
  436. return {};
  437. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  438. if (interpreter.exception())
  439. return {};
  440. if (interpreter.vm().should_unwind()) {
  441. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  442. interpreter.vm().stop_unwind();
  443. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  444. interpreter.vm().stop_unwind();
  445. break;
  446. } else {
  447. return last_value;
  448. }
  449. }
  450. }
  451. object = object->prototype();
  452. if (interpreter.exception())
  453. return {};
  454. }
  455. return last_value;
  456. }
  457. Value ForOfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  458. {
  459. InterpreterNodeScope node_scope { interpreter, *this };
  460. bool has_declaration = is<VariableDeclaration>(*m_lhs);
  461. if (!has_declaration && !is<Identifier>(*m_lhs)) {
  462. // FIXME: Implement "for (foo.bar of baz)", "for (foo[0] of bar)"
  463. VERIFY_NOT_REACHED();
  464. }
  465. RefPtr<BlockStatement> wrapper;
  466. auto target = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  467. auto wrapper_cleanup = ScopeGuard([&] {
  468. if (wrapper)
  469. interpreter.exit_scope(*wrapper);
  470. });
  471. auto last_value = js_undefined();
  472. auto rhs_result = m_rhs->execute(interpreter, global_object);
  473. if (interpreter.exception())
  474. return {};
  475. get_iterator_values(global_object, rhs_result, [&](Value value) {
  476. interpreter.vm().assign(target, value, global_object, has_declaration);
  477. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  478. if (interpreter.exception())
  479. return IterationDecision::Break;
  480. if (interpreter.vm().should_unwind()) {
  481. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  482. interpreter.vm().stop_unwind();
  483. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  484. interpreter.vm().stop_unwind();
  485. return IterationDecision::Break;
  486. } else {
  487. return IterationDecision::Break;
  488. }
  489. }
  490. return IterationDecision::Continue;
  491. });
  492. if (interpreter.exception())
  493. return {};
  494. return last_value;
  495. }
  496. Value BinaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  497. {
  498. InterpreterNodeScope node_scope { interpreter, *this };
  499. auto lhs_result = m_lhs->execute(interpreter, global_object);
  500. if (interpreter.exception())
  501. return {};
  502. auto rhs_result = m_rhs->execute(interpreter, global_object);
  503. if (interpreter.exception())
  504. return {};
  505. switch (m_op) {
  506. case BinaryOp::Addition:
  507. return add(global_object, lhs_result, rhs_result);
  508. case BinaryOp::Subtraction:
  509. return sub(global_object, lhs_result, rhs_result);
  510. case BinaryOp::Multiplication:
  511. return mul(global_object, lhs_result, rhs_result);
  512. case BinaryOp::Division:
  513. return div(global_object, lhs_result, rhs_result);
  514. case BinaryOp::Modulo:
  515. return mod(global_object, lhs_result, rhs_result);
  516. case BinaryOp::Exponentiation:
  517. return exp(global_object, lhs_result, rhs_result);
  518. case BinaryOp::TypedEquals:
  519. return Value(strict_eq(lhs_result, rhs_result));
  520. case BinaryOp::TypedInequals:
  521. return Value(!strict_eq(lhs_result, rhs_result));
  522. case BinaryOp::AbstractEquals:
  523. return Value(abstract_eq(global_object, lhs_result, rhs_result));
  524. case BinaryOp::AbstractInequals:
  525. return Value(!abstract_eq(global_object, lhs_result, rhs_result));
  526. case BinaryOp::GreaterThan:
  527. return greater_than(global_object, lhs_result, rhs_result);
  528. case BinaryOp::GreaterThanEquals:
  529. return greater_than_equals(global_object, lhs_result, rhs_result);
  530. case BinaryOp::LessThan:
  531. return less_than(global_object, lhs_result, rhs_result);
  532. case BinaryOp::LessThanEquals:
  533. return less_than_equals(global_object, lhs_result, rhs_result);
  534. case BinaryOp::BitwiseAnd:
  535. return bitwise_and(global_object, lhs_result, rhs_result);
  536. case BinaryOp::BitwiseOr:
  537. return bitwise_or(global_object, lhs_result, rhs_result);
  538. case BinaryOp::BitwiseXor:
  539. return bitwise_xor(global_object, lhs_result, rhs_result);
  540. case BinaryOp::LeftShift:
  541. return left_shift(global_object, lhs_result, rhs_result);
  542. case BinaryOp::RightShift:
  543. return right_shift(global_object, lhs_result, rhs_result);
  544. case BinaryOp::UnsignedRightShift:
  545. return unsigned_right_shift(global_object, lhs_result, rhs_result);
  546. case BinaryOp::In:
  547. return in(global_object, lhs_result, rhs_result);
  548. case BinaryOp::InstanceOf:
  549. return instance_of(global_object, lhs_result, rhs_result);
  550. }
  551. VERIFY_NOT_REACHED();
  552. }
  553. Value LogicalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  554. {
  555. InterpreterNodeScope node_scope { interpreter, *this };
  556. auto lhs_result = m_lhs->execute(interpreter, global_object);
  557. if (interpreter.exception())
  558. return {};
  559. switch (m_op) {
  560. case LogicalOp::And:
  561. if (lhs_result.to_boolean()) {
  562. auto rhs_result = m_rhs->execute(interpreter, global_object);
  563. if (interpreter.exception())
  564. return {};
  565. return rhs_result;
  566. }
  567. return lhs_result;
  568. case LogicalOp::Or: {
  569. if (lhs_result.to_boolean())
  570. return lhs_result;
  571. auto rhs_result = m_rhs->execute(interpreter, global_object);
  572. if (interpreter.exception())
  573. return {};
  574. return rhs_result;
  575. }
  576. case LogicalOp::NullishCoalescing:
  577. if (lhs_result.is_nullish()) {
  578. auto rhs_result = m_rhs->execute(interpreter, global_object);
  579. if (interpreter.exception())
  580. return {};
  581. return rhs_result;
  582. }
  583. return lhs_result;
  584. }
  585. VERIFY_NOT_REACHED();
  586. }
  587. Reference Expression::to_reference(Interpreter&, GlobalObject&) const
  588. {
  589. return {};
  590. }
  591. Reference Identifier::to_reference(Interpreter& interpreter, GlobalObject&) const
  592. {
  593. return interpreter.vm().get_reference(string());
  594. }
  595. Reference MemberExpression::to_reference(Interpreter& interpreter, GlobalObject& global_object) const
  596. {
  597. auto object_value = m_object->execute(interpreter, global_object);
  598. if (interpreter.exception())
  599. return {};
  600. auto property_name = computed_property_name(interpreter, global_object);
  601. if (!property_name.is_valid())
  602. return {};
  603. return { object_value, property_name };
  604. }
  605. Value UnaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  606. {
  607. InterpreterNodeScope node_scope { interpreter, *this };
  608. auto& vm = interpreter.vm();
  609. if (m_op == UnaryOp::Delete) {
  610. auto reference = m_lhs->to_reference(interpreter, global_object);
  611. if (interpreter.exception())
  612. return {};
  613. return Value(reference.delete_(global_object));
  614. }
  615. Value lhs_result;
  616. if (m_op == UnaryOp::Typeof && is<Identifier>(*m_lhs)) {
  617. auto reference = m_lhs->to_reference(interpreter, global_object);
  618. if (interpreter.exception()) {
  619. return {};
  620. }
  621. // FIXME: standard recommends checking with is_unresolvable but it ALWAYS return false here
  622. if (reference.is_local_variable() || reference.is_global_variable()) {
  623. const auto& name = reference.name();
  624. lhs_result = interpreter.vm().get_variable(name.to_string(), global_object).value_or(js_undefined());
  625. if (interpreter.exception())
  626. return {};
  627. }
  628. } else {
  629. lhs_result = m_lhs->execute(interpreter, global_object);
  630. if (interpreter.exception())
  631. return {};
  632. }
  633. switch (m_op) {
  634. case UnaryOp::BitwiseNot:
  635. return bitwise_not(global_object, lhs_result);
  636. case UnaryOp::Not:
  637. return Value(!lhs_result.to_boolean());
  638. case UnaryOp::Plus:
  639. return unary_plus(global_object, lhs_result);
  640. case UnaryOp::Minus:
  641. return unary_minus(global_object, lhs_result);
  642. case UnaryOp::Typeof:
  643. return js_string(vm, lhs_result.typeof());
  644. case UnaryOp::Void:
  645. return js_undefined();
  646. case UnaryOp::Delete:
  647. VERIFY_NOT_REACHED();
  648. }
  649. VERIFY_NOT_REACHED();
  650. }
  651. Value SuperExpression::execute(Interpreter& interpreter, GlobalObject&) const
  652. {
  653. InterpreterNodeScope node_scope { interpreter, *this };
  654. // The semantics for SuperExpressions are handled in CallExpression::compute_this_and_callee()
  655. VERIFY_NOT_REACHED();
  656. }
  657. Value ClassMethod::execute(Interpreter& interpreter, GlobalObject& global_object) const
  658. {
  659. InterpreterNodeScope node_scope { interpreter, *this };
  660. return m_function->execute(interpreter, global_object);
  661. }
  662. Value ClassExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  663. {
  664. InterpreterNodeScope node_scope { interpreter, *this };
  665. auto& vm = interpreter.vm();
  666. Value class_constructor_value = m_constructor->execute(interpreter, global_object);
  667. if (interpreter.exception())
  668. return {};
  669. update_function_name(class_constructor_value, m_name);
  670. VERIFY(class_constructor_value.is_function() && is<ScriptFunction>(class_constructor_value.as_function()));
  671. auto* class_constructor = static_cast<ScriptFunction*>(&class_constructor_value.as_function());
  672. class_constructor->set_is_class_constructor();
  673. Value super_constructor = js_undefined();
  674. if (!m_super_class.is_null()) {
  675. super_constructor = m_super_class->execute(interpreter, global_object);
  676. if (interpreter.exception())
  677. return {};
  678. if (!super_constructor.is_function() && !super_constructor.is_null()) {
  679. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ClassExtendsValueNotAConstructorOrNull, super_constructor.to_string_without_side_effects());
  680. return {};
  681. }
  682. class_constructor->set_constructor_kind(Function::ConstructorKind::Derived);
  683. Object* prototype = Object::create_empty(global_object);
  684. Object* super_constructor_prototype = nullptr;
  685. if (!super_constructor.is_null()) {
  686. auto super_constructor_prototype_value = super_constructor.as_object().get(vm.names.prototype).value_or(js_undefined());
  687. if (interpreter.exception())
  688. return {};
  689. if (!super_constructor_prototype_value.is_object() && !super_constructor_prototype_value.is_null()) {
  690. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ClassExtendsValueInvalidPrototype, super_constructor_prototype_value.to_string_without_side_effects());
  691. return {};
  692. }
  693. if (super_constructor_prototype_value.is_object())
  694. super_constructor_prototype = &super_constructor_prototype_value.as_object();
  695. }
  696. prototype->set_prototype(super_constructor_prototype);
  697. prototype->define_property(vm.names.constructor, class_constructor, 0);
  698. if (interpreter.exception())
  699. return {};
  700. class_constructor->define_property(vm.names.prototype, prototype, Attribute::Writable);
  701. if (interpreter.exception())
  702. return {};
  703. class_constructor->set_prototype(super_constructor.is_null() ? global_object.function_prototype() : &super_constructor.as_object());
  704. }
  705. auto class_prototype = class_constructor->get(vm.names.prototype);
  706. if (interpreter.exception())
  707. return {};
  708. if (!class_prototype.is_object()) {
  709. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Class prototype");
  710. return {};
  711. }
  712. for (const auto& method : m_methods) {
  713. auto method_value = method.execute(interpreter, global_object);
  714. if (interpreter.exception())
  715. return {};
  716. auto& method_function = method_value.as_function();
  717. auto key = method.key().execute(interpreter, global_object);
  718. if (interpreter.exception())
  719. return {};
  720. auto& target = method.is_static() ? *class_constructor : class_prototype.as_object();
  721. method_function.set_home_object(&target);
  722. switch (method.kind()) {
  723. case ClassMethod::Kind::Method:
  724. target.define_property(key.to_property_key(global_object), method_value);
  725. break;
  726. case ClassMethod::Kind::Getter:
  727. update_function_name(method_value, String::formatted("get {}", get_function_name(global_object, key)));
  728. target.define_accessor(key.to_property_key(global_object), &method_function, nullptr, Attribute::Configurable | Attribute::Enumerable);
  729. break;
  730. case ClassMethod::Kind::Setter:
  731. update_function_name(method_value, String::formatted("set {}", get_function_name(global_object, key)));
  732. target.define_accessor(key.to_property_key(global_object), nullptr, &method_function, Attribute::Configurable | Attribute::Enumerable);
  733. break;
  734. default:
  735. VERIFY_NOT_REACHED();
  736. }
  737. if (interpreter.exception())
  738. return {};
  739. }
  740. return class_constructor;
  741. }
  742. Value ClassDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  743. {
  744. InterpreterNodeScope node_scope { interpreter, *this };
  745. Value class_constructor = m_class_expression->execute(interpreter, global_object);
  746. if (interpreter.exception())
  747. return {};
  748. interpreter.current_scope()->put_to_scope(m_class_expression->name(), { class_constructor, DeclarationKind::Let });
  749. return {};
  750. }
  751. static void print_indent(int indent)
  752. {
  753. out("{}", String::repeated(' ', indent * 2));
  754. }
  755. void ASTNode::dump(int indent) const
  756. {
  757. print_indent(indent);
  758. outln("{}", class_name());
  759. }
  760. void ScopeNode::dump(int indent) const
  761. {
  762. ASTNode::dump(indent);
  763. if (!m_variables.is_empty()) {
  764. print_indent(indent + 1);
  765. outln("(Variables)");
  766. for (auto& variable : m_variables)
  767. variable.dump(indent + 2);
  768. }
  769. if (!m_children.is_empty()) {
  770. print_indent(indent + 1);
  771. outln("(Children)");
  772. for (auto& child : children())
  773. child.dump(indent + 2);
  774. }
  775. }
  776. void BinaryExpression::dump(int indent) const
  777. {
  778. const char* op_string = nullptr;
  779. switch (m_op) {
  780. case BinaryOp::Addition:
  781. op_string = "+";
  782. break;
  783. case BinaryOp::Subtraction:
  784. op_string = "-";
  785. break;
  786. case BinaryOp::Multiplication:
  787. op_string = "*";
  788. break;
  789. case BinaryOp::Division:
  790. op_string = "/";
  791. break;
  792. case BinaryOp::Modulo:
  793. op_string = "%";
  794. break;
  795. case BinaryOp::Exponentiation:
  796. op_string = "**";
  797. break;
  798. case BinaryOp::TypedEquals:
  799. op_string = "===";
  800. break;
  801. case BinaryOp::TypedInequals:
  802. op_string = "!==";
  803. break;
  804. case BinaryOp::AbstractEquals:
  805. op_string = "==";
  806. break;
  807. case BinaryOp::AbstractInequals:
  808. op_string = "!=";
  809. break;
  810. case BinaryOp::GreaterThan:
  811. op_string = ">";
  812. break;
  813. case BinaryOp::GreaterThanEquals:
  814. op_string = ">=";
  815. break;
  816. case BinaryOp::LessThan:
  817. op_string = "<";
  818. break;
  819. case BinaryOp::LessThanEquals:
  820. op_string = "<=";
  821. break;
  822. case BinaryOp::BitwiseAnd:
  823. op_string = "&";
  824. break;
  825. case BinaryOp::BitwiseOr:
  826. op_string = "|";
  827. break;
  828. case BinaryOp::BitwiseXor:
  829. op_string = "^";
  830. break;
  831. case BinaryOp::LeftShift:
  832. op_string = "<<";
  833. break;
  834. case BinaryOp::RightShift:
  835. op_string = ">>";
  836. break;
  837. case BinaryOp::UnsignedRightShift:
  838. op_string = ">>>";
  839. break;
  840. case BinaryOp::In:
  841. op_string = "in";
  842. break;
  843. case BinaryOp::InstanceOf:
  844. op_string = "instanceof";
  845. break;
  846. }
  847. print_indent(indent);
  848. outln("{}", class_name());
  849. m_lhs->dump(indent + 1);
  850. print_indent(indent + 1);
  851. outln("{}", op_string);
  852. m_rhs->dump(indent + 1);
  853. }
  854. void LogicalExpression::dump(int indent) const
  855. {
  856. const char* op_string = nullptr;
  857. switch (m_op) {
  858. case LogicalOp::And:
  859. op_string = "&&";
  860. break;
  861. case LogicalOp::Or:
  862. op_string = "||";
  863. break;
  864. case LogicalOp::NullishCoalescing:
  865. op_string = "??";
  866. break;
  867. }
  868. print_indent(indent);
  869. outln("{}", class_name());
  870. m_lhs->dump(indent + 1);
  871. print_indent(indent + 1);
  872. outln("{}", op_string);
  873. m_rhs->dump(indent + 1);
  874. }
  875. void UnaryExpression::dump(int indent) const
  876. {
  877. const char* op_string = nullptr;
  878. switch (m_op) {
  879. case UnaryOp::BitwiseNot:
  880. op_string = "~";
  881. break;
  882. case UnaryOp::Not:
  883. op_string = "!";
  884. break;
  885. case UnaryOp::Plus:
  886. op_string = "+";
  887. break;
  888. case UnaryOp::Minus:
  889. op_string = "-";
  890. break;
  891. case UnaryOp::Typeof:
  892. op_string = "typeof ";
  893. break;
  894. case UnaryOp::Void:
  895. op_string = "void ";
  896. break;
  897. case UnaryOp::Delete:
  898. op_string = "delete ";
  899. break;
  900. }
  901. print_indent(indent);
  902. outln("{}", class_name());
  903. print_indent(indent + 1);
  904. outln("{}", op_string);
  905. m_lhs->dump(indent + 1);
  906. }
  907. void CallExpression::dump(int indent) const
  908. {
  909. print_indent(indent);
  910. if (is<NewExpression>(*this))
  911. outln("CallExpression [new]");
  912. else
  913. outln("CallExpression");
  914. m_callee->dump(indent + 1);
  915. for (auto& argument : m_arguments)
  916. argument.value->dump(indent + 1);
  917. }
  918. void ClassDeclaration::dump(int indent) const
  919. {
  920. ASTNode::dump(indent);
  921. m_class_expression->dump(indent + 1);
  922. }
  923. void ClassExpression::dump(int indent) const
  924. {
  925. print_indent(indent);
  926. outln("ClassExpression: \"{}\"", m_name);
  927. print_indent(indent);
  928. outln("(Constructor)");
  929. m_constructor->dump(indent + 1);
  930. if (!m_super_class.is_null()) {
  931. print_indent(indent);
  932. outln("(Super Class)");
  933. m_super_class->dump(indent + 1);
  934. }
  935. print_indent(indent);
  936. outln("(Methods)");
  937. for (auto& method : m_methods)
  938. method.dump(indent + 1);
  939. }
  940. void ClassMethod::dump(int indent) const
  941. {
  942. ASTNode::dump(indent);
  943. print_indent(indent);
  944. outln("(Key)");
  945. m_key->dump(indent + 1);
  946. const char* kind_string = nullptr;
  947. switch (m_kind) {
  948. case Kind::Method:
  949. kind_string = "Method";
  950. break;
  951. case Kind::Getter:
  952. kind_string = "Getter";
  953. break;
  954. case Kind::Setter:
  955. kind_string = "Setter";
  956. break;
  957. }
  958. print_indent(indent);
  959. outln("Kind: {}", kind_string);
  960. print_indent(indent);
  961. outln("Static: {}", m_is_static);
  962. print_indent(indent);
  963. outln("(Function)");
  964. m_function->dump(indent + 1);
  965. }
  966. void StringLiteral::dump(int indent) const
  967. {
  968. print_indent(indent);
  969. outln("StringLiteral \"{}\"", m_value);
  970. }
  971. void SuperExpression::dump(int indent) const
  972. {
  973. print_indent(indent);
  974. outln("super");
  975. }
  976. void NumericLiteral::dump(int indent) const
  977. {
  978. print_indent(indent);
  979. outln("NumericLiteral {}", m_value);
  980. }
  981. void BigIntLiteral::dump(int indent) const
  982. {
  983. print_indent(indent);
  984. outln("BigIntLiteral {}", m_value);
  985. }
  986. void BooleanLiteral::dump(int indent) const
  987. {
  988. print_indent(indent);
  989. outln("BooleanLiteral {}", m_value);
  990. }
  991. void NullLiteral::dump(int indent) const
  992. {
  993. print_indent(indent);
  994. outln("null");
  995. }
  996. void BindingPattern::dump(int indent) const
  997. {
  998. print_indent(indent);
  999. outln("BindingPattern {}", kind == Kind::Array ? "Array" : "Object");
  1000. print_indent(++indent);
  1001. outln("(Properties)");
  1002. for (auto& property : properties) {
  1003. print_indent(indent + 1);
  1004. outln("(Identifier)");
  1005. if (property.name) {
  1006. property.name->dump(indent + 2);
  1007. } else {
  1008. print_indent(indent + 2);
  1009. outln("(None)");
  1010. }
  1011. print_indent(indent + 1);
  1012. outln("(Pattern)");
  1013. if (property.pattern) {
  1014. property.pattern->dump(indent + 2);
  1015. } else {
  1016. print_indent(indent + 2);
  1017. outln("(None)");
  1018. }
  1019. print_indent(indent + 1);
  1020. outln("(Is Rest = {})", property.is_rest);
  1021. }
  1022. }
  1023. void FunctionNode::dump(int indent, String const& class_name) const
  1024. {
  1025. print_indent(indent);
  1026. outln("{}{} '{}'", class_name, m_is_generator ? "*" : "", name());
  1027. if (!m_parameters.is_empty()) {
  1028. print_indent(indent + 1);
  1029. outln("(Parameters)");
  1030. for (auto& parameter : m_parameters) {
  1031. print_indent(indent + 2);
  1032. if (parameter.is_rest)
  1033. out("...");
  1034. parameter.binding.visit(
  1035. [&](FlyString const& name) {
  1036. outln("{}", name);
  1037. },
  1038. [&](BindingPattern const& pattern) {
  1039. pattern.dump(indent + 2);
  1040. });
  1041. if (parameter.default_value)
  1042. parameter.default_value->dump(indent + 3);
  1043. }
  1044. }
  1045. if (!m_variables.is_empty()) {
  1046. print_indent(indent + 1);
  1047. outln("(Variables)");
  1048. for (auto& variable : m_variables)
  1049. variable.dump(indent + 2);
  1050. }
  1051. print_indent(indent + 1);
  1052. outln("(Body)");
  1053. body().dump(indent + 2);
  1054. }
  1055. void FunctionDeclaration::dump(int indent) const
  1056. {
  1057. FunctionNode::dump(indent, class_name());
  1058. }
  1059. void FunctionExpression::dump(int indent) const
  1060. {
  1061. FunctionNode::dump(indent, class_name());
  1062. }
  1063. void YieldExpression::dump(int indent) const
  1064. {
  1065. ASTNode::dump(indent);
  1066. if (argument())
  1067. argument()->dump(indent + 1);
  1068. }
  1069. void ReturnStatement::dump(int indent) const
  1070. {
  1071. ASTNode::dump(indent);
  1072. if (argument())
  1073. argument()->dump(indent + 1);
  1074. }
  1075. void IfStatement::dump(int indent) const
  1076. {
  1077. ASTNode::dump(indent);
  1078. print_indent(indent);
  1079. outln("If");
  1080. predicate().dump(indent + 1);
  1081. consequent().dump(indent + 1);
  1082. if (alternate()) {
  1083. print_indent(indent);
  1084. outln("Else");
  1085. alternate()->dump(indent + 1);
  1086. }
  1087. }
  1088. void WhileStatement::dump(int indent) const
  1089. {
  1090. ASTNode::dump(indent);
  1091. print_indent(indent);
  1092. outln("While");
  1093. test().dump(indent + 1);
  1094. body().dump(indent + 1);
  1095. }
  1096. void WithStatement::dump(int indent) const
  1097. {
  1098. ASTNode::dump(indent);
  1099. print_indent(indent + 1);
  1100. outln("Object");
  1101. object().dump(indent + 2);
  1102. print_indent(indent + 1);
  1103. outln("Body");
  1104. body().dump(indent + 2);
  1105. }
  1106. void DoWhileStatement::dump(int indent) const
  1107. {
  1108. ASTNode::dump(indent);
  1109. print_indent(indent);
  1110. outln("DoWhile");
  1111. test().dump(indent + 1);
  1112. body().dump(indent + 1);
  1113. }
  1114. void ForStatement::dump(int indent) const
  1115. {
  1116. ASTNode::dump(indent);
  1117. print_indent(indent);
  1118. outln("For");
  1119. if (init())
  1120. init()->dump(indent + 1);
  1121. if (test())
  1122. test()->dump(indent + 1);
  1123. if (update())
  1124. update()->dump(indent + 1);
  1125. body().dump(indent + 1);
  1126. }
  1127. void ForInStatement::dump(int indent) const
  1128. {
  1129. ASTNode::dump(indent);
  1130. print_indent(indent);
  1131. outln("ForIn");
  1132. lhs().dump(indent + 1);
  1133. rhs().dump(indent + 1);
  1134. body().dump(indent + 1);
  1135. }
  1136. void ForOfStatement::dump(int indent) const
  1137. {
  1138. ASTNode::dump(indent);
  1139. print_indent(indent);
  1140. outln("ForOf");
  1141. lhs().dump(indent + 1);
  1142. rhs().dump(indent + 1);
  1143. body().dump(indent + 1);
  1144. }
  1145. Value Identifier::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1146. {
  1147. InterpreterNodeScope node_scope { interpreter, *this };
  1148. auto value = interpreter.vm().get_variable(string(), global_object);
  1149. if (value.is_empty()) {
  1150. if (!interpreter.exception())
  1151. interpreter.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, string());
  1152. return {};
  1153. }
  1154. return value;
  1155. }
  1156. void Identifier::dump(int indent) const
  1157. {
  1158. print_indent(indent);
  1159. outln("Identifier \"{}\"", m_string);
  1160. }
  1161. void SpreadExpression::dump(int indent) const
  1162. {
  1163. ASTNode::dump(indent);
  1164. m_target->dump(indent + 1);
  1165. }
  1166. Value SpreadExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1167. {
  1168. InterpreterNodeScope node_scope { interpreter, *this };
  1169. return m_target->execute(interpreter, global_object);
  1170. }
  1171. Value ThisExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1172. {
  1173. InterpreterNodeScope node_scope { interpreter, *this };
  1174. return interpreter.vm().resolve_this_binding(global_object);
  1175. }
  1176. void ThisExpression::dump(int indent) const
  1177. {
  1178. ASTNode::dump(indent);
  1179. }
  1180. Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1181. {
  1182. InterpreterNodeScope node_scope { interpreter, *this };
  1183. #define EXECUTE_LHS_AND_RHS() \
  1184. do { \
  1185. lhs_result = m_lhs->execute(interpreter, global_object); \
  1186. if (interpreter.exception()) \
  1187. return {}; \
  1188. rhs_result = m_rhs->execute(interpreter, global_object); \
  1189. if (interpreter.exception()) \
  1190. return {}; \
  1191. } while (0)
  1192. Value lhs_result;
  1193. Value rhs_result;
  1194. switch (m_op) {
  1195. case AssignmentOp::Assignment:
  1196. break;
  1197. case AssignmentOp::AdditionAssignment:
  1198. EXECUTE_LHS_AND_RHS();
  1199. rhs_result = add(global_object, lhs_result, rhs_result);
  1200. break;
  1201. case AssignmentOp::SubtractionAssignment:
  1202. EXECUTE_LHS_AND_RHS();
  1203. rhs_result = sub(global_object, lhs_result, rhs_result);
  1204. break;
  1205. case AssignmentOp::MultiplicationAssignment:
  1206. EXECUTE_LHS_AND_RHS();
  1207. rhs_result = mul(global_object, lhs_result, rhs_result);
  1208. break;
  1209. case AssignmentOp::DivisionAssignment:
  1210. EXECUTE_LHS_AND_RHS();
  1211. rhs_result = div(global_object, lhs_result, rhs_result);
  1212. break;
  1213. case AssignmentOp::ModuloAssignment:
  1214. EXECUTE_LHS_AND_RHS();
  1215. rhs_result = mod(global_object, lhs_result, rhs_result);
  1216. break;
  1217. case AssignmentOp::ExponentiationAssignment:
  1218. EXECUTE_LHS_AND_RHS();
  1219. rhs_result = exp(global_object, lhs_result, rhs_result);
  1220. break;
  1221. case AssignmentOp::BitwiseAndAssignment:
  1222. EXECUTE_LHS_AND_RHS();
  1223. rhs_result = bitwise_and(global_object, lhs_result, rhs_result);
  1224. break;
  1225. case AssignmentOp::BitwiseOrAssignment:
  1226. EXECUTE_LHS_AND_RHS();
  1227. rhs_result = bitwise_or(global_object, lhs_result, rhs_result);
  1228. break;
  1229. case AssignmentOp::BitwiseXorAssignment:
  1230. EXECUTE_LHS_AND_RHS();
  1231. rhs_result = bitwise_xor(global_object, lhs_result, rhs_result);
  1232. break;
  1233. case AssignmentOp::LeftShiftAssignment:
  1234. EXECUTE_LHS_AND_RHS();
  1235. rhs_result = left_shift(global_object, lhs_result, rhs_result);
  1236. break;
  1237. case AssignmentOp::RightShiftAssignment:
  1238. EXECUTE_LHS_AND_RHS();
  1239. rhs_result = right_shift(global_object, lhs_result, rhs_result);
  1240. break;
  1241. case AssignmentOp::UnsignedRightShiftAssignment:
  1242. EXECUTE_LHS_AND_RHS();
  1243. rhs_result = unsigned_right_shift(global_object, lhs_result, rhs_result);
  1244. break;
  1245. case AssignmentOp::AndAssignment:
  1246. lhs_result = m_lhs->execute(interpreter, global_object);
  1247. if (interpreter.exception())
  1248. return {};
  1249. if (!lhs_result.to_boolean())
  1250. return lhs_result;
  1251. rhs_result = m_rhs->execute(interpreter, global_object);
  1252. break;
  1253. case AssignmentOp::OrAssignment:
  1254. lhs_result = m_lhs->execute(interpreter, global_object);
  1255. if (interpreter.exception())
  1256. return {};
  1257. if (lhs_result.to_boolean())
  1258. return lhs_result;
  1259. rhs_result = m_rhs->execute(interpreter, global_object);
  1260. break;
  1261. case AssignmentOp::NullishAssignment:
  1262. lhs_result = m_lhs->execute(interpreter, global_object);
  1263. if (interpreter.exception())
  1264. return {};
  1265. if (!lhs_result.is_nullish())
  1266. return lhs_result;
  1267. rhs_result = m_rhs->execute(interpreter, global_object);
  1268. break;
  1269. }
  1270. if (interpreter.exception())
  1271. return {};
  1272. auto reference = m_lhs->to_reference(interpreter, global_object);
  1273. if (interpreter.exception())
  1274. return {};
  1275. if (m_op == AssignmentOp::Assignment) {
  1276. rhs_result = m_rhs->execute(interpreter, global_object);
  1277. if (interpreter.exception())
  1278. return {};
  1279. }
  1280. if (reference.is_unresolvable()) {
  1281. interpreter.vm().throw_exception<ReferenceError>(global_object, ErrorType::InvalidLeftHandAssignment);
  1282. return {};
  1283. }
  1284. reference.put(global_object, rhs_result);
  1285. if (interpreter.exception())
  1286. return {};
  1287. return rhs_result;
  1288. }
  1289. Value UpdateExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1290. {
  1291. InterpreterNodeScope node_scope { interpreter, *this };
  1292. auto reference = m_argument->to_reference(interpreter, global_object);
  1293. if (interpreter.exception())
  1294. return {};
  1295. auto old_value = reference.get(global_object);
  1296. if (interpreter.exception())
  1297. return {};
  1298. old_value = old_value.to_numeric(global_object);
  1299. if (interpreter.exception())
  1300. return {};
  1301. Value new_value;
  1302. switch (m_op) {
  1303. case UpdateOp::Increment:
  1304. if (old_value.is_number())
  1305. new_value = Value(old_value.as_double() + 1);
  1306. else
  1307. new_value = js_bigint(interpreter.heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  1308. break;
  1309. case UpdateOp::Decrement:
  1310. if (old_value.is_number())
  1311. new_value = Value(old_value.as_double() - 1);
  1312. else
  1313. new_value = js_bigint(interpreter.heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  1314. break;
  1315. default:
  1316. VERIFY_NOT_REACHED();
  1317. }
  1318. reference.put(global_object, new_value);
  1319. if (interpreter.exception())
  1320. return {};
  1321. return m_prefixed ? new_value : old_value;
  1322. }
  1323. void AssignmentExpression::dump(int indent) const
  1324. {
  1325. const char* op_string = nullptr;
  1326. switch (m_op) {
  1327. case AssignmentOp::Assignment:
  1328. op_string = "=";
  1329. break;
  1330. case AssignmentOp::AdditionAssignment:
  1331. op_string = "+=";
  1332. break;
  1333. case AssignmentOp::SubtractionAssignment:
  1334. op_string = "-=";
  1335. break;
  1336. case AssignmentOp::MultiplicationAssignment:
  1337. op_string = "*=";
  1338. break;
  1339. case AssignmentOp::DivisionAssignment:
  1340. op_string = "/=";
  1341. break;
  1342. case AssignmentOp::ModuloAssignment:
  1343. op_string = "%=";
  1344. break;
  1345. case AssignmentOp::ExponentiationAssignment:
  1346. op_string = "**=";
  1347. break;
  1348. case AssignmentOp::BitwiseAndAssignment:
  1349. op_string = "&=";
  1350. break;
  1351. case AssignmentOp::BitwiseOrAssignment:
  1352. op_string = "|=";
  1353. break;
  1354. case AssignmentOp::BitwiseXorAssignment:
  1355. op_string = "^=";
  1356. break;
  1357. case AssignmentOp::LeftShiftAssignment:
  1358. op_string = "<<=";
  1359. break;
  1360. case AssignmentOp::RightShiftAssignment:
  1361. op_string = ">>=";
  1362. break;
  1363. case AssignmentOp::UnsignedRightShiftAssignment:
  1364. op_string = ">>>=";
  1365. break;
  1366. case AssignmentOp::AndAssignment:
  1367. op_string = "&&=";
  1368. break;
  1369. case AssignmentOp::OrAssignment:
  1370. op_string = "||=";
  1371. break;
  1372. case AssignmentOp::NullishAssignment:
  1373. op_string = "\?\?=";
  1374. break;
  1375. }
  1376. ASTNode::dump(indent);
  1377. print_indent(indent + 1);
  1378. outln("{}", op_string);
  1379. m_lhs->dump(indent + 1);
  1380. m_rhs->dump(indent + 1);
  1381. }
  1382. void UpdateExpression::dump(int indent) const
  1383. {
  1384. const char* op_string = nullptr;
  1385. switch (m_op) {
  1386. case UpdateOp::Increment:
  1387. op_string = "++";
  1388. break;
  1389. case UpdateOp::Decrement:
  1390. op_string = "--";
  1391. break;
  1392. }
  1393. ASTNode::dump(indent);
  1394. if (m_prefixed) {
  1395. print_indent(indent + 1);
  1396. outln("{}", op_string);
  1397. }
  1398. m_argument->dump(indent + 1);
  1399. if (!m_prefixed) {
  1400. print_indent(indent + 1);
  1401. outln("{}", op_string);
  1402. }
  1403. }
  1404. Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1405. {
  1406. InterpreterNodeScope node_scope { interpreter, *this };
  1407. for (auto& declarator : m_declarations) {
  1408. if (auto* init = declarator.init()) {
  1409. auto initalizer_result = init->execute(interpreter, global_object);
  1410. if (interpreter.exception())
  1411. return {};
  1412. declarator.target().visit(
  1413. [&](NonnullRefPtr<Identifier> const& id) {
  1414. auto variable_name = id->string();
  1415. if (is<ClassExpression>(*init))
  1416. update_function_name(initalizer_result, variable_name);
  1417. interpreter.vm().set_variable(variable_name, initalizer_result, global_object, true);
  1418. },
  1419. [&](NonnullRefPtr<BindingPattern> const& pattern) {
  1420. interpreter.vm().assign(pattern, initalizer_result, global_object, true);
  1421. });
  1422. }
  1423. }
  1424. return {};
  1425. }
  1426. Value VariableDeclarator::execute(Interpreter& interpreter, GlobalObject&) const
  1427. {
  1428. InterpreterNodeScope node_scope { interpreter, *this };
  1429. // NOTE: VariableDeclarator execution is handled by VariableDeclaration.
  1430. VERIFY_NOT_REACHED();
  1431. }
  1432. void VariableDeclaration::dump(int indent) const
  1433. {
  1434. const char* declaration_kind_string = nullptr;
  1435. switch (m_declaration_kind) {
  1436. case DeclarationKind::Let:
  1437. declaration_kind_string = "Let";
  1438. break;
  1439. case DeclarationKind::Var:
  1440. declaration_kind_string = "Var";
  1441. break;
  1442. case DeclarationKind::Const:
  1443. declaration_kind_string = "Const";
  1444. break;
  1445. }
  1446. ASTNode::dump(indent);
  1447. print_indent(indent + 1);
  1448. outln("{}", declaration_kind_string);
  1449. for (auto& declarator : m_declarations)
  1450. declarator.dump(indent + 1);
  1451. }
  1452. void VariableDeclarator::dump(int indent) const
  1453. {
  1454. ASTNode::dump(indent);
  1455. m_target.visit([indent](const auto& value) { value->dump(indent + 1); });
  1456. if (m_init)
  1457. m_init->dump(indent + 1);
  1458. }
  1459. void ObjectProperty::dump(int indent) const
  1460. {
  1461. ASTNode::dump(indent);
  1462. m_key->dump(indent + 1);
  1463. m_value->dump(indent + 1);
  1464. }
  1465. void ObjectExpression::dump(int indent) const
  1466. {
  1467. ASTNode::dump(indent);
  1468. for (auto& property : m_properties) {
  1469. property.dump(indent + 1);
  1470. }
  1471. }
  1472. void ExpressionStatement::dump(int indent) const
  1473. {
  1474. ASTNode::dump(indent);
  1475. m_expression->dump(indent + 1);
  1476. }
  1477. Value ObjectProperty::execute(Interpreter& interpreter, GlobalObject&) const
  1478. {
  1479. InterpreterNodeScope node_scope { interpreter, *this };
  1480. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  1481. VERIFY_NOT_REACHED();
  1482. }
  1483. Value ObjectExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1484. {
  1485. InterpreterNodeScope node_scope { interpreter, *this };
  1486. auto* object = Object::create_empty(global_object);
  1487. for (auto& property : m_properties) {
  1488. auto key = property.key().execute(interpreter, global_object);
  1489. if (interpreter.exception())
  1490. return {};
  1491. if (property.type() == ObjectProperty::Type::Spread) {
  1492. if (key.is_object() && key.as_object().is_array()) {
  1493. auto& array_to_spread = static_cast<Array&>(key.as_object());
  1494. for (auto& entry : array_to_spread.indexed_properties()) {
  1495. object->indexed_properties().put(object, entry.index(), entry.value_and_attributes(&array_to_spread).value);
  1496. if (interpreter.exception())
  1497. return {};
  1498. }
  1499. } else if (key.is_object()) {
  1500. auto& obj_to_spread = key.as_object();
  1501. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  1502. if (it.value.attributes.is_enumerable()) {
  1503. object->define_property(it.key, obj_to_spread.get(it.key));
  1504. if (interpreter.exception())
  1505. return {};
  1506. }
  1507. }
  1508. } else if (key.is_string()) {
  1509. auto& str_to_spread = key.as_string().string();
  1510. for (size_t i = 0; i < str_to_spread.length(); i++) {
  1511. object->define_property(i, js_string(interpreter.heap(), str_to_spread.substring(i, 1)));
  1512. if (interpreter.exception())
  1513. return {};
  1514. }
  1515. }
  1516. continue;
  1517. }
  1518. auto value = property.value().execute(interpreter, global_object);
  1519. if (interpreter.exception())
  1520. return {};
  1521. if (value.is_function() && property.is_method())
  1522. value.as_function().set_home_object(object);
  1523. String name = get_function_name(global_object, key);
  1524. if (property.type() == ObjectProperty::Type::Getter) {
  1525. name = String::formatted("get {}", name);
  1526. } else if (property.type() == ObjectProperty::Type::Setter) {
  1527. name = String::formatted("set {}", name);
  1528. }
  1529. update_function_name(value, name);
  1530. switch (property.type()) {
  1531. case ObjectProperty::Type::Getter:
  1532. VERIFY(value.is_function());
  1533. object->define_accessor(PropertyName::from_value(global_object, key), &value.as_function(), nullptr, Attribute::Configurable | Attribute::Enumerable);
  1534. break;
  1535. case ObjectProperty::Type::Setter:
  1536. VERIFY(value.is_function());
  1537. object->define_accessor(PropertyName::from_value(global_object, key), nullptr, &value.as_function(), Attribute::Configurable | Attribute::Enumerable);
  1538. break;
  1539. case ObjectProperty::Type::KeyValue:
  1540. object->define_property(PropertyName::from_value(global_object, key), value);
  1541. break;
  1542. case ObjectProperty::Type::Spread:
  1543. default:
  1544. VERIFY_NOT_REACHED();
  1545. }
  1546. if (interpreter.exception())
  1547. return {};
  1548. }
  1549. return object;
  1550. }
  1551. void MemberExpression::dump(int indent) const
  1552. {
  1553. print_indent(indent);
  1554. outln("{}(computed={})", class_name(), is_computed());
  1555. m_object->dump(indent + 1);
  1556. m_property->dump(indent + 1);
  1557. }
  1558. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter, GlobalObject& global_object) const
  1559. {
  1560. if (!is_computed()) {
  1561. VERIFY(is<Identifier>(*m_property));
  1562. return static_cast<Identifier const&>(*m_property).string();
  1563. }
  1564. auto value = m_property->execute(interpreter, global_object);
  1565. if (interpreter.exception())
  1566. return {};
  1567. VERIFY(!value.is_empty());
  1568. return PropertyName::from_value(global_object, value);
  1569. }
  1570. String MemberExpression::to_string_approximation() const
  1571. {
  1572. String object_string = "<object>";
  1573. if (is<Identifier>(*m_object))
  1574. object_string = static_cast<Identifier const&>(*m_object).string();
  1575. if (is_computed())
  1576. return String::formatted("{}[<computed>]", object_string);
  1577. VERIFY(is<Identifier>(*m_property));
  1578. return String::formatted("{}.{}", object_string, static_cast<Identifier const&>(*m_property).string());
  1579. }
  1580. Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1581. {
  1582. InterpreterNodeScope node_scope { interpreter, *this };
  1583. auto reference = to_reference(interpreter, global_object);
  1584. if (interpreter.exception())
  1585. return {};
  1586. return reference.get(global_object);
  1587. }
  1588. void MetaProperty::dump(int indent) const
  1589. {
  1590. String name;
  1591. if (m_type == MetaProperty::Type::NewTarget)
  1592. name = "new.target";
  1593. else if (m_type == MetaProperty::Type::ImportMeta)
  1594. name = "import.meta";
  1595. else
  1596. VERIFY_NOT_REACHED();
  1597. print_indent(indent);
  1598. outln("{} {}", class_name(), name);
  1599. }
  1600. Value MetaProperty::execute(Interpreter& interpreter, GlobalObject&) const
  1601. {
  1602. InterpreterNodeScope node_scope { interpreter, *this };
  1603. if (m_type == MetaProperty::Type::NewTarget)
  1604. return interpreter.vm().get_new_target().value_or(js_undefined());
  1605. if (m_type == MetaProperty::Type::ImportMeta)
  1606. TODO();
  1607. VERIFY_NOT_REACHED();
  1608. }
  1609. Value StringLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1610. {
  1611. InterpreterNodeScope node_scope { interpreter, *this };
  1612. return js_string(interpreter.heap(), m_value);
  1613. }
  1614. Value NumericLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1615. {
  1616. InterpreterNodeScope node_scope { interpreter, *this };
  1617. return Value(m_value);
  1618. }
  1619. Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1620. {
  1621. InterpreterNodeScope node_scope { interpreter, *this };
  1622. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
  1623. }
  1624. Value BooleanLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1625. {
  1626. InterpreterNodeScope node_scope { interpreter, *this };
  1627. return Value(m_value);
  1628. }
  1629. Value NullLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1630. {
  1631. InterpreterNodeScope node_scope { interpreter, *this };
  1632. return js_null();
  1633. }
  1634. void RegExpLiteral::dump(int indent) const
  1635. {
  1636. print_indent(indent);
  1637. outln("{} (/{}/{})", class_name(), pattern(), flags());
  1638. }
  1639. Value RegExpLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1640. {
  1641. InterpreterNodeScope node_scope { interpreter, *this };
  1642. return RegExpObject::create(global_object, pattern(), flags());
  1643. }
  1644. void ArrayExpression::dump(int indent) const
  1645. {
  1646. ASTNode::dump(indent);
  1647. for (auto& element : m_elements) {
  1648. if (element) {
  1649. element->dump(indent + 1);
  1650. } else {
  1651. print_indent(indent + 1);
  1652. outln("<empty>");
  1653. }
  1654. }
  1655. }
  1656. Value ArrayExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1657. {
  1658. InterpreterNodeScope node_scope { interpreter, *this };
  1659. auto* array = Array::create(global_object);
  1660. for (auto& element : m_elements) {
  1661. auto value = Value();
  1662. if (element) {
  1663. value = element->execute(interpreter, global_object);
  1664. if (interpreter.exception())
  1665. return {};
  1666. if (is<SpreadExpression>(*element)) {
  1667. get_iterator_values(global_object, value, [&](Value iterator_value) {
  1668. array->indexed_properties().append(iterator_value);
  1669. return IterationDecision::Continue;
  1670. });
  1671. if (interpreter.exception())
  1672. return {};
  1673. continue;
  1674. }
  1675. }
  1676. array->indexed_properties().append(value);
  1677. }
  1678. return array;
  1679. }
  1680. void TemplateLiteral::dump(int indent) const
  1681. {
  1682. ASTNode::dump(indent);
  1683. for (auto& expression : m_expressions)
  1684. expression.dump(indent + 1);
  1685. }
  1686. Value TemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1687. {
  1688. InterpreterNodeScope node_scope { interpreter, *this };
  1689. StringBuilder string_builder;
  1690. for (auto& expression : m_expressions) {
  1691. auto expr = expression.execute(interpreter, global_object);
  1692. if (interpreter.exception())
  1693. return {};
  1694. auto string = expr.to_string(global_object);
  1695. if (interpreter.exception())
  1696. return {};
  1697. string_builder.append(string);
  1698. }
  1699. return js_string(interpreter.heap(), string_builder.build());
  1700. }
  1701. void TaggedTemplateLiteral::dump(int indent) const
  1702. {
  1703. ASTNode::dump(indent);
  1704. print_indent(indent + 1);
  1705. outln("(Tag)");
  1706. m_tag->dump(indent + 2);
  1707. print_indent(indent + 1);
  1708. outln("(Template Literal)");
  1709. m_template_literal->dump(indent + 2);
  1710. }
  1711. Value TaggedTemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1712. {
  1713. InterpreterNodeScope node_scope { interpreter, *this };
  1714. auto& vm = interpreter.vm();
  1715. auto tag = m_tag->execute(interpreter, global_object);
  1716. if (vm.exception())
  1717. return {};
  1718. if (!tag.is_function()) {
  1719. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, tag.to_string_without_side_effects());
  1720. return {};
  1721. }
  1722. auto& tag_function = tag.as_function();
  1723. auto& expressions = m_template_literal->expressions();
  1724. auto* strings = Array::create(global_object);
  1725. MarkedValueList arguments(vm.heap());
  1726. arguments.append(strings);
  1727. for (size_t i = 0; i < expressions.size(); ++i) {
  1728. auto value = expressions[i].execute(interpreter, global_object);
  1729. if (vm.exception())
  1730. return {};
  1731. // tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
  1732. // tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
  1733. if (i % 2 == 0) {
  1734. strings->indexed_properties().append(value);
  1735. } else {
  1736. arguments.append(value);
  1737. }
  1738. }
  1739. auto* raw_strings = Array::create(global_object);
  1740. for (auto& raw_string : m_template_literal->raw_strings()) {
  1741. auto value = raw_string.execute(interpreter, global_object);
  1742. if (vm.exception())
  1743. return {};
  1744. raw_strings->indexed_properties().append(value);
  1745. }
  1746. strings->define_property(vm.names.raw, raw_strings, 0);
  1747. return vm.call(tag_function, js_undefined(), move(arguments));
  1748. }
  1749. void TryStatement::dump(int indent) const
  1750. {
  1751. ASTNode::dump(indent);
  1752. print_indent(indent);
  1753. outln("(Block)");
  1754. block().dump(indent + 1);
  1755. if (handler()) {
  1756. print_indent(indent);
  1757. outln("(Handler)");
  1758. handler()->dump(indent + 1);
  1759. }
  1760. if (finalizer()) {
  1761. print_indent(indent);
  1762. outln("(Finalizer)");
  1763. finalizer()->dump(indent + 1);
  1764. }
  1765. }
  1766. void CatchClause::dump(int indent) const
  1767. {
  1768. print_indent(indent);
  1769. if (m_parameter.is_null())
  1770. outln("CatchClause");
  1771. else
  1772. outln("CatchClause ({})", m_parameter);
  1773. body().dump(indent + 1);
  1774. }
  1775. void ThrowStatement::dump(int indent) const
  1776. {
  1777. ASTNode::dump(indent);
  1778. argument().dump(indent + 1);
  1779. }
  1780. Value TryStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1781. {
  1782. InterpreterNodeScope node_scope { interpreter, *this };
  1783. auto result = interpreter.execute_statement(global_object, m_block, ScopeType::Try);
  1784. if (auto* exception = interpreter.exception()) {
  1785. if (m_handler) {
  1786. interpreter.vm().clear_exception();
  1787. HashMap<FlyString, Variable> parameters;
  1788. parameters.set(m_handler->parameter(), Variable { exception->value(), DeclarationKind::Var });
  1789. auto* catch_scope = interpreter.heap().allocate<LexicalEnvironment>(global_object, move(parameters), interpreter.vm().call_frame().scope);
  1790. TemporaryChange<ScopeObject*> scope_change(interpreter.vm().call_frame().scope, catch_scope);
  1791. result = interpreter.execute_statement(global_object, m_handler->body());
  1792. }
  1793. }
  1794. if (m_finalizer) {
  1795. // Keep, if any, and then clear the current exception so we can
  1796. // execute() the finalizer without an exception in our way.
  1797. auto* previous_exception = interpreter.exception();
  1798. interpreter.vm().clear_exception();
  1799. // Remember what scope type we were unwinding to, and temporarily
  1800. // clear it as well (e.g. return from handler).
  1801. auto unwind_until = interpreter.vm().unwind_until();
  1802. interpreter.vm().stop_unwind();
  1803. auto finalizer_result = m_finalizer->execute(interpreter, global_object);
  1804. if (interpreter.vm().should_unwind()) {
  1805. // This was NOT a 'normal' completion (e.g. return from finalizer).
  1806. result = finalizer_result;
  1807. } else {
  1808. // Continue unwinding to whatever we found ourselves unwinding
  1809. // to when the finalizer was entered (e.g. return from handler,
  1810. // which is unaffected by normal completion from finalizer).
  1811. interpreter.vm().unwind(unwind_until);
  1812. // If we previously had an exception and the finalizer didn't
  1813. // throw a new one, restore the old one.
  1814. if (previous_exception && !interpreter.exception())
  1815. interpreter.vm().set_exception(*previous_exception);
  1816. }
  1817. }
  1818. return result.value_or(js_undefined());
  1819. }
  1820. Value CatchClause::execute(Interpreter& interpreter, GlobalObject&) const
  1821. {
  1822. InterpreterNodeScope node_scope { interpreter, *this };
  1823. // NOTE: CatchClause execution is handled by TryStatement.
  1824. VERIFY_NOT_REACHED();
  1825. return {};
  1826. }
  1827. Value ThrowStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1828. {
  1829. InterpreterNodeScope node_scope { interpreter, *this };
  1830. auto value = m_argument->execute(interpreter, global_object);
  1831. if (interpreter.vm().exception())
  1832. return {};
  1833. interpreter.vm().throw_exception(global_object, value);
  1834. return {};
  1835. }
  1836. Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1837. {
  1838. InterpreterNodeScope node_scope { interpreter, *this };
  1839. auto discriminant_result = m_discriminant->execute(interpreter, global_object);
  1840. if (interpreter.exception())
  1841. return {};
  1842. bool falling_through = false;
  1843. auto last_value = js_undefined();
  1844. for (auto& switch_case : m_cases) {
  1845. if (!falling_through && switch_case.test()) {
  1846. auto test_result = switch_case.test()->execute(interpreter, global_object);
  1847. if (interpreter.exception())
  1848. return {};
  1849. if (!strict_eq(discriminant_result, test_result))
  1850. continue;
  1851. }
  1852. falling_through = true;
  1853. for (auto& statement : switch_case.consequent()) {
  1854. auto value = statement.execute(interpreter, global_object);
  1855. if (!value.is_empty())
  1856. last_value = value;
  1857. if (interpreter.exception())
  1858. return {};
  1859. if (interpreter.vm().should_unwind()) {
  1860. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  1861. // No stop_unwind(), the outer loop will handle that - we just need to break out of the switch/case.
  1862. return last_value;
  1863. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  1864. interpreter.vm().stop_unwind();
  1865. return last_value;
  1866. } else {
  1867. return last_value;
  1868. }
  1869. }
  1870. }
  1871. }
  1872. return last_value;
  1873. }
  1874. Value SwitchCase::execute(Interpreter& interpreter, GlobalObject&) const
  1875. {
  1876. InterpreterNodeScope node_scope { interpreter, *this };
  1877. // NOTE: SwitchCase execution is handled by SwitchStatement.
  1878. VERIFY_NOT_REACHED();
  1879. return {};
  1880. }
  1881. Value BreakStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1882. {
  1883. InterpreterNodeScope node_scope { interpreter, *this };
  1884. interpreter.vm().unwind(ScopeType::Breakable, m_target_label);
  1885. return {};
  1886. }
  1887. Value ContinueStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1888. {
  1889. InterpreterNodeScope node_scope { interpreter, *this };
  1890. interpreter.vm().unwind(ScopeType::Continuable, m_target_label);
  1891. return {};
  1892. }
  1893. void SwitchStatement::dump(int indent) const
  1894. {
  1895. ASTNode::dump(indent);
  1896. m_discriminant->dump(indent + 1);
  1897. for (auto& switch_case : m_cases) {
  1898. switch_case.dump(indent + 1);
  1899. }
  1900. }
  1901. void SwitchCase::dump(int indent) const
  1902. {
  1903. ASTNode::dump(indent);
  1904. print_indent(indent + 1);
  1905. if (m_test) {
  1906. outln("(Test)");
  1907. m_test->dump(indent + 2);
  1908. } else {
  1909. outln("(Default)");
  1910. }
  1911. print_indent(indent + 1);
  1912. outln("(Consequent)");
  1913. for (auto& statement : m_consequent)
  1914. statement.dump(indent + 2);
  1915. }
  1916. Value ConditionalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1917. {
  1918. InterpreterNodeScope node_scope { interpreter, *this };
  1919. auto test_result = m_test->execute(interpreter, global_object);
  1920. if (interpreter.exception())
  1921. return {};
  1922. Value result;
  1923. if (test_result.to_boolean()) {
  1924. result = m_consequent->execute(interpreter, global_object);
  1925. } else {
  1926. result = m_alternate->execute(interpreter, global_object);
  1927. }
  1928. if (interpreter.exception())
  1929. return {};
  1930. return result;
  1931. }
  1932. void ConditionalExpression::dump(int indent) const
  1933. {
  1934. ASTNode::dump(indent);
  1935. print_indent(indent + 1);
  1936. outln("(Test)");
  1937. m_test->dump(indent + 2);
  1938. print_indent(indent + 1);
  1939. outln("(Consequent)");
  1940. m_consequent->dump(indent + 2);
  1941. print_indent(indent + 1);
  1942. outln("(Alternate)");
  1943. m_alternate->dump(indent + 2);
  1944. }
  1945. void SequenceExpression::dump(int indent) const
  1946. {
  1947. ASTNode::dump(indent);
  1948. for (auto& expression : m_expressions)
  1949. expression.dump(indent + 1);
  1950. }
  1951. Value SequenceExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1952. {
  1953. InterpreterNodeScope node_scope { interpreter, *this };
  1954. Value last_value;
  1955. for (auto& expression : m_expressions) {
  1956. last_value = expression.execute(interpreter, global_object);
  1957. if (interpreter.exception())
  1958. return {};
  1959. }
  1960. return last_value;
  1961. }
  1962. Value DebuggerStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1963. {
  1964. InterpreterNodeScope node_scope { interpreter, *this };
  1965. // Sorry, no JavaScript debugger available (yet)!
  1966. return {};
  1967. }
  1968. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1969. {
  1970. m_variables.append(move(variables));
  1971. }
  1972. void ScopeNode::add_functions(NonnullRefPtrVector<FunctionDeclaration> functions)
  1973. {
  1974. m_functions.append(move(functions));
  1975. }
  1976. }