AST.cpp 39 KB

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