AST.cpp 67 KB

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