AST.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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.do_return();
  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. else
  82. return interpreter.run(*m_alternate);
  83. }
  84. Value WhileStatement::execute(Interpreter& interpreter) const
  85. {
  86. Value last_value = js_undefined();
  87. while (m_predicate->execute(interpreter).to_boolean()) {
  88. last_value = interpreter.run(*m_body);
  89. }
  90. return last_value;
  91. }
  92. Value ForStatement::execute(Interpreter& interpreter) const
  93. {
  94. RefPtr<BlockStatement> wrapper;
  95. if (m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_type() != DeclarationType::Var) {
  96. wrapper = create_ast_node<BlockStatement>();
  97. interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
  98. }
  99. Value last_value = js_undefined();
  100. if (m_init)
  101. m_init->execute(interpreter);
  102. if (m_test) {
  103. while (m_test->execute(interpreter).to_boolean()) {
  104. last_value = interpreter.run(*m_body);
  105. if (m_update)
  106. m_update->execute(interpreter);
  107. }
  108. } else {
  109. while (true) {
  110. last_value = interpreter.run(*m_body);
  111. if (m_update)
  112. m_update->execute(interpreter);
  113. }
  114. }
  115. if (wrapper)
  116. interpreter.exit_scope(*wrapper);
  117. return last_value;
  118. }
  119. Value BinaryExpression::execute(Interpreter& interpreter) const
  120. {
  121. auto lhs_result = m_lhs->execute(interpreter);
  122. auto rhs_result = m_rhs->execute(interpreter);
  123. switch (m_op) {
  124. case BinaryOp::Plus:
  125. return add(lhs_result, rhs_result);
  126. case BinaryOp::Minus:
  127. return sub(lhs_result, rhs_result);
  128. case BinaryOp::Asterisk:
  129. return mul(lhs_result, rhs_result);
  130. case BinaryOp::Slash:
  131. return div(lhs_result, rhs_result);
  132. case BinaryOp::TypedEquals:
  133. return typed_eq(lhs_result, rhs_result);
  134. case BinaryOp::TypedInequals:
  135. return Value(!typed_eq(lhs_result, rhs_result).as_bool());
  136. case BinaryOp::AbstractEquals:
  137. return eq(lhs_result, rhs_result);
  138. case BinaryOp::AbstractInequals:
  139. return Value(!eq(lhs_result, rhs_result).as_bool());
  140. case BinaryOp::GreaterThan:
  141. return greater_than(lhs_result, rhs_result);
  142. case BinaryOp::GreaterThanEquals:
  143. return greater_than_equals(lhs_result, rhs_result);
  144. case BinaryOp::LessThan:
  145. return less_than(lhs_result, rhs_result);
  146. case BinaryOp::LessThanEquals:
  147. return less_than_equals(lhs_result, rhs_result);
  148. case BinaryOp::BitwiseAnd:
  149. return bitwise_and(lhs_result, rhs_result);
  150. case BinaryOp::BitwiseOr:
  151. return bitwise_or(lhs_result, rhs_result);
  152. case BinaryOp::BitwiseXor:
  153. return bitwise_xor(lhs_result, rhs_result);
  154. case BinaryOp::LeftShift:
  155. return left_shift(lhs_result, rhs_result);
  156. case BinaryOp::RightShift:
  157. return right_shift(lhs_result, rhs_result);
  158. }
  159. ASSERT_NOT_REACHED();
  160. }
  161. Value LogicalExpression::execute(Interpreter& interpreter) const
  162. {
  163. auto lhs_result = m_lhs->execute(interpreter).to_boolean();
  164. auto rhs_result = m_rhs->execute(interpreter).to_boolean();
  165. switch (m_op) {
  166. case LogicalOp::And:
  167. return Value(lhs_result && rhs_result);
  168. case LogicalOp::Or:
  169. return Value(lhs_result || rhs_result);
  170. }
  171. ASSERT_NOT_REACHED();
  172. }
  173. Value UnaryExpression::execute(Interpreter& interpreter) const
  174. {
  175. auto lhs_result = m_lhs->execute(interpreter);
  176. switch (m_op) {
  177. case UnaryOp::BitwiseNot:
  178. return bitwise_not(lhs_result);
  179. case UnaryOp::Not:
  180. return Value(!lhs_result.to_boolean());
  181. case UnaryOp::Typeof:
  182. switch (lhs_result.type()) {
  183. case Value::Type::Undefined:
  184. return js_string(interpreter.heap(), "undefined");
  185. case Value::Type::Null:
  186. // yes, this is on purpose. yes, this is how javascript works.
  187. // yes, it's silly.
  188. return js_string(interpreter.heap(), "object");
  189. case Value::Type::Number:
  190. return js_string(interpreter.heap(), "number");
  191. case Value::Type::String:
  192. return js_string(interpreter.heap(), "string");
  193. case Value::Type::Object:
  194. return js_string(interpreter.heap(), "object");
  195. case Value::Type::Boolean:
  196. return js_string(interpreter.heap(), "boolean");
  197. }
  198. }
  199. ASSERT_NOT_REACHED();
  200. }
  201. static void print_indent(int indent)
  202. {
  203. for (int i = 0; i < indent * 2; ++i)
  204. putchar(' ');
  205. }
  206. void ASTNode::dump(int indent) const
  207. {
  208. print_indent(indent);
  209. printf("%s\n", class_name());
  210. }
  211. void ScopeNode::dump(int indent) const
  212. {
  213. ASTNode::dump(indent);
  214. for (auto& child : children())
  215. child.dump(indent + 1);
  216. }
  217. void BinaryExpression::dump(int indent) const
  218. {
  219. const char* op_string = nullptr;
  220. switch (m_op) {
  221. case BinaryOp::Plus:
  222. op_string = "+";
  223. break;
  224. case BinaryOp::Minus:
  225. op_string = "-";
  226. break;
  227. case BinaryOp::Asterisk:
  228. op_string = "*";
  229. break;
  230. case BinaryOp::Slash:
  231. op_string = "/";
  232. break;
  233. case BinaryOp::TypedEquals:
  234. op_string = "===";
  235. break;
  236. case BinaryOp::TypedInequals:
  237. op_string = "!==";
  238. break;
  239. case BinaryOp::AbstractEquals:
  240. op_string = "==";
  241. break;
  242. case BinaryOp::AbstractInequals:
  243. op_string = "!=";
  244. break;
  245. case BinaryOp::GreaterThan:
  246. op_string = ">";
  247. break;
  248. case BinaryOp::GreaterThanEquals:
  249. op_string = ">=";
  250. break;
  251. case BinaryOp::LessThan:
  252. op_string = "<";
  253. break;
  254. case BinaryOp::LessThanEquals:
  255. op_string = "<=";
  256. break;
  257. case BinaryOp::BitwiseAnd:
  258. op_string = "&";
  259. break;
  260. case BinaryOp::BitwiseOr:
  261. op_string = "|";
  262. break;
  263. case BinaryOp::BitwiseXor:
  264. op_string = "^";
  265. break;
  266. case BinaryOp::LeftShift:
  267. op_string = "<<";
  268. break;
  269. case BinaryOp::RightShift:
  270. op_string = ">>";
  271. break;
  272. }
  273. print_indent(indent);
  274. printf("%s\n", class_name());
  275. m_lhs->dump(indent + 1);
  276. print_indent(indent + 1);
  277. printf("%s\n", op_string);
  278. m_rhs->dump(indent + 1);
  279. }
  280. void LogicalExpression::dump(int indent) const
  281. {
  282. const char* op_string = nullptr;
  283. switch (m_op) {
  284. case LogicalOp::And:
  285. op_string = "&&";
  286. break;
  287. case LogicalOp::Or:
  288. op_string = "||";
  289. break;
  290. }
  291. print_indent(indent);
  292. printf("%s\n", class_name());
  293. m_lhs->dump(indent + 1);
  294. print_indent(indent + 1);
  295. printf("%s\n", op_string);
  296. m_rhs->dump(indent + 1);
  297. }
  298. void UnaryExpression::dump(int indent) const
  299. {
  300. const char* op_string = nullptr;
  301. switch (m_op) {
  302. case UnaryOp::BitwiseNot:
  303. op_string = "~";
  304. break;
  305. case UnaryOp::Not:
  306. op_string = "!";
  307. break;
  308. case UnaryOp::Typeof:
  309. op_string = "typeof ";
  310. break;
  311. }
  312. print_indent(indent);
  313. printf("%s\n", class_name());
  314. print_indent(indent + 1);
  315. printf("%s\n", op_string);
  316. m_lhs->dump(indent + 1);
  317. }
  318. void CallExpression::dump(int indent) const
  319. {
  320. ASTNode::dump(indent);
  321. m_callee->dump(indent + 1);
  322. for (auto& argument : m_arguments)
  323. argument.dump(indent + 1);
  324. }
  325. void StringLiteral::dump(int indent) const
  326. {
  327. print_indent(indent);
  328. printf("StringLiteral \"%s\"\n", m_value.characters());
  329. }
  330. void NumericLiteral::dump(int indent) const
  331. {
  332. print_indent(indent);
  333. printf("NumericLiteral %g\n", m_value);
  334. }
  335. void BooleanLiteral::dump(int indent) const
  336. {
  337. print_indent(indent);
  338. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  339. }
  340. void UndefinedLiteral::dump(int indent) const
  341. {
  342. print_indent(indent);
  343. printf("undefined\n");
  344. }
  345. void NullLiteral::dump(int indent) const
  346. {
  347. print_indent(indent);
  348. printf("null\n");
  349. }
  350. void FunctionNode::dump(int indent, const char* class_name) const
  351. {
  352. StringBuilder parameters_builder;
  353. parameters_builder.join(',', parameters());
  354. print_indent(indent);
  355. printf("%s '%s(%s)'\n", class_name, name().characters(), parameters_builder.build().characters());
  356. body().dump(indent + 1);
  357. }
  358. void FunctionDeclaration::dump(int indent) const
  359. {
  360. FunctionNode::dump(indent, class_name());
  361. }
  362. void FunctionExpression::dump(int indent) const
  363. {
  364. FunctionNode::dump(indent, class_name());
  365. }
  366. void ReturnStatement::dump(int indent) const
  367. {
  368. ASTNode::dump(indent);
  369. if (argument())
  370. argument()->dump(indent + 1);
  371. }
  372. void IfStatement::dump(int indent) const
  373. {
  374. ASTNode::dump(indent);
  375. print_indent(indent);
  376. printf("If\n");
  377. predicate().dump(indent + 1);
  378. consequent().dump(indent + 1);
  379. print_indent(indent);
  380. printf("Else\n");
  381. alternate().dump(indent + 1);
  382. }
  383. void WhileStatement::dump(int indent) const
  384. {
  385. ASTNode::dump(indent);
  386. print_indent(indent);
  387. printf("While\n");
  388. predicate().dump(indent + 1);
  389. body().dump(indent + 1);
  390. }
  391. void ForStatement::dump(int indent) const
  392. {
  393. ASTNode::dump(indent);
  394. print_indent(indent);
  395. printf("For\n");
  396. if (init())
  397. init()->dump(indent + 1);
  398. if (test())
  399. test()->dump(indent + 1);
  400. if (update())
  401. update()->dump(indent + 1);
  402. body().dump(indent + 1);
  403. }
  404. Value Identifier::execute(Interpreter& interpreter) const
  405. {
  406. return interpreter.get_variable(string());
  407. }
  408. void Identifier::dump(int indent) const
  409. {
  410. print_indent(indent);
  411. printf("Identifier \"%s\"\n", m_string.characters());
  412. }
  413. Value AssignmentExpression::execute(Interpreter& interpreter) const
  414. {
  415. AK::Function<void(Value)> commit;
  416. if (m_lhs->is_identifier()) {
  417. commit = [&](Value value) {
  418. auto name = static_cast<const Identifier&>(*m_lhs).string();
  419. interpreter.set_variable(name, value);
  420. };
  421. } else if (m_lhs->is_member_expression()) {
  422. commit = [&](Value value) {
  423. auto object = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter).to_object(interpreter.heap());
  424. ASSERT(object.is_object());
  425. auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
  426. object.as_object()->put(property_name, value);
  427. };
  428. } else {
  429. ASSERT_NOT_REACHED();
  430. }
  431. auto rhs_result = m_rhs->execute(interpreter);
  432. switch (m_op) {
  433. case AssignmentOp::Assignment:
  434. break;
  435. case AssignmentOp::AdditionAssignment:
  436. rhs_result = add(m_lhs->execute(interpreter), rhs_result);
  437. break;
  438. case AssignmentOp::SubtractionAssignment:
  439. rhs_result = sub(m_lhs->execute(interpreter), rhs_result);
  440. break;
  441. case AssignmentOp::MultiplicationAssignment:
  442. rhs_result = mul(m_lhs->execute(interpreter), rhs_result);
  443. break;
  444. case AssignmentOp::DivisionAssignment:
  445. rhs_result = div(m_lhs->execute(interpreter), rhs_result);
  446. break;
  447. }
  448. commit(rhs_result);
  449. return rhs_result;
  450. }
  451. Value UpdateExpression::execute(Interpreter& interpreter) const
  452. {
  453. ASSERT(m_argument->is_identifier());
  454. auto name = static_cast<const Identifier&>(*m_argument).string();
  455. auto previous_value = interpreter.get_variable(name);
  456. ASSERT(previous_value.is_number());
  457. int op_result = 0;
  458. switch (m_op) {
  459. case UpdateOp::Increment:
  460. op_result = 1;
  461. break;
  462. case UpdateOp::Decrement:
  463. op_result = -1;
  464. break;
  465. }
  466. interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
  467. if (m_prefixed)
  468. return JS::Value(previous_value.as_double() + op_result);
  469. return previous_value;
  470. }
  471. void AssignmentExpression::dump(int indent) const
  472. {
  473. const char* op_string = nullptr;
  474. switch (m_op) {
  475. case AssignmentOp::Assignment:
  476. op_string = "=";
  477. break;
  478. case AssignmentOp::AdditionAssignment:
  479. op_string = "+=";
  480. break;
  481. case AssignmentOp::SubtractionAssignment:
  482. op_string = "-=";
  483. break;
  484. case AssignmentOp::MultiplicationAssignment:
  485. op_string = "*=";
  486. break;
  487. case AssignmentOp::DivisionAssignment:
  488. op_string = "/=";
  489. break;
  490. }
  491. ASTNode::dump(indent);
  492. print_indent(indent + 1);
  493. printf("%s\n", op_string);
  494. m_lhs->dump(indent + 1);
  495. m_rhs->dump(indent + 1);
  496. }
  497. void UpdateExpression::dump(int indent) const
  498. {
  499. const char* op_string = nullptr;
  500. switch (m_op) {
  501. case UpdateOp::Increment:
  502. op_string = "++";
  503. break;
  504. case UpdateOp::Decrement:
  505. op_string = "--";
  506. break;
  507. }
  508. ASTNode::dump(indent);
  509. print_indent(indent + 1);
  510. if (m_prefixed)
  511. printf("%s\n", op_string);
  512. m_argument->dump(indent + 1);
  513. if (!m_prefixed) {
  514. print_indent(indent + 1);
  515. printf("%s\n", op_string);
  516. }
  517. }
  518. Value VariableDeclaration::execute(Interpreter& interpreter) const
  519. {
  520. interpreter.declare_variable(name().string(), m_declaration_type);
  521. if (m_initializer) {
  522. auto initalizer_result = m_initializer->execute(interpreter);
  523. interpreter.set_variable(name().string(), initalizer_result, true);
  524. }
  525. return js_undefined();
  526. }
  527. void VariableDeclaration::dump(int indent) const
  528. {
  529. const char* declaration_type_string = nullptr;
  530. switch (m_declaration_type) {
  531. case DeclarationType::Let:
  532. declaration_type_string = "Let";
  533. break;
  534. case DeclarationType::Var:
  535. declaration_type_string = "Var";
  536. break;
  537. case DeclarationType::Const:
  538. declaration_type_string = "Const";
  539. break;
  540. }
  541. ASTNode::dump(indent);
  542. print_indent(indent + 1);
  543. printf("%s\n", declaration_type_string);
  544. m_name->dump(indent + 1);
  545. if (m_initializer)
  546. m_initializer->dump(indent + 1);
  547. }
  548. void ObjectExpression::dump(int indent) const
  549. {
  550. ASTNode::dump(indent);
  551. for (String property_key : m_properties.keys()) {
  552. print_indent(indent + 1);
  553. printf("%s: ", property_key.characters());
  554. m_properties.get(property_key).value()->dump(0);
  555. }
  556. }
  557. void ExpressionStatement::dump(int indent) const
  558. {
  559. ASTNode::dump(indent);
  560. m_expression->dump(indent + 1);
  561. }
  562. Value ObjectExpression::execute(Interpreter& interpreter) const
  563. {
  564. auto object = interpreter.heap().allocate<Object>();
  565. for (String property_key : m_properties.keys()) {
  566. object->put(property_key, m_properties.get(property_key).value()->execute(interpreter));
  567. }
  568. return object;
  569. }
  570. void MemberExpression::dump(int indent) const
  571. {
  572. print_indent(indent);
  573. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  574. m_object->dump(indent + 1);
  575. m_property->dump(indent + 1);
  576. }
  577. String MemberExpression::computed_property_name(Interpreter& interpreter) const
  578. {
  579. if (!is_computed()) {
  580. ASSERT(m_property->is_identifier());
  581. return static_cast<const Identifier&>(*m_property).string();
  582. }
  583. return m_property->execute(interpreter).to_string();
  584. }
  585. Value MemberExpression::execute(Interpreter& interpreter) const
  586. {
  587. auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  588. ASSERT(object_result.is_object());
  589. return object_result.as_object()->get(computed_property_name(interpreter));
  590. }
  591. Value StringLiteral::execute(Interpreter& interpreter) const
  592. {
  593. return js_string(interpreter.heap(), m_value);
  594. }
  595. Value NumericLiteral::execute(Interpreter&) const
  596. {
  597. return Value(m_value);
  598. }
  599. Value BooleanLiteral::execute(Interpreter&) const
  600. {
  601. return Value(m_value);
  602. }
  603. Value UndefinedLiteral::execute(Interpreter&) const
  604. {
  605. return js_undefined();
  606. }
  607. Value NullLiteral::execute(Interpreter&) const
  608. {
  609. return js_null();
  610. }
  611. void ArrayExpression::dump(int indent) const
  612. {
  613. ASTNode::dump(indent);
  614. for (auto& element : m_elements) {
  615. element.dump(indent + 1);
  616. }
  617. }
  618. Value ArrayExpression::execute(Interpreter& interpreter) const
  619. {
  620. auto* array = interpreter.heap().allocate<Array>();
  621. for (auto& element : m_elements) {
  622. array->push(element.execute(interpreter));
  623. }
  624. return array;
  625. }
  626. }