AST.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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/ScopeGuard.h>
  29. #include <AK/StringBuilder.h>
  30. #include <LibJS/AST.h>
  31. #include <LibJS/Interpreter.h>
  32. #include <LibJS/Runtime/Array.h>
  33. #include <LibJS/Runtime/Error.h>
  34. #include <LibJS/Runtime/GlobalObject.h>
  35. #include <LibJS/Runtime/NativeFunction.h>
  36. #include <LibJS/Runtime/PrimitiveString.h>
  37. #include <LibJS/Runtime/ScriptFunction.h>
  38. #include <LibJS/Runtime/Value.h>
  39. #include <stdio.h>
  40. namespace JS {
  41. Value ScopeNode::execute(Interpreter& interpreter) const
  42. {
  43. return interpreter.run(*this);
  44. }
  45. Value FunctionDeclaration::execute(Interpreter& interpreter) const
  46. {
  47. auto* function = interpreter.heap().allocate<ScriptFunction>(name(), body(), parameters());
  48. interpreter.set_variable(name(), function);
  49. return js_undefined();
  50. }
  51. Value FunctionExpression::execute(Interpreter& interpreter) const
  52. {
  53. return interpreter.heap().allocate<ScriptFunction>(name(), body(), parameters());
  54. }
  55. Value ExpressionStatement::execute(Interpreter& interpreter) const
  56. {
  57. return m_expression->execute(interpreter);
  58. }
  59. CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter) const
  60. {
  61. if (is_new_expression()) {
  62. // Computing |this| is irrelevant for "new" expression.
  63. return { js_undefined(), m_callee->execute(interpreter) };
  64. }
  65. if (m_callee->is_member_expression()) {
  66. auto& member_expression = static_cast<const MemberExpression&>(*m_callee);
  67. auto object_value = member_expression.object().execute(interpreter);
  68. if (interpreter.exception())
  69. return {};
  70. auto* this_value = object_value.to_object(interpreter.heap());
  71. if (interpreter.exception())
  72. return {};
  73. auto callee = this_value->get(member_expression.computed_property_name(interpreter)).value_or(js_undefined());
  74. return { this_value, callee };
  75. }
  76. return { &interpreter.global_object(), m_callee->execute(interpreter) };
  77. }
  78. Value CallExpression::execute(Interpreter& interpreter) const
  79. {
  80. auto [this_value, callee] = compute_this_and_callee(interpreter);
  81. if (interpreter.exception())
  82. return {};
  83. ASSERT(!callee.is_empty());
  84. if (is_new_expression()) {
  85. if (!callee.is_object()
  86. || !callee.as_object().is_function()
  87. || (callee.as_object().is_native_function()
  88. && !static_cast<NativeFunction&>(callee.as_object()).has_constructor()))
  89. return interpreter.throw_exception<TypeError>(String::format("%s is not a constructor", callee.to_string().characters()));
  90. }
  91. if (!callee.is_object() || !callee.as_object().is_function())
  92. return interpreter.throw_exception<TypeError>(String::format("%s is not a function", callee.to_string().characters()));
  93. auto& function = static_cast<Function&>(callee.as_object());
  94. Vector<Value> arguments;
  95. arguments.ensure_capacity(m_arguments.size());
  96. for (size_t i = 0; i < m_arguments.size(); ++i) {
  97. auto value = m_arguments[i].execute(interpreter);
  98. if (interpreter.exception())
  99. return {};
  100. arguments.append(value);
  101. if (interpreter.exception())
  102. return {};
  103. }
  104. auto& call_frame = interpreter.push_call_frame();
  105. call_frame.function_name = function.name();
  106. call_frame.arguments = move(arguments);
  107. Object* new_object = nullptr;
  108. Value result;
  109. if (is_new_expression()) {
  110. new_object = interpreter.heap().allocate<Object>();
  111. auto prototype = function.get("prototype");
  112. if (prototype.has_value() && prototype.value().is_object())
  113. new_object->set_prototype(&prototype.value().as_object());
  114. call_frame.this_value = new_object;
  115. result = function.construct(interpreter);
  116. } else {
  117. call_frame.this_value = this_value;
  118. result = function.call(interpreter);
  119. }
  120. if (interpreter.exception())
  121. return {};
  122. interpreter.pop_call_frame();
  123. if (is_new_expression()) {
  124. if (result.is_object())
  125. return result;
  126. return new_object;
  127. }
  128. return result;
  129. }
  130. Value ReturnStatement::execute(Interpreter& interpreter) const
  131. {
  132. auto value = argument() ? argument()->execute(interpreter) : js_undefined();
  133. if (interpreter.exception())
  134. return {};
  135. interpreter.unwind(ScopeType::Function);
  136. return value;
  137. }
  138. Value IfStatement::execute(Interpreter& interpreter) const
  139. {
  140. auto predicate_result = m_predicate->execute(interpreter);
  141. if (interpreter.exception())
  142. return {};
  143. if (predicate_result.to_boolean())
  144. return interpreter.run(*m_consequent);
  145. if (m_alternate)
  146. return interpreter.run(*m_alternate);
  147. return js_undefined();
  148. }
  149. Value WhileStatement::execute(Interpreter& interpreter) const
  150. {
  151. Value last_value = js_undefined();
  152. while (m_test->execute(interpreter).to_boolean()) {
  153. if (interpreter.exception())
  154. return {};
  155. last_value = interpreter.run(*m_body);
  156. if (interpreter.exception())
  157. return {};
  158. }
  159. return last_value;
  160. }
  161. Value DoWhileStatement::execute(Interpreter& interpreter) const
  162. {
  163. Value last_value = js_undefined();
  164. do {
  165. if (interpreter.exception())
  166. return {};
  167. last_value = interpreter.run(*m_body);
  168. if (interpreter.exception())
  169. return {};
  170. } while (m_test->execute(interpreter).to_boolean());
  171. return last_value;
  172. }
  173. Value ForStatement::execute(Interpreter& interpreter) const
  174. {
  175. RefPtr<BlockStatement> wrapper;
  176. if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_kind() != DeclarationKind::Var) {
  177. wrapper = create_ast_node<BlockStatement>();
  178. interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
  179. }
  180. auto wrapper_cleanup = ScopeGuard([&] {
  181. if (wrapper)
  182. interpreter.exit_scope(*wrapper);
  183. });
  184. Value last_value = js_undefined();
  185. if (m_init) {
  186. m_init->execute(interpreter);
  187. if (interpreter.exception())
  188. return {};
  189. }
  190. if (m_test) {
  191. while (m_test->execute(interpreter).to_boolean()) {
  192. if (interpreter.exception())
  193. return {};
  194. last_value = interpreter.run(*m_body);
  195. if (interpreter.exception())
  196. return {};
  197. if (interpreter.should_unwind()) {
  198. if (interpreter.should_unwind_until(ScopeType::Continuable)) {
  199. interpreter.stop_unwind();
  200. } else if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  201. interpreter.stop_unwind();
  202. break;
  203. } else {
  204. return js_undefined();
  205. }
  206. }
  207. if (m_update) {
  208. m_update->execute(interpreter);
  209. if (interpreter.exception())
  210. return {};
  211. }
  212. }
  213. } else {
  214. while (true) {
  215. last_value = interpreter.run(*m_body);
  216. if (interpreter.exception())
  217. return {};
  218. if (interpreter.should_unwind()) {
  219. if (interpreter.should_unwind_until(ScopeType::Continuable)) {
  220. interpreter.stop_unwind();
  221. } else if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  222. interpreter.stop_unwind();
  223. break;
  224. } else {
  225. return js_undefined();
  226. }
  227. }
  228. if (m_update) {
  229. m_update->execute(interpreter);
  230. if (interpreter.exception())
  231. return {};
  232. }
  233. }
  234. }
  235. return last_value;
  236. }
  237. Value BinaryExpression::execute(Interpreter& interpreter) const
  238. {
  239. auto lhs_result = m_lhs->execute(interpreter);
  240. if (interpreter.exception())
  241. return {};
  242. auto rhs_result = m_rhs->execute(interpreter);
  243. if (interpreter.exception())
  244. return {};
  245. switch (m_op) {
  246. case BinaryOp::Addition:
  247. return add(lhs_result, rhs_result);
  248. case BinaryOp::Subtraction:
  249. return sub(lhs_result, rhs_result);
  250. case BinaryOp::Multiplication:
  251. return mul(lhs_result, rhs_result);
  252. case BinaryOp::Division:
  253. return div(lhs_result, rhs_result);
  254. case BinaryOp::Modulo:
  255. return mod(lhs_result, rhs_result);
  256. case BinaryOp::Exponentiation:
  257. return exp(lhs_result, rhs_result);
  258. case BinaryOp::TypedEquals:
  259. return typed_eq(lhs_result, rhs_result);
  260. case BinaryOp::TypedInequals:
  261. return Value(!typed_eq(lhs_result, rhs_result).as_bool());
  262. case BinaryOp::AbstractEquals:
  263. return eq(lhs_result, rhs_result);
  264. case BinaryOp::AbstractInequals:
  265. return Value(!eq(lhs_result, rhs_result).as_bool());
  266. case BinaryOp::GreaterThan:
  267. return greater_than(lhs_result, rhs_result);
  268. case BinaryOp::GreaterThanEquals:
  269. return greater_than_equals(lhs_result, rhs_result);
  270. case BinaryOp::LessThan:
  271. return less_than(lhs_result, rhs_result);
  272. case BinaryOp::LessThanEquals:
  273. return less_than_equals(lhs_result, rhs_result);
  274. case BinaryOp::BitwiseAnd:
  275. return bitwise_and(lhs_result, rhs_result);
  276. case BinaryOp::BitwiseOr:
  277. return bitwise_or(lhs_result, rhs_result);
  278. case BinaryOp::BitwiseXor:
  279. return bitwise_xor(lhs_result, rhs_result);
  280. case BinaryOp::LeftShift:
  281. return left_shift(lhs_result, rhs_result);
  282. case BinaryOp::RightShift:
  283. return right_shift(lhs_result, rhs_result);
  284. case BinaryOp::InstanceOf:
  285. return instance_of(lhs_result, rhs_result);
  286. }
  287. ASSERT_NOT_REACHED();
  288. }
  289. Value LogicalExpression::execute(Interpreter& interpreter) const
  290. {
  291. auto lhs_result = m_lhs->execute(interpreter);
  292. if (interpreter.exception())
  293. return {};
  294. switch (m_op) {
  295. case LogicalOp::And:
  296. if (lhs_result.to_boolean()) {
  297. auto rhs_result = m_rhs->execute(interpreter);
  298. if (interpreter.exception())
  299. return {};
  300. return Value(rhs_result);
  301. }
  302. return Value(lhs_result);
  303. case LogicalOp::Or:
  304. if (lhs_result.to_boolean())
  305. return Value(lhs_result);
  306. auto rhs_result = m_rhs->execute(interpreter);
  307. if (interpreter.exception())
  308. return {};
  309. return Value(rhs_result);
  310. }
  311. ASSERT_NOT_REACHED();
  312. }
  313. Value UnaryExpression::execute(Interpreter& interpreter) const
  314. {
  315. auto lhs_result = m_lhs->execute(interpreter);
  316. if (interpreter.exception())
  317. return {};
  318. switch (m_op) {
  319. case UnaryOp::BitwiseNot:
  320. return bitwise_not(lhs_result);
  321. case UnaryOp::Not:
  322. return Value(!lhs_result.to_boolean());
  323. case UnaryOp::Plus:
  324. return unary_plus(lhs_result);
  325. case UnaryOp::Minus:
  326. return unary_minus(lhs_result);
  327. case UnaryOp::Typeof:
  328. switch (lhs_result.type()) {
  329. case Value::Type::Empty:
  330. ASSERT_NOT_REACHED();
  331. return {};
  332. case Value::Type::Undefined:
  333. return js_string(interpreter, "undefined");
  334. case Value::Type::Null:
  335. // yes, this is on purpose. yes, this is how javascript works.
  336. // yes, it's silly.
  337. return js_string(interpreter, "object");
  338. case Value::Type::Number:
  339. return js_string(interpreter, "number");
  340. case Value::Type::String:
  341. return js_string(interpreter, "string");
  342. case Value::Type::Object:
  343. if (lhs_result.as_object().is_function())
  344. return js_string(interpreter, "function");
  345. return js_string(interpreter, "object");
  346. case Value::Type::Boolean:
  347. return js_string(interpreter, "boolean");
  348. }
  349. }
  350. ASSERT_NOT_REACHED();
  351. }
  352. static void print_indent(int indent)
  353. {
  354. for (int i = 0; i < indent * 2; ++i)
  355. putchar(' ');
  356. }
  357. void ASTNode::dump(int indent) const
  358. {
  359. print_indent(indent);
  360. printf("%s\n", class_name());
  361. }
  362. void ScopeNode::dump(int indent) const
  363. {
  364. ASTNode::dump(indent);
  365. if (!m_variables.is_empty()) {
  366. print_indent(indent + 1);
  367. printf("(Variables)\n");
  368. for (auto& variable : m_variables)
  369. variable.dump(indent + 2);
  370. }
  371. if (!m_children.is_empty()) {
  372. print_indent(indent + 1);
  373. printf("(Children)\n");
  374. for (auto& child : children())
  375. child.dump(indent + 2);
  376. }
  377. }
  378. void BinaryExpression::dump(int indent) const
  379. {
  380. const char* op_string = nullptr;
  381. switch (m_op) {
  382. case BinaryOp::Addition:
  383. op_string = "+";
  384. break;
  385. case BinaryOp::Subtraction:
  386. op_string = "-";
  387. break;
  388. case BinaryOp::Multiplication:
  389. op_string = "*";
  390. break;
  391. case BinaryOp::Division:
  392. op_string = "/";
  393. break;
  394. case BinaryOp::Modulo:
  395. op_string = "%";
  396. break;
  397. case BinaryOp::Exponentiation:
  398. op_string = "**";
  399. break;
  400. case BinaryOp::TypedEquals:
  401. op_string = "===";
  402. break;
  403. case BinaryOp::TypedInequals:
  404. op_string = "!==";
  405. break;
  406. case BinaryOp::AbstractEquals:
  407. op_string = "==";
  408. break;
  409. case BinaryOp::AbstractInequals:
  410. op_string = "!=";
  411. break;
  412. case BinaryOp::GreaterThan:
  413. op_string = ">";
  414. break;
  415. case BinaryOp::GreaterThanEquals:
  416. op_string = ">=";
  417. break;
  418. case BinaryOp::LessThan:
  419. op_string = "<";
  420. break;
  421. case BinaryOp::LessThanEquals:
  422. op_string = "<=";
  423. break;
  424. case BinaryOp::BitwiseAnd:
  425. op_string = "&";
  426. break;
  427. case BinaryOp::BitwiseOr:
  428. op_string = "|";
  429. break;
  430. case BinaryOp::BitwiseXor:
  431. op_string = "^";
  432. break;
  433. case BinaryOp::LeftShift:
  434. op_string = "<<";
  435. break;
  436. case BinaryOp::RightShift:
  437. op_string = ">>";
  438. break;
  439. case BinaryOp::InstanceOf:
  440. op_string = "instanceof";
  441. break;
  442. }
  443. print_indent(indent);
  444. printf("%s\n", class_name());
  445. m_lhs->dump(indent + 1);
  446. print_indent(indent + 1);
  447. printf("%s\n", op_string);
  448. m_rhs->dump(indent + 1);
  449. }
  450. void LogicalExpression::dump(int indent) const
  451. {
  452. const char* op_string = nullptr;
  453. switch (m_op) {
  454. case LogicalOp::And:
  455. op_string = "&&";
  456. break;
  457. case LogicalOp::Or:
  458. op_string = "||";
  459. break;
  460. }
  461. print_indent(indent);
  462. printf("%s\n", class_name());
  463. m_lhs->dump(indent + 1);
  464. print_indent(indent + 1);
  465. printf("%s\n", op_string);
  466. m_rhs->dump(indent + 1);
  467. }
  468. void UnaryExpression::dump(int indent) const
  469. {
  470. const char* op_string = nullptr;
  471. switch (m_op) {
  472. case UnaryOp::BitwiseNot:
  473. op_string = "~";
  474. break;
  475. case UnaryOp::Not:
  476. op_string = "!";
  477. break;
  478. case UnaryOp::Plus:
  479. op_string = "+";
  480. break;
  481. case UnaryOp::Minus:
  482. op_string = "-";
  483. break;
  484. case UnaryOp::Typeof:
  485. op_string = "typeof ";
  486. break;
  487. }
  488. print_indent(indent);
  489. printf("%s\n", class_name());
  490. print_indent(indent + 1);
  491. printf("%s\n", op_string);
  492. m_lhs->dump(indent + 1);
  493. }
  494. void CallExpression::dump(int indent) const
  495. {
  496. print_indent(indent);
  497. printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
  498. m_callee->dump(indent + 1);
  499. for (auto& argument : m_arguments)
  500. argument.dump(indent + 1);
  501. }
  502. void StringLiteral::dump(int indent) const
  503. {
  504. print_indent(indent);
  505. printf("StringLiteral \"%s\"\n", m_value.characters());
  506. }
  507. void NumericLiteral::dump(int indent) const
  508. {
  509. print_indent(indent);
  510. printf("NumericLiteral %g\n", m_value);
  511. }
  512. void BooleanLiteral::dump(int indent) const
  513. {
  514. print_indent(indent);
  515. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  516. }
  517. void NullLiteral::dump(int indent) const
  518. {
  519. print_indent(indent);
  520. printf("null\n");
  521. }
  522. void FunctionNode::dump(int indent, const char* class_name) const
  523. {
  524. StringBuilder parameters_builder;
  525. parameters_builder.join(',', parameters());
  526. print_indent(indent);
  527. printf("%s '%s(%s)'\n", class_name, name().characters(), parameters_builder.build().characters());
  528. if (!m_variables.is_empty()) {
  529. print_indent(indent + 1);
  530. printf("(Variables)\n");
  531. }
  532. for (auto& variable : m_variables)
  533. variable.dump(indent + 2);
  534. print_indent(indent + 1);
  535. printf("(Body)\n");
  536. body().dump(indent + 2);
  537. }
  538. void FunctionDeclaration::dump(int indent) const
  539. {
  540. FunctionNode::dump(indent, class_name());
  541. }
  542. void FunctionExpression::dump(int indent) const
  543. {
  544. FunctionNode::dump(indent, class_name());
  545. }
  546. void ReturnStatement::dump(int indent) const
  547. {
  548. ASTNode::dump(indent);
  549. if (argument())
  550. argument()->dump(indent + 1);
  551. }
  552. void IfStatement::dump(int indent) const
  553. {
  554. ASTNode::dump(indent);
  555. print_indent(indent);
  556. printf("If\n");
  557. predicate().dump(indent + 1);
  558. consequent().dump(indent + 1);
  559. if (alternate()) {
  560. print_indent(indent);
  561. printf("Else\n");
  562. alternate()->dump(indent + 1);
  563. }
  564. }
  565. void WhileStatement::dump(int indent) const
  566. {
  567. ASTNode::dump(indent);
  568. print_indent(indent);
  569. printf("While\n");
  570. test().dump(indent + 1);
  571. body().dump(indent + 1);
  572. }
  573. void DoWhileStatement::dump(int indent) const
  574. {
  575. ASTNode::dump(indent);
  576. print_indent(indent);
  577. printf("DoWhile\n");
  578. test().dump(indent + 1);
  579. body().dump(indent + 1);
  580. }
  581. void ForStatement::dump(int indent) const
  582. {
  583. ASTNode::dump(indent);
  584. print_indent(indent);
  585. printf("For\n");
  586. if (init())
  587. init()->dump(indent + 1);
  588. if (test())
  589. test()->dump(indent + 1);
  590. if (update())
  591. update()->dump(indent + 1);
  592. body().dump(indent + 1);
  593. }
  594. Value Identifier::execute(Interpreter& interpreter) const
  595. {
  596. auto variable = interpreter.get_variable(string());
  597. if (!variable.has_value())
  598. return interpreter.throw_exception<ReferenceError>(String::format("'%s' not known", string().characters()));
  599. return variable.value();
  600. }
  601. void Identifier::dump(int indent) const
  602. {
  603. print_indent(indent);
  604. printf("Identifier \"%s\"\n", m_string.characters());
  605. }
  606. Value ThisExpression::execute(Interpreter& interpreter) const
  607. {
  608. return interpreter.this_value();
  609. }
  610. void ThisExpression::dump(int indent) const
  611. {
  612. ASTNode::dump(indent);
  613. }
  614. Value AssignmentExpression::execute(Interpreter& interpreter) const
  615. {
  616. auto rhs_result = m_rhs->execute(interpreter);
  617. if (interpreter.exception())
  618. return {};
  619. Value lhs_result;
  620. switch (m_op) {
  621. case AssignmentOp::Assignment:
  622. break;
  623. case AssignmentOp::AdditionAssignment:
  624. lhs_result = m_lhs->execute(interpreter);
  625. if (interpreter.exception())
  626. return {};
  627. rhs_result = add(lhs_result, rhs_result);
  628. break;
  629. case AssignmentOp::SubtractionAssignment:
  630. lhs_result = m_lhs->execute(interpreter);
  631. if (interpreter.exception())
  632. return {};
  633. rhs_result = sub(lhs_result, rhs_result);
  634. break;
  635. case AssignmentOp::MultiplicationAssignment:
  636. lhs_result = m_lhs->execute(interpreter);
  637. if (interpreter.exception())
  638. return {};
  639. rhs_result = mul(lhs_result, rhs_result);
  640. break;
  641. case AssignmentOp::DivisionAssignment:
  642. lhs_result = m_lhs->execute(interpreter);
  643. if (interpreter.exception())
  644. return {};
  645. rhs_result = div(lhs_result, rhs_result);
  646. break;
  647. }
  648. if (interpreter.exception())
  649. return {};
  650. if (m_lhs->is_identifier()) {
  651. auto name = static_cast<const Identifier&>(*m_lhs).string();
  652. interpreter.set_variable(name, rhs_result);
  653. } else if (m_lhs->is_member_expression()) {
  654. auto object_value = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter);
  655. if (interpreter.exception())
  656. return {};
  657. if (auto* object = object_value.to_object(interpreter.heap())) {
  658. auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
  659. object->put(property_name, rhs_result);
  660. }
  661. } else {
  662. return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
  663. }
  664. return rhs_result;
  665. }
  666. Value UpdateExpression::execute(Interpreter& interpreter) const
  667. {
  668. ASSERT(m_argument->is_identifier());
  669. auto name = static_cast<const Identifier&>(*m_argument).string();
  670. auto previous_variable = interpreter.get_variable(name);
  671. ASSERT(previous_variable.has_value());
  672. auto previous_value = previous_variable.value();
  673. ASSERT(previous_value.is_number());
  674. int op_result = 0;
  675. switch (m_op) {
  676. case UpdateOp::Increment:
  677. op_result = 1;
  678. break;
  679. case UpdateOp::Decrement:
  680. op_result = -1;
  681. break;
  682. }
  683. interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
  684. if (m_prefixed)
  685. return JS::Value(previous_value.as_double() + op_result);
  686. return previous_value;
  687. }
  688. void AssignmentExpression::dump(int indent) const
  689. {
  690. const char* op_string = nullptr;
  691. switch (m_op) {
  692. case AssignmentOp::Assignment:
  693. op_string = "=";
  694. break;
  695. case AssignmentOp::AdditionAssignment:
  696. op_string = "+=";
  697. break;
  698. case AssignmentOp::SubtractionAssignment:
  699. op_string = "-=";
  700. break;
  701. case AssignmentOp::MultiplicationAssignment:
  702. op_string = "*=";
  703. break;
  704. case AssignmentOp::DivisionAssignment:
  705. op_string = "/=";
  706. break;
  707. }
  708. ASTNode::dump(indent);
  709. print_indent(indent + 1);
  710. printf("%s\n", op_string);
  711. m_lhs->dump(indent + 1);
  712. m_rhs->dump(indent + 1);
  713. }
  714. void UpdateExpression::dump(int indent) const
  715. {
  716. const char* op_string = nullptr;
  717. switch (m_op) {
  718. case UpdateOp::Increment:
  719. op_string = "++";
  720. break;
  721. case UpdateOp::Decrement:
  722. op_string = "--";
  723. break;
  724. }
  725. ASTNode::dump(indent);
  726. print_indent(indent + 1);
  727. if (m_prefixed)
  728. printf("%s\n", op_string);
  729. m_argument->dump(indent + 1);
  730. if (!m_prefixed) {
  731. print_indent(indent + 1);
  732. printf("%s\n", op_string);
  733. }
  734. }
  735. Value VariableDeclaration::execute(Interpreter& interpreter) const
  736. {
  737. for (auto& declarator : m_declarations) {
  738. if (auto* init = declarator.init()) {
  739. auto initalizer_result = init->execute(interpreter);
  740. if (interpreter.exception())
  741. return {};
  742. interpreter.set_variable(declarator.id().string(), initalizer_result, true);
  743. }
  744. }
  745. return js_undefined();
  746. }
  747. Value VariableDeclarator::execute(Interpreter&) const
  748. {
  749. // NOTE: This node is handled by VariableDeclaration.
  750. ASSERT_NOT_REACHED();
  751. }
  752. void VariableDeclaration::dump(int indent) const
  753. {
  754. const char* declaration_kind_string = nullptr;
  755. switch (m_declaration_kind) {
  756. case DeclarationKind::Let:
  757. declaration_kind_string = "Let";
  758. break;
  759. case DeclarationKind::Var:
  760. declaration_kind_string = "Var";
  761. break;
  762. case DeclarationKind::Const:
  763. declaration_kind_string = "Const";
  764. break;
  765. }
  766. ASTNode::dump(indent);
  767. print_indent(indent + 1);
  768. printf("%s\n", declaration_kind_string);
  769. for (auto& declarator : m_declarations)
  770. declarator.dump(indent + 1);
  771. }
  772. void VariableDeclarator::dump(int indent) const
  773. {
  774. ASTNode::dump(indent);
  775. m_id->dump(indent + 1);
  776. if (m_init)
  777. m_init->dump(indent + 1);
  778. }
  779. void ObjectExpression::dump(int indent) const
  780. {
  781. ASTNode::dump(indent);
  782. for (auto it : m_properties) {
  783. print_indent(indent + 1);
  784. printf("%s: ", it.key.characters());
  785. it.value->dump(0);
  786. }
  787. }
  788. void ExpressionStatement::dump(int indent) const
  789. {
  790. ASTNode::dump(indent);
  791. m_expression->dump(indent + 1);
  792. }
  793. Value ObjectExpression::execute(Interpreter& interpreter) const
  794. {
  795. auto object = interpreter.heap().allocate<Object>();
  796. for (auto it : m_properties) {
  797. auto value = it.value->execute(interpreter);
  798. if (interpreter.exception())
  799. return {};
  800. object->put(it.key, value);
  801. }
  802. return object;
  803. }
  804. void MemberExpression::dump(int indent) const
  805. {
  806. print_indent(indent);
  807. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  808. m_object->dump(indent + 1);
  809. m_property->dump(indent + 1);
  810. }
  811. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter) const
  812. {
  813. if (!is_computed()) {
  814. ASSERT(m_property->is_identifier());
  815. return PropertyName(static_cast<const Identifier&>(*m_property).string());
  816. }
  817. auto index = m_property->execute(interpreter);
  818. if (interpreter.exception())
  819. return {};
  820. ASSERT(!index.is_empty());
  821. // FIXME: What about non-integer numbers tho.
  822. if (index.is_number() && index.to_i32() >= 0)
  823. return PropertyName(index.to_i32());
  824. return PropertyName(index.to_string());
  825. }
  826. Value MemberExpression::execute(Interpreter& interpreter) const
  827. {
  828. auto object_value = m_object->execute(interpreter);
  829. if (interpreter.exception())
  830. return {};
  831. auto* object_result = object_value.to_object(interpreter.heap());
  832. if (interpreter.exception())
  833. return {};
  834. auto result = object_result->get(computed_property_name(interpreter));
  835. if (result.has_value()) {
  836. ASSERT(!result.value().is_empty());
  837. }
  838. return result.value_or(js_undefined());
  839. }
  840. Value StringLiteral::execute(Interpreter& interpreter) const
  841. {
  842. return js_string(interpreter, m_value);
  843. }
  844. Value NumericLiteral::execute(Interpreter&) const
  845. {
  846. return Value(m_value);
  847. }
  848. Value BooleanLiteral::execute(Interpreter&) const
  849. {
  850. return Value(m_value);
  851. }
  852. Value NullLiteral::execute(Interpreter&) const
  853. {
  854. return js_null();
  855. }
  856. void ArrayExpression::dump(int indent) const
  857. {
  858. ASTNode::dump(indent);
  859. for (auto& element : m_elements) {
  860. element.dump(indent + 1);
  861. }
  862. }
  863. Value ArrayExpression::execute(Interpreter& interpreter) const
  864. {
  865. auto* array = interpreter.heap().allocate<Array>();
  866. for (auto& element : m_elements) {
  867. auto value = element.execute(interpreter);
  868. if (interpreter.exception())
  869. return {};
  870. array->push(value);
  871. }
  872. return array;
  873. }
  874. void TryStatement::dump(int indent) const
  875. {
  876. ASTNode::dump(indent);
  877. print_indent(indent);
  878. printf("(Block)\n");
  879. block().dump(indent + 1);
  880. if (handler()) {
  881. print_indent(indent);
  882. printf("(Handler)\n");
  883. handler()->dump(indent + 1);
  884. }
  885. if (finalizer()) {
  886. print_indent(indent);
  887. printf("(Finalizer)\n");
  888. finalizer()->dump(indent + 1);
  889. }
  890. }
  891. void CatchClause::dump(int indent) const
  892. {
  893. print_indent(indent);
  894. printf("CatchClause");
  895. if (!m_parameter.is_null())
  896. printf(" (%s)", m_parameter.characters());
  897. printf("\n");
  898. body().dump(indent + 1);
  899. }
  900. void ThrowStatement::dump(int indent) const
  901. {
  902. ASTNode::dump(indent);
  903. argument().dump(indent + 1);
  904. }
  905. Value TryStatement::execute(Interpreter& interpreter) const
  906. {
  907. interpreter.run(block(), {}, ScopeType::Try);
  908. if (auto* exception = interpreter.exception()) {
  909. if (m_handler) {
  910. interpreter.clear_exception();
  911. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  912. interpreter.run(m_handler->body(), move(arguments));
  913. }
  914. }
  915. if (m_finalizer)
  916. m_finalizer->execute(interpreter);
  917. return js_undefined();
  918. }
  919. Value CatchClause::execute(Interpreter&) const
  920. {
  921. // NOTE: CatchClause execution is handled by TryStatement.
  922. ASSERT_NOT_REACHED();
  923. return {};
  924. }
  925. Value ThrowStatement::execute(Interpreter& interpreter) const
  926. {
  927. auto value = m_argument->execute(interpreter);
  928. if (interpreter.exception())
  929. return {};
  930. return interpreter.throw_exception(value);
  931. }
  932. Value SwitchStatement::execute(Interpreter& interpreter) const
  933. {
  934. auto discriminant_result = m_discriminant->execute(interpreter);
  935. if (interpreter.exception())
  936. return {};
  937. bool falling_through = false;
  938. for (auto& switch_case : m_cases) {
  939. if (!falling_through && switch_case.test()) {
  940. auto test_result = switch_case.test()->execute(interpreter);
  941. if (interpreter.exception())
  942. return {};
  943. if (!eq(discriminant_result, test_result).to_boolean())
  944. continue;
  945. }
  946. falling_through = true;
  947. for (auto& statement : switch_case.consequent()) {
  948. statement.execute(interpreter);
  949. if (interpreter.exception())
  950. return {};
  951. if (interpreter.should_unwind()) {
  952. if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  953. interpreter.stop_unwind();
  954. return {};
  955. }
  956. return {};
  957. }
  958. }
  959. }
  960. return js_undefined();
  961. }
  962. Value SwitchCase::execute(Interpreter& interpreter) const
  963. {
  964. (void)interpreter;
  965. return {};
  966. }
  967. Value BreakStatement::execute(Interpreter& interpreter) const
  968. {
  969. interpreter.unwind(ScopeType::Breakable);
  970. return js_undefined();
  971. }
  972. Value ContinueStatement::execute(Interpreter& interpreter) const
  973. {
  974. interpreter.unwind(ScopeType::Continuable);
  975. return js_undefined();
  976. }
  977. void SwitchStatement::dump(int indent) const
  978. {
  979. ASTNode::dump(indent);
  980. m_discriminant->dump(indent + 1);
  981. for (auto& switch_case : m_cases) {
  982. switch_case.dump(indent + 1);
  983. }
  984. }
  985. void SwitchCase::dump(int indent) const
  986. {
  987. ASTNode::dump(indent);
  988. print_indent(indent);
  989. if (m_test) {
  990. printf("(Test)\n");
  991. m_test->dump(indent + 1);
  992. } else {
  993. printf("(Default)\n");
  994. }
  995. print_indent(indent);
  996. printf("(Consequent)\n");
  997. int i = 0;
  998. for (auto& statement : m_consequent) {
  999. print_indent(indent);
  1000. printf("[%d]\n", i++);
  1001. statement.dump(indent + 1);
  1002. }
  1003. }
  1004. Value ConditionalExpression::execute(Interpreter& interpreter) const
  1005. {
  1006. auto test_result = m_test->execute(interpreter);
  1007. if (interpreter.exception())
  1008. return {};
  1009. Value result;
  1010. if (test_result.to_boolean()) {
  1011. result = m_consequent->execute(interpreter);
  1012. } else {
  1013. result = m_alternate->execute(interpreter);
  1014. }
  1015. if (interpreter.exception())
  1016. return {};
  1017. return result;
  1018. }
  1019. void ConditionalExpression::dump(int indent) const
  1020. {
  1021. ASTNode::dump(indent);
  1022. print_indent(indent);
  1023. printf("(Test)\n");
  1024. m_test->dump(indent + 1);
  1025. print_indent(indent);
  1026. printf("(Consequent)\n");
  1027. m_test->dump(indent + 1);
  1028. print_indent(indent);
  1029. printf("(Alternate)\n");
  1030. m_test->dump(indent + 1);
  1031. }
  1032. void SequenceExpression::dump(int indent) const
  1033. {
  1034. ASTNode::dump(indent);
  1035. for (auto& expression : m_expressions)
  1036. expression.dump(indent + 1);
  1037. }
  1038. Value SequenceExpression::execute(Interpreter& interpreter) const
  1039. {
  1040. Value last_value;
  1041. for (auto& expression : m_expressions) {
  1042. last_value = expression.execute(interpreter);
  1043. if (interpreter.exception())
  1044. return {};
  1045. }
  1046. return last_value;
  1047. }
  1048. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1049. {
  1050. m_variables.append(move(variables));
  1051. }
  1052. }