AST.cpp 32 KB

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