AST.cpp 33 KB

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