AST.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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. auto* raw_strings = Array::create(interpreter.global_object());
  1216. for (auto& raw_string : m_template_literal->raw_strings()) {
  1217. auto value = raw_string.execute(interpreter);
  1218. if (interpreter.exception())
  1219. return {};
  1220. raw_strings->elements().append(value);
  1221. }
  1222. strings->put("raw", raw_strings, 0);
  1223. return interpreter.call(tag_function, js_undefined(), move(arguments));
  1224. }
  1225. void TryStatement::dump(int indent) const
  1226. {
  1227. ASTNode::dump(indent);
  1228. print_indent(indent);
  1229. printf("(Block)\n");
  1230. block().dump(indent + 1);
  1231. if (handler()) {
  1232. print_indent(indent);
  1233. printf("(Handler)\n");
  1234. handler()->dump(indent + 1);
  1235. }
  1236. if (finalizer()) {
  1237. print_indent(indent);
  1238. printf("(Finalizer)\n");
  1239. finalizer()->dump(indent + 1);
  1240. }
  1241. }
  1242. void CatchClause::dump(int indent) const
  1243. {
  1244. print_indent(indent);
  1245. printf("CatchClause");
  1246. if (!m_parameter.is_null())
  1247. printf(" (%s)", m_parameter.characters());
  1248. printf("\n");
  1249. body().dump(indent + 1);
  1250. }
  1251. void ThrowStatement::dump(int indent) const
  1252. {
  1253. ASTNode::dump(indent);
  1254. argument().dump(indent + 1);
  1255. }
  1256. Value TryStatement::execute(Interpreter& interpreter) const
  1257. {
  1258. interpreter.run(block(), {}, ScopeType::Try);
  1259. if (auto* exception = interpreter.exception()) {
  1260. if (m_handler) {
  1261. interpreter.clear_exception();
  1262. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  1263. interpreter.run(m_handler->body(), move(arguments));
  1264. }
  1265. }
  1266. if (m_finalizer)
  1267. m_finalizer->execute(interpreter);
  1268. return js_undefined();
  1269. }
  1270. Value CatchClause::execute(Interpreter&) const
  1271. {
  1272. // NOTE: CatchClause execution is handled by TryStatement.
  1273. ASSERT_NOT_REACHED();
  1274. return {};
  1275. }
  1276. Value ThrowStatement::execute(Interpreter& interpreter) const
  1277. {
  1278. auto value = m_argument->execute(interpreter);
  1279. if (interpreter.exception())
  1280. return {};
  1281. return interpreter.throw_exception(value);
  1282. }
  1283. Value SwitchStatement::execute(Interpreter& interpreter) const
  1284. {
  1285. auto discriminant_result = m_discriminant->execute(interpreter);
  1286. if (interpreter.exception())
  1287. return {};
  1288. bool falling_through = false;
  1289. for (auto& switch_case : m_cases) {
  1290. if (!falling_through && switch_case.test()) {
  1291. auto test_result = switch_case.test()->execute(interpreter);
  1292. if (interpreter.exception())
  1293. return {};
  1294. if (!eq(interpreter, discriminant_result, test_result).to_boolean())
  1295. continue;
  1296. }
  1297. falling_through = true;
  1298. for (auto& statement : switch_case.consequent()) {
  1299. statement.execute(interpreter);
  1300. if (interpreter.exception())
  1301. return {};
  1302. if (interpreter.should_unwind()) {
  1303. if (interpreter.should_unwind_until(ScopeType::Breakable)) {
  1304. interpreter.stop_unwind();
  1305. return {};
  1306. }
  1307. return {};
  1308. }
  1309. }
  1310. }
  1311. return js_undefined();
  1312. }
  1313. Value SwitchCase::execute(Interpreter& interpreter) const
  1314. {
  1315. (void)interpreter;
  1316. return {};
  1317. }
  1318. Value BreakStatement::execute(Interpreter& interpreter) const
  1319. {
  1320. interpreter.unwind(ScopeType::Breakable);
  1321. return js_undefined();
  1322. }
  1323. Value ContinueStatement::execute(Interpreter& interpreter) const
  1324. {
  1325. interpreter.unwind(ScopeType::Continuable);
  1326. return js_undefined();
  1327. }
  1328. void SwitchStatement::dump(int indent) const
  1329. {
  1330. ASTNode::dump(indent);
  1331. m_discriminant->dump(indent + 1);
  1332. for (auto& switch_case : m_cases) {
  1333. switch_case.dump(indent + 1);
  1334. }
  1335. }
  1336. void SwitchCase::dump(int indent) const
  1337. {
  1338. ASTNode::dump(indent);
  1339. print_indent(indent + 1);
  1340. if (m_test) {
  1341. printf("(Test)\n");
  1342. m_test->dump(indent + 2);
  1343. } else {
  1344. printf("(Default)\n");
  1345. }
  1346. print_indent(indent + 1);
  1347. printf("(Consequent)\n");
  1348. for (auto& statement : m_consequent)
  1349. statement.dump(indent + 2);
  1350. }
  1351. Value ConditionalExpression::execute(Interpreter& interpreter) const
  1352. {
  1353. auto test_result = m_test->execute(interpreter);
  1354. if (interpreter.exception())
  1355. return {};
  1356. Value result;
  1357. if (test_result.to_boolean()) {
  1358. result = m_consequent->execute(interpreter);
  1359. } else {
  1360. result = m_alternate->execute(interpreter);
  1361. }
  1362. if (interpreter.exception())
  1363. return {};
  1364. return result;
  1365. }
  1366. void ConditionalExpression::dump(int indent) const
  1367. {
  1368. ASTNode::dump(indent);
  1369. print_indent(indent + 1);
  1370. printf("(Test)\n");
  1371. m_test->dump(indent + 2);
  1372. print_indent(indent + 1);
  1373. printf("(Consequent)\n");
  1374. m_consequent->dump(indent + 2);
  1375. print_indent(indent + 1);
  1376. printf("(Alternate)\n");
  1377. m_alternate->dump(indent + 2);
  1378. }
  1379. void SequenceExpression::dump(int indent) const
  1380. {
  1381. ASTNode::dump(indent);
  1382. for (auto& expression : m_expressions)
  1383. expression.dump(indent + 1);
  1384. }
  1385. Value SequenceExpression::execute(Interpreter& interpreter) const
  1386. {
  1387. Value last_value;
  1388. for (auto& expression : m_expressions) {
  1389. last_value = expression.execute(interpreter);
  1390. if (interpreter.exception())
  1391. return {};
  1392. }
  1393. return last_value;
  1394. }
  1395. Value DebuggerStatement::execute(Interpreter&) const
  1396. {
  1397. dbg() << "Sorry, no JavaScript debugger available (yet)!";
  1398. return js_undefined();
  1399. }
  1400. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1401. {
  1402. m_variables.append(move(variables));
  1403. }
  1404. }