AST.cpp 47 KB

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