AST.cpp 70 KB

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