AST.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <AK/HashMap.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibJS/AST.h>
  30. #include <LibJS/Interpreter.h>
  31. #include <LibJS/Runtime/Array.h>
  32. #include <LibJS/Runtime/PrimitiveString.h>
  33. #include <LibJS/Runtime/ScriptFunction.h>
  34. #include <LibJS/Runtime/Value.h>
  35. #include <stdio.h>
  36. namespace JS {
  37. Value ScopeNode::execute(Interpreter& interpreter) const
  38. {
  39. return interpreter.run(*this);
  40. }
  41. Value FunctionDeclaration::execute(Interpreter& interpreter) const
  42. {
  43. auto* function = interpreter.heap().allocate<ScriptFunction>(body(), parameters());
  44. interpreter.set_variable(name(), function);
  45. return function;
  46. }
  47. Value FunctionExpression::execute(Interpreter& interpreter) const
  48. {
  49. return interpreter.heap().allocate<ScriptFunction>(body(), parameters());
  50. }
  51. Value ExpressionStatement::execute(Interpreter& interpreter) const
  52. {
  53. return m_expression->execute(interpreter);
  54. }
  55. Value CallExpression::execute(Interpreter& interpreter) const
  56. {
  57. auto callee = m_callee->execute(interpreter);
  58. ASSERT(callee.is_object());
  59. ASSERT(callee.as_object()->is_function());
  60. auto* function = static_cast<Function*>(callee.as_object());
  61. auto& call_frame = interpreter.push_call_frame();
  62. for (size_t i = 0; i < m_arguments.size(); ++i)
  63. call_frame.arguments.append(m_arguments[i].execute(interpreter));
  64. if (m_callee->is_member_expression())
  65. call_frame.this_value = static_cast<const MemberExpression&>(*m_callee).object().execute(interpreter).to_object(interpreter.heap());
  66. auto result = function->call(interpreter, call_frame.arguments);
  67. interpreter.pop_call_frame();
  68. return result;
  69. }
  70. Value ReturnStatement::execute(Interpreter& interpreter) const
  71. {
  72. auto value = argument() ? argument()->execute(interpreter) : js_undefined();
  73. interpreter.unwind(ScopeType::Function);
  74. return value;
  75. }
  76. Value IfStatement::execute(Interpreter& interpreter) const
  77. {
  78. auto predicate_result = m_predicate->execute(interpreter);
  79. if (predicate_result.to_boolean())
  80. return interpreter.run(*m_consequent);
  81. if (m_alternate)
  82. return interpreter.run(*m_alternate);
  83. return js_undefined();
  84. }
  85. Value WhileStatement::execute(Interpreter& interpreter) const
  86. {
  87. Value last_value = js_undefined();
  88. while (m_predicate->execute(interpreter).to_boolean()) {
  89. last_value = interpreter.run(*m_body);
  90. }
  91. return last_value;
  92. }
  93. Value ForStatement::execute(Interpreter& interpreter) const
  94. {
  95. RefPtr<BlockStatement> wrapper;
  96. if (m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_type() != DeclarationType::Var) {
  97. wrapper = create_ast_node<BlockStatement>();
  98. interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
  99. }
  100. Value last_value = js_undefined();
  101. if (m_init)
  102. m_init->execute(interpreter);
  103. if (m_test) {
  104. while (m_test->execute(interpreter).to_boolean()) {
  105. last_value = interpreter.run(*m_body);
  106. if (m_update)
  107. m_update->execute(interpreter);
  108. }
  109. } else {
  110. while (true) {
  111. last_value = interpreter.run(*m_body);
  112. if (m_update)
  113. m_update->execute(interpreter);
  114. }
  115. }
  116. if (wrapper)
  117. interpreter.exit_scope(*wrapper);
  118. return last_value;
  119. }
  120. Value BinaryExpression::execute(Interpreter& interpreter) const
  121. {
  122. auto lhs_result = m_lhs->execute(interpreter);
  123. auto rhs_result = m_rhs->execute(interpreter);
  124. switch (m_op) {
  125. case BinaryOp::Plus:
  126. return add(lhs_result, rhs_result);
  127. case BinaryOp::Minus:
  128. return sub(lhs_result, rhs_result);
  129. case BinaryOp::Asterisk:
  130. return mul(lhs_result, rhs_result);
  131. case BinaryOp::Slash:
  132. return div(lhs_result, rhs_result);
  133. case BinaryOp::TypedEquals:
  134. return typed_eq(lhs_result, rhs_result);
  135. case BinaryOp::TypedInequals:
  136. return Value(!typed_eq(lhs_result, rhs_result).as_bool());
  137. case BinaryOp::AbstractEquals:
  138. return eq(lhs_result, rhs_result);
  139. case BinaryOp::AbstractInequals:
  140. return Value(!eq(lhs_result, rhs_result).as_bool());
  141. case BinaryOp::GreaterThan:
  142. return greater_than(lhs_result, rhs_result);
  143. case BinaryOp::GreaterThanEquals:
  144. return greater_than_equals(lhs_result, rhs_result);
  145. case BinaryOp::LessThan:
  146. return less_than(lhs_result, rhs_result);
  147. case BinaryOp::LessThanEquals:
  148. return less_than_equals(lhs_result, rhs_result);
  149. case BinaryOp::BitwiseAnd:
  150. return bitwise_and(lhs_result, rhs_result);
  151. case BinaryOp::BitwiseOr:
  152. return bitwise_or(lhs_result, rhs_result);
  153. case BinaryOp::BitwiseXor:
  154. return bitwise_xor(lhs_result, rhs_result);
  155. case BinaryOp::LeftShift:
  156. return left_shift(lhs_result, rhs_result);
  157. case BinaryOp::RightShift:
  158. return right_shift(lhs_result, rhs_result);
  159. }
  160. ASSERT_NOT_REACHED();
  161. }
  162. Value LogicalExpression::execute(Interpreter& interpreter) const
  163. {
  164. auto lhs_result = m_lhs->execute(interpreter).to_boolean();
  165. auto rhs_result = m_rhs->execute(interpreter).to_boolean();
  166. switch (m_op) {
  167. case LogicalOp::And:
  168. return Value(lhs_result && rhs_result);
  169. case LogicalOp::Or:
  170. return Value(lhs_result || rhs_result);
  171. }
  172. ASSERT_NOT_REACHED();
  173. }
  174. Value UnaryExpression::execute(Interpreter& interpreter) const
  175. {
  176. auto lhs_result = m_lhs->execute(interpreter);
  177. switch (m_op) {
  178. case UnaryOp::BitwiseNot:
  179. return bitwise_not(lhs_result);
  180. case UnaryOp::Not:
  181. return Value(!lhs_result.to_boolean());
  182. case UnaryOp::Typeof:
  183. switch (lhs_result.type()) {
  184. case Value::Type::Undefined:
  185. return js_string(interpreter.heap(), "undefined");
  186. case Value::Type::Null:
  187. // yes, this is on purpose. yes, this is how javascript works.
  188. // yes, it's silly.
  189. return js_string(interpreter.heap(), "object");
  190. case Value::Type::Number:
  191. return js_string(interpreter.heap(), "number");
  192. case Value::Type::String:
  193. return js_string(interpreter.heap(), "string");
  194. case Value::Type::Object:
  195. return js_string(interpreter.heap(), "object");
  196. case Value::Type::Boolean:
  197. return js_string(interpreter.heap(), "boolean");
  198. }
  199. }
  200. ASSERT_NOT_REACHED();
  201. }
  202. static void print_indent(int indent)
  203. {
  204. for (int i = 0; i < indent * 2; ++i)
  205. putchar(' ');
  206. }
  207. void ASTNode::dump(int indent) const
  208. {
  209. print_indent(indent);
  210. printf("%s\n", class_name());
  211. }
  212. void ScopeNode::dump(int indent) const
  213. {
  214. ASTNode::dump(indent);
  215. for (auto& child : children())
  216. child.dump(indent + 1);
  217. }
  218. void BinaryExpression::dump(int indent) const
  219. {
  220. const char* op_string = nullptr;
  221. switch (m_op) {
  222. case BinaryOp::Plus:
  223. op_string = "+";
  224. break;
  225. case BinaryOp::Minus:
  226. op_string = "-";
  227. break;
  228. case BinaryOp::Asterisk:
  229. op_string = "*";
  230. break;
  231. case BinaryOp::Slash:
  232. op_string = "/";
  233. break;
  234. case BinaryOp::TypedEquals:
  235. op_string = "===";
  236. break;
  237. case BinaryOp::TypedInequals:
  238. op_string = "!==";
  239. break;
  240. case BinaryOp::AbstractEquals:
  241. op_string = "==";
  242. break;
  243. case BinaryOp::AbstractInequals:
  244. op_string = "!=";
  245. break;
  246. case BinaryOp::GreaterThan:
  247. op_string = ">";
  248. break;
  249. case BinaryOp::GreaterThanEquals:
  250. op_string = ">=";
  251. break;
  252. case BinaryOp::LessThan:
  253. op_string = "<";
  254. break;
  255. case BinaryOp::LessThanEquals:
  256. op_string = "<=";
  257. break;
  258. case BinaryOp::BitwiseAnd:
  259. op_string = "&";
  260. break;
  261. case BinaryOp::BitwiseOr:
  262. op_string = "|";
  263. break;
  264. case BinaryOp::BitwiseXor:
  265. op_string = "^";
  266. break;
  267. case BinaryOp::LeftShift:
  268. op_string = "<<";
  269. break;
  270. case BinaryOp::RightShift:
  271. op_string = ">>";
  272. break;
  273. }
  274. print_indent(indent);
  275. printf("%s\n", class_name());
  276. m_lhs->dump(indent + 1);
  277. print_indent(indent + 1);
  278. printf("%s\n", op_string);
  279. m_rhs->dump(indent + 1);
  280. }
  281. void LogicalExpression::dump(int indent) const
  282. {
  283. const char* op_string = nullptr;
  284. switch (m_op) {
  285. case LogicalOp::And:
  286. op_string = "&&";
  287. break;
  288. case LogicalOp::Or:
  289. op_string = "||";
  290. break;
  291. }
  292. print_indent(indent);
  293. printf("%s\n", class_name());
  294. m_lhs->dump(indent + 1);
  295. print_indent(indent + 1);
  296. printf("%s\n", op_string);
  297. m_rhs->dump(indent + 1);
  298. }
  299. void UnaryExpression::dump(int indent) const
  300. {
  301. const char* op_string = nullptr;
  302. switch (m_op) {
  303. case UnaryOp::BitwiseNot:
  304. op_string = "~";
  305. break;
  306. case UnaryOp::Not:
  307. op_string = "!";
  308. break;
  309. case UnaryOp::Typeof:
  310. op_string = "typeof ";
  311. break;
  312. }
  313. print_indent(indent);
  314. printf("%s\n", class_name());
  315. print_indent(indent + 1);
  316. printf("%s\n", op_string);
  317. m_lhs->dump(indent + 1);
  318. }
  319. void CallExpression::dump(int indent) const
  320. {
  321. ASTNode::dump(indent);
  322. m_callee->dump(indent + 1);
  323. for (auto& argument : m_arguments)
  324. argument.dump(indent + 1);
  325. }
  326. void StringLiteral::dump(int indent) const
  327. {
  328. print_indent(indent);
  329. printf("StringLiteral \"%s\"\n", m_value.characters());
  330. }
  331. void NumericLiteral::dump(int indent) const
  332. {
  333. print_indent(indent);
  334. printf("NumericLiteral %g\n", m_value);
  335. }
  336. void BooleanLiteral::dump(int indent) const
  337. {
  338. print_indent(indent);
  339. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  340. }
  341. void UndefinedLiteral::dump(int indent) const
  342. {
  343. print_indent(indent);
  344. printf("undefined\n");
  345. }
  346. void NullLiteral::dump(int indent) const
  347. {
  348. print_indent(indent);
  349. printf("null\n");
  350. }
  351. void FunctionNode::dump(int indent, const char* class_name) const
  352. {
  353. StringBuilder parameters_builder;
  354. parameters_builder.join(',', parameters());
  355. print_indent(indent);
  356. printf("%s '%s(%s)'\n", class_name, name().characters(), parameters_builder.build().characters());
  357. body().dump(indent + 1);
  358. }
  359. void FunctionDeclaration::dump(int indent) const
  360. {
  361. FunctionNode::dump(indent, class_name());
  362. }
  363. void FunctionExpression::dump(int indent) const
  364. {
  365. FunctionNode::dump(indent, class_name());
  366. }
  367. void ReturnStatement::dump(int indent) const
  368. {
  369. ASTNode::dump(indent);
  370. if (argument())
  371. argument()->dump(indent + 1);
  372. }
  373. void IfStatement::dump(int indent) const
  374. {
  375. ASTNode::dump(indent);
  376. print_indent(indent);
  377. printf("If\n");
  378. predicate().dump(indent + 1);
  379. consequent().dump(indent + 1);
  380. if (alternate()) {
  381. print_indent(indent);
  382. printf("Else\n");
  383. alternate()->dump(indent + 1);
  384. }
  385. }
  386. void WhileStatement::dump(int indent) const
  387. {
  388. ASTNode::dump(indent);
  389. print_indent(indent);
  390. printf("While\n");
  391. predicate().dump(indent + 1);
  392. body().dump(indent + 1);
  393. }
  394. void ForStatement::dump(int indent) const
  395. {
  396. ASTNode::dump(indent);
  397. print_indent(indent);
  398. printf("For\n");
  399. if (init())
  400. init()->dump(indent + 1);
  401. if (test())
  402. test()->dump(indent + 1);
  403. if (update())
  404. update()->dump(indent + 1);
  405. body().dump(indent + 1);
  406. }
  407. Value Identifier::execute(Interpreter& interpreter) const
  408. {
  409. return interpreter.get_variable(string());
  410. }
  411. void Identifier::dump(int indent) const
  412. {
  413. print_indent(indent);
  414. printf("Identifier \"%s\"\n", m_string.characters());
  415. }
  416. Value AssignmentExpression::execute(Interpreter& interpreter) const
  417. {
  418. AK::Function<void(Value)> commit;
  419. if (m_lhs->is_identifier()) {
  420. commit = [&](Value value) {
  421. auto name = static_cast<const Identifier&>(*m_lhs).string();
  422. interpreter.set_variable(name, value);
  423. };
  424. } else if (m_lhs->is_member_expression()) {
  425. commit = [&](Value value) {
  426. auto object = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter).to_object(interpreter.heap());
  427. ASSERT(object.is_object());
  428. auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
  429. object.as_object()->put(property_name, value);
  430. };
  431. } else {
  432. ASSERT_NOT_REACHED();
  433. }
  434. auto rhs_result = m_rhs->execute(interpreter);
  435. switch (m_op) {
  436. case AssignmentOp::Assignment:
  437. break;
  438. case AssignmentOp::AdditionAssignment:
  439. rhs_result = add(m_lhs->execute(interpreter), rhs_result);
  440. break;
  441. case AssignmentOp::SubtractionAssignment:
  442. rhs_result = sub(m_lhs->execute(interpreter), rhs_result);
  443. break;
  444. case AssignmentOp::MultiplicationAssignment:
  445. rhs_result = mul(m_lhs->execute(interpreter), rhs_result);
  446. break;
  447. case AssignmentOp::DivisionAssignment:
  448. rhs_result = div(m_lhs->execute(interpreter), rhs_result);
  449. break;
  450. }
  451. commit(rhs_result);
  452. return rhs_result;
  453. }
  454. Value UpdateExpression::execute(Interpreter& interpreter) const
  455. {
  456. ASSERT(m_argument->is_identifier());
  457. auto name = static_cast<const Identifier&>(*m_argument).string();
  458. auto previous_value = interpreter.get_variable(name);
  459. ASSERT(previous_value.is_number());
  460. int op_result = 0;
  461. switch (m_op) {
  462. case UpdateOp::Increment:
  463. op_result = 1;
  464. break;
  465. case UpdateOp::Decrement:
  466. op_result = -1;
  467. break;
  468. }
  469. interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
  470. if (m_prefixed)
  471. return JS::Value(previous_value.as_double() + op_result);
  472. return previous_value;
  473. }
  474. void AssignmentExpression::dump(int indent) const
  475. {
  476. const char* op_string = nullptr;
  477. switch (m_op) {
  478. case AssignmentOp::Assignment:
  479. op_string = "=";
  480. break;
  481. case AssignmentOp::AdditionAssignment:
  482. op_string = "+=";
  483. break;
  484. case AssignmentOp::SubtractionAssignment:
  485. op_string = "-=";
  486. break;
  487. case AssignmentOp::MultiplicationAssignment:
  488. op_string = "*=";
  489. break;
  490. case AssignmentOp::DivisionAssignment:
  491. op_string = "/=";
  492. break;
  493. }
  494. ASTNode::dump(indent);
  495. print_indent(indent + 1);
  496. printf("%s\n", op_string);
  497. m_lhs->dump(indent + 1);
  498. m_rhs->dump(indent + 1);
  499. }
  500. void UpdateExpression::dump(int indent) const
  501. {
  502. const char* op_string = nullptr;
  503. switch (m_op) {
  504. case UpdateOp::Increment:
  505. op_string = "++";
  506. break;
  507. case UpdateOp::Decrement:
  508. op_string = "--";
  509. break;
  510. }
  511. ASTNode::dump(indent);
  512. print_indent(indent + 1);
  513. if (m_prefixed)
  514. printf("%s\n", op_string);
  515. m_argument->dump(indent + 1);
  516. if (!m_prefixed) {
  517. print_indent(indent + 1);
  518. printf("%s\n", op_string);
  519. }
  520. }
  521. Value VariableDeclaration::execute(Interpreter& interpreter) const
  522. {
  523. interpreter.declare_variable(name().string(), m_declaration_type);
  524. if (m_initializer) {
  525. auto initalizer_result = m_initializer->execute(interpreter);
  526. interpreter.set_variable(name().string(), initalizer_result, true);
  527. }
  528. return js_undefined();
  529. }
  530. void VariableDeclaration::dump(int indent) const
  531. {
  532. const char* declaration_type_string = nullptr;
  533. switch (m_declaration_type) {
  534. case DeclarationType::Let:
  535. declaration_type_string = "Let";
  536. break;
  537. case DeclarationType::Var:
  538. declaration_type_string = "Var";
  539. break;
  540. case DeclarationType::Const:
  541. declaration_type_string = "Const";
  542. break;
  543. }
  544. ASTNode::dump(indent);
  545. print_indent(indent + 1);
  546. printf("%s\n", declaration_type_string);
  547. m_name->dump(indent + 1);
  548. if (m_initializer)
  549. m_initializer->dump(indent + 1);
  550. }
  551. void ObjectExpression::dump(int indent) const
  552. {
  553. ASTNode::dump(indent);
  554. for (auto it : m_properties) {
  555. print_indent(indent + 1);
  556. printf("%s: ", it.key.characters());
  557. it.value->dump(0);
  558. }
  559. }
  560. void ExpressionStatement::dump(int indent) const
  561. {
  562. ASTNode::dump(indent);
  563. m_expression->dump(indent + 1);
  564. }
  565. Value ObjectExpression::execute(Interpreter& interpreter) const
  566. {
  567. auto object = interpreter.heap().allocate<Object>();
  568. for (auto it : m_properties)
  569. object->put(it.key, it.value->execute(interpreter));
  570. return object;
  571. }
  572. void MemberExpression::dump(int indent) const
  573. {
  574. print_indent(indent);
  575. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  576. m_object->dump(indent + 1);
  577. m_property->dump(indent + 1);
  578. }
  579. FlyString MemberExpression::computed_property_name(Interpreter& interpreter) const
  580. {
  581. if (!is_computed()) {
  582. ASSERT(m_property->is_identifier());
  583. return static_cast<const Identifier&>(*m_property).string();
  584. }
  585. return m_property->execute(interpreter).to_string();
  586. }
  587. Value MemberExpression::execute(Interpreter& interpreter) const
  588. {
  589. auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  590. ASSERT(object_result.is_object());
  591. return object_result.as_object()->get(computed_property_name(interpreter));
  592. }
  593. Value StringLiteral::execute(Interpreter& interpreter) const
  594. {
  595. return js_string(interpreter.heap(), m_value);
  596. }
  597. Value NumericLiteral::execute(Interpreter&) const
  598. {
  599. return Value(m_value);
  600. }
  601. Value BooleanLiteral::execute(Interpreter&) const
  602. {
  603. return Value(m_value);
  604. }
  605. Value UndefinedLiteral::execute(Interpreter&) const
  606. {
  607. return js_undefined();
  608. }
  609. Value NullLiteral::execute(Interpreter&) const
  610. {
  611. return js_null();
  612. }
  613. void ArrayExpression::dump(int indent) const
  614. {
  615. ASTNode::dump(indent);
  616. for (auto& element : m_elements) {
  617. element.dump(indent + 1);
  618. }
  619. }
  620. Value ArrayExpression::execute(Interpreter& interpreter) const
  621. {
  622. auto* array = interpreter.heap().allocate<Array>();
  623. for (auto& element : m_elements) {
  624. array->push(element.execute(interpreter));
  625. }
  626. return array;
  627. }
  628. void TryStatement::dump(int indent) const
  629. {
  630. ASTNode::dump(indent);
  631. print_indent(indent);
  632. printf("(Block)\n");
  633. block().dump(indent + 1);
  634. if (handler()) {
  635. print_indent(indent);
  636. printf("(Handler)\n");
  637. handler()->dump(indent + 1);
  638. }
  639. if (finalizer()) {
  640. print_indent(indent);
  641. printf("(Finalizer)\n");
  642. finalizer()->dump(indent + 1);
  643. }
  644. }
  645. void CatchClause::dump(int indent) const
  646. {
  647. print_indent(indent);
  648. printf("CatchClause");
  649. if (!m_parameter.is_null())
  650. printf(" (%s)", m_parameter.characters());
  651. printf("\n");
  652. body().dump(indent + 1);
  653. }
  654. Value TryStatement::execute(Interpreter&) const
  655. {
  656. return {};
  657. }
  658. Value CatchClause::execute(Interpreter&) const
  659. {
  660. return {};
  661. }
  662. }