AST.cpp 23 KB

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