AST.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Function.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/ScopeGuard.h>
  30. #include <AK/StringBuilder.h>
  31. #include <LibJS/AST.h>
  32. #include <LibJS/Interpreter.h>
  33. #include <LibJS/Runtime/Array.h>
  34. #include <LibJS/Runtime/Error.h>
  35. #include <LibJS/Runtime/GlobalObject.h>
  36. #include <LibJS/Runtime/MarkedValueList.h>
  37. #include <LibJS/Runtime/NativeFunction.h>
  38. #include <LibJS/Runtime/PrimitiveString.h>
  39. #include <LibJS/Runtime/ScriptFunction.h>
  40. #include <LibJS/Runtime/Value.h>
  41. #include <stdio.h>
  42. namespace JS {
  43. Value ScopeNode::execute(Interpreter& interpreter) const
  44. {
  45. return interpreter.run(*this);
  46. }
  47. Value FunctionDeclaration::execute(Interpreter& interpreter) const
  48. {
  49. auto* function = ScriptFunction::create(interpreter.global_object(), name(), body(), parameters(), interpreter.current_environment());
  50. interpreter.set_variable(name(), function);
  51. return js_undefined();
  52. }
  53. Value FunctionExpression::execute(Interpreter& interpreter) const
  54. {
  55. return ScriptFunction::create(interpreter.global_object(), name(), body(), parameters(), interpreter.current_environment());
  56. }
  57. Value ExpressionStatement::execute(Interpreter& interpreter) const
  58. {
  59. return m_expression->execute(interpreter);
  60. }
  61. CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter) const
  62. {
  63. if (is_new_expression()) {
  64. // Computing |this| is irrelevant for "new" expression.
  65. return { js_undefined(), m_callee->execute(interpreter) };
  66. }
  67. if (m_callee->is_member_expression()) {
  68. auto& member_expression = static_cast<const MemberExpression&>(*m_callee);
  69. auto object_value = member_expression.object().execute(interpreter);
  70. if (interpreter.exception())
  71. return {};
  72. auto* this_value = object_value.to_object(interpreter.heap());
  73. if (interpreter.exception())
  74. return {};
  75. auto callee = this_value->get(member_expression.computed_property_name(interpreter)).value_or(js_undefined());
  76. return { this_value, callee };
  77. }
  78. return { &interpreter.global_object(), m_callee->execute(interpreter) };
  79. }
  80. Value CallExpression::execute(Interpreter& interpreter) const
  81. {
  82. auto [this_value, callee] = compute_this_and_callee(interpreter);
  83. if (interpreter.exception())
  84. return {};
  85. ASSERT(!callee.is_empty());
  86. if (!callee.is_object()
  87. || !callee.as_object().is_function()
  88. || (is_new_expression() && (callee.as_object().is_native_function() && !static_cast<NativeFunction&>(callee.as_object()).has_constructor()))) {
  89. String error_message;
  90. auto call_type = is_new_expression() ? "constructor" : "function";
  91. if (m_callee->is_identifier() || m_callee->is_member_expression()) {
  92. String expression_string;
  93. if (m_callee->is_identifier())
  94. expression_string = static_cast<const Identifier&>(*m_callee).string();
  95. else
  96. expression_string = static_cast<const MemberExpression&>(*m_callee).to_string_approximation();
  97. error_message = String::format("%s is not a %s (evaluated from '%s')", callee.to_string().characters(), call_type, expression_string.characters());
  98. } else {
  99. error_message = String::format("%s is not a %s", callee.to_string().characters(), call_type);
  100. }
  101. return interpreter.throw_exception<TypeError>(error_message);
  102. }
  103. auto& function = static_cast<Function&>(callee.as_object());
  104. MarkedValueList arguments(interpreter.heap());
  105. arguments.values().append(function.bound_arguments());
  106. for (size_t i = 0; i < m_arguments.size(); ++i) {
  107. auto value = m_arguments[i].execute(interpreter);
  108. if (interpreter.exception())
  109. return {};
  110. arguments.append(value);
  111. if (interpreter.exception())
  112. return {};
  113. }
  114. auto& call_frame = interpreter.push_call_frame();
  115. call_frame.function_name = function.name();
  116. call_frame.arguments = arguments.values();
  117. call_frame.environment = function.create_environment();
  118. Object* new_object = nullptr;
  119. Value result;
  120. if (is_new_expression()) {
  121. new_object = Object::create_empty(interpreter, interpreter.global_object());
  122. auto prototype = function.get("prototype");
  123. if (prototype.is_object())
  124. new_object->set_prototype(&prototype.as_object());
  125. call_frame.this_value = new_object;
  126. result = function.construct(interpreter);
  127. } else {
  128. call_frame.this_value = function.bound_this().value_or(this_value);
  129. result = function.call(interpreter);
  130. }
  131. interpreter.pop_call_frame();
  132. if (interpreter.exception())
  133. return {};
  134. if (is_new_expression()) {
  135. if (result.is_object())
  136. return result;
  137. return new_object;
  138. }
  139. return result;
  140. }
  141. Value ReturnStatement::execute(Interpreter& interpreter) const
  142. {
  143. auto value = argument() ? argument()->execute(interpreter) : js_undefined();
  144. if (interpreter.exception())
  145. return {};
  146. interpreter.unwind(ScopeType::Function);
  147. return value;
  148. }
  149. Value IfStatement::execute(Interpreter& interpreter) const
  150. {
  151. auto predicate_result = m_predicate->execute(interpreter);
  152. if (interpreter.exception())
  153. return {};
  154. if (predicate_result.to_boolean())
  155. return interpreter.run(*m_consequent);
  156. if (m_alternate)
  157. return interpreter.run(*m_alternate);
  158. return js_undefined();
  159. }
  160. Value WhileStatement::execute(Interpreter& interpreter) const
  161. {
  162. Value last_value = js_undefined();
  163. while (m_test->execute(interpreter).to_boolean()) {
  164. if (interpreter.exception())
  165. return {};
  166. last_value = interpreter.run(*m_body);
  167. if (interpreter.exception())
  168. return {};
  169. }
  170. return last_value;
  171. }
  172. Value DoWhileStatement::execute(Interpreter& interpreter) const
  173. {
  174. Value last_value = js_undefined();
  175. do {
  176. if (interpreter.exception())
  177. return {};
  178. last_value = interpreter.run(*m_body);
  179. if (interpreter.exception())
  180. return {};
  181. } while (m_test->execute(interpreter).to_boolean());
  182. return last_value;
  183. }
  184. Value ForStatement::execute(Interpreter& interpreter) const
  185. {
  186. RefPtr<BlockStatement> wrapper;
  187. if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_kind() != DeclarationKind::Var) {
  188. wrapper = create_ast_node<BlockStatement>();
  189. interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
  190. }
  191. auto wrapper_cleanup = ScopeGuard([&] {
  192. if (wrapper)
  193. interpreter.exit_scope(*wrapper);
  194. });
  195. Value last_value = js_undefined();
  196. if (m_init) {
  197. m_init->execute(interpreter);
  198. if (interpreter.exception())
  199. return {};
  200. }
  201. if (m_test) {
  202. while (m_test->execute(interpreter).to_boolean()) {
  203. if (interpreter.exception())
  204. return {};
  205. last_value = interpreter.run(*m_body);
  206. if (interpreter.exception())
  207. return {};
  208. if (interpreter.should_unwind()) {
  209. if (interpreter.should_unwind_until(ScopeType::Continuable)) {
  210. interpreter.stop_unwind();
  211. } else if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  212. interpreter.stop_unwind();
  213. break;
  214. } else {
  215. return js_undefined();
  216. }
  217. }
  218. if (m_update) {
  219. m_update->execute(interpreter);
  220. if (interpreter.exception())
  221. return {};
  222. }
  223. }
  224. } else {
  225. while (true) {
  226. last_value = interpreter.run(*m_body);
  227. if (interpreter.exception())
  228. return {};
  229. if (interpreter.should_unwind()) {
  230. if (interpreter.should_unwind_until(ScopeType::Continuable)) {
  231. interpreter.stop_unwind();
  232. } else if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  233. interpreter.stop_unwind();
  234. break;
  235. } else {
  236. return js_undefined();
  237. }
  238. }
  239. if (m_update) {
  240. m_update->execute(interpreter);
  241. if (interpreter.exception())
  242. return {};
  243. }
  244. }
  245. }
  246. return last_value;
  247. }
  248. Value BinaryExpression::execute(Interpreter& interpreter) const
  249. {
  250. auto lhs_result = m_lhs->execute(interpreter);
  251. if (interpreter.exception())
  252. return {};
  253. auto rhs_result = m_rhs->execute(interpreter);
  254. if (interpreter.exception())
  255. return {};
  256. switch (m_op) {
  257. case BinaryOp::Addition:
  258. return add(interpreter, lhs_result, rhs_result);
  259. case BinaryOp::Subtraction:
  260. return sub(interpreter, lhs_result, rhs_result);
  261. case BinaryOp::Multiplication:
  262. return mul(interpreter, lhs_result, rhs_result);
  263. case BinaryOp::Division:
  264. return div(interpreter, lhs_result, rhs_result);
  265. case BinaryOp::Modulo:
  266. return mod(interpreter, lhs_result, rhs_result);
  267. case BinaryOp::Exponentiation:
  268. return exp(interpreter, lhs_result, rhs_result);
  269. case BinaryOp::TypedEquals:
  270. return typed_eq(interpreter, lhs_result, rhs_result);
  271. case BinaryOp::TypedInequals:
  272. return Value(!typed_eq(interpreter, lhs_result, rhs_result).as_bool());
  273. case BinaryOp::AbstractEquals:
  274. return eq(interpreter, lhs_result, rhs_result);
  275. case BinaryOp::AbstractInequals:
  276. return Value(!eq(interpreter, lhs_result, rhs_result).as_bool());
  277. case BinaryOp::GreaterThan:
  278. return greater_than(interpreter, lhs_result, rhs_result);
  279. case BinaryOp::GreaterThanEquals:
  280. return greater_than_equals(interpreter, lhs_result, rhs_result);
  281. case BinaryOp::LessThan:
  282. return less_than(interpreter, lhs_result, rhs_result);
  283. case BinaryOp::LessThanEquals:
  284. return less_than_equals(interpreter, lhs_result, rhs_result);
  285. case BinaryOp::BitwiseAnd:
  286. return bitwise_and(interpreter, lhs_result, rhs_result);
  287. case BinaryOp::BitwiseOr:
  288. return bitwise_or(interpreter, lhs_result, rhs_result);
  289. case BinaryOp::BitwiseXor:
  290. return bitwise_xor(interpreter, lhs_result, rhs_result);
  291. case BinaryOp::LeftShift:
  292. return left_shift(interpreter, lhs_result, rhs_result);
  293. case BinaryOp::RightShift:
  294. return right_shift(interpreter, lhs_result, rhs_result);
  295. case BinaryOp::UnsignedRightShift:
  296. return unsigned_right_shift(interpreter, lhs_result, rhs_result);
  297. case BinaryOp::In:
  298. return in(interpreter, lhs_result, rhs_result);
  299. case BinaryOp::InstanceOf:
  300. return instance_of(interpreter, lhs_result, rhs_result);
  301. }
  302. ASSERT_NOT_REACHED();
  303. }
  304. Value LogicalExpression::execute(Interpreter& interpreter) const
  305. {
  306. auto lhs_result = m_lhs->execute(interpreter);
  307. if (interpreter.exception())
  308. return {};
  309. switch (m_op) {
  310. case LogicalOp::And:
  311. if (lhs_result.to_boolean()) {
  312. auto rhs_result = m_rhs->execute(interpreter);
  313. if (interpreter.exception())
  314. return {};
  315. return rhs_result;
  316. }
  317. return lhs_result;
  318. case LogicalOp::Or: {
  319. if (lhs_result.to_boolean())
  320. return lhs_result;
  321. auto rhs_result = m_rhs->execute(interpreter);
  322. if (interpreter.exception())
  323. return {};
  324. return rhs_result;
  325. }
  326. case LogicalOp::NullishCoalescing:
  327. if (lhs_result.is_null() || lhs_result.is_undefined()) {
  328. auto rhs_result = m_rhs->execute(interpreter);
  329. if (interpreter.exception())
  330. return {};
  331. return rhs_result;
  332. }
  333. return lhs_result;
  334. }
  335. ASSERT_NOT_REACHED();
  336. }
  337. Value UnaryExpression::execute(Interpreter& interpreter) const
  338. {
  339. if (m_op == UnaryOp::Delete) {
  340. if (!m_lhs->is_member_expression())
  341. return Value(true);
  342. auto object_value = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter);
  343. if (interpreter.exception())
  344. return {};
  345. auto* object = object_value.to_object(interpreter.heap());
  346. if (!object)
  347. return {};
  348. auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
  349. if (!property_name.is_valid())
  350. return {};
  351. return object->delete_property(property_name);
  352. }
  353. auto lhs_result = m_lhs->execute(interpreter);
  354. if (interpreter.exception())
  355. return {};
  356. switch (m_op) {
  357. case UnaryOp::BitwiseNot:
  358. return bitwise_not(interpreter, lhs_result);
  359. case UnaryOp::Not:
  360. return Value(!lhs_result.to_boolean());
  361. case UnaryOp::Plus:
  362. return unary_plus(interpreter, lhs_result);
  363. case UnaryOp::Minus:
  364. return unary_minus(interpreter, lhs_result);
  365. case UnaryOp::Typeof:
  366. switch (lhs_result.type()) {
  367. case Value::Type::Empty:
  368. ASSERT_NOT_REACHED();
  369. return {};
  370. case Value::Type::Undefined:
  371. return js_string(interpreter, "undefined");
  372. case Value::Type::Null:
  373. // yes, this is on purpose. yes, this is how javascript works.
  374. // yes, it's silly.
  375. return js_string(interpreter, "object");
  376. case Value::Type::Number:
  377. return js_string(interpreter, "number");
  378. case Value::Type::String:
  379. return js_string(interpreter, "string");
  380. case Value::Type::Object:
  381. if (lhs_result.as_object().is_function())
  382. return js_string(interpreter, "function");
  383. return js_string(interpreter, "object");
  384. case Value::Type::Boolean:
  385. return js_string(interpreter, "boolean");
  386. default:
  387. ASSERT_NOT_REACHED();
  388. }
  389. case UnaryOp::Void:
  390. return js_undefined();
  391. case UnaryOp::Delete:
  392. ASSERT_NOT_REACHED();
  393. }
  394. ASSERT_NOT_REACHED();
  395. }
  396. static void print_indent(int indent)
  397. {
  398. for (int i = 0; i < indent * 2; ++i)
  399. putchar(' ');
  400. }
  401. void ASTNode::dump(int indent) const
  402. {
  403. print_indent(indent);
  404. printf("%s\n", class_name());
  405. }
  406. void ScopeNode::dump(int indent) const
  407. {
  408. ASTNode::dump(indent);
  409. if (!m_variables.is_empty()) {
  410. print_indent(indent + 1);
  411. printf("(Variables)\n");
  412. for (auto& variable : m_variables)
  413. variable.dump(indent + 2);
  414. }
  415. if (!m_children.is_empty()) {
  416. print_indent(indent + 1);
  417. printf("(Children)\n");
  418. for (auto& child : children())
  419. child.dump(indent + 2);
  420. }
  421. }
  422. void BinaryExpression::dump(int indent) const
  423. {
  424. const char* op_string = nullptr;
  425. switch (m_op) {
  426. case BinaryOp::Addition:
  427. op_string = "+";
  428. break;
  429. case BinaryOp::Subtraction:
  430. op_string = "-";
  431. break;
  432. case BinaryOp::Multiplication:
  433. op_string = "*";
  434. break;
  435. case BinaryOp::Division:
  436. op_string = "/";
  437. break;
  438. case BinaryOp::Modulo:
  439. op_string = "%";
  440. break;
  441. case BinaryOp::Exponentiation:
  442. op_string = "**";
  443. break;
  444. case BinaryOp::TypedEquals:
  445. op_string = "===";
  446. break;
  447. case BinaryOp::TypedInequals:
  448. op_string = "!==";
  449. break;
  450. case BinaryOp::AbstractEquals:
  451. op_string = "==";
  452. break;
  453. case BinaryOp::AbstractInequals:
  454. op_string = "!=";
  455. break;
  456. case BinaryOp::GreaterThan:
  457. op_string = ">";
  458. break;
  459. case BinaryOp::GreaterThanEquals:
  460. op_string = ">=";
  461. break;
  462. case BinaryOp::LessThan:
  463. op_string = "<";
  464. break;
  465. case BinaryOp::LessThanEquals:
  466. op_string = "<=";
  467. break;
  468. case BinaryOp::BitwiseAnd:
  469. op_string = "&";
  470. break;
  471. case BinaryOp::BitwiseOr:
  472. op_string = "|";
  473. break;
  474. case BinaryOp::BitwiseXor:
  475. op_string = "^";
  476. break;
  477. case BinaryOp::LeftShift:
  478. op_string = "<<";
  479. break;
  480. case BinaryOp::RightShift:
  481. op_string = ">>";
  482. break;
  483. case BinaryOp::UnsignedRightShift:
  484. op_string = ">>>";
  485. break;
  486. case BinaryOp::In:
  487. op_string = "in";
  488. break;
  489. case BinaryOp::InstanceOf:
  490. op_string = "instanceof";
  491. break;
  492. }
  493. print_indent(indent);
  494. printf("%s\n", class_name());
  495. m_lhs->dump(indent + 1);
  496. print_indent(indent + 1);
  497. printf("%s\n", op_string);
  498. m_rhs->dump(indent + 1);
  499. }
  500. void LogicalExpression::dump(int indent) const
  501. {
  502. const char* op_string = nullptr;
  503. switch (m_op) {
  504. case LogicalOp::And:
  505. op_string = "&&";
  506. break;
  507. case LogicalOp::Or:
  508. op_string = "||";
  509. break;
  510. case LogicalOp::NullishCoalescing:
  511. op_string = "??";
  512. break;
  513. }
  514. print_indent(indent);
  515. printf("%s\n", class_name());
  516. m_lhs->dump(indent + 1);
  517. print_indent(indent + 1);
  518. printf("%s\n", op_string);
  519. m_rhs->dump(indent + 1);
  520. }
  521. void UnaryExpression::dump(int indent) const
  522. {
  523. const char* op_string = nullptr;
  524. switch (m_op) {
  525. case UnaryOp::BitwiseNot:
  526. op_string = "~";
  527. break;
  528. case UnaryOp::Not:
  529. op_string = "!";
  530. break;
  531. case UnaryOp::Plus:
  532. op_string = "+";
  533. break;
  534. case UnaryOp::Minus:
  535. op_string = "-";
  536. break;
  537. case UnaryOp::Typeof:
  538. op_string = "typeof ";
  539. break;
  540. case UnaryOp::Void:
  541. op_string = "void ";
  542. break;
  543. case UnaryOp::Delete:
  544. op_string = "delete ";
  545. break;
  546. }
  547. print_indent(indent);
  548. printf("%s\n", class_name());
  549. print_indent(indent + 1);
  550. printf("%s\n", op_string);
  551. m_lhs->dump(indent + 1);
  552. }
  553. void CallExpression::dump(int indent) const
  554. {
  555. print_indent(indent);
  556. printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
  557. m_callee->dump(indent + 1);
  558. for (auto& argument : m_arguments)
  559. argument.dump(indent + 1);
  560. }
  561. void StringLiteral::dump(int indent) const
  562. {
  563. print_indent(indent);
  564. printf("StringLiteral \"%s\"\n", m_value.characters());
  565. }
  566. void NumericLiteral::dump(int indent) const
  567. {
  568. print_indent(indent);
  569. printf("NumericLiteral %g\n", m_value);
  570. }
  571. void BooleanLiteral::dump(int indent) const
  572. {
  573. print_indent(indent);
  574. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  575. }
  576. void NullLiteral::dump(int indent) const
  577. {
  578. print_indent(indent);
  579. printf("null\n");
  580. }
  581. void FunctionNode::dump(int indent, const char* class_name) const
  582. {
  583. StringBuilder parameters_builder;
  584. parameters_builder.join(',', parameters());
  585. print_indent(indent);
  586. printf("%s '%s(%s)'\n", class_name, name().characters(), parameters_builder.build().characters());
  587. if (!m_variables.is_empty()) {
  588. print_indent(indent + 1);
  589. printf("(Variables)\n");
  590. }
  591. for (auto& variable : m_variables)
  592. variable.dump(indent + 2);
  593. print_indent(indent + 1);
  594. printf("(Body)\n");
  595. body().dump(indent + 2);
  596. }
  597. void FunctionDeclaration::dump(int indent) const
  598. {
  599. FunctionNode::dump(indent, class_name());
  600. }
  601. void FunctionExpression::dump(int indent) const
  602. {
  603. FunctionNode::dump(indent, class_name());
  604. }
  605. void ReturnStatement::dump(int indent) const
  606. {
  607. ASTNode::dump(indent);
  608. if (argument())
  609. argument()->dump(indent + 1);
  610. }
  611. void IfStatement::dump(int indent) const
  612. {
  613. ASTNode::dump(indent);
  614. print_indent(indent);
  615. printf("If\n");
  616. predicate().dump(indent + 1);
  617. consequent().dump(indent + 1);
  618. if (alternate()) {
  619. print_indent(indent);
  620. printf("Else\n");
  621. alternate()->dump(indent + 1);
  622. }
  623. }
  624. void WhileStatement::dump(int indent) const
  625. {
  626. ASTNode::dump(indent);
  627. print_indent(indent);
  628. printf("While\n");
  629. test().dump(indent + 1);
  630. body().dump(indent + 1);
  631. }
  632. void DoWhileStatement::dump(int indent) const
  633. {
  634. ASTNode::dump(indent);
  635. print_indent(indent);
  636. printf("DoWhile\n");
  637. test().dump(indent + 1);
  638. body().dump(indent + 1);
  639. }
  640. void ForStatement::dump(int indent) const
  641. {
  642. ASTNode::dump(indent);
  643. print_indent(indent);
  644. printf("For\n");
  645. if (init())
  646. init()->dump(indent + 1);
  647. if (test())
  648. test()->dump(indent + 1);
  649. if (update())
  650. update()->dump(indent + 1);
  651. body().dump(indent + 1);
  652. }
  653. Value Identifier::execute(Interpreter& interpreter) const
  654. {
  655. auto value = interpreter.get_variable(string());
  656. if (value.is_empty())
  657. return interpreter.throw_exception<ReferenceError>(String::format("'%s' not known", string().characters()));
  658. return value;
  659. }
  660. void Identifier::dump(int indent) const
  661. {
  662. print_indent(indent);
  663. printf("Identifier \"%s\"\n", m_string.characters());
  664. }
  665. void SpreadExpression::dump(int indent) const
  666. {
  667. ASTNode::dump(indent);
  668. m_target->dump(indent + 1);
  669. }
  670. Value SpreadExpression::execute(Interpreter& interpreter) const
  671. {
  672. return m_target->execute(interpreter);
  673. }
  674. Value ThisExpression::execute(Interpreter& interpreter) const
  675. {
  676. return interpreter.this_value();
  677. }
  678. void ThisExpression::dump(int indent) const
  679. {
  680. ASTNode::dump(indent);
  681. }
  682. Value AssignmentExpression::execute(Interpreter& interpreter) const
  683. {
  684. auto rhs_result = m_rhs->execute(interpreter);
  685. if (interpreter.exception())
  686. return {};
  687. Value lhs_result;
  688. switch (m_op) {
  689. case AssignmentOp::Assignment:
  690. break;
  691. case AssignmentOp::AdditionAssignment:
  692. lhs_result = m_lhs->execute(interpreter);
  693. if (interpreter.exception())
  694. return {};
  695. rhs_result = add(interpreter, lhs_result, rhs_result);
  696. break;
  697. case AssignmentOp::SubtractionAssignment:
  698. lhs_result = m_lhs->execute(interpreter);
  699. if (interpreter.exception())
  700. return {};
  701. rhs_result = sub(interpreter, lhs_result, rhs_result);
  702. break;
  703. case AssignmentOp::MultiplicationAssignment:
  704. lhs_result = m_lhs->execute(interpreter);
  705. if (interpreter.exception())
  706. return {};
  707. rhs_result = mul(interpreter, lhs_result, rhs_result);
  708. break;
  709. case AssignmentOp::DivisionAssignment:
  710. lhs_result = m_lhs->execute(interpreter);
  711. if (interpreter.exception())
  712. return {};
  713. rhs_result = div(interpreter, lhs_result, rhs_result);
  714. break;
  715. case AssignmentOp::LeftShiftAssignment:
  716. lhs_result = m_lhs->execute(interpreter);
  717. if (interpreter.exception())
  718. return {};
  719. rhs_result = left_shift(interpreter, lhs_result, rhs_result);
  720. break;
  721. case AssignmentOp::RightShiftAssignment:
  722. lhs_result = m_lhs->execute(interpreter);
  723. if (interpreter.exception())
  724. return {};
  725. rhs_result = right_shift(interpreter, lhs_result, rhs_result);
  726. break;
  727. case AssignmentOp::UnsignedRightShiftAssignment:
  728. lhs_result = m_lhs->execute(interpreter);
  729. if (interpreter.exception())
  730. return {};
  731. rhs_result = unsigned_right_shift(interpreter, lhs_result, rhs_result);
  732. break;
  733. }
  734. if (interpreter.exception())
  735. return {};
  736. if (m_lhs->is_identifier()) {
  737. auto name = static_cast<const Identifier&>(*m_lhs).string();
  738. interpreter.set_variable(name, rhs_result);
  739. } else if (m_lhs->is_member_expression()) {
  740. auto object_value = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter);
  741. if (interpreter.exception())
  742. return {};
  743. if (auto* object = object_value.to_object(interpreter.heap())) {
  744. auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
  745. object->put(property_name, rhs_result);
  746. }
  747. } else {
  748. return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
  749. }
  750. return rhs_result;
  751. }
  752. Value UpdateExpression::execute(Interpreter& interpreter) const
  753. {
  754. ASSERT(m_argument->is_identifier());
  755. auto name = static_cast<const Identifier&>(*m_argument).string();
  756. auto old_value = m_argument->execute(interpreter);
  757. if (interpreter.exception())
  758. return {};
  759. old_value = old_value.to_number();
  760. int op_result = 0;
  761. switch (m_op) {
  762. case UpdateOp::Increment:
  763. op_result = 1;
  764. break;
  765. case UpdateOp::Decrement:
  766. op_result = -1;
  767. break;
  768. default:
  769. ASSERT_NOT_REACHED();
  770. }
  771. auto new_value = Value(old_value.as_double() + op_result);
  772. interpreter.set_variable(name, new_value);
  773. return m_prefixed ? new_value : old_value;
  774. }
  775. void AssignmentExpression::dump(int indent) const
  776. {
  777. const char* op_string = nullptr;
  778. switch (m_op) {
  779. case AssignmentOp::Assignment:
  780. op_string = "=";
  781. break;
  782. case AssignmentOp::AdditionAssignment:
  783. op_string = "+=";
  784. break;
  785. case AssignmentOp::SubtractionAssignment:
  786. op_string = "-=";
  787. break;
  788. case AssignmentOp::MultiplicationAssignment:
  789. op_string = "*=";
  790. break;
  791. case AssignmentOp::DivisionAssignment:
  792. op_string = "/=";
  793. break;
  794. case AssignmentOp::LeftShiftAssignment:
  795. op_string = "<<=";
  796. break;
  797. case AssignmentOp::RightShiftAssignment:
  798. op_string = ">>=";
  799. break;
  800. case AssignmentOp::UnsignedRightShiftAssignment:
  801. op_string = ">>>=";
  802. break;
  803. }
  804. ASTNode::dump(indent);
  805. print_indent(indent + 1);
  806. printf("%s\n", op_string);
  807. m_lhs->dump(indent + 1);
  808. m_rhs->dump(indent + 1);
  809. }
  810. void UpdateExpression::dump(int indent) const
  811. {
  812. const char* op_string = nullptr;
  813. switch (m_op) {
  814. case UpdateOp::Increment:
  815. op_string = "++";
  816. break;
  817. case UpdateOp::Decrement:
  818. op_string = "--";
  819. break;
  820. }
  821. ASTNode::dump(indent);
  822. print_indent(indent + 1);
  823. if (m_prefixed)
  824. printf("%s\n", op_string);
  825. m_argument->dump(indent + 1);
  826. if (!m_prefixed) {
  827. print_indent(indent + 1);
  828. printf("%s\n", op_string);
  829. }
  830. }
  831. Value VariableDeclaration::execute(Interpreter& interpreter) const
  832. {
  833. for (auto& declarator : m_declarations) {
  834. if (auto* init = declarator.init()) {
  835. auto initalizer_result = init->execute(interpreter);
  836. if (interpreter.exception())
  837. return {};
  838. interpreter.set_variable(declarator.id().string(), initalizer_result, true);
  839. }
  840. }
  841. return js_undefined();
  842. }
  843. Value VariableDeclarator::execute(Interpreter&) const
  844. {
  845. // NOTE: This node is handled by VariableDeclaration.
  846. ASSERT_NOT_REACHED();
  847. }
  848. void VariableDeclaration::dump(int indent) const
  849. {
  850. const char* declaration_kind_string = nullptr;
  851. switch (m_declaration_kind) {
  852. case DeclarationKind::Let:
  853. declaration_kind_string = "Let";
  854. break;
  855. case DeclarationKind::Var:
  856. declaration_kind_string = "Var";
  857. break;
  858. case DeclarationKind::Const:
  859. declaration_kind_string = "Const";
  860. break;
  861. }
  862. ASTNode::dump(indent);
  863. print_indent(indent + 1);
  864. printf("%s\n", declaration_kind_string);
  865. for (auto& declarator : m_declarations)
  866. declarator.dump(indent + 1);
  867. }
  868. void VariableDeclarator::dump(int indent) const
  869. {
  870. ASTNode::dump(indent);
  871. m_id->dump(indent + 1);
  872. if (m_init)
  873. m_init->dump(indent + 1);
  874. }
  875. void ObjectProperty::dump(int indent) const
  876. {
  877. ASTNode::dump(indent);
  878. m_key->dump(indent + 1);
  879. m_value->dump(indent + 1);
  880. }
  881. void ObjectExpression::dump(int indent) const
  882. {
  883. ASTNode::dump(indent);
  884. for (auto& property : m_properties) {
  885. property.dump(indent + 1);
  886. }
  887. }
  888. void ExpressionStatement::dump(int indent) const
  889. {
  890. ASTNode::dump(indent);
  891. m_expression->dump(indent + 1);
  892. }
  893. Value ObjectProperty::execute(Interpreter&) const
  894. {
  895. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  896. ASSERT_NOT_REACHED();
  897. }
  898. Value ObjectExpression::execute(Interpreter& interpreter) const
  899. {
  900. auto* object = Object::create_empty(interpreter, interpreter.global_object());
  901. for (auto& property : m_properties) {
  902. auto key_result = property.key().execute(interpreter);
  903. if (interpreter.exception())
  904. return {};
  905. auto key = key_result.to_string();
  906. auto value = property.value().execute(interpreter);
  907. if (interpreter.exception())
  908. return {};
  909. object->put(key, value);
  910. }
  911. return object;
  912. }
  913. void MemberExpression::dump(int indent) const
  914. {
  915. print_indent(indent);
  916. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  917. m_object->dump(indent + 1);
  918. m_property->dump(indent + 1);
  919. }
  920. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter) const
  921. {
  922. if (!is_computed()) {
  923. ASSERT(m_property->is_identifier());
  924. return PropertyName(static_cast<const Identifier&>(*m_property).string());
  925. }
  926. auto index = m_property->execute(interpreter);
  927. if (interpreter.exception())
  928. return {};
  929. ASSERT(!index.is_empty());
  930. // FIXME: What about non-integer numbers tho.
  931. if (index.is_number() && index.to_i32() >= 0)
  932. return PropertyName(index.to_i32());
  933. return PropertyName(index.to_string());
  934. }
  935. String MemberExpression::to_string_approximation() const
  936. {
  937. String object_string = "<object>";
  938. if (m_object->is_identifier())
  939. object_string = static_cast<const Identifier&>(*m_object).string();
  940. if (is_computed())
  941. return String::format("%s[<computed>]", object_string.characters());
  942. ASSERT(m_property->is_identifier());
  943. return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
  944. }
  945. Value MemberExpression::execute(Interpreter& interpreter) const
  946. {
  947. auto object_value = m_object->execute(interpreter);
  948. if (interpreter.exception())
  949. return {};
  950. auto* object_result = object_value.to_object(interpreter.heap());
  951. if (interpreter.exception())
  952. return {};
  953. return object_result->get(computed_property_name(interpreter)).value_or(js_undefined());
  954. }
  955. Value StringLiteral::execute(Interpreter& interpreter) const
  956. {
  957. return js_string(interpreter, m_value);
  958. }
  959. Value NumericLiteral::execute(Interpreter&) const
  960. {
  961. return Value(m_value);
  962. }
  963. Value BooleanLiteral::execute(Interpreter&) const
  964. {
  965. return Value(m_value);
  966. }
  967. Value NullLiteral::execute(Interpreter&) const
  968. {
  969. return js_null();
  970. }
  971. void ArrayExpression::dump(int indent) const
  972. {
  973. ASTNode::dump(indent);
  974. for (auto& element : m_elements) {
  975. if (element) {
  976. element->dump(indent + 1);
  977. } else {
  978. print_indent(indent + 1);
  979. printf("<empty>\n");
  980. }
  981. }
  982. }
  983. Value ArrayExpression::execute(Interpreter& interpreter) const
  984. {
  985. auto* array = Array::create(interpreter.global_object());
  986. for (auto& element : m_elements) {
  987. auto value = Value();
  988. if (element) {
  989. value = element->execute(interpreter);
  990. if (interpreter.exception())
  991. return {};
  992. if (element->is_spread_expression()) {
  993. if (!value.is_array()) {
  994. interpreter.throw_exception<TypeError>(String::format("%s is not iterable", value.to_string().characters()));
  995. return {};
  996. }
  997. auto& array_to_spread = static_cast<Array&>(value.as_object());
  998. for (auto& it : array_to_spread.elements()) {
  999. if (it.is_empty()) {
  1000. array->elements().append(js_undefined());
  1001. } else {
  1002. array->elements().append(it);
  1003. }
  1004. }
  1005. continue;
  1006. }
  1007. }
  1008. array->elements().append(value);
  1009. }
  1010. return array;
  1011. }
  1012. void TryStatement::dump(int indent) const
  1013. {
  1014. ASTNode::dump(indent);
  1015. print_indent(indent);
  1016. printf("(Block)\n");
  1017. block().dump(indent + 1);
  1018. if (handler()) {
  1019. print_indent(indent);
  1020. printf("(Handler)\n");
  1021. handler()->dump(indent + 1);
  1022. }
  1023. if (finalizer()) {
  1024. print_indent(indent);
  1025. printf("(Finalizer)\n");
  1026. finalizer()->dump(indent + 1);
  1027. }
  1028. }
  1029. void CatchClause::dump(int indent) const
  1030. {
  1031. print_indent(indent);
  1032. printf("CatchClause");
  1033. if (!m_parameter.is_null())
  1034. printf(" (%s)", m_parameter.characters());
  1035. printf("\n");
  1036. body().dump(indent + 1);
  1037. }
  1038. void ThrowStatement::dump(int indent) const
  1039. {
  1040. ASTNode::dump(indent);
  1041. argument().dump(indent + 1);
  1042. }
  1043. Value TryStatement::execute(Interpreter& interpreter) const
  1044. {
  1045. interpreter.run(block(), {}, ScopeType::Try);
  1046. if (auto* exception = interpreter.exception()) {
  1047. if (m_handler) {
  1048. interpreter.clear_exception();
  1049. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  1050. interpreter.run(m_handler->body(), move(arguments));
  1051. }
  1052. }
  1053. if (m_finalizer)
  1054. m_finalizer->execute(interpreter);
  1055. return js_undefined();
  1056. }
  1057. Value CatchClause::execute(Interpreter&) const
  1058. {
  1059. // NOTE: CatchClause execution is handled by TryStatement.
  1060. ASSERT_NOT_REACHED();
  1061. return {};
  1062. }
  1063. Value ThrowStatement::execute(Interpreter& interpreter) const
  1064. {
  1065. auto value = m_argument->execute(interpreter);
  1066. if (interpreter.exception())
  1067. return {};
  1068. return interpreter.throw_exception(value);
  1069. }
  1070. Value SwitchStatement::execute(Interpreter& interpreter) const
  1071. {
  1072. auto discriminant_result = m_discriminant->execute(interpreter);
  1073. if (interpreter.exception())
  1074. return {};
  1075. bool falling_through = false;
  1076. for (auto& switch_case : m_cases) {
  1077. if (!falling_through && switch_case.test()) {
  1078. auto test_result = switch_case.test()->execute(interpreter);
  1079. if (interpreter.exception())
  1080. return {};
  1081. if (!eq(interpreter, discriminant_result, test_result).to_boolean())
  1082. continue;
  1083. }
  1084. falling_through = true;
  1085. for (auto& statement : switch_case.consequent()) {
  1086. statement.execute(interpreter);
  1087. if (interpreter.exception())
  1088. return {};
  1089. if (interpreter.should_unwind()) {
  1090. if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  1091. interpreter.stop_unwind();
  1092. return {};
  1093. }
  1094. return {};
  1095. }
  1096. }
  1097. }
  1098. return js_undefined();
  1099. }
  1100. Value SwitchCase::execute(Interpreter& interpreter) const
  1101. {
  1102. (void)interpreter;
  1103. return {};
  1104. }
  1105. Value BreakStatement::execute(Interpreter& interpreter) const
  1106. {
  1107. interpreter.unwind(ScopeType::Breakable);
  1108. return js_undefined();
  1109. }
  1110. Value ContinueStatement::execute(Interpreter& interpreter) const
  1111. {
  1112. interpreter.unwind(ScopeType::Continuable);
  1113. return js_undefined();
  1114. }
  1115. void SwitchStatement::dump(int indent) const
  1116. {
  1117. ASTNode::dump(indent);
  1118. m_discriminant->dump(indent + 1);
  1119. for (auto& switch_case : m_cases) {
  1120. switch_case.dump(indent + 1);
  1121. }
  1122. }
  1123. void SwitchCase::dump(int indent) const
  1124. {
  1125. ASTNode::dump(indent);
  1126. print_indent(indent);
  1127. if (m_test) {
  1128. printf("(Test)\n");
  1129. m_test->dump(indent + 1);
  1130. } else {
  1131. printf("(Default)\n");
  1132. }
  1133. print_indent(indent);
  1134. printf("(Consequent)\n");
  1135. int i = 0;
  1136. for (auto& statement : m_consequent) {
  1137. print_indent(indent);
  1138. printf("[%d]\n", i++);
  1139. statement.dump(indent + 1);
  1140. }
  1141. }
  1142. Value ConditionalExpression::execute(Interpreter& interpreter) const
  1143. {
  1144. auto test_result = m_test->execute(interpreter);
  1145. if (interpreter.exception())
  1146. return {};
  1147. Value result;
  1148. if (test_result.to_boolean()) {
  1149. result = m_consequent->execute(interpreter);
  1150. } else {
  1151. result = m_alternate->execute(interpreter);
  1152. }
  1153. if (interpreter.exception())
  1154. return {};
  1155. return result;
  1156. }
  1157. void ConditionalExpression::dump(int indent) const
  1158. {
  1159. ASTNode::dump(indent);
  1160. print_indent(indent);
  1161. printf("(Test)\n");
  1162. m_test->dump(indent + 1);
  1163. print_indent(indent);
  1164. printf("(Consequent)\n");
  1165. m_test->dump(indent + 1);
  1166. print_indent(indent);
  1167. printf("(Alternate)\n");
  1168. m_test->dump(indent + 1);
  1169. }
  1170. void SequenceExpression::dump(int indent) const
  1171. {
  1172. ASTNode::dump(indent);
  1173. for (auto& expression : m_expressions)
  1174. expression.dump(indent + 1);
  1175. }
  1176. Value SequenceExpression::execute(Interpreter& interpreter) const
  1177. {
  1178. Value last_value;
  1179. for (auto& expression : m_expressions) {
  1180. last_value = expression.execute(interpreter);
  1181. if (interpreter.exception())
  1182. return {};
  1183. }
  1184. return last_value;
  1185. }
  1186. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1187. {
  1188. m_variables.append(move(variables));
  1189. }
  1190. }