AST.cpp 43 KB

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