AST.cpp 21 KB

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