AST.cpp 36 KB

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