AST.cpp 23 KB

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