AST.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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/Function.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/ScopeGuard.h>
  30. #include <AK/StringBuilder.h>
  31. #include <LibJS/AST.h>
  32. #include <LibJS/Interpreter.h>
  33. #include <LibJS/Runtime/Array.h>
  34. #include <LibJS/Runtime/Error.h>
  35. #include <LibJS/Runtime/GlobalObject.h>
  36. #include <LibJS/Runtime/MarkedValueList.h>
  37. #include <LibJS/Runtime/NativeFunction.h>
  38. #include <LibJS/Runtime/PrimitiveString.h>
  39. #include <LibJS/Runtime/Reference.h>
  40. #include <LibJS/Runtime/ScriptFunction.h>
  41. #include <LibJS/Runtime/Shape.h>
  42. #include <LibJS/Runtime/StringObject.h>
  43. #include <LibJS/Runtime/Value.h>
  44. #include <stdio.h>
  45. namespace JS {
  46. Value ScopeNode::execute(Interpreter& interpreter) const
  47. {
  48. return interpreter.run(*this);
  49. }
  50. Value FunctionDeclaration::execute(Interpreter& interpreter) const
  51. {
  52. auto* function = ScriptFunction::create(interpreter.global_object(), name(), body(), parameters(), interpreter.current_environment());
  53. interpreter.set_variable(name(), function);
  54. return js_undefined();
  55. }
  56. Value FunctionExpression::execute(Interpreter& interpreter) const
  57. {
  58. return ScriptFunction::create(interpreter.global_object(), name(), body(), parameters(), interpreter.current_environment());
  59. }
  60. Value ExpressionStatement::execute(Interpreter& interpreter) const
  61. {
  62. return m_expression->execute(interpreter);
  63. }
  64. CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter) const
  65. {
  66. if (is_new_expression()) {
  67. // Computing |this| is irrelevant for "new" expression.
  68. return { js_undefined(), m_callee->execute(interpreter) };
  69. }
  70. if (m_callee->is_member_expression()) {
  71. auto& member_expression = static_cast<const MemberExpression&>(*m_callee);
  72. auto object_value = member_expression.object().execute(interpreter);
  73. if (interpreter.exception())
  74. return {};
  75. auto* this_value = object_value.to_object(interpreter.heap());
  76. if (interpreter.exception())
  77. return {};
  78. auto callee = this_value->get(member_expression.computed_property_name(interpreter)).value_or(js_undefined());
  79. return { this_value, callee };
  80. }
  81. return { &interpreter.global_object(), m_callee->execute(interpreter) };
  82. }
  83. Value CallExpression::execute(Interpreter& interpreter) const
  84. {
  85. auto [this_value, callee] = compute_this_and_callee(interpreter);
  86. if (interpreter.exception())
  87. return {};
  88. ASSERT(!callee.is_empty());
  89. if (!callee.is_object()
  90. || !callee.as_object().is_function()
  91. || (is_new_expression() && (callee.as_object().is_native_function() && !static_cast<NativeFunction&>(callee.as_object()).has_constructor()))) {
  92. String error_message;
  93. auto call_type = is_new_expression() ? "constructor" : "function";
  94. if (m_callee->is_identifier() || m_callee->is_member_expression()) {
  95. String expression_string;
  96. if (m_callee->is_identifier())
  97. expression_string = static_cast<const Identifier&>(*m_callee).string();
  98. else
  99. expression_string = static_cast<const MemberExpression&>(*m_callee).to_string_approximation();
  100. error_message = String::format("%s is not a %s (evaluated from '%s')", callee.to_string().characters(), call_type, expression_string.characters());
  101. } else {
  102. error_message = String::format("%s is not a %s", callee.to_string().characters(), call_type);
  103. }
  104. return interpreter.throw_exception<TypeError>(error_message);
  105. }
  106. auto& function = static_cast<Function&>(callee.as_object());
  107. MarkedValueList arguments(interpreter.heap());
  108. arguments.values().append(function.bound_arguments());
  109. for (size_t i = 0; i < m_arguments.size(); ++i) {
  110. auto value = m_arguments[i].execute(interpreter);
  111. if (interpreter.exception())
  112. return {};
  113. arguments.append(value);
  114. if (interpreter.exception())
  115. return {};
  116. }
  117. auto& call_frame = interpreter.push_call_frame();
  118. call_frame.function_name = function.name();
  119. call_frame.arguments = arguments.values();
  120. call_frame.environment = function.create_environment();
  121. Object* new_object = nullptr;
  122. Value result;
  123. if (is_new_expression()) {
  124. new_object = Object::create_empty(interpreter, interpreter.global_object());
  125. auto prototype = function.get("prototype");
  126. if (prototype.is_object())
  127. new_object->set_prototype(&prototype.as_object());
  128. call_frame.this_value = new_object;
  129. result = function.construct(interpreter);
  130. } else {
  131. call_frame.this_value = function.bound_this().value_or(this_value);
  132. result = function.call(interpreter);
  133. }
  134. interpreter.pop_call_frame();
  135. if (interpreter.exception())
  136. return {};
  137. if (is_new_expression()) {
  138. if (result.is_object())
  139. return result;
  140. return new_object;
  141. }
  142. return result;
  143. }
  144. Value ReturnStatement::execute(Interpreter& interpreter) const
  145. {
  146. auto value = argument() ? argument()->execute(interpreter) : js_undefined();
  147. if (interpreter.exception())
  148. return {};
  149. interpreter.unwind(ScopeType::Function);
  150. return value;
  151. }
  152. Value IfStatement::execute(Interpreter& interpreter) const
  153. {
  154. auto predicate_result = m_predicate->execute(interpreter);
  155. if (interpreter.exception())
  156. return {};
  157. if (predicate_result.to_boolean())
  158. return interpreter.run(*m_consequent);
  159. if (m_alternate)
  160. return interpreter.run(*m_alternate);
  161. return js_undefined();
  162. }
  163. Value WhileStatement::execute(Interpreter& interpreter) const
  164. {
  165. Value last_value = js_undefined();
  166. while (m_test->execute(interpreter).to_boolean()) {
  167. if (interpreter.exception())
  168. return {};
  169. last_value = interpreter.run(*m_body);
  170. if (interpreter.exception())
  171. return {};
  172. }
  173. return last_value;
  174. }
  175. Value DoWhileStatement::execute(Interpreter& interpreter) const
  176. {
  177. Value last_value = js_undefined();
  178. do {
  179. if (interpreter.exception())
  180. return {};
  181. last_value = interpreter.run(*m_body);
  182. if (interpreter.exception())
  183. return {};
  184. } while (m_test->execute(interpreter).to_boolean());
  185. return last_value;
  186. }
  187. Value ForStatement::execute(Interpreter& interpreter) const
  188. {
  189. RefPtr<BlockStatement> wrapper;
  190. if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_kind() != DeclarationKind::Var) {
  191. wrapper = create_ast_node<BlockStatement>();
  192. interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
  193. }
  194. auto wrapper_cleanup = ScopeGuard([&] {
  195. if (wrapper)
  196. interpreter.exit_scope(*wrapper);
  197. });
  198. Value last_value = js_undefined();
  199. if (m_init) {
  200. m_init->execute(interpreter);
  201. if (interpreter.exception())
  202. return {};
  203. }
  204. if (m_test) {
  205. while (true) {
  206. auto test_result = m_test->execute(interpreter);
  207. if (interpreter.exception())
  208. return {};
  209. if (!test_result.to_boolean())
  210. break;
  211. last_value = interpreter.run(*m_body);
  212. if (interpreter.exception())
  213. return {};
  214. if (interpreter.should_unwind()) {
  215. if (interpreter.should_unwind_until(ScopeType::Continuable)) {
  216. interpreter.stop_unwind();
  217. } else if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  218. interpreter.stop_unwind();
  219. break;
  220. } else {
  221. return js_undefined();
  222. }
  223. }
  224. if (m_update) {
  225. m_update->execute(interpreter);
  226. if (interpreter.exception())
  227. return {};
  228. }
  229. }
  230. } else {
  231. while (true) {
  232. last_value = interpreter.run(*m_body);
  233. if (interpreter.exception())
  234. return {};
  235. if (interpreter.should_unwind()) {
  236. if (interpreter.should_unwind_until(ScopeType::Continuable)) {
  237. interpreter.stop_unwind();
  238. } else if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  239. interpreter.stop_unwind();
  240. break;
  241. } else {
  242. return js_undefined();
  243. }
  244. }
  245. if (m_update) {
  246. m_update->execute(interpreter);
  247. if (interpreter.exception())
  248. return {};
  249. }
  250. }
  251. }
  252. return last_value;
  253. }
  254. Value BinaryExpression::execute(Interpreter& interpreter) const
  255. {
  256. auto lhs_result = m_lhs->execute(interpreter);
  257. if (interpreter.exception())
  258. return {};
  259. auto rhs_result = m_rhs->execute(interpreter);
  260. if (interpreter.exception())
  261. return {};
  262. switch (m_op) {
  263. case BinaryOp::Addition:
  264. return add(interpreter, lhs_result, rhs_result);
  265. case BinaryOp::Subtraction:
  266. return sub(interpreter, lhs_result, rhs_result);
  267. case BinaryOp::Multiplication:
  268. return mul(interpreter, lhs_result, rhs_result);
  269. case BinaryOp::Division:
  270. return div(interpreter, lhs_result, rhs_result);
  271. case BinaryOp::Modulo:
  272. return mod(interpreter, lhs_result, rhs_result);
  273. case BinaryOp::Exponentiation:
  274. return exp(interpreter, lhs_result, rhs_result);
  275. case BinaryOp::TypedEquals:
  276. return typed_eq(interpreter, lhs_result, rhs_result);
  277. case BinaryOp::TypedInequals:
  278. return Value(!typed_eq(interpreter, lhs_result, rhs_result).as_bool());
  279. case BinaryOp::AbstractEquals:
  280. return eq(interpreter, lhs_result, rhs_result);
  281. case BinaryOp::AbstractInequals:
  282. return Value(!eq(interpreter, lhs_result, rhs_result).as_bool());
  283. case BinaryOp::GreaterThan:
  284. return greater_than(interpreter, lhs_result, rhs_result);
  285. case BinaryOp::GreaterThanEquals:
  286. return greater_than_equals(interpreter, lhs_result, rhs_result);
  287. case BinaryOp::LessThan:
  288. return less_than(interpreter, lhs_result, rhs_result);
  289. case BinaryOp::LessThanEquals:
  290. return less_than_equals(interpreter, lhs_result, rhs_result);
  291. case BinaryOp::BitwiseAnd:
  292. return bitwise_and(interpreter, lhs_result, rhs_result);
  293. case BinaryOp::BitwiseOr:
  294. return bitwise_or(interpreter, lhs_result, rhs_result);
  295. case BinaryOp::BitwiseXor:
  296. return bitwise_xor(interpreter, lhs_result, rhs_result);
  297. case BinaryOp::LeftShift:
  298. return left_shift(interpreter, lhs_result, rhs_result);
  299. case BinaryOp::RightShift:
  300. return right_shift(interpreter, lhs_result, rhs_result);
  301. case BinaryOp::UnsignedRightShift:
  302. return unsigned_right_shift(interpreter, lhs_result, rhs_result);
  303. case BinaryOp::In:
  304. return in(interpreter, lhs_result, rhs_result);
  305. case BinaryOp::InstanceOf:
  306. return instance_of(interpreter, lhs_result, rhs_result);
  307. }
  308. ASSERT_NOT_REACHED();
  309. }
  310. Value LogicalExpression::execute(Interpreter& interpreter) const
  311. {
  312. auto lhs_result = m_lhs->execute(interpreter);
  313. if (interpreter.exception())
  314. return {};
  315. switch (m_op) {
  316. case LogicalOp::And:
  317. if (lhs_result.to_boolean()) {
  318. auto rhs_result = m_rhs->execute(interpreter);
  319. if (interpreter.exception())
  320. return {};
  321. return rhs_result;
  322. }
  323. return lhs_result;
  324. case LogicalOp::Or: {
  325. if (lhs_result.to_boolean())
  326. return lhs_result;
  327. auto rhs_result = m_rhs->execute(interpreter);
  328. if (interpreter.exception())
  329. return {};
  330. return rhs_result;
  331. }
  332. case LogicalOp::NullishCoalescing:
  333. if (lhs_result.is_null() || lhs_result.is_undefined()) {
  334. auto rhs_result = m_rhs->execute(interpreter);
  335. if (interpreter.exception())
  336. return {};
  337. return rhs_result;
  338. }
  339. return lhs_result;
  340. }
  341. ASSERT_NOT_REACHED();
  342. }
  343. Reference Expression::to_reference(Interpreter&) const
  344. {
  345. return {};
  346. }
  347. Reference Identifier::to_reference(Interpreter& interpreter) const
  348. {
  349. return interpreter.get_reference(string());
  350. }
  351. Reference MemberExpression::to_reference(Interpreter& interpreter) const
  352. {
  353. auto object_value = m_object->execute(interpreter);
  354. if (object_value.is_empty())
  355. return {};
  356. auto* object = object_value.to_object(interpreter.heap());
  357. if (!object)
  358. return {};
  359. auto property_name = computed_property_name(interpreter);
  360. if (!property_name.is_valid())
  361. return {};
  362. return { object, property_name };
  363. }
  364. Value UnaryExpression::execute(Interpreter& interpreter) const
  365. {
  366. if (m_op == UnaryOp::Delete) {
  367. auto reference = m_lhs->to_reference(interpreter);
  368. if (interpreter.exception())
  369. return {};
  370. if (reference.is_unresolvable())
  371. return Value(true);
  372. // FIXME: Support deleting locals
  373. ASSERT(!reference.is_local_variable());
  374. if (reference.is_global_variable())
  375. return interpreter.global_object().delete_property(reference.name());
  376. auto* base_object = reference.base().to_object(interpreter.heap());
  377. if (!base_object)
  378. return {};
  379. return base_object->delete_property(reference.name());
  380. }
  381. auto lhs_result = m_lhs->execute(interpreter);
  382. if (interpreter.exception())
  383. return {};
  384. switch (m_op) {
  385. case UnaryOp::BitwiseNot:
  386. return bitwise_not(interpreter, lhs_result);
  387. case UnaryOp::Not:
  388. return Value(!lhs_result.to_boolean());
  389. case UnaryOp::Plus:
  390. return unary_plus(interpreter, lhs_result);
  391. case UnaryOp::Minus:
  392. return unary_minus(interpreter, lhs_result);
  393. case UnaryOp::Typeof:
  394. switch (lhs_result.type()) {
  395. case Value::Type::Empty:
  396. ASSERT_NOT_REACHED();
  397. return {};
  398. case Value::Type::Undefined:
  399. return js_string(interpreter, "undefined");
  400. case Value::Type::Null:
  401. // yes, this is on purpose. yes, this is how javascript works.
  402. // yes, it's silly.
  403. return js_string(interpreter, "object");
  404. case Value::Type::Number:
  405. return js_string(interpreter, "number");
  406. case Value::Type::String:
  407. return js_string(interpreter, "string");
  408. case Value::Type::Object:
  409. if (lhs_result.as_object().is_function())
  410. return js_string(interpreter, "function");
  411. return js_string(interpreter, "object");
  412. case Value::Type::Boolean:
  413. return js_string(interpreter, "boolean");
  414. default:
  415. ASSERT_NOT_REACHED();
  416. }
  417. case UnaryOp::Void:
  418. return js_undefined();
  419. case UnaryOp::Delete:
  420. ASSERT_NOT_REACHED();
  421. }
  422. ASSERT_NOT_REACHED();
  423. }
  424. static void print_indent(int indent)
  425. {
  426. for (int i = 0; i < indent * 2; ++i)
  427. putchar(' ');
  428. }
  429. void ASTNode::dump(int indent) const
  430. {
  431. print_indent(indent);
  432. printf("%s\n", class_name());
  433. }
  434. void ScopeNode::dump(int indent) const
  435. {
  436. ASTNode::dump(indent);
  437. if (!m_variables.is_empty()) {
  438. print_indent(indent + 1);
  439. printf("(Variables)\n");
  440. for (auto& variable : m_variables)
  441. variable.dump(indent + 2);
  442. }
  443. if (!m_children.is_empty()) {
  444. print_indent(indent + 1);
  445. printf("(Children)\n");
  446. for (auto& child : children())
  447. child.dump(indent + 2);
  448. }
  449. }
  450. void BinaryExpression::dump(int indent) const
  451. {
  452. const char* op_string = nullptr;
  453. switch (m_op) {
  454. case BinaryOp::Addition:
  455. op_string = "+";
  456. break;
  457. case BinaryOp::Subtraction:
  458. op_string = "-";
  459. break;
  460. case BinaryOp::Multiplication:
  461. op_string = "*";
  462. break;
  463. case BinaryOp::Division:
  464. op_string = "/";
  465. break;
  466. case BinaryOp::Modulo:
  467. op_string = "%";
  468. break;
  469. case BinaryOp::Exponentiation:
  470. op_string = "**";
  471. break;
  472. case BinaryOp::TypedEquals:
  473. op_string = "===";
  474. break;
  475. case BinaryOp::TypedInequals:
  476. op_string = "!==";
  477. break;
  478. case BinaryOp::AbstractEquals:
  479. op_string = "==";
  480. break;
  481. case BinaryOp::AbstractInequals:
  482. op_string = "!=";
  483. break;
  484. case BinaryOp::GreaterThan:
  485. op_string = ">";
  486. break;
  487. case BinaryOp::GreaterThanEquals:
  488. op_string = ">=";
  489. break;
  490. case BinaryOp::LessThan:
  491. op_string = "<";
  492. break;
  493. case BinaryOp::LessThanEquals:
  494. op_string = "<=";
  495. break;
  496. case BinaryOp::BitwiseAnd:
  497. op_string = "&";
  498. break;
  499. case BinaryOp::BitwiseOr:
  500. op_string = "|";
  501. break;
  502. case BinaryOp::BitwiseXor:
  503. op_string = "^";
  504. break;
  505. case BinaryOp::LeftShift:
  506. op_string = "<<";
  507. break;
  508. case BinaryOp::RightShift:
  509. op_string = ">>";
  510. break;
  511. case BinaryOp::UnsignedRightShift:
  512. op_string = ">>>";
  513. break;
  514. case BinaryOp::In:
  515. op_string = "in";
  516. break;
  517. case BinaryOp::InstanceOf:
  518. op_string = "instanceof";
  519. break;
  520. }
  521. print_indent(indent);
  522. printf("%s\n", class_name());
  523. m_lhs->dump(indent + 1);
  524. print_indent(indent + 1);
  525. printf("%s\n", op_string);
  526. m_rhs->dump(indent + 1);
  527. }
  528. void LogicalExpression::dump(int indent) const
  529. {
  530. const char* op_string = nullptr;
  531. switch (m_op) {
  532. case LogicalOp::And:
  533. op_string = "&&";
  534. break;
  535. case LogicalOp::Or:
  536. op_string = "||";
  537. break;
  538. case LogicalOp::NullishCoalescing:
  539. op_string = "??";
  540. break;
  541. }
  542. print_indent(indent);
  543. printf("%s\n", class_name());
  544. m_lhs->dump(indent + 1);
  545. print_indent(indent + 1);
  546. printf("%s\n", op_string);
  547. m_rhs->dump(indent + 1);
  548. }
  549. void UnaryExpression::dump(int indent) const
  550. {
  551. const char* op_string = nullptr;
  552. switch (m_op) {
  553. case UnaryOp::BitwiseNot:
  554. op_string = "~";
  555. break;
  556. case UnaryOp::Not:
  557. op_string = "!";
  558. break;
  559. case UnaryOp::Plus:
  560. op_string = "+";
  561. break;
  562. case UnaryOp::Minus:
  563. op_string = "-";
  564. break;
  565. case UnaryOp::Typeof:
  566. op_string = "typeof ";
  567. break;
  568. case UnaryOp::Void:
  569. op_string = "void ";
  570. break;
  571. case UnaryOp::Delete:
  572. op_string = "delete ";
  573. break;
  574. }
  575. print_indent(indent);
  576. printf("%s\n", class_name());
  577. print_indent(indent + 1);
  578. printf("%s\n", op_string);
  579. m_lhs->dump(indent + 1);
  580. }
  581. void CallExpression::dump(int indent) const
  582. {
  583. print_indent(indent);
  584. printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
  585. m_callee->dump(indent + 1);
  586. for (auto& argument : m_arguments)
  587. argument.dump(indent + 1);
  588. }
  589. void StringLiteral::dump(int indent) const
  590. {
  591. print_indent(indent);
  592. printf("StringLiteral \"%s\"\n", m_value.characters());
  593. }
  594. void NumericLiteral::dump(int indent) const
  595. {
  596. print_indent(indent);
  597. printf("NumericLiteral %g\n", m_value);
  598. }
  599. void BooleanLiteral::dump(int indent) const
  600. {
  601. print_indent(indent);
  602. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  603. }
  604. void NullLiteral::dump(int indent) const
  605. {
  606. print_indent(indent);
  607. printf("null\n");
  608. }
  609. void FunctionNode::dump(int indent, const char* class_name) const
  610. {
  611. print_indent(indent);
  612. printf("%s '%s'\n", class_name, name().characters());
  613. if (!m_parameters.is_empty()) {
  614. print_indent(indent + 1);
  615. printf("(Parameters)\n");
  616. for (auto& parameter : m_parameters) {
  617. print_indent(indent + 2);
  618. printf("%s\n", parameter.name.characters());
  619. if (parameter.default_value) {
  620. parameter.default_value->dump(indent + 3);
  621. }
  622. }
  623. }
  624. if (!m_variables.is_empty()) {
  625. print_indent(indent + 1);
  626. printf("(Variables)\n");
  627. for (auto& variable : m_variables)
  628. variable.dump(indent + 2);
  629. }
  630. print_indent(indent + 1);
  631. printf("(Body)\n");
  632. body().dump(indent + 2);
  633. }
  634. void FunctionDeclaration::dump(int indent) const
  635. {
  636. FunctionNode::dump(indent, class_name());
  637. }
  638. void FunctionExpression::dump(int indent) const
  639. {
  640. FunctionNode::dump(indent, class_name());
  641. }
  642. void ReturnStatement::dump(int indent) const
  643. {
  644. ASTNode::dump(indent);
  645. if (argument())
  646. argument()->dump(indent + 1);
  647. }
  648. void IfStatement::dump(int indent) const
  649. {
  650. ASTNode::dump(indent);
  651. print_indent(indent);
  652. printf("If\n");
  653. predicate().dump(indent + 1);
  654. consequent().dump(indent + 1);
  655. if (alternate()) {
  656. print_indent(indent);
  657. printf("Else\n");
  658. alternate()->dump(indent + 1);
  659. }
  660. }
  661. void WhileStatement::dump(int indent) const
  662. {
  663. ASTNode::dump(indent);
  664. print_indent(indent);
  665. printf("While\n");
  666. test().dump(indent + 1);
  667. body().dump(indent + 1);
  668. }
  669. void DoWhileStatement::dump(int indent) const
  670. {
  671. ASTNode::dump(indent);
  672. print_indent(indent);
  673. printf("DoWhile\n");
  674. test().dump(indent + 1);
  675. body().dump(indent + 1);
  676. }
  677. void ForStatement::dump(int indent) const
  678. {
  679. ASTNode::dump(indent);
  680. print_indent(indent);
  681. printf("For\n");
  682. if (init())
  683. init()->dump(indent + 1);
  684. if (test())
  685. test()->dump(indent + 1);
  686. if (update())
  687. update()->dump(indent + 1);
  688. body().dump(indent + 1);
  689. }
  690. Value Identifier::execute(Interpreter& interpreter) const
  691. {
  692. auto value = interpreter.get_variable(string());
  693. if (value.is_empty())
  694. return interpreter.throw_exception<ReferenceError>(String::format("'%s' not known", string().characters()));
  695. return value;
  696. }
  697. void Identifier::dump(int indent) const
  698. {
  699. print_indent(indent);
  700. printf("Identifier \"%s\"\n", m_string.characters());
  701. }
  702. void SpreadExpression::dump(int indent) const
  703. {
  704. ASTNode::dump(indent);
  705. m_target->dump(indent + 1);
  706. }
  707. Value SpreadExpression::execute(Interpreter& interpreter) const
  708. {
  709. return m_target->execute(interpreter);
  710. }
  711. Value ThisExpression::execute(Interpreter& interpreter) const
  712. {
  713. return interpreter.this_value();
  714. }
  715. void ThisExpression::dump(int indent) const
  716. {
  717. ASTNode::dump(indent);
  718. }
  719. Value AssignmentExpression::execute(Interpreter& interpreter) const
  720. {
  721. auto rhs_result = m_rhs->execute(interpreter);
  722. if (interpreter.exception())
  723. return {};
  724. Value lhs_result;
  725. switch (m_op) {
  726. case AssignmentOp::Assignment:
  727. break;
  728. case AssignmentOp::AdditionAssignment:
  729. lhs_result = m_lhs->execute(interpreter);
  730. if (interpreter.exception())
  731. return {};
  732. rhs_result = add(interpreter, lhs_result, rhs_result);
  733. break;
  734. case AssignmentOp::SubtractionAssignment:
  735. lhs_result = m_lhs->execute(interpreter);
  736. if (interpreter.exception())
  737. return {};
  738. rhs_result = sub(interpreter, lhs_result, rhs_result);
  739. break;
  740. case AssignmentOp::MultiplicationAssignment:
  741. lhs_result = m_lhs->execute(interpreter);
  742. if (interpreter.exception())
  743. return {};
  744. rhs_result = mul(interpreter, lhs_result, rhs_result);
  745. break;
  746. case AssignmentOp::DivisionAssignment:
  747. lhs_result = m_lhs->execute(interpreter);
  748. if (interpreter.exception())
  749. return {};
  750. rhs_result = div(interpreter, lhs_result, rhs_result);
  751. break;
  752. case AssignmentOp::LeftShiftAssignment:
  753. lhs_result = m_lhs->execute(interpreter);
  754. if (interpreter.exception())
  755. return {};
  756. rhs_result = left_shift(interpreter, lhs_result, rhs_result);
  757. break;
  758. case AssignmentOp::RightShiftAssignment:
  759. lhs_result = m_lhs->execute(interpreter);
  760. if (interpreter.exception())
  761. return {};
  762. rhs_result = right_shift(interpreter, lhs_result, rhs_result);
  763. break;
  764. case AssignmentOp::UnsignedRightShiftAssignment:
  765. lhs_result = m_lhs->execute(interpreter);
  766. if (interpreter.exception())
  767. return {};
  768. rhs_result = unsigned_right_shift(interpreter, lhs_result, rhs_result);
  769. break;
  770. }
  771. if (interpreter.exception())
  772. return {};
  773. auto reference = m_lhs->to_reference(interpreter);
  774. if (interpreter.exception())
  775. return {};
  776. if (reference.is_unresolvable())
  777. return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
  778. reference.put(interpreter, rhs_result);
  779. if (interpreter.exception())
  780. return {};
  781. return rhs_result;
  782. }
  783. Value UpdateExpression::execute(Interpreter& interpreter) const
  784. {
  785. auto reference = m_argument->to_reference(interpreter);
  786. if (interpreter.exception())
  787. return {};
  788. auto old_value = reference.get(interpreter);
  789. if (interpreter.exception())
  790. return {};
  791. old_value = old_value.to_number();
  792. int op_result = 0;
  793. switch (m_op) {
  794. case UpdateOp::Increment:
  795. op_result = 1;
  796. break;
  797. case UpdateOp::Decrement:
  798. op_result = -1;
  799. break;
  800. default:
  801. ASSERT_NOT_REACHED();
  802. }
  803. auto new_value = Value(old_value.as_double() + op_result);
  804. reference.put(interpreter, new_value);
  805. if (interpreter.exception())
  806. return {};
  807. return m_prefixed ? new_value : old_value;
  808. }
  809. void AssignmentExpression::dump(int indent) const
  810. {
  811. const char* op_string = nullptr;
  812. switch (m_op) {
  813. case AssignmentOp::Assignment:
  814. op_string = "=";
  815. break;
  816. case AssignmentOp::AdditionAssignment:
  817. op_string = "+=";
  818. break;
  819. case AssignmentOp::SubtractionAssignment:
  820. op_string = "-=";
  821. break;
  822. case AssignmentOp::MultiplicationAssignment:
  823. op_string = "*=";
  824. break;
  825. case AssignmentOp::DivisionAssignment:
  826. op_string = "/=";
  827. break;
  828. case AssignmentOp::LeftShiftAssignment:
  829. op_string = "<<=";
  830. break;
  831. case AssignmentOp::RightShiftAssignment:
  832. op_string = ">>=";
  833. break;
  834. case AssignmentOp::UnsignedRightShiftAssignment:
  835. op_string = ">>>=";
  836. break;
  837. }
  838. ASTNode::dump(indent);
  839. print_indent(indent + 1);
  840. printf("%s\n", op_string);
  841. m_lhs->dump(indent + 1);
  842. m_rhs->dump(indent + 1);
  843. }
  844. void UpdateExpression::dump(int indent) const
  845. {
  846. const char* op_string = nullptr;
  847. switch (m_op) {
  848. case UpdateOp::Increment:
  849. op_string = "++";
  850. break;
  851. case UpdateOp::Decrement:
  852. op_string = "--";
  853. break;
  854. }
  855. ASTNode::dump(indent);
  856. print_indent(indent + 1);
  857. if (m_prefixed)
  858. printf("%s\n", op_string);
  859. m_argument->dump(indent + 1);
  860. if (!m_prefixed) {
  861. print_indent(indent + 1);
  862. printf("%s\n", op_string);
  863. }
  864. }
  865. Value VariableDeclaration::execute(Interpreter& interpreter) const
  866. {
  867. for (auto& declarator : m_declarations) {
  868. if (auto* init = declarator.init()) {
  869. auto initalizer_result = init->execute(interpreter);
  870. if (interpreter.exception())
  871. return {};
  872. interpreter.set_variable(declarator.id().string(), initalizer_result, true);
  873. }
  874. }
  875. return js_undefined();
  876. }
  877. Value VariableDeclarator::execute(Interpreter&) const
  878. {
  879. // NOTE: This node is handled by VariableDeclaration.
  880. ASSERT_NOT_REACHED();
  881. }
  882. void VariableDeclaration::dump(int indent) const
  883. {
  884. const char* declaration_kind_string = nullptr;
  885. switch (m_declaration_kind) {
  886. case DeclarationKind::Let:
  887. declaration_kind_string = "Let";
  888. break;
  889. case DeclarationKind::Var:
  890. declaration_kind_string = "Var";
  891. break;
  892. case DeclarationKind::Const:
  893. declaration_kind_string = "Const";
  894. break;
  895. }
  896. ASTNode::dump(indent);
  897. print_indent(indent + 1);
  898. printf("%s\n", declaration_kind_string);
  899. for (auto& declarator : m_declarations)
  900. declarator.dump(indent + 1);
  901. }
  902. void VariableDeclarator::dump(int indent) const
  903. {
  904. ASTNode::dump(indent);
  905. m_id->dump(indent + 1);
  906. if (m_init)
  907. m_init->dump(indent + 1);
  908. }
  909. void ObjectProperty::dump(int indent) const
  910. {
  911. ASTNode::dump(indent);
  912. m_key->dump(indent + 1);
  913. m_value->dump(indent + 1);
  914. }
  915. void ObjectExpression::dump(int indent) const
  916. {
  917. ASTNode::dump(indent);
  918. for (auto& property : m_properties) {
  919. property.dump(indent + 1);
  920. }
  921. }
  922. void ExpressionStatement::dump(int indent) const
  923. {
  924. ASTNode::dump(indent);
  925. m_expression->dump(indent + 1);
  926. }
  927. Value ObjectProperty::execute(Interpreter&) const
  928. {
  929. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  930. ASSERT_NOT_REACHED();
  931. }
  932. Value ObjectExpression::execute(Interpreter& interpreter) const
  933. {
  934. auto* object = Object::create_empty(interpreter, interpreter.global_object());
  935. for (auto& property : m_properties) {
  936. auto key_result = property.key().execute(interpreter);
  937. if (interpreter.exception())
  938. return {};
  939. if (property.is_spread()) {
  940. if (key_result.is_array()) {
  941. auto& array_to_spread = static_cast<Array&>(key_result.as_object());
  942. auto& elements = array_to_spread.elements();
  943. for (size_t i = 0; i < elements.size(); ++i) {
  944. auto element = elements.at(i);
  945. if (!element.is_empty())
  946. object->put_by_index(i, element);
  947. }
  948. } else if (key_result.is_object()) {
  949. auto& obj_to_spread = key_result.as_object();
  950. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  951. if (it.value.attributes & Attribute::Enumerable)
  952. object->put(it.key, obj_to_spread.get(it.key));
  953. }
  954. } else if (key_result.is_string()) {
  955. auto& str_to_spread = key_result.as_string().string();
  956. for (size_t i = 0; i < str_to_spread.length(); i++) {
  957. object->put_by_index(i, js_string(interpreter, str_to_spread.substring(i, 1)));
  958. }
  959. }
  960. continue;
  961. }
  962. auto key = key_result.to_string();
  963. auto value = property.value().execute(interpreter);
  964. if (interpreter.exception())
  965. return {};
  966. object->put(key, value);
  967. }
  968. return object;
  969. }
  970. void MemberExpression::dump(int indent) const
  971. {
  972. print_indent(indent);
  973. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  974. m_object->dump(indent + 1);
  975. m_property->dump(indent + 1);
  976. }
  977. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter) const
  978. {
  979. if (!is_computed()) {
  980. ASSERT(m_property->is_identifier());
  981. return PropertyName(static_cast<const Identifier&>(*m_property).string());
  982. }
  983. auto index = m_property->execute(interpreter);
  984. if (interpreter.exception())
  985. return {};
  986. ASSERT(!index.is_empty());
  987. if (!index.to_number().is_finite_number())
  988. return PropertyName(index.to_string());
  989. auto index_as_double = index.to_double();
  990. if (index_as_double < 0 || (i32)index_as_double != index_as_double)
  991. return PropertyName(index.to_string());
  992. return PropertyName(index.to_i32());
  993. }
  994. String MemberExpression::to_string_approximation() const
  995. {
  996. String object_string = "<object>";
  997. if (m_object->is_identifier())
  998. object_string = static_cast<const Identifier&>(*m_object).string();
  999. if (is_computed())
  1000. return String::format("%s[<computed>]", object_string.characters());
  1001. ASSERT(m_property->is_identifier());
  1002. return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
  1003. }
  1004. Value MemberExpression::execute(Interpreter& interpreter) const
  1005. {
  1006. auto object_value = m_object->execute(interpreter);
  1007. if (interpreter.exception())
  1008. return {};
  1009. auto* object_result = object_value.to_object(interpreter.heap());
  1010. if (interpreter.exception())
  1011. return {};
  1012. return object_result->get(computed_property_name(interpreter)).value_or(js_undefined());
  1013. }
  1014. Value StringLiteral::execute(Interpreter& interpreter) const
  1015. {
  1016. return js_string(interpreter, m_value);
  1017. }
  1018. Value NumericLiteral::execute(Interpreter&) const
  1019. {
  1020. return Value(m_value);
  1021. }
  1022. Value BooleanLiteral::execute(Interpreter&) const
  1023. {
  1024. return Value(m_value);
  1025. }
  1026. Value NullLiteral::execute(Interpreter&) const
  1027. {
  1028. return js_null();
  1029. }
  1030. void ArrayExpression::dump(int indent) const
  1031. {
  1032. ASTNode::dump(indent);
  1033. for (auto& element : m_elements) {
  1034. if (element) {
  1035. element->dump(indent + 1);
  1036. } else {
  1037. print_indent(indent + 1);
  1038. printf("<empty>\n");
  1039. }
  1040. }
  1041. }
  1042. Value ArrayExpression::execute(Interpreter& interpreter) const
  1043. {
  1044. auto* array = Array::create(interpreter.global_object());
  1045. for (auto& element : m_elements) {
  1046. auto value = Value();
  1047. if (element) {
  1048. value = element->execute(interpreter);
  1049. if (interpreter.exception())
  1050. return {};
  1051. if (element->is_spread_expression()) {
  1052. // FIXME: Support arbitrary iterables
  1053. if (value.is_array()) {
  1054. auto& array_to_spread = static_cast<Array&>(value.as_object());
  1055. for (auto& it : array_to_spread.elements()) {
  1056. if (it.is_empty()) {
  1057. array->elements().append(js_undefined());
  1058. } else {
  1059. array->elements().append(it);
  1060. }
  1061. }
  1062. continue;
  1063. }
  1064. if (value.is_string() || (value.is_object() && value.as_object().is_string_object())) {
  1065. String string_to_spread;
  1066. if (value.is_string())
  1067. string_to_spread = value.as_string().string();
  1068. else
  1069. string_to_spread = static_cast<const StringObject&>(value.as_object()).primitive_string().string();
  1070. for (size_t i = 0; i < string_to_spread.length(); ++i)
  1071. array->elements().append(js_string(interpreter, string_to_spread.substring(i, 1)));
  1072. continue;
  1073. }
  1074. interpreter.throw_exception<TypeError>(String::format("%s is not iterable", value.to_string().characters()));
  1075. return {};
  1076. }
  1077. }
  1078. array->elements().append(value);
  1079. }
  1080. return array;
  1081. }
  1082. void TryStatement::dump(int indent) const
  1083. {
  1084. ASTNode::dump(indent);
  1085. print_indent(indent);
  1086. printf("(Block)\n");
  1087. block().dump(indent + 1);
  1088. if (handler()) {
  1089. print_indent(indent);
  1090. printf("(Handler)\n");
  1091. handler()->dump(indent + 1);
  1092. }
  1093. if (finalizer()) {
  1094. print_indent(indent);
  1095. printf("(Finalizer)\n");
  1096. finalizer()->dump(indent + 1);
  1097. }
  1098. }
  1099. void CatchClause::dump(int indent) const
  1100. {
  1101. print_indent(indent);
  1102. printf("CatchClause");
  1103. if (!m_parameter.is_null())
  1104. printf(" (%s)", m_parameter.characters());
  1105. printf("\n");
  1106. body().dump(indent + 1);
  1107. }
  1108. void ThrowStatement::dump(int indent) const
  1109. {
  1110. ASTNode::dump(indent);
  1111. argument().dump(indent + 1);
  1112. }
  1113. Value TryStatement::execute(Interpreter& interpreter) const
  1114. {
  1115. interpreter.run(block(), {}, ScopeType::Try);
  1116. if (auto* exception = interpreter.exception()) {
  1117. if (m_handler) {
  1118. interpreter.clear_exception();
  1119. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  1120. interpreter.run(m_handler->body(), move(arguments));
  1121. }
  1122. }
  1123. if (m_finalizer)
  1124. m_finalizer->execute(interpreter);
  1125. return js_undefined();
  1126. }
  1127. Value CatchClause::execute(Interpreter&) const
  1128. {
  1129. // NOTE: CatchClause execution is handled by TryStatement.
  1130. ASSERT_NOT_REACHED();
  1131. return {};
  1132. }
  1133. Value ThrowStatement::execute(Interpreter& interpreter) const
  1134. {
  1135. auto value = m_argument->execute(interpreter);
  1136. if (interpreter.exception())
  1137. return {};
  1138. return interpreter.throw_exception(value);
  1139. }
  1140. Value SwitchStatement::execute(Interpreter& interpreter) const
  1141. {
  1142. auto discriminant_result = m_discriminant->execute(interpreter);
  1143. if (interpreter.exception())
  1144. return {};
  1145. bool falling_through = false;
  1146. for (auto& switch_case : m_cases) {
  1147. if (!falling_through && switch_case.test()) {
  1148. auto test_result = switch_case.test()->execute(interpreter);
  1149. if (interpreter.exception())
  1150. return {};
  1151. if (!eq(interpreter, discriminant_result, test_result).to_boolean())
  1152. continue;
  1153. }
  1154. falling_through = true;
  1155. for (auto& statement : switch_case.consequent()) {
  1156. statement.execute(interpreter);
  1157. if (interpreter.exception())
  1158. return {};
  1159. if (interpreter.should_unwind()) {
  1160. if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  1161. interpreter.stop_unwind();
  1162. return {};
  1163. }
  1164. return {};
  1165. }
  1166. }
  1167. }
  1168. return js_undefined();
  1169. }
  1170. Value SwitchCase::execute(Interpreter& interpreter) const
  1171. {
  1172. (void)interpreter;
  1173. return {};
  1174. }
  1175. Value BreakStatement::execute(Interpreter& interpreter) const
  1176. {
  1177. interpreter.unwind(ScopeType::Breakable);
  1178. return js_undefined();
  1179. }
  1180. Value ContinueStatement::execute(Interpreter& interpreter) const
  1181. {
  1182. interpreter.unwind(ScopeType::Continuable);
  1183. return js_undefined();
  1184. }
  1185. void SwitchStatement::dump(int indent) const
  1186. {
  1187. ASTNode::dump(indent);
  1188. m_discriminant->dump(indent + 1);
  1189. for (auto& switch_case : m_cases) {
  1190. switch_case.dump(indent + 1);
  1191. }
  1192. }
  1193. void SwitchCase::dump(int indent) const
  1194. {
  1195. ASTNode::dump(indent);
  1196. print_indent(indent);
  1197. if (m_test) {
  1198. printf("(Test)\n");
  1199. m_test->dump(indent + 1);
  1200. } else {
  1201. printf("(Default)\n");
  1202. }
  1203. print_indent(indent);
  1204. printf("(Consequent)\n");
  1205. int i = 0;
  1206. for (auto& statement : m_consequent) {
  1207. print_indent(indent);
  1208. printf("[%d]\n", i++);
  1209. statement.dump(indent + 1);
  1210. }
  1211. }
  1212. Value ConditionalExpression::execute(Interpreter& interpreter) const
  1213. {
  1214. auto test_result = m_test->execute(interpreter);
  1215. if (interpreter.exception())
  1216. return {};
  1217. Value result;
  1218. if (test_result.to_boolean()) {
  1219. result = m_consequent->execute(interpreter);
  1220. } else {
  1221. result = m_alternate->execute(interpreter);
  1222. }
  1223. if (interpreter.exception())
  1224. return {};
  1225. return result;
  1226. }
  1227. void ConditionalExpression::dump(int indent) const
  1228. {
  1229. ASTNode::dump(indent);
  1230. print_indent(indent);
  1231. printf("(Test)\n");
  1232. m_test->dump(indent + 1);
  1233. print_indent(indent);
  1234. printf("(Consequent)\n");
  1235. m_consequent->dump(indent + 1);
  1236. print_indent(indent);
  1237. printf("(Alternate)\n");
  1238. m_alternate->dump(indent + 1);
  1239. }
  1240. void SequenceExpression::dump(int indent) const
  1241. {
  1242. ASTNode::dump(indent);
  1243. for (auto& expression : m_expressions)
  1244. expression.dump(indent + 1);
  1245. }
  1246. Value SequenceExpression::execute(Interpreter& interpreter) const
  1247. {
  1248. Value last_value;
  1249. for (auto& expression : m_expressions) {
  1250. last_value = expression.execute(interpreter);
  1251. if (interpreter.exception())
  1252. return {};
  1253. }
  1254. return last_value;
  1255. }
  1256. Value DebuggerStatement::execute(Interpreter&) const
  1257. {
  1258. dbg() << "Sorry, no JavaScript debugger available (yet)!";
  1259. return js_undefined();
  1260. }
  1261. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1262. {
  1263. m_variables.append(move(variables));
  1264. }
  1265. }