AST.cpp 27 KB

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