AST.cpp 46 KB

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