AST.cpp 66 KB

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