AST.cpp 27 KB

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