AST.cpp 30 KB

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