AST.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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(interpreter, lhs_result, rhs_result);
  248. case BinaryOp::Subtraction:
  249. return sub(interpreter, lhs_result, rhs_result);
  250. case BinaryOp::Multiplication:
  251. return mul(interpreter, lhs_result, rhs_result);
  252. case BinaryOp::Division:
  253. return div(interpreter, lhs_result, rhs_result);
  254. case BinaryOp::Modulo:
  255. return mod(interpreter, lhs_result, rhs_result);
  256. case BinaryOp::Exponentiation:
  257. return exp(interpreter, lhs_result, rhs_result);
  258. case BinaryOp::TypedEquals:
  259. return typed_eq(interpreter, lhs_result, rhs_result);
  260. case BinaryOp::TypedInequals:
  261. return Value(!typed_eq(interpreter, lhs_result, rhs_result).as_bool());
  262. case BinaryOp::AbstractEquals:
  263. return eq(interpreter, lhs_result, rhs_result);
  264. case BinaryOp::AbstractInequals:
  265. return Value(!eq(interpreter, lhs_result, rhs_result).as_bool());
  266. case BinaryOp::GreaterThan:
  267. return greater_than(interpreter, lhs_result, rhs_result);
  268. case BinaryOp::GreaterThanEquals:
  269. return greater_than_equals(interpreter, lhs_result, rhs_result);
  270. case BinaryOp::LessThan:
  271. return less_than(interpreter, lhs_result, rhs_result);
  272. case BinaryOp::LessThanEquals:
  273. return less_than_equals(interpreter, lhs_result, rhs_result);
  274. case BinaryOp::BitwiseAnd:
  275. return bitwise_and(interpreter, lhs_result, rhs_result);
  276. case BinaryOp::BitwiseOr:
  277. return bitwise_or(interpreter, lhs_result, rhs_result);
  278. case BinaryOp::BitwiseXor:
  279. return bitwise_xor(interpreter, lhs_result, rhs_result);
  280. case BinaryOp::LeftShift:
  281. return left_shift(interpreter, lhs_result, rhs_result);
  282. case BinaryOp::RightShift:
  283. return right_shift(interpreter, lhs_result, rhs_result);
  284. case BinaryOp::InstanceOf:
  285. return instance_of(interpreter, 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(interpreter, lhs_result);
  321. case UnaryOp::Not:
  322. return Value(!lhs_result.to_boolean());
  323. case UnaryOp::Plus:
  324. return unary_plus(interpreter, lhs_result);
  325. case UnaryOp::Minus:
  326. return unary_minus(interpreter, 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. default:
  349. ASSERT_NOT_REACHED();
  350. }
  351. case UnaryOp::Void:
  352. return js_undefined();
  353. }
  354. ASSERT_NOT_REACHED();
  355. }
  356. static void print_indent(int indent)
  357. {
  358. for (int i = 0; i < indent * 2; ++i)
  359. putchar(' ');
  360. }
  361. void ASTNode::dump(int indent) const
  362. {
  363. print_indent(indent);
  364. printf("%s\n", class_name());
  365. }
  366. void ScopeNode::dump(int indent) const
  367. {
  368. ASTNode::dump(indent);
  369. if (!m_variables.is_empty()) {
  370. print_indent(indent + 1);
  371. printf("(Variables)\n");
  372. for (auto& variable : m_variables)
  373. variable.dump(indent + 2);
  374. }
  375. if (!m_children.is_empty()) {
  376. print_indent(indent + 1);
  377. printf("(Children)\n");
  378. for (auto& child : children())
  379. child.dump(indent + 2);
  380. }
  381. }
  382. void BinaryExpression::dump(int indent) const
  383. {
  384. const char* op_string = nullptr;
  385. switch (m_op) {
  386. case BinaryOp::Addition:
  387. op_string = "+";
  388. break;
  389. case BinaryOp::Subtraction:
  390. op_string = "-";
  391. break;
  392. case BinaryOp::Multiplication:
  393. op_string = "*";
  394. break;
  395. case BinaryOp::Division:
  396. op_string = "/";
  397. break;
  398. case BinaryOp::Modulo:
  399. op_string = "%";
  400. break;
  401. case BinaryOp::Exponentiation:
  402. op_string = "**";
  403. break;
  404. case BinaryOp::TypedEquals:
  405. op_string = "===";
  406. break;
  407. case BinaryOp::TypedInequals:
  408. op_string = "!==";
  409. break;
  410. case BinaryOp::AbstractEquals:
  411. op_string = "==";
  412. break;
  413. case BinaryOp::AbstractInequals:
  414. op_string = "!=";
  415. break;
  416. case BinaryOp::GreaterThan:
  417. op_string = ">";
  418. break;
  419. case BinaryOp::GreaterThanEquals:
  420. op_string = ">=";
  421. break;
  422. case BinaryOp::LessThan:
  423. op_string = "<";
  424. break;
  425. case BinaryOp::LessThanEquals:
  426. op_string = "<=";
  427. break;
  428. case BinaryOp::BitwiseAnd:
  429. op_string = "&";
  430. break;
  431. case BinaryOp::BitwiseOr:
  432. op_string = "|";
  433. break;
  434. case BinaryOp::BitwiseXor:
  435. op_string = "^";
  436. break;
  437. case BinaryOp::LeftShift:
  438. op_string = "<<";
  439. break;
  440. case BinaryOp::RightShift:
  441. op_string = ">>";
  442. break;
  443. case BinaryOp::InstanceOf:
  444. op_string = "instanceof";
  445. break;
  446. }
  447. print_indent(indent);
  448. printf("%s\n", class_name());
  449. m_lhs->dump(indent + 1);
  450. print_indent(indent + 1);
  451. printf("%s\n", op_string);
  452. m_rhs->dump(indent + 1);
  453. }
  454. void LogicalExpression::dump(int indent) const
  455. {
  456. const char* op_string = nullptr;
  457. switch (m_op) {
  458. case LogicalOp::And:
  459. op_string = "&&";
  460. break;
  461. case LogicalOp::Or:
  462. op_string = "||";
  463. break;
  464. }
  465. print_indent(indent);
  466. printf("%s\n", class_name());
  467. m_lhs->dump(indent + 1);
  468. print_indent(indent + 1);
  469. printf("%s\n", op_string);
  470. m_rhs->dump(indent + 1);
  471. }
  472. void UnaryExpression::dump(int indent) const
  473. {
  474. const char* op_string = nullptr;
  475. switch (m_op) {
  476. case UnaryOp::BitwiseNot:
  477. op_string = "~";
  478. break;
  479. case UnaryOp::Not:
  480. op_string = "!";
  481. break;
  482. case UnaryOp::Plus:
  483. op_string = "+";
  484. break;
  485. case UnaryOp::Minus:
  486. op_string = "-";
  487. break;
  488. case UnaryOp::Typeof:
  489. op_string = "typeof ";
  490. break;
  491. case UnaryOp::Void:
  492. op_string = "void ";
  493. break;
  494. }
  495. print_indent(indent);
  496. printf("%s\n", class_name());
  497. print_indent(indent + 1);
  498. printf("%s\n", op_string);
  499. m_lhs->dump(indent + 1);
  500. }
  501. void CallExpression::dump(int indent) const
  502. {
  503. print_indent(indent);
  504. printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
  505. m_callee->dump(indent + 1);
  506. for (auto& argument : m_arguments)
  507. argument.dump(indent + 1);
  508. }
  509. void StringLiteral::dump(int indent) const
  510. {
  511. print_indent(indent);
  512. printf("StringLiteral \"%s\"\n", m_value.characters());
  513. }
  514. void NumericLiteral::dump(int indent) const
  515. {
  516. print_indent(indent);
  517. printf("NumericLiteral %g\n", m_value);
  518. }
  519. void BooleanLiteral::dump(int indent) const
  520. {
  521. print_indent(indent);
  522. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  523. }
  524. void NullLiteral::dump(int indent) const
  525. {
  526. print_indent(indent);
  527. printf("null\n");
  528. }
  529. void FunctionNode::dump(int indent, const char* class_name) const
  530. {
  531. StringBuilder parameters_builder;
  532. parameters_builder.join(',', parameters());
  533. print_indent(indent);
  534. printf("%s '%s(%s)'\n", class_name, name().characters(), parameters_builder.build().characters());
  535. if (!m_variables.is_empty()) {
  536. print_indent(indent + 1);
  537. printf("(Variables)\n");
  538. }
  539. for (auto& variable : m_variables)
  540. variable.dump(indent + 2);
  541. print_indent(indent + 1);
  542. printf("(Body)\n");
  543. body().dump(indent + 2);
  544. }
  545. void FunctionDeclaration::dump(int indent) const
  546. {
  547. FunctionNode::dump(indent, class_name());
  548. }
  549. void FunctionExpression::dump(int indent) const
  550. {
  551. FunctionNode::dump(indent, class_name());
  552. }
  553. void ReturnStatement::dump(int indent) const
  554. {
  555. ASTNode::dump(indent);
  556. if (argument())
  557. argument()->dump(indent + 1);
  558. }
  559. void IfStatement::dump(int indent) const
  560. {
  561. ASTNode::dump(indent);
  562. print_indent(indent);
  563. printf("If\n");
  564. predicate().dump(indent + 1);
  565. consequent().dump(indent + 1);
  566. if (alternate()) {
  567. print_indent(indent);
  568. printf("Else\n");
  569. alternate()->dump(indent + 1);
  570. }
  571. }
  572. void WhileStatement::dump(int indent) const
  573. {
  574. ASTNode::dump(indent);
  575. print_indent(indent);
  576. printf("While\n");
  577. test().dump(indent + 1);
  578. body().dump(indent + 1);
  579. }
  580. void DoWhileStatement::dump(int indent) const
  581. {
  582. ASTNode::dump(indent);
  583. print_indent(indent);
  584. printf("DoWhile\n");
  585. test().dump(indent + 1);
  586. body().dump(indent + 1);
  587. }
  588. void ForStatement::dump(int indent) const
  589. {
  590. ASTNode::dump(indent);
  591. print_indent(indent);
  592. printf("For\n");
  593. if (init())
  594. init()->dump(indent + 1);
  595. if (test())
  596. test()->dump(indent + 1);
  597. if (update())
  598. update()->dump(indent + 1);
  599. body().dump(indent + 1);
  600. }
  601. Value Identifier::execute(Interpreter& interpreter) const
  602. {
  603. auto variable = interpreter.get_variable(string());
  604. if (!variable.has_value())
  605. return interpreter.throw_exception<ReferenceError>(String::format("'%s' not known", string().characters()));
  606. return variable.value();
  607. }
  608. void Identifier::dump(int indent) const
  609. {
  610. print_indent(indent);
  611. printf("Identifier \"%s\"\n", m_string.characters());
  612. }
  613. Value ThisExpression::execute(Interpreter& interpreter) const
  614. {
  615. return interpreter.this_value();
  616. }
  617. void ThisExpression::dump(int indent) const
  618. {
  619. ASTNode::dump(indent);
  620. }
  621. Value AssignmentExpression::execute(Interpreter& interpreter) const
  622. {
  623. auto rhs_result = m_rhs->execute(interpreter);
  624. if (interpreter.exception())
  625. return {};
  626. Value lhs_result;
  627. switch (m_op) {
  628. case AssignmentOp::Assignment:
  629. break;
  630. case AssignmentOp::AdditionAssignment:
  631. lhs_result = m_lhs->execute(interpreter);
  632. if (interpreter.exception())
  633. return {};
  634. rhs_result = add(interpreter, lhs_result, rhs_result);
  635. break;
  636. case AssignmentOp::SubtractionAssignment:
  637. lhs_result = m_lhs->execute(interpreter);
  638. if (interpreter.exception())
  639. return {};
  640. rhs_result = sub(interpreter, lhs_result, rhs_result);
  641. break;
  642. case AssignmentOp::MultiplicationAssignment:
  643. lhs_result = m_lhs->execute(interpreter);
  644. if (interpreter.exception())
  645. return {};
  646. rhs_result = mul(interpreter, lhs_result, rhs_result);
  647. break;
  648. case AssignmentOp::DivisionAssignment:
  649. lhs_result = m_lhs->execute(interpreter);
  650. if (interpreter.exception())
  651. return {};
  652. rhs_result = div(interpreter, lhs_result, rhs_result);
  653. break;
  654. }
  655. if (interpreter.exception())
  656. return {};
  657. if (m_lhs->is_identifier()) {
  658. auto name = static_cast<const Identifier&>(*m_lhs).string();
  659. interpreter.set_variable(name, rhs_result);
  660. } else if (m_lhs->is_member_expression()) {
  661. auto object_value = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter);
  662. if (interpreter.exception())
  663. return {};
  664. if (auto* object = object_value.to_object(interpreter.heap())) {
  665. auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
  666. object->put(property_name, rhs_result);
  667. }
  668. } else {
  669. return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
  670. }
  671. return rhs_result;
  672. }
  673. Value UpdateExpression::execute(Interpreter& interpreter) const
  674. {
  675. ASSERT(m_argument->is_identifier());
  676. auto name = static_cast<const Identifier&>(*m_argument).string();
  677. auto previous_variable = interpreter.get_variable(name);
  678. ASSERT(previous_variable.has_value());
  679. auto previous_value = previous_variable.value();
  680. ASSERT(previous_value.is_number());
  681. int op_result = 0;
  682. switch (m_op) {
  683. case UpdateOp::Increment:
  684. op_result = 1;
  685. break;
  686. case UpdateOp::Decrement:
  687. op_result = -1;
  688. break;
  689. }
  690. interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
  691. if (m_prefixed)
  692. return JS::Value(previous_value.as_double() + op_result);
  693. return previous_value;
  694. }
  695. void AssignmentExpression::dump(int indent) const
  696. {
  697. const char* op_string = nullptr;
  698. switch (m_op) {
  699. case AssignmentOp::Assignment:
  700. op_string = "=";
  701. break;
  702. case AssignmentOp::AdditionAssignment:
  703. op_string = "+=";
  704. break;
  705. case AssignmentOp::SubtractionAssignment:
  706. op_string = "-=";
  707. break;
  708. case AssignmentOp::MultiplicationAssignment:
  709. op_string = "*=";
  710. break;
  711. case AssignmentOp::DivisionAssignment:
  712. op_string = "/=";
  713. break;
  714. }
  715. ASTNode::dump(indent);
  716. print_indent(indent + 1);
  717. printf("%s\n", op_string);
  718. m_lhs->dump(indent + 1);
  719. m_rhs->dump(indent + 1);
  720. }
  721. void UpdateExpression::dump(int indent) const
  722. {
  723. const char* op_string = nullptr;
  724. switch (m_op) {
  725. case UpdateOp::Increment:
  726. op_string = "++";
  727. break;
  728. case UpdateOp::Decrement:
  729. op_string = "--";
  730. break;
  731. }
  732. ASTNode::dump(indent);
  733. print_indent(indent + 1);
  734. if (m_prefixed)
  735. printf("%s\n", op_string);
  736. m_argument->dump(indent + 1);
  737. if (!m_prefixed) {
  738. print_indent(indent + 1);
  739. printf("%s\n", op_string);
  740. }
  741. }
  742. Value VariableDeclaration::execute(Interpreter& interpreter) const
  743. {
  744. for (auto& declarator : m_declarations) {
  745. if (auto* init = declarator.init()) {
  746. auto initalizer_result = init->execute(interpreter);
  747. if (interpreter.exception())
  748. return {};
  749. interpreter.set_variable(declarator.id().string(), initalizer_result, true);
  750. }
  751. }
  752. return js_undefined();
  753. }
  754. Value VariableDeclarator::execute(Interpreter&) const
  755. {
  756. // NOTE: This node is handled by VariableDeclaration.
  757. ASSERT_NOT_REACHED();
  758. }
  759. void VariableDeclaration::dump(int indent) const
  760. {
  761. const char* declaration_kind_string = nullptr;
  762. switch (m_declaration_kind) {
  763. case DeclarationKind::Let:
  764. declaration_kind_string = "Let";
  765. break;
  766. case DeclarationKind::Var:
  767. declaration_kind_string = "Var";
  768. break;
  769. case DeclarationKind::Const:
  770. declaration_kind_string = "Const";
  771. break;
  772. }
  773. ASTNode::dump(indent);
  774. print_indent(indent + 1);
  775. printf("%s\n", declaration_kind_string);
  776. for (auto& declarator : m_declarations)
  777. declarator.dump(indent + 1);
  778. }
  779. void VariableDeclarator::dump(int indent) const
  780. {
  781. ASTNode::dump(indent);
  782. m_id->dump(indent + 1);
  783. if (m_init)
  784. m_init->dump(indent + 1);
  785. }
  786. void ObjectExpression::dump(int indent) const
  787. {
  788. ASTNode::dump(indent);
  789. for (auto it : m_properties) {
  790. print_indent(indent + 1);
  791. printf("%s: ", it.key.characters());
  792. it.value->dump(0);
  793. }
  794. }
  795. void ExpressionStatement::dump(int indent) const
  796. {
  797. ASTNode::dump(indent);
  798. m_expression->dump(indent + 1);
  799. }
  800. Value ObjectExpression::execute(Interpreter& interpreter) const
  801. {
  802. auto object = interpreter.heap().allocate<Object>();
  803. for (auto it : m_properties) {
  804. auto value = it.value->execute(interpreter);
  805. if (interpreter.exception())
  806. return {};
  807. object->put(it.key, value);
  808. }
  809. return object;
  810. }
  811. void MemberExpression::dump(int indent) const
  812. {
  813. print_indent(indent);
  814. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  815. m_object->dump(indent + 1);
  816. m_property->dump(indent + 1);
  817. }
  818. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter) const
  819. {
  820. if (!is_computed()) {
  821. ASSERT(m_property->is_identifier());
  822. return PropertyName(static_cast<const Identifier&>(*m_property).string());
  823. }
  824. auto index = m_property->execute(interpreter);
  825. if (interpreter.exception())
  826. return {};
  827. ASSERT(!index.is_empty());
  828. // FIXME: What about non-integer numbers tho.
  829. if (index.is_number() && index.to_i32() >= 0)
  830. return PropertyName(index.to_i32());
  831. return PropertyName(index.to_string());
  832. }
  833. Value MemberExpression::execute(Interpreter& interpreter) const
  834. {
  835. auto object_value = m_object->execute(interpreter);
  836. if (interpreter.exception())
  837. return {};
  838. auto* object_result = object_value.to_object(interpreter.heap());
  839. if (interpreter.exception())
  840. return {};
  841. auto result = object_result->get(computed_property_name(interpreter));
  842. if (result.has_value()) {
  843. ASSERT(!result.value().is_empty());
  844. }
  845. return result.value_or(js_undefined());
  846. }
  847. Value StringLiteral::execute(Interpreter& interpreter) const
  848. {
  849. return js_string(interpreter, m_value);
  850. }
  851. Value NumericLiteral::execute(Interpreter&) const
  852. {
  853. return Value(m_value);
  854. }
  855. Value BooleanLiteral::execute(Interpreter&) const
  856. {
  857. return Value(m_value);
  858. }
  859. Value NullLiteral::execute(Interpreter&) const
  860. {
  861. return js_null();
  862. }
  863. void ArrayExpression::dump(int indent) const
  864. {
  865. ASTNode::dump(indent);
  866. for (auto& element : m_elements) {
  867. if (element) {
  868. element->dump(indent + 1);
  869. } else {
  870. print_indent(indent + 1);
  871. printf("<empty>\n");
  872. }
  873. }
  874. }
  875. Value ArrayExpression::execute(Interpreter& interpreter) const
  876. {
  877. auto* array = interpreter.heap().allocate<Array>();
  878. for (auto& element : m_elements) {
  879. auto value = Value();
  880. if (element) {
  881. value = element->execute(interpreter);
  882. if (interpreter.exception())
  883. return {};
  884. }
  885. array->elements().append(value);
  886. }
  887. return array;
  888. }
  889. void TryStatement::dump(int indent) const
  890. {
  891. ASTNode::dump(indent);
  892. print_indent(indent);
  893. printf("(Block)\n");
  894. block().dump(indent + 1);
  895. if (handler()) {
  896. print_indent(indent);
  897. printf("(Handler)\n");
  898. handler()->dump(indent + 1);
  899. }
  900. if (finalizer()) {
  901. print_indent(indent);
  902. printf("(Finalizer)\n");
  903. finalizer()->dump(indent + 1);
  904. }
  905. }
  906. void CatchClause::dump(int indent) const
  907. {
  908. print_indent(indent);
  909. printf("CatchClause");
  910. if (!m_parameter.is_null())
  911. printf(" (%s)", m_parameter.characters());
  912. printf("\n");
  913. body().dump(indent + 1);
  914. }
  915. void ThrowStatement::dump(int indent) const
  916. {
  917. ASTNode::dump(indent);
  918. argument().dump(indent + 1);
  919. }
  920. Value TryStatement::execute(Interpreter& interpreter) const
  921. {
  922. interpreter.run(block(), {}, ScopeType::Try);
  923. if (auto* exception = interpreter.exception()) {
  924. if (m_handler) {
  925. interpreter.clear_exception();
  926. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  927. interpreter.run(m_handler->body(), move(arguments));
  928. }
  929. }
  930. if (m_finalizer)
  931. m_finalizer->execute(interpreter);
  932. return js_undefined();
  933. }
  934. Value CatchClause::execute(Interpreter&) const
  935. {
  936. // NOTE: CatchClause execution is handled by TryStatement.
  937. ASSERT_NOT_REACHED();
  938. return {};
  939. }
  940. Value ThrowStatement::execute(Interpreter& interpreter) const
  941. {
  942. auto value = m_argument->execute(interpreter);
  943. if (interpreter.exception())
  944. return {};
  945. return interpreter.throw_exception(value);
  946. }
  947. Value SwitchStatement::execute(Interpreter& interpreter) const
  948. {
  949. auto discriminant_result = m_discriminant->execute(interpreter);
  950. if (interpreter.exception())
  951. return {};
  952. bool falling_through = false;
  953. for (auto& switch_case : m_cases) {
  954. if (!falling_through && switch_case.test()) {
  955. auto test_result = switch_case.test()->execute(interpreter);
  956. if (interpreter.exception())
  957. return {};
  958. if (!eq(interpreter, discriminant_result, test_result).to_boolean())
  959. continue;
  960. }
  961. falling_through = true;
  962. for (auto& statement : switch_case.consequent()) {
  963. statement.execute(interpreter);
  964. if (interpreter.exception())
  965. return {};
  966. if (interpreter.should_unwind()) {
  967. if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  968. interpreter.stop_unwind();
  969. return {};
  970. }
  971. return {};
  972. }
  973. }
  974. }
  975. return js_undefined();
  976. }
  977. Value SwitchCase::execute(Interpreter& interpreter) const
  978. {
  979. (void)interpreter;
  980. return {};
  981. }
  982. Value BreakStatement::execute(Interpreter& interpreter) const
  983. {
  984. interpreter.unwind(ScopeType::Breakable);
  985. return js_undefined();
  986. }
  987. Value ContinueStatement::execute(Interpreter& interpreter) const
  988. {
  989. interpreter.unwind(ScopeType::Continuable);
  990. return js_undefined();
  991. }
  992. void SwitchStatement::dump(int indent) const
  993. {
  994. ASTNode::dump(indent);
  995. m_discriminant->dump(indent + 1);
  996. for (auto& switch_case : m_cases) {
  997. switch_case.dump(indent + 1);
  998. }
  999. }
  1000. void SwitchCase::dump(int indent) const
  1001. {
  1002. ASTNode::dump(indent);
  1003. print_indent(indent);
  1004. if (m_test) {
  1005. printf("(Test)\n");
  1006. m_test->dump(indent + 1);
  1007. } else {
  1008. printf("(Default)\n");
  1009. }
  1010. print_indent(indent);
  1011. printf("(Consequent)\n");
  1012. int i = 0;
  1013. for (auto& statement : m_consequent) {
  1014. print_indent(indent);
  1015. printf("[%d]\n", i++);
  1016. statement.dump(indent + 1);
  1017. }
  1018. }
  1019. Value ConditionalExpression::execute(Interpreter& interpreter) const
  1020. {
  1021. auto test_result = m_test->execute(interpreter);
  1022. if (interpreter.exception())
  1023. return {};
  1024. Value result;
  1025. if (test_result.to_boolean()) {
  1026. result = m_consequent->execute(interpreter);
  1027. } else {
  1028. result = m_alternate->execute(interpreter);
  1029. }
  1030. if (interpreter.exception())
  1031. return {};
  1032. return result;
  1033. }
  1034. void ConditionalExpression::dump(int indent) const
  1035. {
  1036. ASTNode::dump(indent);
  1037. print_indent(indent);
  1038. printf("(Test)\n");
  1039. m_test->dump(indent + 1);
  1040. print_indent(indent);
  1041. printf("(Consequent)\n");
  1042. m_test->dump(indent + 1);
  1043. print_indent(indent);
  1044. printf("(Alternate)\n");
  1045. m_test->dump(indent + 1);
  1046. }
  1047. void SequenceExpression::dump(int indent) const
  1048. {
  1049. ASTNode::dump(indent);
  1050. for (auto& expression : m_expressions)
  1051. expression.dump(indent + 1);
  1052. }
  1053. Value SequenceExpression::execute(Interpreter& interpreter) const
  1054. {
  1055. Value last_value;
  1056. for (auto& expression : m_expressions) {
  1057. last_value = expression.execute(interpreter);
  1058. if (interpreter.exception())
  1059. return {};
  1060. }
  1061. return last_value;
  1062. }
  1063. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1064. {
  1065. m_variables.append(move(variables));
  1066. }
  1067. }