AST.cpp 62 KB

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