AST.cpp 48 KB

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