AST.cpp 41 KB

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