AST.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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. StringBuilder parameters_builder;
  612. parameters_builder.join(',', parameters());
  613. print_indent(indent);
  614. printf("%s '%s(%s)'\n", class_name, name().characters(), parameters_builder.build().characters());
  615. if (!m_variables.is_empty()) {
  616. print_indent(indent + 1);
  617. printf("(Variables)\n");
  618. }
  619. for (auto& variable : m_variables)
  620. variable.dump(indent + 2);
  621. print_indent(indent + 1);
  622. printf("(Body)\n");
  623. body().dump(indent + 2);
  624. }
  625. void FunctionDeclaration::dump(int indent) const
  626. {
  627. FunctionNode::dump(indent, class_name());
  628. }
  629. void FunctionExpression::dump(int indent) const
  630. {
  631. FunctionNode::dump(indent, class_name());
  632. }
  633. void ReturnStatement::dump(int indent) const
  634. {
  635. ASTNode::dump(indent);
  636. if (argument())
  637. argument()->dump(indent + 1);
  638. }
  639. void IfStatement::dump(int indent) const
  640. {
  641. ASTNode::dump(indent);
  642. print_indent(indent);
  643. printf("If\n");
  644. predicate().dump(indent + 1);
  645. consequent().dump(indent + 1);
  646. if (alternate()) {
  647. print_indent(indent);
  648. printf("Else\n");
  649. alternate()->dump(indent + 1);
  650. }
  651. }
  652. void WhileStatement::dump(int indent) const
  653. {
  654. ASTNode::dump(indent);
  655. print_indent(indent);
  656. printf("While\n");
  657. test().dump(indent + 1);
  658. body().dump(indent + 1);
  659. }
  660. void DoWhileStatement::dump(int indent) const
  661. {
  662. ASTNode::dump(indent);
  663. print_indent(indent);
  664. printf("DoWhile\n");
  665. test().dump(indent + 1);
  666. body().dump(indent + 1);
  667. }
  668. void ForStatement::dump(int indent) const
  669. {
  670. ASTNode::dump(indent);
  671. print_indent(indent);
  672. printf("For\n");
  673. if (init())
  674. init()->dump(indent + 1);
  675. if (test())
  676. test()->dump(indent + 1);
  677. if (update())
  678. update()->dump(indent + 1);
  679. body().dump(indent + 1);
  680. }
  681. Value Identifier::execute(Interpreter& interpreter) const
  682. {
  683. auto value = interpreter.get_variable(string());
  684. if (value.is_empty())
  685. return interpreter.throw_exception<ReferenceError>(String::format("'%s' not known", string().characters()));
  686. return value;
  687. }
  688. void Identifier::dump(int indent) const
  689. {
  690. print_indent(indent);
  691. printf("Identifier \"%s\"\n", m_string.characters());
  692. }
  693. void SpreadExpression::dump(int indent) const
  694. {
  695. ASTNode::dump(indent);
  696. m_target->dump(indent + 1);
  697. }
  698. Value SpreadExpression::execute(Interpreter& interpreter) const
  699. {
  700. return m_target->execute(interpreter);
  701. }
  702. Value ThisExpression::execute(Interpreter& interpreter) const
  703. {
  704. return interpreter.this_value();
  705. }
  706. void ThisExpression::dump(int indent) const
  707. {
  708. ASTNode::dump(indent);
  709. }
  710. Value AssignmentExpression::execute(Interpreter& interpreter) const
  711. {
  712. auto rhs_result = m_rhs->execute(interpreter);
  713. if (interpreter.exception())
  714. return {};
  715. Value lhs_result;
  716. switch (m_op) {
  717. case AssignmentOp::Assignment:
  718. break;
  719. case AssignmentOp::AdditionAssignment:
  720. lhs_result = m_lhs->execute(interpreter);
  721. if (interpreter.exception())
  722. return {};
  723. rhs_result = add(interpreter, lhs_result, rhs_result);
  724. break;
  725. case AssignmentOp::SubtractionAssignment:
  726. lhs_result = m_lhs->execute(interpreter);
  727. if (interpreter.exception())
  728. return {};
  729. rhs_result = sub(interpreter, lhs_result, rhs_result);
  730. break;
  731. case AssignmentOp::MultiplicationAssignment:
  732. lhs_result = m_lhs->execute(interpreter);
  733. if (interpreter.exception())
  734. return {};
  735. rhs_result = mul(interpreter, lhs_result, rhs_result);
  736. break;
  737. case AssignmentOp::DivisionAssignment:
  738. lhs_result = m_lhs->execute(interpreter);
  739. if (interpreter.exception())
  740. return {};
  741. rhs_result = div(interpreter, lhs_result, rhs_result);
  742. break;
  743. case AssignmentOp::LeftShiftAssignment:
  744. lhs_result = m_lhs->execute(interpreter);
  745. if (interpreter.exception())
  746. return {};
  747. rhs_result = left_shift(interpreter, lhs_result, rhs_result);
  748. break;
  749. case AssignmentOp::RightShiftAssignment:
  750. lhs_result = m_lhs->execute(interpreter);
  751. if (interpreter.exception())
  752. return {};
  753. rhs_result = right_shift(interpreter, lhs_result, rhs_result);
  754. break;
  755. case AssignmentOp::UnsignedRightShiftAssignment:
  756. lhs_result = m_lhs->execute(interpreter);
  757. if (interpreter.exception())
  758. return {};
  759. rhs_result = unsigned_right_shift(interpreter, lhs_result, rhs_result);
  760. break;
  761. }
  762. if (interpreter.exception())
  763. return {};
  764. auto reference = m_lhs->to_reference(interpreter);
  765. if (interpreter.exception())
  766. return {};
  767. if (reference.is_unresolvable())
  768. return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
  769. reference.put(interpreter, rhs_result);
  770. if (interpreter.exception())
  771. return {};
  772. return rhs_result;
  773. }
  774. Value UpdateExpression::execute(Interpreter& interpreter) const
  775. {
  776. auto reference = m_argument->to_reference(interpreter);
  777. if (interpreter.exception())
  778. return {};
  779. auto old_value = reference.get(interpreter);
  780. if (interpreter.exception())
  781. return {};
  782. old_value = old_value.to_number();
  783. int op_result = 0;
  784. switch (m_op) {
  785. case UpdateOp::Increment:
  786. op_result = 1;
  787. break;
  788. case UpdateOp::Decrement:
  789. op_result = -1;
  790. break;
  791. default:
  792. ASSERT_NOT_REACHED();
  793. }
  794. auto new_value = Value(old_value.as_double() + op_result);
  795. reference.put(interpreter, new_value);
  796. if (interpreter.exception())
  797. return {};
  798. return m_prefixed ? new_value : old_value;
  799. }
  800. void AssignmentExpression::dump(int indent) const
  801. {
  802. const char* op_string = nullptr;
  803. switch (m_op) {
  804. case AssignmentOp::Assignment:
  805. op_string = "=";
  806. break;
  807. case AssignmentOp::AdditionAssignment:
  808. op_string = "+=";
  809. break;
  810. case AssignmentOp::SubtractionAssignment:
  811. op_string = "-=";
  812. break;
  813. case AssignmentOp::MultiplicationAssignment:
  814. op_string = "*=";
  815. break;
  816. case AssignmentOp::DivisionAssignment:
  817. op_string = "/=";
  818. break;
  819. case AssignmentOp::LeftShiftAssignment:
  820. op_string = "<<=";
  821. break;
  822. case AssignmentOp::RightShiftAssignment:
  823. op_string = ">>=";
  824. break;
  825. case AssignmentOp::UnsignedRightShiftAssignment:
  826. op_string = ">>>=";
  827. break;
  828. }
  829. ASTNode::dump(indent);
  830. print_indent(indent + 1);
  831. printf("%s\n", op_string);
  832. m_lhs->dump(indent + 1);
  833. m_rhs->dump(indent + 1);
  834. }
  835. void UpdateExpression::dump(int indent) const
  836. {
  837. const char* op_string = nullptr;
  838. switch (m_op) {
  839. case UpdateOp::Increment:
  840. op_string = "++";
  841. break;
  842. case UpdateOp::Decrement:
  843. op_string = "--";
  844. break;
  845. }
  846. ASTNode::dump(indent);
  847. print_indent(indent + 1);
  848. if (m_prefixed)
  849. printf("%s\n", op_string);
  850. m_argument->dump(indent + 1);
  851. if (!m_prefixed) {
  852. print_indent(indent + 1);
  853. printf("%s\n", op_string);
  854. }
  855. }
  856. Value VariableDeclaration::execute(Interpreter& interpreter) const
  857. {
  858. for (auto& declarator : m_declarations) {
  859. if (auto* init = declarator.init()) {
  860. auto initalizer_result = init->execute(interpreter);
  861. if (interpreter.exception())
  862. return {};
  863. interpreter.set_variable(declarator.id().string(), initalizer_result, true);
  864. }
  865. }
  866. return js_undefined();
  867. }
  868. Value VariableDeclarator::execute(Interpreter&) const
  869. {
  870. // NOTE: This node is handled by VariableDeclaration.
  871. ASSERT_NOT_REACHED();
  872. }
  873. void VariableDeclaration::dump(int indent) const
  874. {
  875. const char* declaration_kind_string = nullptr;
  876. switch (m_declaration_kind) {
  877. case DeclarationKind::Let:
  878. declaration_kind_string = "Let";
  879. break;
  880. case DeclarationKind::Var:
  881. declaration_kind_string = "Var";
  882. break;
  883. case DeclarationKind::Const:
  884. declaration_kind_string = "Const";
  885. break;
  886. }
  887. ASTNode::dump(indent);
  888. print_indent(indent + 1);
  889. printf("%s\n", declaration_kind_string);
  890. for (auto& declarator : m_declarations)
  891. declarator.dump(indent + 1);
  892. }
  893. void VariableDeclarator::dump(int indent) const
  894. {
  895. ASTNode::dump(indent);
  896. m_id->dump(indent + 1);
  897. if (m_init)
  898. m_init->dump(indent + 1);
  899. }
  900. void ObjectProperty::dump(int indent) const
  901. {
  902. ASTNode::dump(indent);
  903. m_key->dump(indent + 1);
  904. m_value->dump(indent + 1);
  905. }
  906. void ObjectExpression::dump(int indent) const
  907. {
  908. ASTNode::dump(indent);
  909. for (auto& property : m_properties) {
  910. property.dump(indent + 1);
  911. }
  912. }
  913. void ExpressionStatement::dump(int indent) const
  914. {
  915. ASTNode::dump(indent);
  916. m_expression->dump(indent + 1);
  917. }
  918. Value ObjectProperty::execute(Interpreter&) const
  919. {
  920. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  921. ASSERT_NOT_REACHED();
  922. }
  923. Value ObjectExpression::execute(Interpreter& interpreter) const
  924. {
  925. auto* object = Object::create_empty(interpreter, interpreter.global_object());
  926. for (auto& property : m_properties) {
  927. auto key_result = property.key().execute(interpreter);
  928. if (interpreter.exception())
  929. return {};
  930. if (property.is_spread()) {
  931. if (key_result.is_array()) {
  932. auto& array_to_spread = static_cast<Array&>(key_result.as_object());
  933. auto& elements = array_to_spread.elements();
  934. for (size_t i = 0; i < elements.size(); ++i) {
  935. auto element = elements.at(i);
  936. if (!element.is_empty())
  937. object->put_by_index(i, element);
  938. }
  939. } else if (key_result.is_object()) {
  940. auto& obj_to_spread = key_result.as_object();
  941. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  942. if (it.value.attributes & Attribute::Enumerable)
  943. object->put(it.key, obj_to_spread.get(it.key));
  944. }
  945. } else if (key_result.is_string()) {
  946. auto& str_to_spread = key_result.as_string().string();
  947. for (size_t i = 0; i < str_to_spread.length(); i++) {
  948. object->put_by_index(i, js_string(interpreter, str_to_spread.substring(i, 1)));
  949. }
  950. }
  951. continue;
  952. }
  953. auto key = key_result.to_string();
  954. auto value = property.value().execute(interpreter);
  955. if (interpreter.exception())
  956. return {};
  957. object->put(key, value);
  958. }
  959. return object;
  960. }
  961. void MemberExpression::dump(int indent) const
  962. {
  963. print_indent(indent);
  964. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  965. m_object->dump(indent + 1);
  966. m_property->dump(indent + 1);
  967. }
  968. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter) const
  969. {
  970. if (!is_computed()) {
  971. ASSERT(m_property->is_identifier());
  972. return PropertyName(static_cast<const Identifier&>(*m_property).string());
  973. }
  974. auto index = m_property->execute(interpreter);
  975. if (interpreter.exception())
  976. return {};
  977. ASSERT(!index.is_empty());
  978. // FIXME: What about non-integer numbers tho.
  979. if (index.is_number() && index.to_i32() >= 0)
  980. return PropertyName(index.to_i32());
  981. return PropertyName(index.to_string());
  982. }
  983. String MemberExpression::to_string_approximation() const
  984. {
  985. String object_string = "<object>";
  986. if (m_object->is_identifier())
  987. object_string = static_cast<const Identifier&>(*m_object).string();
  988. if (is_computed())
  989. return String::format("%s[<computed>]", object_string.characters());
  990. ASSERT(m_property->is_identifier());
  991. return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
  992. }
  993. Value MemberExpression::execute(Interpreter& interpreter) const
  994. {
  995. auto object_value = m_object->execute(interpreter);
  996. if (interpreter.exception())
  997. return {};
  998. auto* object_result = object_value.to_object(interpreter.heap());
  999. if (interpreter.exception())
  1000. return {};
  1001. return object_result->get(computed_property_name(interpreter)).value_or(js_undefined());
  1002. }
  1003. Value StringLiteral::execute(Interpreter& interpreter) const
  1004. {
  1005. return js_string(interpreter, m_value);
  1006. }
  1007. Value NumericLiteral::execute(Interpreter&) const
  1008. {
  1009. return Value(m_value);
  1010. }
  1011. Value BooleanLiteral::execute(Interpreter&) const
  1012. {
  1013. return Value(m_value);
  1014. }
  1015. Value NullLiteral::execute(Interpreter&) const
  1016. {
  1017. return js_null();
  1018. }
  1019. void ArrayExpression::dump(int indent) const
  1020. {
  1021. ASTNode::dump(indent);
  1022. for (auto& element : m_elements) {
  1023. if (element) {
  1024. element->dump(indent + 1);
  1025. } else {
  1026. print_indent(indent + 1);
  1027. printf("<empty>\n");
  1028. }
  1029. }
  1030. }
  1031. Value ArrayExpression::execute(Interpreter& interpreter) const
  1032. {
  1033. auto* array = Array::create(interpreter.global_object());
  1034. for (auto& element : m_elements) {
  1035. auto value = Value();
  1036. if (element) {
  1037. value = element->execute(interpreter);
  1038. if (interpreter.exception())
  1039. return {};
  1040. if (element->is_spread_expression()) {
  1041. // FIXME: Support arbitrary iterables
  1042. if (value.is_array()) {
  1043. auto& array_to_spread = static_cast<Array&>(value.as_object());
  1044. for (auto& it : array_to_spread.elements()) {
  1045. if (it.is_empty()) {
  1046. array->elements().append(js_undefined());
  1047. } else {
  1048. array->elements().append(it);
  1049. }
  1050. }
  1051. continue;
  1052. }
  1053. if (value.is_string() || (value.is_object() && value.as_object().is_string_object())) {
  1054. String string_to_spread;
  1055. if (value.is_string())
  1056. string_to_spread = value.as_string().string();
  1057. else
  1058. string_to_spread = static_cast<const StringObject&>(value.as_object()).primitive_string().string();
  1059. for (size_t i = 0; i < string_to_spread.length(); ++i)
  1060. array->elements().append(js_string(interpreter, string_to_spread.substring(i, 1)));
  1061. continue;
  1062. }
  1063. interpreter.throw_exception<TypeError>(String::format("%s is not iterable", value.to_string().characters()));
  1064. return {};
  1065. }
  1066. }
  1067. array->elements().append(value);
  1068. }
  1069. return array;
  1070. }
  1071. void TryStatement::dump(int indent) const
  1072. {
  1073. ASTNode::dump(indent);
  1074. print_indent(indent);
  1075. printf("(Block)\n");
  1076. block().dump(indent + 1);
  1077. if (handler()) {
  1078. print_indent(indent);
  1079. printf("(Handler)\n");
  1080. handler()->dump(indent + 1);
  1081. }
  1082. if (finalizer()) {
  1083. print_indent(indent);
  1084. printf("(Finalizer)\n");
  1085. finalizer()->dump(indent + 1);
  1086. }
  1087. }
  1088. void CatchClause::dump(int indent) const
  1089. {
  1090. print_indent(indent);
  1091. printf("CatchClause");
  1092. if (!m_parameter.is_null())
  1093. printf(" (%s)", m_parameter.characters());
  1094. printf("\n");
  1095. body().dump(indent + 1);
  1096. }
  1097. void ThrowStatement::dump(int indent) const
  1098. {
  1099. ASTNode::dump(indent);
  1100. argument().dump(indent + 1);
  1101. }
  1102. Value TryStatement::execute(Interpreter& interpreter) const
  1103. {
  1104. interpreter.run(block(), {}, ScopeType::Try);
  1105. if (auto* exception = interpreter.exception()) {
  1106. if (m_handler) {
  1107. interpreter.clear_exception();
  1108. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  1109. interpreter.run(m_handler->body(), move(arguments));
  1110. }
  1111. }
  1112. if (m_finalizer)
  1113. m_finalizer->execute(interpreter);
  1114. return js_undefined();
  1115. }
  1116. Value CatchClause::execute(Interpreter&) const
  1117. {
  1118. // NOTE: CatchClause execution is handled by TryStatement.
  1119. ASSERT_NOT_REACHED();
  1120. return {};
  1121. }
  1122. Value ThrowStatement::execute(Interpreter& interpreter) const
  1123. {
  1124. auto value = m_argument->execute(interpreter);
  1125. if (interpreter.exception())
  1126. return {};
  1127. return interpreter.throw_exception(value);
  1128. }
  1129. Value SwitchStatement::execute(Interpreter& interpreter) const
  1130. {
  1131. auto discriminant_result = m_discriminant->execute(interpreter);
  1132. if (interpreter.exception())
  1133. return {};
  1134. bool falling_through = false;
  1135. for (auto& switch_case : m_cases) {
  1136. if (!falling_through && switch_case.test()) {
  1137. auto test_result = switch_case.test()->execute(interpreter);
  1138. if (interpreter.exception())
  1139. return {};
  1140. if (!eq(interpreter, discriminant_result, test_result).to_boolean())
  1141. continue;
  1142. }
  1143. falling_through = true;
  1144. for (auto& statement : switch_case.consequent()) {
  1145. statement.execute(interpreter);
  1146. if (interpreter.exception())
  1147. return {};
  1148. if (interpreter.should_unwind()) {
  1149. if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  1150. interpreter.stop_unwind();
  1151. return {};
  1152. }
  1153. return {};
  1154. }
  1155. }
  1156. }
  1157. return js_undefined();
  1158. }
  1159. Value SwitchCase::execute(Interpreter& interpreter) const
  1160. {
  1161. (void)interpreter;
  1162. return {};
  1163. }
  1164. Value BreakStatement::execute(Interpreter& interpreter) const
  1165. {
  1166. interpreter.unwind(ScopeType::Breakable);
  1167. return js_undefined();
  1168. }
  1169. Value ContinueStatement::execute(Interpreter& interpreter) const
  1170. {
  1171. interpreter.unwind(ScopeType::Continuable);
  1172. return js_undefined();
  1173. }
  1174. void SwitchStatement::dump(int indent) const
  1175. {
  1176. ASTNode::dump(indent);
  1177. m_discriminant->dump(indent + 1);
  1178. for (auto& switch_case : m_cases) {
  1179. switch_case.dump(indent + 1);
  1180. }
  1181. }
  1182. void SwitchCase::dump(int indent) const
  1183. {
  1184. ASTNode::dump(indent);
  1185. print_indent(indent);
  1186. if (m_test) {
  1187. printf("(Test)\n");
  1188. m_test->dump(indent + 1);
  1189. } else {
  1190. printf("(Default)\n");
  1191. }
  1192. print_indent(indent);
  1193. printf("(Consequent)\n");
  1194. int i = 0;
  1195. for (auto& statement : m_consequent) {
  1196. print_indent(indent);
  1197. printf("[%d]\n", i++);
  1198. statement.dump(indent + 1);
  1199. }
  1200. }
  1201. Value ConditionalExpression::execute(Interpreter& interpreter) const
  1202. {
  1203. auto test_result = m_test->execute(interpreter);
  1204. if (interpreter.exception())
  1205. return {};
  1206. Value result;
  1207. if (test_result.to_boolean()) {
  1208. result = m_consequent->execute(interpreter);
  1209. } else {
  1210. result = m_alternate->execute(interpreter);
  1211. }
  1212. if (interpreter.exception())
  1213. return {};
  1214. return result;
  1215. }
  1216. void ConditionalExpression::dump(int indent) const
  1217. {
  1218. ASTNode::dump(indent);
  1219. print_indent(indent);
  1220. printf("(Test)\n");
  1221. m_test->dump(indent + 1);
  1222. print_indent(indent);
  1223. printf("(Consequent)\n");
  1224. m_test->dump(indent + 1);
  1225. print_indent(indent);
  1226. printf("(Alternate)\n");
  1227. m_test->dump(indent + 1);
  1228. }
  1229. void SequenceExpression::dump(int indent) const
  1230. {
  1231. ASTNode::dump(indent);
  1232. for (auto& expression : m_expressions)
  1233. expression.dump(indent + 1);
  1234. }
  1235. Value SequenceExpression::execute(Interpreter& interpreter) const
  1236. {
  1237. Value last_value;
  1238. for (auto& expression : m_expressions) {
  1239. last_value = expression.execute(interpreter);
  1240. if (interpreter.exception())
  1241. return {};
  1242. }
  1243. return last_value;
  1244. }
  1245. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1246. {
  1247. m_variables.append(move(variables));
  1248. }
  1249. }