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::Plus:
  242. return add(lhs_result, rhs_result);
  243. case BinaryOp::Minus:
  244. return sub(lhs_result, rhs_result);
  245. case BinaryOp::Asterisk:
  246. return mul(lhs_result, rhs_result);
  247. case BinaryOp::Slash:
  248. return div(lhs_result, rhs_result);
  249. case BinaryOp::Modulo:
  250. return mod(lhs_result, rhs_result);
  251. case BinaryOp::TypedEquals:
  252. return typed_eq(lhs_result, rhs_result);
  253. case BinaryOp::TypedInequals:
  254. return Value(!typed_eq(lhs_result, rhs_result).as_bool());
  255. case BinaryOp::AbstractEquals:
  256. return eq(lhs_result, rhs_result);
  257. case BinaryOp::AbstractInequals:
  258. return Value(!eq(lhs_result, rhs_result).as_bool());
  259. case BinaryOp::GreaterThan:
  260. return greater_than(lhs_result, rhs_result);
  261. case BinaryOp::GreaterThanEquals:
  262. return greater_than_equals(lhs_result, rhs_result);
  263. case BinaryOp::LessThan:
  264. return less_than(lhs_result, rhs_result);
  265. case BinaryOp::LessThanEquals:
  266. return less_than_equals(lhs_result, rhs_result);
  267. case BinaryOp::BitwiseAnd:
  268. return bitwise_and(lhs_result, rhs_result);
  269. case BinaryOp::BitwiseOr:
  270. return bitwise_or(lhs_result, rhs_result);
  271. case BinaryOp::BitwiseXor:
  272. return bitwise_xor(lhs_result, rhs_result);
  273. case BinaryOp::LeftShift:
  274. return left_shift(lhs_result, rhs_result);
  275. case BinaryOp::RightShift:
  276. return right_shift(lhs_result, rhs_result);
  277. case BinaryOp::InstanceOf:
  278. return instance_of(lhs_result, rhs_result);
  279. }
  280. ASSERT_NOT_REACHED();
  281. }
  282. Value LogicalExpression::execute(Interpreter& interpreter) const
  283. {
  284. auto lhs_result = m_lhs->execute(interpreter);
  285. if (interpreter.exception())
  286. return {};
  287. switch (m_op) {
  288. case LogicalOp::And:
  289. if (lhs_result.to_boolean()) {
  290. auto rhs_result = m_rhs->execute(interpreter);
  291. if (interpreter.exception())
  292. return {};
  293. return Value(rhs_result);
  294. }
  295. return Value(lhs_result);
  296. case LogicalOp::Or:
  297. if (lhs_result.to_boolean())
  298. return Value(lhs_result);
  299. auto rhs_result = m_rhs->execute(interpreter);
  300. if (interpreter.exception())
  301. return {};
  302. return Value(rhs_result);
  303. }
  304. ASSERT_NOT_REACHED();
  305. }
  306. Value UnaryExpression::execute(Interpreter& interpreter) const
  307. {
  308. auto lhs_result = m_lhs->execute(interpreter);
  309. switch (m_op) {
  310. case UnaryOp::BitwiseNot:
  311. return bitwise_not(lhs_result);
  312. case UnaryOp::Not:
  313. return Value(!lhs_result.to_boolean());
  314. case UnaryOp::Plus:
  315. return unary_plus(lhs_result);
  316. case UnaryOp::Minus:
  317. return unary_minus(lhs_result);
  318. case UnaryOp::Typeof:
  319. switch (lhs_result.type()) {
  320. case Value::Type::Undefined:
  321. return js_string(interpreter, "undefined");
  322. case Value::Type::Null:
  323. // yes, this is on purpose. yes, this is how javascript works.
  324. // yes, it's silly.
  325. return js_string(interpreter, "object");
  326. case Value::Type::Number:
  327. return js_string(interpreter, "number");
  328. case Value::Type::String:
  329. return js_string(interpreter, "string");
  330. case Value::Type::Object:
  331. if (lhs_result.as_object().is_function())
  332. return js_string(interpreter, "function");
  333. return js_string(interpreter, "object");
  334. case Value::Type::Boolean:
  335. return js_string(interpreter, "boolean");
  336. }
  337. }
  338. ASSERT_NOT_REACHED();
  339. }
  340. static void print_indent(int indent)
  341. {
  342. for (int i = 0; i < indent * 2; ++i)
  343. putchar(' ');
  344. }
  345. void ASTNode::dump(int indent) const
  346. {
  347. print_indent(indent);
  348. printf("%s\n", class_name());
  349. }
  350. void ScopeNode::dump(int indent) const
  351. {
  352. ASTNode::dump(indent);
  353. for (auto& child : children())
  354. child.dump(indent + 1);
  355. }
  356. void BinaryExpression::dump(int indent) const
  357. {
  358. const char* op_string = nullptr;
  359. switch (m_op) {
  360. case BinaryOp::Plus:
  361. op_string = "+";
  362. break;
  363. case BinaryOp::Minus:
  364. op_string = "-";
  365. break;
  366. case BinaryOp::Asterisk:
  367. op_string = "*";
  368. break;
  369. case BinaryOp::Slash:
  370. op_string = "/";
  371. break;
  372. case BinaryOp::Modulo:
  373. op_string = "%";
  374. break;
  375. case BinaryOp::TypedEquals:
  376. op_string = "===";
  377. break;
  378. case BinaryOp::TypedInequals:
  379. op_string = "!==";
  380. break;
  381. case BinaryOp::AbstractEquals:
  382. op_string = "==";
  383. break;
  384. case BinaryOp::AbstractInequals:
  385. op_string = "!=";
  386. break;
  387. case BinaryOp::GreaterThan:
  388. op_string = ">";
  389. break;
  390. case BinaryOp::GreaterThanEquals:
  391. op_string = ">=";
  392. break;
  393. case BinaryOp::LessThan:
  394. op_string = "<";
  395. break;
  396. case BinaryOp::LessThanEquals:
  397. op_string = "<=";
  398. break;
  399. case BinaryOp::BitwiseAnd:
  400. op_string = "&";
  401. break;
  402. case BinaryOp::BitwiseOr:
  403. op_string = "|";
  404. break;
  405. case BinaryOp::BitwiseXor:
  406. op_string = "^";
  407. break;
  408. case BinaryOp::LeftShift:
  409. op_string = "<<";
  410. break;
  411. case BinaryOp::RightShift:
  412. op_string = ">>";
  413. break;
  414. case BinaryOp::InstanceOf:
  415. op_string = "instanceof";
  416. break;
  417. }
  418. print_indent(indent);
  419. printf("%s\n", class_name());
  420. m_lhs->dump(indent + 1);
  421. print_indent(indent + 1);
  422. printf("%s\n", op_string);
  423. m_rhs->dump(indent + 1);
  424. }
  425. void LogicalExpression::dump(int indent) const
  426. {
  427. const char* op_string = nullptr;
  428. switch (m_op) {
  429. case LogicalOp::And:
  430. op_string = "&&";
  431. break;
  432. case LogicalOp::Or:
  433. op_string = "||";
  434. break;
  435. }
  436. print_indent(indent);
  437. printf("%s\n", class_name());
  438. m_lhs->dump(indent + 1);
  439. print_indent(indent + 1);
  440. printf("%s\n", op_string);
  441. m_rhs->dump(indent + 1);
  442. }
  443. void UnaryExpression::dump(int indent) const
  444. {
  445. const char* op_string = nullptr;
  446. switch (m_op) {
  447. case UnaryOp::BitwiseNot:
  448. op_string = "~";
  449. break;
  450. case UnaryOp::Not:
  451. op_string = "!";
  452. break;
  453. case UnaryOp::Plus:
  454. op_string = "+";
  455. break;
  456. case UnaryOp::Minus:
  457. op_string = "-";
  458. break;
  459. case UnaryOp::Typeof:
  460. op_string = "typeof ";
  461. break;
  462. }
  463. print_indent(indent);
  464. printf("%s\n", class_name());
  465. print_indent(indent + 1);
  466. printf("%s\n", op_string);
  467. m_lhs->dump(indent + 1);
  468. }
  469. void CallExpression::dump(int indent) const
  470. {
  471. ASTNode::dump(indent);
  472. m_callee->dump(indent + 1);
  473. for (auto& argument : m_arguments)
  474. argument.dump(indent + 1);
  475. }
  476. void StringLiteral::dump(int indent) const
  477. {
  478. print_indent(indent);
  479. printf("StringLiteral \"%s\"\n", m_value.characters());
  480. }
  481. void NumericLiteral::dump(int indent) const
  482. {
  483. print_indent(indent);
  484. printf("NumericLiteral %g\n", m_value);
  485. }
  486. void BooleanLiteral::dump(int indent) const
  487. {
  488. print_indent(indent);
  489. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  490. }
  491. void NullLiteral::dump(int indent) const
  492. {
  493. print_indent(indent);
  494. printf("null\n");
  495. }
  496. void FunctionNode::dump(int indent, const char* class_name) const
  497. {
  498. StringBuilder parameters_builder;
  499. parameters_builder.join(',', parameters());
  500. print_indent(indent);
  501. printf("%s '%s(%s)'\n", class_name, name().characters(), parameters_builder.build().characters());
  502. body().dump(indent + 1);
  503. }
  504. void FunctionDeclaration::dump(int indent) const
  505. {
  506. FunctionNode::dump(indent, class_name());
  507. }
  508. void FunctionExpression::dump(int indent) const
  509. {
  510. FunctionNode::dump(indent, class_name());
  511. }
  512. void ReturnStatement::dump(int indent) const
  513. {
  514. ASTNode::dump(indent);
  515. if (argument())
  516. argument()->dump(indent + 1);
  517. }
  518. void IfStatement::dump(int indent) const
  519. {
  520. ASTNode::dump(indent);
  521. print_indent(indent);
  522. printf("If\n");
  523. predicate().dump(indent + 1);
  524. consequent().dump(indent + 1);
  525. if (alternate()) {
  526. print_indent(indent);
  527. printf("Else\n");
  528. alternate()->dump(indent + 1);
  529. }
  530. }
  531. void WhileStatement::dump(int indent) const
  532. {
  533. ASTNode::dump(indent);
  534. print_indent(indent);
  535. printf("While\n");
  536. test().dump(indent + 1);
  537. body().dump(indent + 1);
  538. }
  539. void DoWhileStatement::dump(int indent) const
  540. {
  541. ASTNode::dump(indent);
  542. print_indent(indent);
  543. printf("DoWhile\n");
  544. test().dump(indent + 1);
  545. body().dump(indent + 1);
  546. }
  547. void ForStatement::dump(int indent) const
  548. {
  549. ASTNode::dump(indent);
  550. print_indent(indent);
  551. printf("For\n");
  552. if (init())
  553. init()->dump(indent + 1);
  554. if (test())
  555. test()->dump(indent + 1);
  556. if (update())
  557. update()->dump(indent + 1);
  558. body().dump(indent + 1);
  559. }
  560. Value Identifier::execute(Interpreter& interpreter) const
  561. {
  562. auto variable = interpreter.get_variable(string());
  563. if (!variable.has_value())
  564. return interpreter.throw_exception<Error>("ReferenceError", String::format("'%s' not known", string().characters()));
  565. return variable.value();
  566. }
  567. void Identifier::dump(int indent) const
  568. {
  569. print_indent(indent);
  570. printf("Identifier \"%s\"\n", m_string.characters());
  571. }
  572. Value AssignmentExpression::execute(Interpreter& interpreter) const
  573. {
  574. AK::Function<void(Value)> commit;
  575. if (m_lhs->is_identifier()) {
  576. commit = [&](Value value) {
  577. auto name = static_cast<const Identifier&>(*m_lhs).string();
  578. interpreter.set_variable(name, value);
  579. };
  580. } else if (m_lhs->is_member_expression()) {
  581. commit = [&](Value value) {
  582. if (auto* object = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter).to_object(interpreter.heap())) {
  583. auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
  584. object->put(property_name, value);
  585. }
  586. };
  587. } else {
  588. ASSERT_NOT_REACHED();
  589. }
  590. auto rhs_result = m_rhs->execute(interpreter);
  591. if (interpreter.exception())
  592. return {};
  593. switch (m_op) {
  594. case AssignmentOp::Assignment:
  595. break;
  596. case AssignmentOp::AdditionAssignment:
  597. rhs_result = add(m_lhs->execute(interpreter), rhs_result);
  598. break;
  599. case AssignmentOp::SubtractionAssignment:
  600. rhs_result = sub(m_lhs->execute(interpreter), rhs_result);
  601. break;
  602. case AssignmentOp::MultiplicationAssignment:
  603. rhs_result = mul(m_lhs->execute(interpreter), rhs_result);
  604. break;
  605. case AssignmentOp::DivisionAssignment:
  606. rhs_result = div(m_lhs->execute(interpreter), rhs_result);
  607. break;
  608. }
  609. if (interpreter.exception())
  610. return {};
  611. commit(rhs_result);
  612. return rhs_result;
  613. }
  614. Value UpdateExpression::execute(Interpreter& interpreter) const
  615. {
  616. ASSERT(m_argument->is_identifier());
  617. auto name = static_cast<const Identifier&>(*m_argument).string();
  618. auto previous_variable = interpreter.get_variable(name);
  619. ASSERT(previous_variable.has_value());
  620. auto previous_value = previous_variable.value();
  621. ASSERT(previous_value.is_number());
  622. int op_result = 0;
  623. switch (m_op) {
  624. case UpdateOp::Increment:
  625. op_result = 1;
  626. break;
  627. case UpdateOp::Decrement:
  628. op_result = -1;
  629. break;
  630. }
  631. interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
  632. if (m_prefixed)
  633. return JS::Value(previous_value.as_double() + op_result);
  634. return previous_value;
  635. }
  636. void AssignmentExpression::dump(int indent) const
  637. {
  638. const char* op_string = nullptr;
  639. switch (m_op) {
  640. case AssignmentOp::Assignment:
  641. op_string = "=";
  642. break;
  643. case AssignmentOp::AdditionAssignment:
  644. op_string = "+=";
  645. break;
  646. case AssignmentOp::SubtractionAssignment:
  647. op_string = "-=";
  648. break;
  649. case AssignmentOp::MultiplicationAssignment:
  650. op_string = "*=";
  651. break;
  652. case AssignmentOp::DivisionAssignment:
  653. op_string = "/=";
  654. break;
  655. }
  656. ASTNode::dump(indent);
  657. print_indent(indent + 1);
  658. printf("%s\n", op_string);
  659. m_lhs->dump(indent + 1);
  660. m_rhs->dump(indent + 1);
  661. }
  662. void UpdateExpression::dump(int indent) const
  663. {
  664. const char* op_string = nullptr;
  665. switch (m_op) {
  666. case UpdateOp::Increment:
  667. op_string = "++";
  668. break;
  669. case UpdateOp::Decrement:
  670. op_string = "--";
  671. break;
  672. }
  673. ASTNode::dump(indent);
  674. print_indent(indent + 1);
  675. if (m_prefixed)
  676. printf("%s\n", op_string);
  677. m_argument->dump(indent + 1);
  678. if (!m_prefixed) {
  679. print_indent(indent + 1);
  680. printf("%s\n", op_string);
  681. }
  682. }
  683. Value VariableDeclaration::execute(Interpreter& interpreter) const
  684. {
  685. for (auto& declarator : m_declarations) {
  686. interpreter.declare_variable(declarator.id().string(), m_declaration_type);
  687. if (auto* init = declarator.init()) {
  688. auto initalizer_result = init->execute(interpreter);
  689. if (interpreter.exception())
  690. return {};
  691. interpreter.set_variable(declarator.id().string(), initalizer_result, true);
  692. }
  693. }
  694. return {};
  695. }
  696. Value VariableDeclarator::execute(Interpreter&) const
  697. {
  698. // NOTE: This node is handled by VariableDeclaration.
  699. ASSERT_NOT_REACHED();
  700. }
  701. void VariableDeclaration::dump(int indent) const
  702. {
  703. const char* declaration_type_string = nullptr;
  704. switch (m_declaration_type) {
  705. case DeclarationType::Let:
  706. declaration_type_string = "Let";
  707. break;
  708. case DeclarationType::Var:
  709. declaration_type_string = "Var";
  710. break;
  711. case DeclarationType::Const:
  712. declaration_type_string = "Const";
  713. break;
  714. }
  715. ASTNode::dump(indent);
  716. print_indent(indent + 1);
  717. printf("%s\n", declaration_type_string);
  718. for (auto& declarator : m_declarations)
  719. declarator.dump(indent + 1);
  720. }
  721. void VariableDeclarator::dump(int indent) const
  722. {
  723. ASTNode::dump(indent);
  724. m_id->dump(indent + 1);
  725. if (m_init)
  726. m_init->dump(indent + 1);
  727. }
  728. void ObjectExpression::dump(int indent) const
  729. {
  730. ASTNode::dump(indent);
  731. for (auto it : m_properties) {
  732. print_indent(indent + 1);
  733. printf("%s: ", it.key.characters());
  734. it.value->dump(0);
  735. }
  736. }
  737. void ExpressionStatement::dump(int indent) const
  738. {
  739. ASTNode::dump(indent);
  740. m_expression->dump(indent + 1);
  741. }
  742. Value ObjectExpression::execute(Interpreter& interpreter) const
  743. {
  744. auto object = interpreter.heap().allocate<Object>();
  745. for (auto it : m_properties) {
  746. auto value = it.value->execute(interpreter);
  747. if (interpreter.exception())
  748. return {};
  749. object->put(it.key, value);
  750. }
  751. return object;
  752. }
  753. void MemberExpression::dump(int indent) const
  754. {
  755. print_indent(indent);
  756. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  757. m_object->dump(indent + 1);
  758. m_property->dump(indent + 1);
  759. }
  760. FlyString MemberExpression::computed_property_name(Interpreter& interpreter) const
  761. {
  762. if (!is_computed()) {
  763. ASSERT(m_property->is_identifier());
  764. return static_cast<const Identifier&>(*m_property).string();
  765. }
  766. return m_property->execute(interpreter).to_string();
  767. }
  768. Value MemberExpression::execute(Interpreter& interpreter) const
  769. {
  770. auto* object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  771. if (interpreter.exception())
  772. return {};
  773. auto result = object_result->get(computed_property_name(interpreter));
  774. return result.value_or({});
  775. }
  776. Value StringLiteral::execute(Interpreter& interpreter) const
  777. {
  778. return js_string(interpreter, m_value);
  779. }
  780. Value NumericLiteral::execute(Interpreter&) const
  781. {
  782. return Value(m_value);
  783. }
  784. Value BooleanLiteral::execute(Interpreter&) const
  785. {
  786. return Value(m_value);
  787. }
  788. Value NullLiteral::execute(Interpreter&) const
  789. {
  790. return js_null();
  791. }
  792. void ArrayExpression::dump(int indent) const
  793. {
  794. ASTNode::dump(indent);
  795. for (auto& element : m_elements) {
  796. element.dump(indent + 1);
  797. }
  798. }
  799. Value ArrayExpression::execute(Interpreter& interpreter) const
  800. {
  801. auto* array = interpreter.heap().allocate<Array>();
  802. for (auto& element : m_elements) {
  803. auto value = element.execute(interpreter);
  804. if (interpreter.exception())
  805. return {};
  806. array->push(value);
  807. }
  808. return array;
  809. }
  810. void TryStatement::dump(int indent) const
  811. {
  812. ASTNode::dump(indent);
  813. print_indent(indent);
  814. printf("(Block)\n");
  815. block().dump(indent + 1);
  816. if (handler()) {
  817. print_indent(indent);
  818. printf("(Handler)\n");
  819. handler()->dump(indent + 1);
  820. }
  821. if (finalizer()) {
  822. print_indent(indent);
  823. printf("(Finalizer)\n");
  824. finalizer()->dump(indent + 1);
  825. }
  826. }
  827. void CatchClause::dump(int indent) const
  828. {
  829. print_indent(indent);
  830. printf("CatchClause");
  831. if (!m_parameter.is_null())
  832. printf(" (%s)", m_parameter.characters());
  833. printf("\n");
  834. body().dump(indent + 1);
  835. }
  836. void ThrowStatement::dump(int indent) const
  837. {
  838. ASTNode::dump(indent);
  839. argument().dump(indent + 1);
  840. }
  841. Value TryStatement::execute(Interpreter& interpreter) const
  842. {
  843. interpreter.run(block(), {}, ScopeType::Try);
  844. if (auto* exception = interpreter.exception()) {
  845. if (m_handler) {
  846. interpreter.clear_exception();
  847. Vector<Argument> arguments { { m_handler->parameter(), exception->value() } };
  848. interpreter.run(m_handler->body(), move(arguments));
  849. }
  850. }
  851. if (m_finalizer)
  852. m_finalizer->execute(interpreter);
  853. return {};
  854. }
  855. Value CatchClause::execute(Interpreter&) const
  856. {
  857. // NOTE: CatchClause execution is handled by TryStatement.
  858. ASSERT_NOT_REACHED();
  859. return {};
  860. }
  861. Value ThrowStatement::execute(Interpreter& interpreter) const
  862. {
  863. auto value = m_argument->execute(interpreter);
  864. if (interpreter.exception())
  865. return {};
  866. return interpreter.throw_exception(value);
  867. }
  868. Value SwitchStatement::execute(Interpreter& interpreter) const
  869. {
  870. auto discriminant_result = m_discriminant->execute(interpreter);
  871. if (interpreter.exception())
  872. return {};
  873. bool falling_through = false;
  874. for (auto& switch_case : m_cases) {
  875. if (!falling_through && switch_case.test()) {
  876. auto test_result = switch_case.test()->execute(interpreter);
  877. if (interpreter.exception())
  878. return {};
  879. if (!eq(discriminant_result, test_result).to_boolean())
  880. continue;
  881. }
  882. falling_through = true;
  883. for (auto& statement : switch_case.consequent()) {
  884. statement.execute(interpreter);
  885. if (interpreter.exception())
  886. return {};
  887. if (interpreter.should_unwind()) {
  888. if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  889. interpreter.stop_unwind();
  890. return {};
  891. }
  892. return {};
  893. }
  894. }
  895. }
  896. return {};
  897. }
  898. Value SwitchCase::execute(Interpreter& interpreter) const
  899. {
  900. (void)interpreter;
  901. return {};
  902. }
  903. Value BreakStatement::execute(Interpreter& interpreter) const
  904. {
  905. interpreter.unwind(ScopeType::Breakable);
  906. return {};
  907. }
  908. Value ContinueStatement::execute(Interpreter& interpreter) const
  909. {
  910. interpreter.unwind(ScopeType::Continuable);
  911. return {};
  912. }
  913. void SwitchStatement::dump(int indent) const
  914. {
  915. ASTNode::dump(indent);
  916. m_discriminant->dump(indent + 1);
  917. for (auto& switch_case : m_cases) {
  918. switch_case.dump(indent + 1);
  919. }
  920. }
  921. void SwitchCase::dump(int indent) const
  922. {
  923. ASTNode::dump(indent);
  924. print_indent(indent);
  925. if (m_test) {
  926. printf("(Test)\n");
  927. m_test->dump(indent + 1);
  928. } else {
  929. printf("(Default)\n");
  930. }
  931. print_indent(indent);
  932. printf("(Consequent)\n");
  933. int i = 0;
  934. for (auto& statement : m_consequent) {
  935. print_indent(indent);
  936. printf("[%d]\n", i++);
  937. statement.dump(indent + 1);
  938. }
  939. }
  940. Value ConditionalExpression::execute(Interpreter& interpreter) const
  941. {
  942. auto test_result = m_test->execute(interpreter);
  943. if (interpreter.exception())
  944. return {};
  945. Value result;
  946. if (test_result.to_boolean()) {
  947. result = m_consequent->execute(interpreter);
  948. } else {
  949. result = m_alternate->execute(interpreter);
  950. }
  951. if (interpreter.exception())
  952. return {};
  953. return result;
  954. }
  955. void ConditionalExpression::dump(int indent) const
  956. {
  957. ASTNode::dump(indent);
  958. print_indent(indent);
  959. printf("(Test)\n");
  960. m_test->dump(indent + 1);
  961. print_indent(indent);
  962. printf("(Consequent)\n");
  963. m_test->dump(indent + 1);
  964. print_indent(indent);
  965. printf("(Alternate)\n");
  966. m_test->dump(indent + 1);
  967. }
  968. }