AST.cpp 46 KB

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