AST.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter) const
  760. {
  761. if (!is_computed()) {
  762. ASSERT(m_property->is_identifier());
  763. return PropertyName(static_cast<const Identifier&>(*m_property).string());
  764. }
  765. auto index = m_property->execute(interpreter);
  766. // FIXME: What about non-integer numbers tho.
  767. if (index.is_number())
  768. return PropertyName(index.to_i32());
  769. return PropertyName(index.to_string());
  770. }
  771. Value MemberExpression::execute(Interpreter& interpreter) const
  772. {
  773. auto* object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  774. if (interpreter.exception())
  775. return {};
  776. auto result = object_result->get(computed_property_name(interpreter));
  777. return result.value_or({});
  778. }
  779. Value StringLiteral::execute(Interpreter& interpreter) const
  780. {
  781. return js_string(interpreter, m_value);
  782. }
  783. Value NumericLiteral::execute(Interpreter&) const
  784. {
  785. return Value(m_value);
  786. }
  787. Value BooleanLiteral::execute(Interpreter&) const
  788. {
  789. return Value(m_value);
  790. }
  791. Value NullLiteral::execute(Interpreter&) const
  792. {
  793. return js_null();
  794. }
  795. void ArrayExpression::dump(int indent) const
  796. {
  797. ASTNode::dump(indent);
  798. for (auto& element : m_elements) {
  799. element.dump(indent + 1);
  800. }
  801. }
  802. Value ArrayExpression::execute(Interpreter& interpreter) const
  803. {
  804. auto* array = interpreter.heap().allocate<Array>();
  805. for (auto& element : m_elements) {
  806. auto value = element.execute(interpreter);
  807. if (interpreter.exception())
  808. return {};
  809. array->push(value);
  810. }
  811. return array;
  812. }
  813. void TryStatement::dump(int indent) const
  814. {
  815. ASTNode::dump(indent);
  816. print_indent(indent);
  817. printf("(Block)\n");
  818. block().dump(indent + 1);
  819. if (handler()) {
  820. print_indent(indent);
  821. printf("(Handler)\n");
  822. handler()->dump(indent + 1);
  823. }
  824. if (finalizer()) {
  825. print_indent(indent);
  826. printf("(Finalizer)\n");
  827. finalizer()->dump(indent + 1);
  828. }
  829. }
  830. void CatchClause::dump(int indent) const
  831. {
  832. print_indent(indent);
  833. printf("CatchClause");
  834. if (!m_parameter.is_null())
  835. printf(" (%s)", m_parameter.characters());
  836. printf("\n");
  837. body().dump(indent + 1);
  838. }
  839. void ThrowStatement::dump(int indent) const
  840. {
  841. ASTNode::dump(indent);
  842. argument().dump(indent + 1);
  843. }
  844. Value TryStatement::execute(Interpreter& interpreter) const
  845. {
  846. interpreter.run(block(), {}, ScopeType::Try);
  847. if (auto* exception = interpreter.exception()) {
  848. if (m_handler) {
  849. interpreter.clear_exception();
  850. Vector<Argument> arguments { { m_handler->parameter(), exception->value() } };
  851. interpreter.run(m_handler->body(), move(arguments));
  852. }
  853. }
  854. if (m_finalizer)
  855. m_finalizer->execute(interpreter);
  856. return {};
  857. }
  858. Value CatchClause::execute(Interpreter&) const
  859. {
  860. // NOTE: CatchClause execution is handled by TryStatement.
  861. ASSERT_NOT_REACHED();
  862. return {};
  863. }
  864. Value ThrowStatement::execute(Interpreter& interpreter) const
  865. {
  866. auto value = m_argument->execute(interpreter);
  867. if (interpreter.exception())
  868. return {};
  869. return interpreter.throw_exception(value);
  870. }
  871. Value SwitchStatement::execute(Interpreter& interpreter) const
  872. {
  873. auto discriminant_result = m_discriminant->execute(interpreter);
  874. if (interpreter.exception())
  875. return {};
  876. bool falling_through = false;
  877. for (auto& switch_case : m_cases) {
  878. if (!falling_through && switch_case.test()) {
  879. auto test_result = switch_case.test()->execute(interpreter);
  880. if (interpreter.exception())
  881. return {};
  882. if (!eq(discriminant_result, test_result).to_boolean())
  883. continue;
  884. }
  885. falling_through = true;
  886. for (auto& statement : switch_case.consequent()) {
  887. statement.execute(interpreter);
  888. if (interpreter.exception())
  889. return {};
  890. if (interpreter.should_unwind()) {
  891. if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  892. interpreter.stop_unwind();
  893. return {};
  894. }
  895. return {};
  896. }
  897. }
  898. }
  899. return {};
  900. }
  901. Value SwitchCase::execute(Interpreter& interpreter) const
  902. {
  903. (void)interpreter;
  904. return {};
  905. }
  906. Value BreakStatement::execute(Interpreter& interpreter) const
  907. {
  908. interpreter.unwind(ScopeType::Breakable);
  909. return {};
  910. }
  911. Value ContinueStatement::execute(Interpreter& interpreter) const
  912. {
  913. interpreter.unwind(ScopeType::Continuable);
  914. return {};
  915. }
  916. void SwitchStatement::dump(int indent) const
  917. {
  918. ASTNode::dump(indent);
  919. m_discriminant->dump(indent + 1);
  920. for (auto& switch_case : m_cases) {
  921. switch_case.dump(indent + 1);
  922. }
  923. }
  924. void SwitchCase::dump(int indent) const
  925. {
  926. ASTNode::dump(indent);
  927. print_indent(indent);
  928. if (m_test) {
  929. printf("(Test)\n");
  930. m_test->dump(indent + 1);
  931. } else {
  932. printf("(Default)\n");
  933. }
  934. print_indent(indent);
  935. printf("(Consequent)\n");
  936. int i = 0;
  937. for (auto& statement : m_consequent) {
  938. print_indent(indent);
  939. printf("[%d]\n", i++);
  940. statement.dump(indent + 1);
  941. }
  942. }
  943. Value ConditionalExpression::execute(Interpreter& interpreter) const
  944. {
  945. auto test_result = m_test->execute(interpreter);
  946. if (interpreter.exception())
  947. return {};
  948. Value result;
  949. if (test_result.to_boolean()) {
  950. result = m_consequent->execute(interpreter);
  951. } else {
  952. result = m_alternate->execute(interpreter);
  953. }
  954. if (interpreter.exception())
  955. return {};
  956. return result;
  957. }
  958. void ConditionalExpression::dump(int indent) const
  959. {
  960. ASTNode::dump(indent);
  961. print_indent(indent);
  962. printf("(Test)\n");
  963. m_test->dump(indent + 1);
  964. print_indent(indent);
  965. printf("(Consequent)\n");
  966. m_test->dump(indent + 1);
  967. print_indent(indent);
  968. printf("(Alternate)\n");
  969. m_test->dump(indent + 1);
  970. }
  971. }