AST.cpp 31 KB

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