AST.cpp 35 KB

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