AST.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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.heap());
  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().characters(), call_type, expression_string.characters());
  115. } else {
  116. error_message = String::format("%s is not a %s", callee.to_string().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().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.heap());
  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.heap());
  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. default:
  448. ASSERT_NOT_REACHED();
  449. }
  450. case UnaryOp::Void:
  451. return js_undefined();
  452. case UnaryOp::Delete:
  453. ASSERT_NOT_REACHED();
  454. }
  455. ASSERT_NOT_REACHED();
  456. }
  457. static void print_indent(int indent)
  458. {
  459. for (int i = 0; i < indent * 2; ++i)
  460. putchar(' ');
  461. }
  462. void ASTNode::dump(int indent) const
  463. {
  464. print_indent(indent);
  465. printf("%s\n", class_name());
  466. }
  467. void ScopeNode::dump(int indent) const
  468. {
  469. ASTNode::dump(indent);
  470. if (!m_variables.is_empty()) {
  471. print_indent(indent + 1);
  472. printf("(Variables)\n");
  473. for (auto& variable : m_variables)
  474. variable.dump(indent + 2);
  475. }
  476. if (!m_children.is_empty()) {
  477. print_indent(indent + 1);
  478. printf("(Children)\n");
  479. for (auto& child : children())
  480. child.dump(indent + 2);
  481. }
  482. }
  483. void BinaryExpression::dump(int indent) const
  484. {
  485. const char* op_string = nullptr;
  486. switch (m_op) {
  487. case BinaryOp::Addition:
  488. op_string = "+";
  489. break;
  490. case BinaryOp::Subtraction:
  491. op_string = "-";
  492. break;
  493. case BinaryOp::Multiplication:
  494. op_string = "*";
  495. break;
  496. case BinaryOp::Division:
  497. op_string = "/";
  498. break;
  499. case BinaryOp::Modulo:
  500. op_string = "%";
  501. break;
  502. case BinaryOp::Exponentiation:
  503. op_string = "**";
  504. break;
  505. case BinaryOp::TypedEquals:
  506. op_string = "===";
  507. break;
  508. case BinaryOp::TypedInequals:
  509. op_string = "!==";
  510. break;
  511. case BinaryOp::AbstractEquals:
  512. op_string = "==";
  513. break;
  514. case BinaryOp::AbstractInequals:
  515. op_string = "!=";
  516. break;
  517. case BinaryOp::GreaterThan:
  518. op_string = ">";
  519. break;
  520. case BinaryOp::GreaterThanEquals:
  521. op_string = ">=";
  522. break;
  523. case BinaryOp::LessThan:
  524. op_string = "<";
  525. break;
  526. case BinaryOp::LessThanEquals:
  527. op_string = "<=";
  528. break;
  529. case BinaryOp::BitwiseAnd:
  530. op_string = "&";
  531. break;
  532. case BinaryOp::BitwiseOr:
  533. op_string = "|";
  534. break;
  535. case BinaryOp::BitwiseXor:
  536. op_string = "^";
  537. break;
  538. case BinaryOp::LeftShift:
  539. op_string = "<<";
  540. break;
  541. case BinaryOp::RightShift:
  542. op_string = ">>";
  543. break;
  544. case BinaryOp::UnsignedRightShift:
  545. op_string = ">>>";
  546. break;
  547. case BinaryOp::In:
  548. op_string = "in";
  549. break;
  550. case BinaryOp::InstanceOf:
  551. op_string = "instanceof";
  552. break;
  553. }
  554. print_indent(indent);
  555. printf("%s\n", class_name());
  556. m_lhs->dump(indent + 1);
  557. print_indent(indent + 1);
  558. printf("%s\n", op_string);
  559. m_rhs->dump(indent + 1);
  560. }
  561. void LogicalExpression::dump(int indent) const
  562. {
  563. const char* op_string = nullptr;
  564. switch (m_op) {
  565. case LogicalOp::And:
  566. op_string = "&&";
  567. break;
  568. case LogicalOp::Or:
  569. op_string = "||";
  570. break;
  571. case LogicalOp::NullishCoalescing:
  572. op_string = "??";
  573. break;
  574. }
  575. print_indent(indent);
  576. printf("%s\n", class_name());
  577. m_lhs->dump(indent + 1);
  578. print_indent(indent + 1);
  579. printf("%s\n", op_string);
  580. m_rhs->dump(indent + 1);
  581. }
  582. void UnaryExpression::dump(int indent) const
  583. {
  584. const char* op_string = nullptr;
  585. switch (m_op) {
  586. case UnaryOp::BitwiseNot:
  587. op_string = "~";
  588. break;
  589. case UnaryOp::Not:
  590. op_string = "!";
  591. break;
  592. case UnaryOp::Plus:
  593. op_string = "+";
  594. break;
  595. case UnaryOp::Minus:
  596. op_string = "-";
  597. break;
  598. case UnaryOp::Typeof:
  599. op_string = "typeof ";
  600. break;
  601. case UnaryOp::Void:
  602. op_string = "void ";
  603. break;
  604. case UnaryOp::Delete:
  605. op_string = "delete ";
  606. break;
  607. }
  608. print_indent(indent);
  609. printf("%s\n", class_name());
  610. print_indent(indent + 1);
  611. printf("%s\n", op_string);
  612. m_lhs->dump(indent + 1);
  613. }
  614. void CallExpression::dump(int indent) const
  615. {
  616. print_indent(indent);
  617. printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
  618. m_callee->dump(indent + 1);
  619. for (auto& argument : m_arguments)
  620. argument.value->dump(indent + 1);
  621. }
  622. void StringLiteral::dump(int indent) const
  623. {
  624. print_indent(indent);
  625. printf("StringLiteral \"%s\"\n", m_value.characters());
  626. }
  627. void NumericLiteral::dump(int indent) const
  628. {
  629. print_indent(indent);
  630. printf("NumericLiteral %g\n", m_value);
  631. }
  632. void BooleanLiteral::dump(int indent) const
  633. {
  634. print_indent(indent);
  635. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  636. }
  637. void NullLiteral::dump(int indent) const
  638. {
  639. print_indent(indent);
  640. printf("null\n");
  641. }
  642. void FunctionNode::dump(int indent, const char* class_name) const
  643. {
  644. print_indent(indent);
  645. printf("%s '%s'\n", class_name, name().characters());
  646. if (!m_parameters.is_empty()) {
  647. print_indent(indent + 1);
  648. printf("(Parameters)\n");
  649. for (auto& parameter : m_parameters) {
  650. print_indent(indent + 2);
  651. if (parameter.is_rest)
  652. printf("...");
  653. printf("%s\n", parameter.name.characters());
  654. if (parameter.default_value)
  655. parameter.default_value->dump(indent + 3);
  656. }
  657. }
  658. if (!m_variables.is_empty()) {
  659. print_indent(indent + 1);
  660. printf("(Variables)\n");
  661. for (auto& variable : m_variables)
  662. variable.dump(indent + 2);
  663. }
  664. print_indent(indent + 1);
  665. printf("(Body)\n");
  666. body().dump(indent + 2);
  667. }
  668. void FunctionDeclaration::dump(int indent) const
  669. {
  670. FunctionNode::dump(indent, class_name());
  671. }
  672. void FunctionExpression::dump(int indent) const
  673. {
  674. FunctionNode::dump(indent, class_name());
  675. }
  676. void ReturnStatement::dump(int indent) const
  677. {
  678. ASTNode::dump(indent);
  679. if (argument())
  680. argument()->dump(indent + 1);
  681. }
  682. void IfStatement::dump(int indent) const
  683. {
  684. ASTNode::dump(indent);
  685. print_indent(indent);
  686. printf("If\n");
  687. predicate().dump(indent + 1);
  688. consequent().dump(indent + 1);
  689. if (alternate()) {
  690. print_indent(indent);
  691. printf("Else\n");
  692. alternate()->dump(indent + 1);
  693. }
  694. }
  695. void WhileStatement::dump(int indent) const
  696. {
  697. ASTNode::dump(indent);
  698. print_indent(indent);
  699. printf("While\n");
  700. test().dump(indent + 1);
  701. body().dump(indent + 1);
  702. }
  703. void DoWhileStatement::dump(int indent) const
  704. {
  705. ASTNode::dump(indent);
  706. print_indent(indent);
  707. printf("DoWhile\n");
  708. test().dump(indent + 1);
  709. body().dump(indent + 1);
  710. }
  711. void ForStatement::dump(int indent) const
  712. {
  713. ASTNode::dump(indent);
  714. print_indent(indent);
  715. printf("For\n");
  716. if (init())
  717. init()->dump(indent + 1);
  718. if (test())
  719. test()->dump(indent + 1);
  720. if (update())
  721. update()->dump(indent + 1);
  722. body().dump(indent + 1);
  723. }
  724. Value Identifier::execute(Interpreter& interpreter) const
  725. {
  726. auto value = interpreter.get_variable(string());
  727. if (value.is_empty())
  728. return interpreter.throw_exception<ReferenceError>(String::format("'%s' not known", string().characters()));
  729. return value;
  730. }
  731. void Identifier::dump(int indent) const
  732. {
  733. print_indent(indent);
  734. printf("Identifier \"%s\"\n", m_string.characters());
  735. }
  736. void SpreadExpression::dump(int indent) const
  737. {
  738. ASTNode::dump(indent);
  739. m_target->dump(indent + 1);
  740. }
  741. Value SpreadExpression::execute(Interpreter& interpreter) const
  742. {
  743. return m_target->execute(interpreter);
  744. }
  745. Value ThisExpression::execute(Interpreter& interpreter) const
  746. {
  747. return interpreter.this_value();
  748. }
  749. void ThisExpression::dump(int indent) const
  750. {
  751. ASTNode::dump(indent);
  752. }
  753. Value AssignmentExpression::execute(Interpreter& interpreter) const
  754. {
  755. auto rhs_result = m_rhs->execute(interpreter);
  756. if (interpreter.exception())
  757. return {};
  758. Value lhs_result;
  759. switch (m_op) {
  760. case AssignmentOp::Assignment:
  761. break;
  762. case AssignmentOp::AdditionAssignment:
  763. lhs_result = m_lhs->execute(interpreter);
  764. if (interpreter.exception())
  765. return {};
  766. rhs_result = add(interpreter, lhs_result, rhs_result);
  767. break;
  768. case AssignmentOp::SubtractionAssignment:
  769. lhs_result = m_lhs->execute(interpreter);
  770. if (interpreter.exception())
  771. return {};
  772. rhs_result = sub(interpreter, lhs_result, rhs_result);
  773. break;
  774. case AssignmentOp::MultiplicationAssignment:
  775. lhs_result = m_lhs->execute(interpreter);
  776. if (interpreter.exception())
  777. return {};
  778. rhs_result = mul(interpreter, lhs_result, rhs_result);
  779. break;
  780. case AssignmentOp::DivisionAssignment:
  781. lhs_result = m_lhs->execute(interpreter);
  782. if (interpreter.exception())
  783. return {};
  784. rhs_result = div(interpreter, lhs_result, rhs_result);
  785. break;
  786. case AssignmentOp::ModuloAssignment:
  787. lhs_result = m_lhs->execute(interpreter);
  788. if (interpreter.exception())
  789. return {};
  790. rhs_result = mod(interpreter, lhs_result, rhs_result);
  791. break;
  792. case AssignmentOp::ExponentiationAssignment:
  793. lhs_result = m_lhs->execute(interpreter);
  794. if (interpreter.exception())
  795. return {};
  796. rhs_result = exp(interpreter, lhs_result, rhs_result);
  797. break;
  798. case AssignmentOp::BitwiseAndAssignment:
  799. lhs_result = m_lhs->execute(interpreter);
  800. if (interpreter.exception())
  801. return {};
  802. rhs_result = bitwise_and(interpreter, lhs_result, rhs_result);
  803. break;
  804. case AssignmentOp::BitwiseOrAssignment:
  805. lhs_result = m_lhs->execute(interpreter);
  806. if (interpreter.exception())
  807. return {};
  808. rhs_result = bitwise_or(interpreter, lhs_result, rhs_result);
  809. break;
  810. case AssignmentOp::BitwiseXorAssignment:
  811. lhs_result = m_lhs->execute(interpreter);
  812. if (interpreter.exception())
  813. return {};
  814. rhs_result = bitwise_xor(interpreter, lhs_result, rhs_result);
  815. break;
  816. case AssignmentOp::LeftShiftAssignment:
  817. lhs_result = m_lhs->execute(interpreter);
  818. if (interpreter.exception())
  819. return {};
  820. rhs_result = left_shift(interpreter, lhs_result, rhs_result);
  821. break;
  822. case AssignmentOp::RightShiftAssignment:
  823. lhs_result = m_lhs->execute(interpreter);
  824. if (interpreter.exception())
  825. return {};
  826. rhs_result = right_shift(interpreter, lhs_result, rhs_result);
  827. break;
  828. case AssignmentOp::UnsignedRightShiftAssignment:
  829. lhs_result = m_lhs->execute(interpreter);
  830. if (interpreter.exception())
  831. return {};
  832. rhs_result = unsigned_right_shift(interpreter, lhs_result, rhs_result);
  833. break;
  834. }
  835. if (interpreter.exception())
  836. return {};
  837. auto reference = m_lhs->to_reference(interpreter);
  838. if (interpreter.exception())
  839. return {};
  840. if (reference.is_unresolvable())
  841. return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
  842. update_function_name(rhs_result, reference.name().as_string());
  843. reference.put(interpreter, rhs_result);
  844. if (interpreter.exception())
  845. return {};
  846. return rhs_result;
  847. }
  848. Value UpdateExpression::execute(Interpreter& interpreter) const
  849. {
  850. auto reference = m_argument->to_reference(interpreter);
  851. if (interpreter.exception())
  852. return {};
  853. auto old_value = reference.get(interpreter);
  854. if (interpreter.exception())
  855. return {};
  856. old_value = old_value.to_number();
  857. int op_result = 0;
  858. switch (m_op) {
  859. case UpdateOp::Increment:
  860. op_result = 1;
  861. break;
  862. case UpdateOp::Decrement:
  863. op_result = -1;
  864. break;
  865. default:
  866. ASSERT_NOT_REACHED();
  867. }
  868. auto new_value = Value(old_value.as_double() + op_result);
  869. reference.put(interpreter, new_value);
  870. if (interpreter.exception())
  871. return {};
  872. return m_prefixed ? new_value : old_value;
  873. }
  874. void AssignmentExpression::dump(int indent) const
  875. {
  876. const char* op_string = nullptr;
  877. switch (m_op) {
  878. case AssignmentOp::Assignment:
  879. op_string = "=";
  880. break;
  881. case AssignmentOp::AdditionAssignment:
  882. op_string = "+=";
  883. break;
  884. case AssignmentOp::SubtractionAssignment:
  885. op_string = "-=";
  886. break;
  887. case AssignmentOp::MultiplicationAssignment:
  888. op_string = "*=";
  889. break;
  890. case AssignmentOp::DivisionAssignment:
  891. op_string = "/=";
  892. break;
  893. case AssignmentOp::ModuloAssignment:
  894. op_string = "%=";
  895. break;
  896. case AssignmentOp::ExponentiationAssignment:
  897. op_string = "**=";
  898. break;
  899. case AssignmentOp::BitwiseAndAssignment:
  900. op_string = "&=";
  901. break;
  902. case AssignmentOp::BitwiseOrAssignment:
  903. op_string = "|=";
  904. break;
  905. case AssignmentOp::BitwiseXorAssignment:
  906. op_string = "^=";
  907. break;
  908. case AssignmentOp::LeftShiftAssignment:
  909. op_string = "<<=";
  910. break;
  911. case AssignmentOp::RightShiftAssignment:
  912. op_string = ">>=";
  913. break;
  914. case AssignmentOp::UnsignedRightShiftAssignment:
  915. op_string = ">>>=";
  916. break;
  917. }
  918. ASTNode::dump(indent);
  919. print_indent(indent + 1);
  920. printf("%s\n", op_string);
  921. m_lhs->dump(indent + 1);
  922. m_rhs->dump(indent + 1);
  923. }
  924. void UpdateExpression::dump(int indent) const
  925. {
  926. const char* op_string = nullptr;
  927. switch (m_op) {
  928. case UpdateOp::Increment:
  929. op_string = "++";
  930. break;
  931. case UpdateOp::Decrement:
  932. op_string = "--";
  933. break;
  934. }
  935. ASTNode::dump(indent);
  936. print_indent(indent + 1);
  937. if (m_prefixed)
  938. printf("%s\n", op_string);
  939. m_argument->dump(indent + 1);
  940. if (!m_prefixed) {
  941. print_indent(indent + 1);
  942. printf("%s\n", op_string);
  943. }
  944. }
  945. Value VariableDeclaration::execute(Interpreter& interpreter) const
  946. {
  947. for (auto& declarator : m_declarations) {
  948. if (auto* init = declarator.init()) {
  949. auto initalizer_result = init->execute(interpreter);
  950. if (interpreter.exception())
  951. return {};
  952. auto variable_name = declarator.id().string();
  953. update_function_name(initalizer_result, variable_name);
  954. interpreter.set_variable(variable_name, initalizer_result, true);
  955. }
  956. }
  957. return js_undefined();
  958. }
  959. Value VariableDeclarator::execute(Interpreter&) const
  960. {
  961. // NOTE: This node is handled by VariableDeclaration.
  962. ASSERT_NOT_REACHED();
  963. }
  964. void VariableDeclaration::dump(int indent) const
  965. {
  966. const char* declaration_kind_string = nullptr;
  967. switch (m_declaration_kind) {
  968. case DeclarationKind::Let:
  969. declaration_kind_string = "Let";
  970. break;
  971. case DeclarationKind::Var:
  972. declaration_kind_string = "Var";
  973. break;
  974. case DeclarationKind::Const:
  975. declaration_kind_string = "Const";
  976. break;
  977. }
  978. ASTNode::dump(indent);
  979. print_indent(indent + 1);
  980. printf("%s\n", declaration_kind_string);
  981. for (auto& declarator : m_declarations)
  982. declarator.dump(indent + 1);
  983. }
  984. void VariableDeclarator::dump(int indent) const
  985. {
  986. ASTNode::dump(indent);
  987. m_id->dump(indent + 1);
  988. if (m_init)
  989. m_init->dump(indent + 1);
  990. }
  991. void ObjectProperty::dump(int indent) const
  992. {
  993. ASTNode::dump(indent);
  994. m_key->dump(indent + 1);
  995. m_value->dump(indent + 1);
  996. }
  997. void ObjectExpression::dump(int indent) const
  998. {
  999. ASTNode::dump(indent);
  1000. for (auto& property : m_properties) {
  1001. property.dump(indent + 1);
  1002. }
  1003. }
  1004. void ExpressionStatement::dump(int indent) const
  1005. {
  1006. ASTNode::dump(indent);
  1007. m_expression->dump(indent + 1);
  1008. }
  1009. Value ObjectProperty::execute(Interpreter&) const
  1010. {
  1011. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  1012. ASSERT_NOT_REACHED();
  1013. }
  1014. Value ObjectExpression::execute(Interpreter& interpreter) const
  1015. {
  1016. auto* object = Object::create_empty(interpreter, interpreter.global_object());
  1017. for (auto& property : m_properties) {
  1018. auto key_result = property.key().execute(interpreter);
  1019. if (interpreter.exception())
  1020. return {};
  1021. if (property.is_spread()) {
  1022. if (key_result.is_array()) {
  1023. auto& array_to_spread = static_cast<Array&>(key_result.as_object());
  1024. auto& elements = array_to_spread.elements();
  1025. for (size_t i = 0; i < elements.size(); ++i) {
  1026. auto element = elements.at(i);
  1027. if (!element.is_empty())
  1028. object->put_by_index(i, element);
  1029. }
  1030. } else if (key_result.is_object()) {
  1031. auto& obj_to_spread = key_result.as_object();
  1032. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  1033. if (it.value.attributes & Attribute::Enumerable)
  1034. object->put(it.key, obj_to_spread.get(it.key));
  1035. }
  1036. } else if (key_result.is_string()) {
  1037. auto& str_to_spread = key_result.as_string().string();
  1038. for (size_t i = 0; i < str_to_spread.length(); i++) {
  1039. object->put_by_index(i, js_string(interpreter, str_to_spread.substring(i, 1)));
  1040. }
  1041. }
  1042. continue;
  1043. }
  1044. auto key = key_result.to_string();
  1045. auto value = property.value().execute(interpreter);
  1046. if (interpreter.exception())
  1047. return {};
  1048. update_function_name(value, key);
  1049. object->put(key, value);
  1050. }
  1051. return object;
  1052. }
  1053. void MemberExpression::dump(int indent) const
  1054. {
  1055. print_indent(indent);
  1056. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  1057. m_object->dump(indent + 1);
  1058. m_property->dump(indent + 1);
  1059. }
  1060. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter) const
  1061. {
  1062. if (!is_computed()) {
  1063. ASSERT(m_property->is_identifier());
  1064. return PropertyName(static_cast<const Identifier&>(*m_property).string());
  1065. }
  1066. auto index = m_property->execute(interpreter);
  1067. if (interpreter.exception())
  1068. return {};
  1069. ASSERT(!index.is_empty());
  1070. if (!index.to_number().is_finite_number())
  1071. return PropertyName(index.to_string());
  1072. auto index_as_double = index.to_double();
  1073. if (index_as_double < 0 || (i32)index_as_double != index_as_double)
  1074. return PropertyName(index.to_string());
  1075. return PropertyName(index.to_i32());
  1076. }
  1077. String MemberExpression::to_string_approximation() const
  1078. {
  1079. String object_string = "<object>";
  1080. if (m_object->is_identifier())
  1081. object_string = static_cast<const Identifier&>(*m_object).string();
  1082. if (is_computed())
  1083. return String::format("%s[<computed>]", object_string.characters());
  1084. ASSERT(m_property->is_identifier());
  1085. return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
  1086. }
  1087. Value MemberExpression::execute(Interpreter& interpreter) const
  1088. {
  1089. auto object_value = m_object->execute(interpreter);
  1090. if (interpreter.exception())
  1091. return {};
  1092. auto* object_result = object_value.to_object(interpreter.heap());
  1093. if (interpreter.exception())
  1094. return {};
  1095. return object_result->get(computed_property_name(interpreter)).value_or(js_undefined());
  1096. }
  1097. Value StringLiteral::execute(Interpreter& interpreter) const
  1098. {
  1099. return js_string(interpreter, m_value);
  1100. }
  1101. Value NumericLiteral::execute(Interpreter&) const
  1102. {
  1103. return Value(m_value);
  1104. }
  1105. Value BooleanLiteral::execute(Interpreter&) const
  1106. {
  1107. return Value(m_value);
  1108. }
  1109. Value NullLiteral::execute(Interpreter&) const
  1110. {
  1111. return js_null();
  1112. }
  1113. void ArrayExpression::dump(int indent) const
  1114. {
  1115. ASTNode::dump(indent);
  1116. for (auto& element : m_elements) {
  1117. if (element) {
  1118. element->dump(indent + 1);
  1119. } else {
  1120. print_indent(indent + 1);
  1121. printf("<empty>\n");
  1122. }
  1123. }
  1124. }
  1125. Value ArrayExpression::execute(Interpreter& interpreter) const
  1126. {
  1127. auto* array = Array::create(interpreter.global_object());
  1128. for (auto& element : m_elements) {
  1129. auto value = Value();
  1130. if (element) {
  1131. value = element->execute(interpreter);
  1132. if (interpreter.exception())
  1133. return {};
  1134. if (element->is_spread_expression()) {
  1135. // FIXME: Support arbitrary iterables
  1136. if (value.is_array()) {
  1137. auto& array_to_spread = static_cast<Array&>(value.as_object());
  1138. for (auto& it : array_to_spread.elements()) {
  1139. if (it.is_empty()) {
  1140. array->elements().append(js_undefined());
  1141. } else {
  1142. array->elements().append(it);
  1143. }
  1144. }
  1145. continue;
  1146. }
  1147. if (value.is_string() || (value.is_object() && value.as_object().is_string_object())) {
  1148. String string_to_spread;
  1149. if (value.is_string())
  1150. string_to_spread = value.as_string().string();
  1151. else
  1152. string_to_spread = static_cast<const StringObject&>(value.as_object()).primitive_string().string();
  1153. for (size_t i = 0; i < string_to_spread.length(); ++i)
  1154. array->elements().append(js_string(interpreter, string_to_spread.substring(i, 1)));
  1155. continue;
  1156. }
  1157. interpreter.throw_exception<TypeError>(String::format("%s is not iterable", value.to_string().characters()));
  1158. return {};
  1159. }
  1160. }
  1161. array->elements().append(value);
  1162. }
  1163. return array;
  1164. }
  1165. void TemplateLiteral::dump(int indent) const
  1166. {
  1167. ASTNode::dump(indent);
  1168. for (auto& expression : m_expressions)
  1169. expression.dump(indent + 1);
  1170. }
  1171. Value TemplateLiteral::execute(Interpreter& interpreter) const
  1172. {
  1173. StringBuilder string_builder;
  1174. for (auto& expression : m_expressions) {
  1175. auto expr = expression.execute(interpreter);
  1176. if (interpreter.exception())
  1177. return {};
  1178. string_builder.append(expr.to_string());
  1179. }
  1180. return js_string(interpreter, string_builder.build());
  1181. }
  1182. void TaggedTemplateLiteral::dump(int indent) const
  1183. {
  1184. ASTNode::dump(indent);
  1185. print_indent(indent + 1);
  1186. printf("(Tag)\n");
  1187. m_tag->dump(indent + 2);
  1188. print_indent(indent + 1);
  1189. printf("(Template Literal)\n");
  1190. m_template_literal->dump(indent + 2);
  1191. }
  1192. Value TaggedTemplateLiteral::execute(Interpreter& interpreter) const
  1193. {
  1194. auto tag = m_tag->execute(interpreter);
  1195. if (interpreter.exception())
  1196. return {};
  1197. if (!tag.is_function()) {
  1198. interpreter.throw_exception<TypeError>(String::format("%s is not a function", tag.to_string().characters()));
  1199. return {};
  1200. }
  1201. auto& tag_function = tag.as_function();
  1202. auto& expressions = m_template_literal->expressions();
  1203. auto* strings = Array::create(interpreter.global_object());
  1204. MarkedValueList arguments(interpreter.heap());
  1205. arguments.append(strings);
  1206. for (size_t i = 0; i < expressions.size(); ++i) {
  1207. auto value = expressions[i].execute(interpreter);
  1208. if (interpreter.exception())
  1209. return {};
  1210. // tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
  1211. // tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
  1212. if (i % 2 == 0)
  1213. strings->elements().append(value);
  1214. else
  1215. arguments.append(value);
  1216. }
  1217. auto* raw_strings = Array::create(interpreter.global_object());
  1218. for (auto& raw_string : m_template_literal->raw_strings()) {
  1219. auto value = raw_string.execute(interpreter);
  1220. if (interpreter.exception())
  1221. return {};
  1222. raw_strings->elements().append(value);
  1223. }
  1224. strings->put("raw", raw_strings, 0);
  1225. return interpreter.call(tag_function, js_undefined(), move(arguments));
  1226. }
  1227. void TryStatement::dump(int indent) const
  1228. {
  1229. ASTNode::dump(indent);
  1230. print_indent(indent);
  1231. printf("(Block)\n");
  1232. block().dump(indent + 1);
  1233. if (handler()) {
  1234. print_indent(indent);
  1235. printf("(Handler)\n");
  1236. handler()->dump(indent + 1);
  1237. }
  1238. if (finalizer()) {
  1239. print_indent(indent);
  1240. printf("(Finalizer)\n");
  1241. finalizer()->dump(indent + 1);
  1242. }
  1243. }
  1244. void CatchClause::dump(int indent) const
  1245. {
  1246. print_indent(indent);
  1247. printf("CatchClause");
  1248. if (!m_parameter.is_null())
  1249. printf(" (%s)", m_parameter.characters());
  1250. printf("\n");
  1251. body().dump(indent + 1);
  1252. }
  1253. void ThrowStatement::dump(int indent) const
  1254. {
  1255. ASTNode::dump(indent);
  1256. argument().dump(indent + 1);
  1257. }
  1258. Value TryStatement::execute(Interpreter& interpreter) const
  1259. {
  1260. interpreter.run(block(), {}, ScopeType::Try);
  1261. if (auto* exception = interpreter.exception()) {
  1262. if (m_handler) {
  1263. interpreter.clear_exception();
  1264. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  1265. interpreter.run(m_handler->body(), move(arguments));
  1266. }
  1267. }
  1268. if (m_finalizer)
  1269. m_finalizer->execute(interpreter);
  1270. return js_undefined();
  1271. }
  1272. Value CatchClause::execute(Interpreter&) const
  1273. {
  1274. // NOTE: CatchClause execution is handled by TryStatement.
  1275. ASSERT_NOT_REACHED();
  1276. return {};
  1277. }
  1278. Value ThrowStatement::execute(Interpreter& interpreter) const
  1279. {
  1280. auto value = m_argument->execute(interpreter);
  1281. if (interpreter.exception())
  1282. return {};
  1283. return interpreter.throw_exception(value);
  1284. }
  1285. Value SwitchStatement::execute(Interpreter& interpreter) const
  1286. {
  1287. auto discriminant_result = m_discriminant->execute(interpreter);
  1288. if (interpreter.exception())
  1289. return {};
  1290. bool falling_through = false;
  1291. for (auto& switch_case : m_cases) {
  1292. if (!falling_through && switch_case.test()) {
  1293. auto test_result = switch_case.test()->execute(interpreter);
  1294. if (interpreter.exception())
  1295. return {};
  1296. if (!strict_eq(interpreter, discriminant_result, test_result))
  1297. continue;
  1298. }
  1299. falling_through = true;
  1300. for (auto& statement : switch_case.consequent()) {
  1301. statement.execute(interpreter);
  1302. if (interpreter.exception())
  1303. return {};
  1304. if (interpreter.should_unwind()) {
  1305. if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  1306. interpreter.stop_unwind();
  1307. return {};
  1308. }
  1309. return {};
  1310. }
  1311. }
  1312. }
  1313. return js_undefined();
  1314. }
  1315. Value SwitchCase::execute(Interpreter& interpreter) const
  1316. {
  1317. (void)interpreter;
  1318. return {};
  1319. }
  1320. Value BreakStatement::execute(Interpreter& interpreter) const
  1321. {
  1322. interpreter.unwind(ScopeType::Breakable);
  1323. return js_undefined();
  1324. }
  1325. Value ContinueStatement::execute(Interpreter& interpreter) const
  1326. {
  1327. interpreter.unwind(ScopeType::Continuable);
  1328. return js_undefined();
  1329. }
  1330. void SwitchStatement::dump(int indent) const
  1331. {
  1332. ASTNode::dump(indent);
  1333. m_discriminant->dump(indent + 1);
  1334. for (auto& switch_case : m_cases) {
  1335. switch_case.dump(indent + 1);
  1336. }
  1337. }
  1338. void SwitchCase::dump(int indent) const
  1339. {
  1340. ASTNode::dump(indent);
  1341. print_indent(indent + 1);
  1342. if (m_test) {
  1343. printf("(Test)\n");
  1344. m_test->dump(indent + 2);
  1345. } else {
  1346. printf("(Default)\n");
  1347. }
  1348. print_indent(indent + 1);
  1349. printf("(Consequent)\n");
  1350. for (auto& statement : m_consequent)
  1351. statement.dump(indent + 2);
  1352. }
  1353. Value ConditionalExpression::execute(Interpreter& interpreter) const
  1354. {
  1355. auto test_result = m_test->execute(interpreter);
  1356. if (interpreter.exception())
  1357. return {};
  1358. Value result;
  1359. if (test_result.to_boolean()) {
  1360. result = m_consequent->execute(interpreter);
  1361. } else {
  1362. result = m_alternate->execute(interpreter);
  1363. }
  1364. if (interpreter.exception())
  1365. return {};
  1366. return result;
  1367. }
  1368. void ConditionalExpression::dump(int indent) const
  1369. {
  1370. ASTNode::dump(indent);
  1371. print_indent(indent + 1);
  1372. printf("(Test)\n");
  1373. m_test->dump(indent + 2);
  1374. print_indent(indent + 1);
  1375. printf("(Consequent)\n");
  1376. m_consequent->dump(indent + 2);
  1377. print_indent(indent + 1);
  1378. printf("(Alternate)\n");
  1379. m_alternate->dump(indent + 2);
  1380. }
  1381. void SequenceExpression::dump(int indent) const
  1382. {
  1383. ASTNode::dump(indent);
  1384. for (auto& expression : m_expressions)
  1385. expression.dump(indent + 1);
  1386. }
  1387. Value SequenceExpression::execute(Interpreter& interpreter) const
  1388. {
  1389. Value last_value;
  1390. for (auto& expression : m_expressions) {
  1391. last_value = expression.execute(interpreter);
  1392. if (interpreter.exception())
  1393. return {};
  1394. }
  1395. return last_value;
  1396. }
  1397. Value DebuggerStatement::execute(Interpreter&) const
  1398. {
  1399. dbg() << "Sorry, no JavaScript debugger available (yet)!";
  1400. return js_undefined();
  1401. }
  1402. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1403. {
  1404. m_variables.append(move(variables));
  1405. }
  1406. }