AST.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. AK::Function<void(Value)> commit;
  580. if (m_lhs->is_identifier()) {
  581. commit = [&](Value value) {
  582. auto name = static_cast<const Identifier&>(*m_lhs).string();
  583. interpreter.set_variable(name, value);
  584. };
  585. } else if (m_lhs->is_member_expression()) {
  586. commit = [&](Value value) {
  587. if (auto* object = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter).to_object(interpreter.heap())) {
  588. auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
  589. object->put(property_name, value);
  590. }
  591. };
  592. } else {
  593. ASSERT_NOT_REACHED();
  594. }
  595. auto rhs_result = m_rhs->execute(interpreter);
  596. if (interpreter.exception())
  597. return {};
  598. switch (m_op) {
  599. case AssignmentOp::Assignment:
  600. break;
  601. case AssignmentOp::AdditionAssignment:
  602. rhs_result = add(m_lhs->execute(interpreter), rhs_result);
  603. break;
  604. case AssignmentOp::SubtractionAssignment:
  605. rhs_result = sub(m_lhs->execute(interpreter), rhs_result);
  606. break;
  607. case AssignmentOp::MultiplicationAssignment:
  608. rhs_result = mul(m_lhs->execute(interpreter), rhs_result);
  609. break;
  610. case AssignmentOp::DivisionAssignment:
  611. rhs_result = div(m_lhs->execute(interpreter), rhs_result);
  612. break;
  613. }
  614. if (interpreter.exception())
  615. return {};
  616. commit(rhs_result);
  617. return rhs_result;
  618. }
  619. Value UpdateExpression::execute(Interpreter& interpreter) const
  620. {
  621. ASSERT(m_argument->is_identifier());
  622. auto name = static_cast<const Identifier&>(*m_argument).string();
  623. auto previous_variable = interpreter.get_variable(name);
  624. ASSERT(previous_variable.has_value());
  625. auto previous_value = previous_variable.value();
  626. ASSERT(previous_value.is_number());
  627. int op_result = 0;
  628. switch (m_op) {
  629. case UpdateOp::Increment:
  630. op_result = 1;
  631. break;
  632. case UpdateOp::Decrement:
  633. op_result = -1;
  634. break;
  635. }
  636. interpreter.set_variable(name, Value(previous_value.as_double() + op_result));
  637. if (m_prefixed)
  638. return JS::Value(previous_value.as_double() + op_result);
  639. return previous_value;
  640. }
  641. void AssignmentExpression::dump(int indent) const
  642. {
  643. const char* op_string = nullptr;
  644. switch (m_op) {
  645. case AssignmentOp::Assignment:
  646. op_string = "=";
  647. break;
  648. case AssignmentOp::AdditionAssignment:
  649. op_string = "+=";
  650. break;
  651. case AssignmentOp::SubtractionAssignment:
  652. op_string = "-=";
  653. break;
  654. case AssignmentOp::MultiplicationAssignment:
  655. op_string = "*=";
  656. break;
  657. case AssignmentOp::DivisionAssignment:
  658. op_string = "/=";
  659. break;
  660. }
  661. ASTNode::dump(indent);
  662. print_indent(indent + 1);
  663. printf("%s\n", op_string);
  664. m_lhs->dump(indent + 1);
  665. m_rhs->dump(indent + 1);
  666. }
  667. void UpdateExpression::dump(int indent) const
  668. {
  669. const char* op_string = nullptr;
  670. switch (m_op) {
  671. case UpdateOp::Increment:
  672. op_string = "++";
  673. break;
  674. case UpdateOp::Decrement:
  675. op_string = "--";
  676. break;
  677. }
  678. ASTNode::dump(indent);
  679. print_indent(indent + 1);
  680. if (m_prefixed)
  681. printf("%s\n", op_string);
  682. m_argument->dump(indent + 1);
  683. if (!m_prefixed) {
  684. print_indent(indent + 1);
  685. printf("%s\n", op_string);
  686. }
  687. }
  688. Value VariableDeclaration::execute(Interpreter& interpreter) const
  689. {
  690. for (auto& declarator : m_declarations) {
  691. interpreter.declare_variable(declarator.id().string(), m_declaration_type);
  692. if (auto* init = declarator.init()) {
  693. auto initalizer_result = init->execute(interpreter);
  694. if (interpreter.exception())
  695. return {};
  696. interpreter.set_variable(declarator.id().string(), initalizer_result, true);
  697. }
  698. }
  699. return {};
  700. }
  701. Value VariableDeclarator::execute(Interpreter&) const
  702. {
  703. // NOTE: This node is handled by VariableDeclaration.
  704. ASSERT_NOT_REACHED();
  705. }
  706. void VariableDeclaration::dump(int indent) const
  707. {
  708. const char* declaration_type_string = nullptr;
  709. switch (m_declaration_type) {
  710. case DeclarationType::Let:
  711. declaration_type_string = "Let";
  712. break;
  713. case DeclarationType::Var:
  714. declaration_type_string = "Var";
  715. break;
  716. case DeclarationType::Const:
  717. declaration_type_string = "Const";
  718. break;
  719. }
  720. ASTNode::dump(indent);
  721. print_indent(indent + 1);
  722. printf("%s\n", declaration_type_string);
  723. for (auto& declarator : m_declarations)
  724. declarator.dump(indent + 1);
  725. }
  726. void VariableDeclarator::dump(int indent) const
  727. {
  728. ASTNode::dump(indent);
  729. m_id->dump(indent + 1);
  730. if (m_init)
  731. m_init->dump(indent + 1);
  732. }
  733. void ObjectExpression::dump(int indent) const
  734. {
  735. ASTNode::dump(indent);
  736. for (auto it : m_properties) {
  737. print_indent(indent + 1);
  738. printf("%s: ", it.key.characters());
  739. it.value->dump(0);
  740. }
  741. }
  742. void ExpressionStatement::dump(int indent) const
  743. {
  744. ASTNode::dump(indent);
  745. m_expression->dump(indent + 1);
  746. }
  747. Value ObjectExpression::execute(Interpreter& interpreter) const
  748. {
  749. auto object = interpreter.heap().allocate<Object>();
  750. for (auto it : m_properties) {
  751. auto value = it.value->execute(interpreter);
  752. if (interpreter.exception())
  753. return {};
  754. object->put(it.key, value);
  755. }
  756. return object;
  757. }
  758. void MemberExpression::dump(int indent) const
  759. {
  760. print_indent(indent);
  761. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  762. m_object->dump(indent + 1);
  763. m_property->dump(indent + 1);
  764. }
  765. FlyString MemberExpression::computed_property_name(Interpreter& interpreter) const
  766. {
  767. if (!is_computed()) {
  768. ASSERT(m_property->is_identifier());
  769. return static_cast<const Identifier&>(*m_property).string();
  770. }
  771. return m_property->execute(interpreter).to_string();
  772. }
  773. Value MemberExpression::execute(Interpreter& interpreter) const
  774. {
  775. auto* object_result = m_object->execute(interpreter).to_object(interpreter.heap());
  776. if (interpreter.exception())
  777. return {};
  778. auto result = object_result->get(computed_property_name(interpreter));
  779. return result.value_or({});
  780. }
  781. Value StringLiteral::execute(Interpreter& interpreter) const
  782. {
  783. return js_string(interpreter, m_value);
  784. }
  785. Value NumericLiteral::execute(Interpreter&) const
  786. {
  787. return Value(m_value);
  788. }
  789. Value BooleanLiteral::execute(Interpreter&) const
  790. {
  791. return Value(m_value);
  792. }
  793. Value NullLiteral::execute(Interpreter&) const
  794. {
  795. return js_null();
  796. }
  797. void ArrayExpression::dump(int indent) const
  798. {
  799. ASTNode::dump(indent);
  800. for (auto& element : m_elements) {
  801. element.dump(indent + 1);
  802. }
  803. }
  804. Value ArrayExpression::execute(Interpreter& interpreter) const
  805. {
  806. auto* array = interpreter.heap().allocate<Array>();
  807. for (auto& element : m_elements) {
  808. auto value = element.execute(interpreter);
  809. if (interpreter.exception())
  810. return {};
  811. array->push(value);
  812. }
  813. return array;
  814. }
  815. void TryStatement::dump(int indent) const
  816. {
  817. ASTNode::dump(indent);
  818. print_indent(indent);
  819. printf("(Block)\n");
  820. block().dump(indent + 1);
  821. if (handler()) {
  822. print_indent(indent);
  823. printf("(Handler)\n");
  824. handler()->dump(indent + 1);
  825. }
  826. if (finalizer()) {
  827. print_indent(indent);
  828. printf("(Finalizer)\n");
  829. finalizer()->dump(indent + 1);
  830. }
  831. }
  832. void CatchClause::dump(int indent) const
  833. {
  834. print_indent(indent);
  835. printf("CatchClause");
  836. if (!m_parameter.is_null())
  837. printf(" (%s)", m_parameter.characters());
  838. printf("\n");
  839. body().dump(indent + 1);
  840. }
  841. void ThrowStatement::dump(int indent) const
  842. {
  843. ASTNode::dump(indent);
  844. argument().dump(indent + 1);
  845. }
  846. Value TryStatement::execute(Interpreter& interpreter) const
  847. {
  848. interpreter.run(block(), {}, ScopeType::Try);
  849. if (auto* exception = interpreter.exception()) {
  850. if (m_handler) {
  851. interpreter.clear_exception();
  852. Vector<Argument> arguments { { m_handler->parameter(), exception->value() } };
  853. interpreter.run(m_handler->body(), move(arguments));
  854. }
  855. }
  856. if (m_finalizer)
  857. m_finalizer->execute(interpreter);
  858. return {};
  859. }
  860. Value CatchClause::execute(Interpreter&) const
  861. {
  862. // NOTE: CatchClause execution is handled by TryStatement.
  863. ASSERT_NOT_REACHED();
  864. return {};
  865. }
  866. Value ThrowStatement::execute(Interpreter& interpreter) const
  867. {
  868. auto value = m_argument->execute(interpreter);
  869. if (interpreter.exception())
  870. return {};
  871. return interpreter.throw_exception(value);
  872. }
  873. Value SwitchStatement::execute(Interpreter& interpreter) const
  874. {
  875. auto discriminant_result = m_discriminant->execute(interpreter);
  876. if (interpreter.exception())
  877. return {};
  878. bool falling_through = false;
  879. for (auto& switch_case : m_cases) {
  880. if (!falling_through && switch_case.test()) {
  881. auto test_result = switch_case.test()->execute(interpreter);
  882. if (interpreter.exception())
  883. return {};
  884. if (!eq(discriminant_result, test_result).to_boolean())
  885. continue;
  886. }
  887. falling_through = true;
  888. for (auto& statement : switch_case.consequent()) {
  889. statement.execute(interpreter);
  890. if (interpreter.exception())
  891. return {};
  892. if (interpreter.should_unwind()) {
  893. if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  894. interpreter.stop_unwind();
  895. return {};
  896. }
  897. return {};
  898. }
  899. }
  900. }
  901. return {};
  902. }
  903. Value SwitchCase::execute(Interpreter& interpreter) const
  904. {
  905. (void)interpreter;
  906. return {};
  907. }
  908. Value BreakStatement::execute(Interpreter& interpreter) const
  909. {
  910. interpreter.unwind(ScopeType::Breakable);
  911. return {};
  912. }
  913. Value ContinueStatement::execute(Interpreter& interpreter) const
  914. {
  915. interpreter.unwind(ScopeType::Continuable);
  916. return {};
  917. }
  918. void SwitchStatement::dump(int indent) const
  919. {
  920. ASTNode::dump(indent);
  921. m_discriminant->dump(indent + 1);
  922. for (auto& switch_case : m_cases) {
  923. switch_case.dump(indent + 1);
  924. }
  925. }
  926. void SwitchCase::dump(int indent) const
  927. {
  928. ASTNode::dump(indent);
  929. print_indent(indent);
  930. if (m_test) {
  931. printf("(Test)\n");
  932. m_test->dump(indent + 1);
  933. } else {
  934. printf("(Default)\n");
  935. }
  936. print_indent(indent);
  937. printf("(Consequent)\n");
  938. int i = 0;
  939. for (auto& statement : m_consequent) {
  940. print_indent(indent);
  941. printf("[%d]\n", i++);
  942. statement.dump(indent + 1);
  943. }
  944. }
  945. Value ConditionalExpression::execute(Interpreter& interpreter) const
  946. {
  947. auto test_result = m_test->execute(interpreter);
  948. if (interpreter.exception())
  949. return {};
  950. Value result;
  951. if (test_result.to_boolean()) {
  952. result = m_consequent->execute(interpreter);
  953. } else {
  954. result = m_alternate->execute(interpreter);
  955. }
  956. if (interpreter.exception())
  957. return {};
  958. return result;
  959. }
  960. void ConditionalExpression::dump(int indent) const
  961. {
  962. ASTNode::dump(indent);
  963. print_indent(indent);
  964. printf("(Test)\n");
  965. m_test->dump(indent + 1);
  966. print_indent(indent);
  967. printf("(Consequent)\n");
  968. m_test->dump(indent + 1);
  969. print_indent(indent);
  970. printf("(Alternate)\n");
  971. m_test->dump(indent + 1);
  972. }
  973. }