AST.cpp 34 KB

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