AST.cpp 35 KB

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