AST.cpp 63 KB

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