AST.cpp 64 KB

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