AST.cpp 36 KB

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