AST.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  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/HashMap.h>
  28. #include <AK/ScopeGuard.h>
  29. #include <AK/StringBuilder.h>
  30. #include <LibCrypto/BigInt/SignedBigInteger.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/BigInt.h>
  36. #include <LibJS/Runtime/Error.h>
  37. #include <LibJS/Runtime/GlobalObject.h>
  38. #include <LibJS/Runtime/IteratorOperations.h>
  39. #include <LibJS/Runtime/MarkedValueList.h>
  40. #include <LibJS/Runtime/NativeFunction.h>
  41. #include <LibJS/Runtime/PrimitiveString.h>
  42. #include <LibJS/Runtime/Reference.h>
  43. #include <LibJS/Runtime/RegExpObject.h>
  44. #include <LibJS/Runtime/ScriptFunction.h>
  45. #include <LibJS/Runtime/Shape.h>
  46. #include <LibJS/Runtime/StringObject.h>
  47. #include <stdio.h>
  48. namespace JS {
  49. static void update_function_name(Value& value, const FlyString& name)
  50. {
  51. if (!value.is_object())
  52. return;
  53. auto& object = value.as_object();
  54. if (object.is_function()) {
  55. auto& function = static_cast<Function&>(object);
  56. if (function.is_script_function() && function.name().is_empty())
  57. static_cast<ScriptFunction&>(function).set_name(name);
  58. } else if (object.is_array()) {
  59. auto& array = static_cast<Array&>(object);
  60. for (auto& entry : array.indexed_properties().values_unordered())
  61. update_function_name(entry.value, name);
  62. }
  63. }
  64. static String get_function_name(Interpreter& interpreter, Value value)
  65. {
  66. if (value.is_symbol())
  67. return String::format("[%s]", value.as_symbol().description().characters());
  68. if (value.is_string())
  69. return value.as_string().string();
  70. return value.to_string(interpreter);
  71. }
  72. Value ScopeNode::execute(Interpreter& interpreter, GlobalObject& global_object) const
  73. {
  74. return interpreter.run(global_object, *this);
  75. }
  76. Value FunctionDeclaration::execute(Interpreter&, GlobalObject&) const
  77. {
  78. return js_undefined();
  79. }
  80. Value FunctionExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  81. {
  82. return ScriptFunction::create(global_object, name(), body(), parameters(), function_length(), interpreter.current_environment(), m_is_arrow_function);
  83. }
  84. Value ExpressionStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  85. {
  86. return m_expression->execute(interpreter, global_object);
  87. }
  88. CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter, GlobalObject& global_object) const
  89. {
  90. if (is_new_expression()) {
  91. // Computing |this| is irrelevant for "new" expression.
  92. return { js_undefined(), m_callee->execute(interpreter, global_object) };
  93. }
  94. if (m_callee->is_super_expression()) {
  95. // If we are calling super, |this| has not been initalized yet, and would not be meaningful to provide.
  96. auto new_target = interpreter.get_new_target();
  97. ASSERT(new_target.is_function());
  98. return { js_undefined(), new_target };
  99. }
  100. if (m_callee->is_member_expression()) {
  101. auto& member_expression = static_cast<const MemberExpression&>(*m_callee);
  102. bool is_super_property_lookup = member_expression.object().is_super_expression();
  103. auto lookup_target = is_super_property_lookup ? interpreter.current_environment()->get_super_base() : member_expression.object().execute(interpreter, global_object);
  104. if (interpreter.exception())
  105. return {};
  106. if (is_super_property_lookup && (lookup_target.is_null() || lookup_target.is_undefined())) {
  107. interpreter.throw_exception<TypeError>(ErrorType::ObjectPrototypeNullOrUndefinedOnSuperPropertyAccess, lookup_target.to_string_without_side_effects().characters());
  108. return {};
  109. }
  110. auto* this_value = is_super_property_lookup ? &interpreter.this_value(global_object).as_object() : lookup_target.to_object(interpreter, global_object);
  111. if (interpreter.exception())
  112. return {};
  113. auto callee = lookup_target.to_object(interpreter, global_object)->get(member_expression.computed_property_name(interpreter, global_object)).value_or(js_undefined());
  114. return { this_value, callee };
  115. }
  116. return { &global_object, m_callee->execute(interpreter, global_object) };
  117. }
  118. Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  119. {
  120. auto [this_value, callee] = compute_this_and_callee(interpreter, global_object);
  121. if (interpreter.exception())
  122. return {};
  123. ASSERT(!callee.is_empty());
  124. if (!callee.is_function()
  125. || (is_new_expression() && (callee.as_object().is_native_function() && !static_cast<NativeFunction&>(callee.as_object()).has_constructor()))) {
  126. String error_message;
  127. auto call_type = is_new_expression() ? "constructor" : "function";
  128. if (m_callee->is_identifier() || m_callee->is_member_expression()) {
  129. String expression_string;
  130. if (m_callee->is_identifier()) {
  131. expression_string = static_cast<const Identifier&>(*m_callee).string();
  132. } else {
  133. expression_string = static_cast<const MemberExpression&>(*m_callee).to_string_approximation();
  134. }
  135. return interpreter.throw_exception<TypeError>(ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects().characters(), call_type, expression_string.characters());
  136. } else {
  137. return interpreter.throw_exception<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects().characters(), call_type);
  138. }
  139. }
  140. auto& function = callee.as_function();
  141. MarkedValueList arguments(interpreter.heap());
  142. for (size_t i = 0; i < m_arguments.size(); ++i) {
  143. auto value = m_arguments[i].value->execute(interpreter, global_object);
  144. if (interpreter.exception())
  145. return {};
  146. if (m_arguments[i].is_spread) {
  147. get_iterator_values(global_object, value, [&](Value& iterator_value) {
  148. if (interpreter.exception())
  149. return IterationDecision::Break;
  150. arguments.append(iterator_value);
  151. return IterationDecision::Continue;
  152. });
  153. if (interpreter.exception())
  154. return {};
  155. } else {
  156. arguments.append(value);
  157. }
  158. }
  159. Object* new_object = nullptr;
  160. Value result;
  161. if (is_new_expression()) {
  162. result = interpreter.construct(function, function, move(arguments), global_object);
  163. if (result.is_object())
  164. new_object = &result.as_object();
  165. } else if (m_callee->is_super_expression()) {
  166. auto* super_constructor = interpreter.current_environment()->current_function()->prototype();
  167. // FIXME: Functions should track their constructor kind.
  168. if (!super_constructor || !super_constructor->is_function())
  169. return interpreter.throw_exception<TypeError>(ErrorType::NotAConstructor, "Super constructor");
  170. result = interpreter.construct(static_cast<Function&>(*super_constructor), function, move(arguments), global_object);
  171. if (interpreter.exception())
  172. return {};
  173. interpreter.current_environment()->bind_this_value(result);
  174. } else {
  175. result = interpreter.call(function, this_value, move(arguments));
  176. }
  177. if (interpreter.exception())
  178. return {};
  179. if (is_new_expression()) {
  180. if (result.is_object())
  181. return result;
  182. return new_object;
  183. }
  184. return result;
  185. }
  186. Value ReturnStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  187. {
  188. auto value = argument() ? argument()->execute(interpreter, global_object) : js_undefined();
  189. if (interpreter.exception())
  190. return {};
  191. interpreter.unwind(ScopeType::Function);
  192. return value;
  193. }
  194. Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  195. {
  196. auto predicate_result = m_predicate->execute(interpreter, global_object);
  197. if (interpreter.exception())
  198. return {};
  199. if (predicate_result.to_boolean())
  200. return interpreter.run(global_object, *m_consequent);
  201. if (m_alternate)
  202. return interpreter.run(global_object, *m_alternate);
  203. return js_undefined();
  204. }
  205. Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  206. {
  207. Value last_value = js_undefined();
  208. while (m_test->execute(interpreter, global_object).to_boolean()) {
  209. if (interpreter.exception())
  210. return {};
  211. last_value = interpreter.run(global_object, *m_body);
  212. if (interpreter.exception())
  213. return {};
  214. }
  215. return last_value;
  216. }
  217. Value DoWhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  218. {
  219. Value last_value = js_undefined();
  220. do {
  221. if (interpreter.exception())
  222. return {};
  223. last_value = interpreter.run(global_object, *m_body);
  224. if (interpreter.exception())
  225. return {};
  226. } while (m_test->execute(interpreter, global_object).to_boolean());
  227. return last_value;
  228. }
  229. Value ForStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  230. {
  231. RefPtr<BlockStatement> wrapper;
  232. if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_kind() != DeclarationKind::Var) {
  233. wrapper = create_ast_node<BlockStatement>();
  234. NonnullRefPtrVector<VariableDeclaration> decls;
  235. decls.append(*static_cast<const VariableDeclaration*>(m_init.ptr()));
  236. wrapper->add_variables(decls);
  237. interpreter.enter_scope(*wrapper, {}, ScopeType::Block, global_object);
  238. }
  239. auto wrapper_cleanup = ScopeGuard([&] {
  240. if (wrapper)
  241. interpreter.exit_scope(*wrapper);
  242. });
  243. Value last_value = js_undefined();
  244. if (m_init) {
  245. m_init->execute(interpreter, global_object);
  246. if (interpreter.exception())
  247. return {};
  248. }
  249. if (m_test) {
  250. while (true) {
  251. auto test_result = m_test->execute(interpreter, global_object);
  252. if (interpreter.exception())
  253. return {};
  254. if (!test_result.to_boolean())
  255. break;
  256. last_value = interpreter.run(global_object, *m_body);
  257. if (interpreter.exception())
  258. return {};
  259. if (interpreter.should_unwind()) {
  260. if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
  261. interpreter.stop_unwind();
  262. } else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  263. interpreter.stop_unwind();
  264. break;
  265. } else {
  266. return js_undefined();
  267. }
  268. }
  269. if (m_update) {
  270. m_update->execute(interpreter, global_object);
  271. if (interpreter.exception())
  272. return {};
  273. }
  274. }
  275. } else {
  276. while (true) {
  277. last_value = interpreter.run(global_object, *m_body);
  278. if (interpreter.exception())
  279. return {};
  280. if (interpreter.should_unwind()) {
  281. if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
  282. interpreter.stop_unwind();
  283. } else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  284. interpreter.stop_unwind();
  285. break;
  286. } else {
  287. return js_undefined();
  288. }
  289. }
  290. if (m_update) {
  291. m_update->execute(interpreter, global_object);
  292. if (interpreter.exception())
  293. return {};
  294. }
  295. }
  296. }
  297. return last_value;
  298. }
  299. static FlyString variable_from_for_declaration(Interpreter& interpreter, GlobalObject& global_object, NonnullRefPtr<ASTNode> node, RefPtr<BlockStatement> wrapper)
  300. {
  301. FlyString variable_name;
  302. if (node->is_variable_declaration()) {
  303. auto* variable_declaration = static_cast<const VariableDeclaration*>(node.ptr());
  304. ASSERT(!variable_declaration->declarations().is_empty());
  305. if (variable_declaration->declaration_kind() != DeclarationKind::Var) {
  306. wrapper = create_ast_node<BlockStatement>();
  307. interpreter.enter_scope(*wrapper, {}, ScopeType::Block, global_object);
  308. }
  309. variable_declaration->execute(interpreter, global_object);
  310. variable_name = variable_declaration->declarations().first().id().string();
  311. } else if (node->is_identifier()) {
  312. variable_name = static_cast<const Identifier&>(*node).string();
  313. } else {
  314. ASSERT_NOT_REACHED();
  315. }
  316. return variable_name;
  317. }
  318. Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  319. {
  320. if (!m_lhs->is_variable_declaration() && !m_lhs->is_identifier()) {
  321. // FIXME: Implement "for (foo.bar in baz)", "for (foo[0] in bar)"
  322. ASSERT_NOT_REACHED();
  323. }
  324. RefPtr<BlockStatement> wrapper;
  325. auto variable_name = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  326. auto wrapper_cleanup = ScopeGuard([&] {
  327. if (wrapper)
  328. interpreter.exit_scope(*wrapper);
  329. });
  330. auto last_value = js_undefined();
  331. auto rhs_result = m_rhs->execute(interpreter, global_object);
  332. if (interpreter.exception())
  333. return {};
  334. auto* object = rhs_result.to_object(interpreter, global_object);
  335. while (object) {
  336. auto property_names = object->get_own_properties(*object, Object::PropertyKind::Key, true);
  337. for (auto& property_name : property_names.as_object().indexed_properties()) {
  338. interpreter.set_variable(variable_name, property_name.value_and_attributes(object).value, global_object);
  339. if (interpreter.exception())
  340. return {};
  341. last_value = interpreter.run(global_object, *m_body);
  342. if (interpreter.exception())
  343. return {};
  344. if (interpreter.should_unwind()) {
  345. if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
  346. interpreter.stop_unwind();
  347. } else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  348. interpreter.stop_unwind();
  349. break;
  350. } else {
  351. return js_undefined();
  352. }
  353. }
  354. }
  355. object = object->prototype();
  356. if (interpreter.exception())
  357. return {};
  358. }
  359. return last_value;
  360. }
  361. Value ForOfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  362. {
  363. if (!m_lhs->is_variable_declaration() && !m_lhs->is_identifier()) {
  364. // FIXME: Implement "for (foo.bar of baz)", "for (foo[0] of bar)"
  365. ASSERT_NOT_REACHED();
  366. }
  367. RefPtr<BlockStatement> wrapper;
  368. auto variable_name = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  369. auto wrapper_cleanup = ScopeGuard([&] {
  370. if (wrapper)
  371. interpreter.exit_scope(*wrapper);
  372. });
  373. auto last_value = js_undefined();
  374. auto rhs_result = m_rhs->execute(interpreter, global_object);
  375. if (interpreter.exception())
  376. return {};
  377. get_iterator_values(global_object, rhs_result, [&](Value& value) {
  378. interpreter.set_variable(variable_name, value, global_object);
  379. last_value = interpreter.run(global_object, *m_body);
  380. if (interpreter.exception())
  381. return IterationDecision::Break;
  382. if (interpreter.should_unwind()) {
  383. if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
  384. interpreter.stop_unwind();
  385. } else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  386. interpreter.stop_unwind();
  387. return IterationDecision::Break;
  388. } else {
  389. return IterationDecision::Break;
  390. }
  391. }
  392. return IterationDecision::Continue;
  393. });
  394. if (interpreter.exception())
  395. return {};
  396. if (interpreter.should_unwind())
  397. return js_undefined();
  398. return last_value;
  399. }
  400. Value BinaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  401. {
  402. auto lhs_result = m_lhs->execute(interpreter, global_object);
  403. if (interpreter.exception())
  404. return {};
  405. auto rhs_result = m_rhs->execute(interpreter, global_object);
  406. if (interpreter.exception())
  407. return {};
  408. switch (m_op) {
  409. case BinaryOp::Addition:
  410. return add(interpreter, lhs_result, rhs_result);
  411. case BinaryOp::Subtraction:
  412. return sub(interpreter, lhs_result, rhs_result);
  413. case BinaryOp::Multiplication:
  414. return mul(interpreter, lhs_result, rhs_result);
  415. case BinaryOp::Division:
  416. return div(interpreter, lhs_result, rhs_result);
  417. case BinaryOp::Modulo:
  418. return mod(interpreter, lhs_result, rhs_result);
  419. case BinaryOp::Exponentiation:
  420. return exp(interpreter, lhs_result, rhs_result);
  421. case BinaryOp::TypedEquals:
  422. return Value(strict_eq(interpreter, lhs_result, rhs_result));
  423. case BinaryOp::TypedInequals:
  424. return Value(!strict_eq(interpreter, lhs_result, rhs_result));
  425. case BinaryOp::AbstractEquals:
  426. return Value(abstract_eq(interpreter, lhs_result, rhs_result));
  427. case BinaryOp::AbstractInequals:
  428. return Value(!abstract_eq(interpreter, lhs_result, rhs_result));
  429. case BinaryOp::GreaterThan:
  430. return greater_than(interpreter, lhs_result, rhs_result);
  431. case BinaryOp::GreaterThanEquals:
  432. return greater_than_equals(interpreter, lhs_result, rhs_result);
  433. case BinaryOp::LessThan:
  434. return less_than(interpreter, lhs_result, rhs_result);
  435. case BinaryOp::LessThanEquals:
  436. return less_than_equals(interpreter, lhs_result, rhs_result);
  437. case BinaryOp::BitwiseAnd:
  438. return bitwise_and(interpreter, lhs_result, rhs_result);
  439. case BinaryOp::BitwiseOr:
  440. return bitwise_or(interpreter, lhs_result, rhs_result);
  441. case BinaryOp::BitwiseXor:
  442. return bitwise_xor(interpreter, lhs_result, rhs_result);
  443. case BinaryOp::LeftShift:
  444. return left_shift(interpreter, lhs_result, rhs_result);
  445. case BinaryOp::RightShift:
  446. return right_shift(interpreter, lhs_result, rhs_result);
  447. case BinaryOp::UnsignedRightShift:
  448. return unsigned_right_shift(interpreter, lhs_result, rhs_result);
  449. case BinaryOp::In:
  450. return in(interpreter, lhs_result, rhs_result);
  451. case BinaryOp::InstanceOf:
  452. return instance_of(interpreter, lhs_result, rhs_result);
  453. }
  454. ASSERT_NOT_REACHED();
  455. }
  456. Value LogicalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  457. {
  458. auto lhs_result = m_lhs->execute(interpreter, global_object);
  459. if (interpreter.exception())
  460. return {};
  461. switch (m_op) {
  462. case LogicalOp::And:
  463. if (lhs_result.to_boolean()) {
  464. auto rhs_result = m_rhs->execute(interpreter, global_object);
  465. if (interpreter.exception())
  466. return {};
  467. return rhs_result;
  468. }
  469. return lhs_result;
  470. case LogicalOp::Or: {
  471. if (lhs_result.to_boolean())
  472. return lhs_result;
  473. auto rhs_result = m_rhs->execute(interpreter, global_object);
  474. if (interpreter.exception())
  475. return {};
  476. return rhs_result;
  477. }
  478. case LogicalOp::NullishCoalescing:
  479. if (lhs_result.is_null() || lhs_result.is_undefined()) {
  480. auto rhs_result = m_rhs->execute(interpreter, global_object);
  481. if (interpreter.exception())
  482. return {};
  483. return rhs_result;
  484. }
  485. return lhs_result;
  486. }
  487. ASSERT_NOT_REACHED();
  488. }
  489. Reference Expression::to_reference(Interpreter&, GlobalObject&) const
  490. {
  491. return {};
  492. }
  493. Reference Identifier::to_reference(Interpreter& interpreter, GlobalObject&) const
  494. {
  495. return interpreter.get_reference(string());
  496. }
  497. Reference MemberExpression::to_reference(Interpreter& interpreter, GlobalObject& global_object) const
  498. {
  499. auto object_value = m_object->execute(interpreter, global_object);
  500. if (interpreter.exception())
  501. return {};
  502. auto property_name = computed_property_name(interpreter, global_object);
  503. if (!property_name.is_valid())
  504. return {};
  505. return { object_value, property_name };
  506. }
  507. Value UnaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  508. {
  509. if (m_op == UnaryOp::Delete) {
  510. auto reference = m_lhs->to_reference(interpreter, global_object);
  511. if (interpreter.exception())
  512. return {};
  513. if (reference.is_unresolvable())
  514. return Value(true);
  515. // FIXME: Support deleting locals
  516. ASSERT(!reference.is_local_variable());
  517. if (reference.is_global_variable())
  518. return global_object.delete_property(reference.name());
  519. auto* base_object = reference.base().to_object(interpreter, global_object);
  520. if (!base_object)
  521. return {};
  522. return base_object->delete_property(reference.name());
  523. }
  524. Value lhs_result;
  525. if (m_op == UnaryOp::Typeof && m_lhs->is_identifier()) {
  526. auto reference = m_lhs->to_reference(interpreter, global_object);
  527. if (interpreter.exception()) {
  528. return {};
  529. }
  530. // FIXME: standard recommends checking with is_unresolvable but it ALWAYS return false here
  531. if (reference.is_local_variable() || reference.is_global_variable()) {
  532. auto name = reference.name();
  533. lhs_result = interpreter.get_variable(name.to_string(), global_object).value_or(js_undefined());
  534. if (interpreter.exception())
  535. return {};
  536. }
  537. } else {
  538. lhs_result = m_lhs->execute(interpreter, global_object);
  539. if (interpreter.exception())
  540. return {};
  541. }
  542. switch (m_op) {
  543. case UnaryOp::BitwiseNot:
  544. return bitwise_not(interpreter, lhs_result);
  545. case UnaryOp::Not:
  546. return Value(!lhs_result.to_boolean());
  547. case UnaryOp::Plus:
  548. return unary_plus(interpreter, lhs_result);
  549. case UnaryOp::Minus:
  550. return unary_minus(interpreter, lhs_result);
  551. case UnaryOp::Typeof:
  552. switch (lhs_result.type()) {
  553. case Value::Type::Empty:
  554. ASSERT_NOT_REACHED();
  555. return {};
  556. case Value::Type::Undefined:
  557. return js_string(interpreter, "undefined");
  558. case Value::Type::Null:
  559. // yes, this is on purpose. yes, this is how javascript works.
  560. // yes, it's silly.
  561. return js_string(interpreter, "object");
  562. case Value::Type::Number:
  563. return js_string(interpreter, "number");
  564. case Value::Type::String:
  565. return js_string(interpreter, "string");
  566. case Value::Type::Object:
  567. if (lhs_result.is_function())
  568. return js_string(interpreter, "function");
  569. return js_string(interpreter, "object");
  570. case Value::Type::Boolean:
  571. return js_string(interpreter, "boolean");
  572. case Value::Type::Symbol:
  573. return js_string(interpreter, "symbol");
  574. case Value::Type::BigInt:
  575. return js_string(interpreter, "bigint");
  576. default:
  577. ASSERT_NOT_REACHED();
  578. }
  579. case UnaryOp::Void:
  580. return js_undefined();
  581. case UnaryOp::Delete:
  582. ASSERT_NOT_REACHED();
  583. }
  584. ASSERT_NOT_REACHED();
  585. }
  586. Value SuperExpression::execute(Interpreter&, GlobalObject&) const
  587. {
  588. // The semantics for SuperExpressions are handled in CallExpression::compute_this_and_callee()
  589. ASSERT_NOT_REACHED();
  590. }
  591. Value ClassMethod::execute(Interpreter& interpreter, GlobalObject& global_object) const
  592. {
  593. return m_function->execute(interpreter, global_object);
  594. }
  595. Value ClassExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  596. {
  597. Value class_constructor_value = m_constructor->execute(interpreter, global_object);
  598. if (interpreter.exception())
  599. return {};
  600. update_function_name(class_constructor_value, m_name);
  601. ASSERT(class_constructor_value.is_function() && class_constructor_value.as_function().is_script_function());
  602. ScriptFunction* class_constructor = static_cast<ScriptFunction*>(&class_constructor_value.as_function());
  603. Value super_constructor = js_undefined();
  604. if (!m_super_class.is_null()) {
  605. super_constructor = m_super_class->execute(interpreter, global_object);
  606. if (interpreter.exception())
  607. return {};
  608. if (!super_constructor.is_function() && !super_constructor.is_null())
  609. return interpreter.throw_exception<TypeError>(ErrorType::ClassDoesNotExtendAConstructorOrNull, super_constructor.to_string_without_side_effects().characters());
  610. class_constructor->set_constructor_kind(Function::ConstructorKind::Derived);
  611. Object* prototype = Object::create_empty(global_object);
  612. Object* super_constructor_prototype = nullptr;
  613. if (!super_constructor.is_null()) {
  614. super_constructor_prototype = &super_constructor.as_object().get("prototype").as_object();
  615. if (interpreter.exception())
  616. return {};
  617. }
  618. prototype->set_prototype(super_constructor_prototype);
  619. prototype->define_property("constructor", class_constructor, 0);
  620. if (interpreter.exception())
  621. return {};
  622. class_constructor->define_property("prototype", prototype, 0);
  623. if (interpreter.exception())
  624. return {};
  625. class_constructor->set_prototype(super_constructor.is_null() ? global_object.function_prototype() : &super_constructor.as_object());
  626. }
  627. auto class_prototype = class_constructor->get("prototype");
  628. if (interpreter.exception())
  629. return {};
  630. if (!class_prototype.is_object())
  631. return interpreter.throw_exception<TypeError>(ErrorType::NotAnObject, "Class prototype");
  632. for (const auto& method : m_methods) {
  633. auto method_value = method.execute(interpreter, global_object);
  634. if (interpreter.exception())
  635. return {};
  636. auto& method_function = method_value.as_function();
  637. auto key = method.key().execute(interpreter, global_object);
  638. if (interpreter.exception())
  639. return {};
  640. auto& target = method.is_static() ? *class_constructor : class_prototype.as_object();
  641. method_function.set_home_object(&target);
  642. if (method.kind() == ClassMethod::Kind::Method) {
  643. target.define_property(StringOrSymbol::from_value(interpreter, key), method_value);
  644. } else {
  645. String accessor_name = [&] {
  646. switch (method.kind()) {
  647. case ClassMethod::Kind::Getter:
  648. return String::format("get %s", get_function_name(interpreter, key).characters());
  649. case ClassMethod::Kind::Setter:
  650. return String::format("set %s", get_function_name(interpreter, key).characters());
  651. default:
  652. ASSERT_NOT_REACHED();
  653. }
  654. }();
  655. update_function_name(method_value, accessor_name);
  656. target.define_accessor(StringOrSymbol::from_value(interpreter, key), method_function, method.kind() == ClassMethod::Kind::Getter, Attribute::Configurable | Attribute::Enumerable);
  657. }
  658. if (interpreter.exception())
  659. return {};
  660. }
  661. return class_constructor;
  662. }
  663. Value ClassDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  664. {
  665. Value class_constructor = m_class_expression->execute(interpreter, global_object);
  666. if (interpreter.exception())
  667. return {};
  668. interpreter.current_environment()->set(m_class_expression->name(), { class_constructor, DeclarationKind::Let });
  669. return js_undefined();
  670. }
  671. static void print_indent(int indent)
  672. {
  673. for (int i = 0; i < indent * 2; ++i)
  674. putchar(' ');
  675. }
  676. void ASTNode::dump(int indent) const
  677. {
  678. print_indent(indent);
  679. printf("%s\n", class_name());
  680. }
  681. void ScopeNode::dump(int indent) const
  682. {
  683. ASTNode::dump(indent);
  684. if (!m_variables.is_empty()) {
  685. print_indent(indent + 1);
  686. printf("(Variables)\n");
  687. for (auto& variable : m_variables)
  688. variable.dump(indent + 2);
  689. }
  690. if (!m_children.is_empty()) {
  691. print_indent(indent + 1);
  692. printf("(Children)\n");
  693. for (auto& child : children())
  694. child.dump(indent + 2);
  695. }
  696. }
  697. void BinaryExpression::dump(int indent) const
  698. {
  699. const char* op_string = nullptr;
  700. switch (m_op) {
  701. case BinaryOp::Addition:
  702. op_string = "+";
  703. break;
  704. case BinaryOp::Subtraction:
  705. op_string = "-";
  706. break;
  707. case BinaryOp::Multiplication:
  708. op_string = "*";
  709. break;
  710. case BinaryOp::Division:
  711. op_string = "/";
  712. break;
  713. case BinaryOp::Modulo:
  714. op_string = "%";
  715. break;
  716. case BinaryOp::Exponentiation:
  717. op_string = "**";
  718. break;
  719. case BinaryOp::TypedEquals:
  720. op_string = "===";
  721. break;
  722. case BinaryOp::TypedInequals:
  723. op_string = "!==";
  724. break;
  725. case BinaryOp::AbstractEquals:
  726. op_string = "==";
  727. break;
  728. case BinaryOp::AbstractInequals:
  729. op_string = "!=";
  730. break;
  731. case BinaryOp::GreaterThan:
  732. op_string = ">";
  733. break;
  734. case BinaryOp::GreaterThanEquals:
  735. op_string = ">=";
  736. break;
  737. case BinaryOp::LessThan:
  738. op_string = "<";
  739. break;
  740. case BinaryOp::LessThanEquals:
  741. op_string = "<=";
  742. break;
  743. case BinaryOp::BitwiseAnd:
  744. op_string = "&";
  745. break;
  746. case BinaryOp::BitwiseOr:
  747. op_string = "|";
  748. break;
  749. case BinaryOp::BitwiseXor:
  750. op_string = "^";
  751. break;
  752. case BinaryOp::LeftShift:
  753. op_string = "<<";
  754. break;
  755. case BinaryOp::RightShift:
  756. op_string = ">>";
  757. break;
  758. case BinaryOp::UnsignedRightShift:
  759. op_string = ">>>";
  760. break;
  761. case BinaryOp::In:
  762. op_string = "in";
  763. break;
  764. case BinaryOp::InstanceOf:
  765. op_string = "instanceof";
  766. break;
  767. }
  768. print_indent(indent);
  769. printf("%s\n", class_name());
  770. m_lhs->dump(indent + 1);
  771. print_indent(indent + 1);
  772. printf("%s\n", op_string);
  773. m_rhs->dump(indent + 1);
  774. }
  775. void LogicalExpression::dump(int indent) const
  776. {
  777. const char* op_string = nullptr;
  778. switch (m_op) {
  779. case LogicalOp::And:
  780. op_string = "&&";
  781. break;
  782. case LogicalOp::Or:
  783. op_string = "||";
  784. break;
  785. case LogicalOp::NullishCoalescing:
  786. op_string = "??";
  787. break;
  788. }
  789. print_indent(indent);
  790. printf("%s\n", class_name());
  791. m_lhs->dump(indent + 1);
  792. print_indent(indent + 1);
  793. printf("%s\n", op_string);
  794. m_rhs->dump(indent + 1);
  795. }
  796. void UnaryExpression::dump(int indent) const
  797. {
  798. const char* op_string = nullptr;
  799. switch (m_op) {
  800. case UnaryOp::BitwiseNot:
  801. op_string = "~";
  802. break;
  803. case UnaryOp::Not:
  804. op_string = "!";
  805. break;
  806. case UnaryOp::Plus:
  807. op_string = "+";
  808. break;
  809. case UnaryOp::Minus:
  810. op_string = "-";
  811. break;
  812. case UnaryOp::Typeof:
  813. op_string = "typeof ";
  814. break;
  815. case UnaryOp::Void:
  816. op_string = "void ";
  817. break;
  818. case UnaryOp::Delete:
  819. op_string = "delete ";
  820. break;
  821. }
  822. print_indent(indent);
  823. printf("%s\n", class_name());
  824. print_indent(indent + 1);
  825. printf("%s\n", op_string);
  826. m_lhs->dump(indent + 1);
  827. }
  828. void CallExpression::dump(int indent) const
  829. {
  830. print_indent(indent);
  831. printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
  832. m_callee->dump(indent + 1);
  833. for (auto& argument : m_arguments)
  834. argument.value->dump(indent + 1);
  835. }
  836. void ClassDeclaration::dump(int indent) const
  837. {
  838. ASTNode::dump(indent);
  839. m_class_expression->dump(indent + 1);
  840. }
  841. void ClassExpression::dump(int indent) const
  842. {
  843. print_indent(indent);
  844. printf("ClassExpression: \"%s\"\n", m_name.characters());
  845. print_indent(indent);
  846. printf("(Constructor)\n");
  847. m_constructor->dump(indent + 1);
  848. if (!m_super_class.is_null()) {
  849. print_indent(indent);
  850. printf("(Super Class)\n");
  851. m_super_class->dump(indent + 1);
  852. }
  853. print_indent(indent);
  854. printf("(Methods)\n");
  855. for (auto& method : m_methods)
  856. method.dump(indent + 1);
  857. }
  858. void ClassMethod::dump(int indent) const
  859. {
  860. ASTNode::dump(indent);
  861. print_indent(indent);
  862. printf("(Key)\n");
  863. m_key->dump(indent + 1);
  864. const char* kind_string = nullptr;
  865. switch (m_kind) {
  866. case Kind::Method:
  867. kind_string = "Method";
  868. break;
  869. case Kind::Getter:
  870. kind_string = "Getter";
  871. break;
  872. case Kind::Setter:
  873. kind_string = "Setter";
  874. break;
  875. }
  876. print_indent(indent);
  877. printf("Kind: %s\n", kind_string);
  878. print_indent(indent);
  879. printf("Static: %s\n", m_is_static ? "true" : "false");
  880. print_indent(indent);
  881. printf("(Function)\n");
  882. m_function->dump(indent + 1);
  883. }
  884. void StringLiteral::dump(int indent) const
  885. {
  886. print_indent(indent);
  887. printf("StringLiteral \"%s\"\n", m_value.characters());
  888. }
  889. void SuperExpression::dump(int indent) const
  890. {
  891. print_indent(indent);
  892. printf("super\n");
  893. }
  894. void NumericLiteral::dump(int indent) const
  895. {
  896. print_indent(indent);
  897. printf("NumericLiteral %g\n", m_value);
  898. }
  899. void BigIntLiteral::dump(int indent) const
  900. {
  901. print_indent(indent);
  902. printf("BigIntLiteral %s\n", m_value.characters());
  903. }
  904. void BooleanLiteral::dump(int indent) const
  905. {
  906. print_indent(indent);
  907. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  908. }
  909. void NullLiteral::dump(int indent) const
  910. {
  911. print_indent(indent);
  912. printf("null\n");
  913. }
  914. void FunctionNode::dump(int indent, const char* class_name) const
  915. {
  916. print_indent(indent);
  917. printf("%s '%s'\n", class_name, name().characters());
  918. if (!m_parameters.is_empty()) {
  919. print_indent(indent + 1);
  920. printf("(Parameters)\n");
  921. for (auto& parameter : m_parameters) {
  922. print_indent(indent + 2);
  923. if (parameter.is_rest)
  924. printf("...");
  925. printf("%s\n", parameter.name.characters());
  926. if (parameter.default_value)
  927. parameter.default_value->dump(indent + 3);
  928. }
  929. }
  930. if (!m_variables.is_empty()) {
  931. print_indent(indent + 1);
  932. printf("(Variables)\n");
  933. for (auto& variable : m_variables)
  934. variable.dump(indent + 2);
  935. }
  936. print_indent(indent + 1);
  937. printf("(Body)\n");
  938. body().dump(indent + 2);
  939. }
  940. void FunctionDeclaration::dump(int indent) const
  941. {
  942. FunctionNode::dump(indent, class_name());
  943. }
  944. void FunctionExpression::dump(int indent) const
  945. {
  946. FunctionNode::dump(indent, class_name());
  947. }
  948. void ReturnStatement::dump(int indent) const
  949. {
  950. ASTNode::dump(indent);
  951. if (argument())
  952. argument()->dump(indent + 1);
  953. }
  954. void IfStatement::dump(int indent) const
  955. {
  956. ASTNode::dump(indent);
  957. print_indent(indent);
  958. printf("If\n");
  959. predicate().dump(indent + 1);
  960. consequent().dump(indent + 1);
  961. if (alternate()) {
  962. print_indent(indent);
  963. printf("Else\n");
  964. alternate()->dump(indent + 1);
  965. }
  966. }
  967. void WhileStatement::dump(int indent) const
  968. {
  969. ASTNode::dump(indent);
  970. print_indent(indent);
  971. printf("While\n");
  972. test().dump(indent + 1);
  973. body().dump(indent + 1);
  974. }
  975. void DoWhileStatement::dump(int indent) const
  976. {
  977. ASTNode::dump(indent);
  978. print_indent(indent);
  979. printf("DoWhile\n");
  980. test().dump(indent + 1);
  981. body().dump(indent + 1);
  982. }
  983. void ForStatement::dump(int indent) const
  984. {
  985. ASTNode::dump(indent);
  986. print_indent(indent);
  987. printf("For\n");
  988. if (init())
  989. init()->dump(indent + 1);
  990. if (test())
  991. test()->dump(indent + 1);
  992. if (update())
  993. update()->dump(indent + 1);
  994. body().dump(indent + 1);
  995. }
  996. void ForInStatement::dump(int indent) const
  997. {
  998. ASTNode::dump(indent);
  999. print_indent(indent);
  1000. printf("ForIn\n");
  1001. lhs().dump(indent + 1);
  1002. rhs().dump(indent + 1);
  1003. body().dump(indent + 1);
  1004. }
  1005. void ForOfStatement::dump(int indent) const
  1006. {
  1007. ASTNode::dump(indent);
  1008. print_indent(indent);
  1009. printf("ForOf\n");
  1010. lhs().dump(indent + 1);
  1011. rhs().dump(indent + 1);
  1012. body().dump(indent + 1);
  1013. }
  1014. Value Identifier::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1015. {
  1016. auto value = interpreter.get_variable(string(), global_object);
  1017. if (value.is_empty())
  1018. return interpreter.throw_exception<ReferenceError>(ErrorType::UnknownIdentifier, string().characters());
  1019. return value;
  1020. }
  1021. void Identifier::dump(int indent) const
  1022. {
  1023. print_indent(indent);
  1024. printf("Identifier \"%s\"\n", m_string.characters());
  1025. }
  1026. void SpreadExpression::dump(int indent) const
  1027. {
  1028. ASTNode::dump(indent);
  1029. m_target->dump(indent + 1);
  1030. }
  1031. Value SpreadExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1032. {
  1033. return m_target->execute(interpreter, global_object);
  1034. }
  1035. Value ThisExpression::execute(Interpreter& interpreter, GlobalObject&) const
  1036. {
  1037. return interpreter.resolve_this_binding();
  1038. }
  1039. void ThisExpression::dump(int indent) const
  1040. {
  1041. ASTNode::dump(indent);
  1042. }
  1043. Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1044. {
  1045. auto rhs_result = m_rhs->execute(interpreter, global_object);
  1046. if (interpreter.exception())
  1047. return {};
  1048. Value lhs_result;
  1049. switch (m_op) {
  1050. case AssignmentOp::Assignment:
  1051. break;
  1052. case AssignmentOp::AdditionAssignment:
  1053. lhs_result = m_lhs->execute(interpreter, global_object);
  1054. if (interpreter.exception())
  1055. return {};
  1056. rhs_result = add(interpreter, lhs_result, rhs_result);
  1057. break;
  1058. case AssignmentOp::SubtractionAssignment:
  1059. lhs_result = m_lhs->execute(interpreter, global_object);
  1060. if (interpreter.exception())
  1061. return {};
  1062. rhs_result = sub(interpreter, lhs_result, rhs_result);
  1063. break;
  1064. case AssignmentOp::MultiplicationAssignment:
  1065. lhs_result = m_lhs->execute(interpreter, global_object);
  1066. if (interpreter.exception())
  1067. return {};
  1068. rhs_result = mul(interpreter, lhs_result, rhs_result);
  1069. break;
  1070. case AssignmentOp::DivisionAssignment:
  1071. lhs_result = m_lhs->execute(interpreter, global_object);
  1072. if (interpreter.exception())
  1073. return {};
  1074. rhs_result = div(interpreter, lhs_result, rhs_result);
  1075. break;
  1076. case AssignmentOp::ModuloAssignment:
  1077. lhs_result = m_lhs->execute(interpreter, global_object);
  1078. if (interpreter.exception())
  1079. return {};
  1080. rhs_result = mod(interpreter, lhs_result, rhs_result);
  1081. break;
  1082. case AssignmentOp::ExponentiationAssignment:
  1083. lhs_result = m_lhs->execute(interpreter, global_object);
  1084. if (interpreter.exception())
  1085. return {};
  1086. rhs_result = exp(interpreter, lhs_result, rhs_result);
  1087. break;
  1088. case AssignmentOp::BitwiseAndAssignment:
  1089. lhs_result = m_lhs->execute(interpreter, global_object);
  1090. if (interpreter.exception())
  1091. return {};
  1092. rhs_result = bitwise_and(interpreter, lhs_result, rhs_result);
  1093. break;
  1094. case AssignmentOp::BitwiseOrAssignment:
  1095. lhs_result = m_lhs->execute(interpreter, global_object);
  1096. if (interpreter.exception())
  1097. return {};
  1098. rhs_result = bitwise_or(interpreter, lhs_result, rhs_result);
  1099. break;
  1100. case AssignmentOp::BitwiseXorAssignment:
  1101. lhs_result = m_lhs->execute(interpreter, global_object);
  1102. if (interpreter.exception())
  1103. return {};
  1104. rhs_result = bitwise_xor(interpreter, lhs_result, rhs_result);
  1105. break;
  1106. case AssignmentOp::LeftShiftAssignment:
  1107. lhs_result = m_lhs->execute(interpreter, global_object);
  1108. if (interpreter.exception())
  1109. return {};
  1110. rhs_result = left_shift(interpreter, lhs_result, rhs_result);
  1111. break;
  1112. case AssignmentOp::RightShiftAssignment:
  1113. lhs_result = m_lhs->execute(interpreter, global_object);
  1114. if (interpreter.exception())
  1115. return {};
  1116. rhs_result = right_shift(interpreter, lhs_result, rhs_result);
  1117. break;
  1118. case AssignmentOp::UnsignedRightShiftAssignment:
  1119. lhs_result = m_lhs->execute(interpreter, global_object);
  1120. if (interpreter.exception())
  1121. return {};
  1122. rhs_result = unsigned_right_shift(interpreter, lhs_result, rhs_result);
  1123. break;
  1124. }
  1125. if (interpreter.exception())
  1126. return {};
  1127. auto reference = m_lhs->to_reference(interpreter, global_object);
  1128. if (interpreter.exception())
  1129. return {};
  1130. if (reference.is_unresolvable())
  1131. return interpreter.throw_exception<ReferenceError>(ErrorType::InvalidLeftHandAssignment);
  1132. update_function_name(rhs_result, get_function_name(interpreter, reference.name().to_value(interpreter)));
  1133. reference.put(interpreter, global_object, rhs_result);
  1134. if (interpreter.exception())
  1135. return {};
  1136. return rhs_result;
  1137. }
  1138. Value UpdateExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1139. {
  1140. auto reference = m_argument->to_reference(interpreter, global_object);
  1141. if (interpreter.exception())
  1142. return {};
  1143. auto old_value = reference.get(interpreter, global_object);
  1144. if (interpreter.exception())
  1145. return {};
  1146. old_value = old_value.to_numeric(interpreter);
  1147. if (interpreter.exception())
  1148. return {};
  1149. Value new_value;
  1150. switch (m_op) {
  1151. case UpdateOp::Increment:
  1152. if (old_value.is_number())
  1153. new_value = Value(old_value.as_double() + 1);
  1154. else
  1155. new_value = js_bigint(interpreter, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  1156. break;
  1157. case UpdateOp::Decrement:
  1158. if (old_value.is_number())
  1159. new_value = Value(old_value.as_double() - 1);
  1160. else
  1161. new_value = js_bigint(interpreter, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  1162. break;
  1163. default:
  1164. ASSERT_NOT_REACHED();
  1165. }
  1166. reference.put(interpreter, global_object, new_value);
  1167. if (interpreter.exception())
  1168. return {};
  1169. return m_prefixed ? new_value : old_value;
  1170. }
  1171. void AssignmentExpression::dump(int indent) const
  1172. {
  1173. const char* op_string = nullptr;
  1174. switch (m_op) {
  1175. case AssignmentOp::Assignment:
  1176. op_string = "=";
  1177. break;
  1178. case AssignmentOp::AdditionAssignment:
  1179. op_string = "+=";
  1180. break;
  1181. case AssignmentOp::SubtractionAssignment:
  1182. op_string = "-=";
  1183. break;
  1184. case AssignmentOp::MultiplicationAssignment:
  1185. op_string = "*=";
  1186. break;
  1187. case AssignmentOp::DivisionAssignment:
  1188. op_string = "/=";
  1189. break;
  1190. case AssignmentOp::ModuloAssignment:
  1191. op_string = "%=";
  1192. break;
  1193. case AssignmentOp::ExponentiationAssignment:
  1194. op_string = "**=";
  1195. break;
  1196. case AssignmentOp::BitwiseAndAssignment:
  1197. op_string = "&=";
  1198. break;
  1199. case AssignmentOp::BitwiseOrAssignment:
  1200. op_string = "|=";
  1201. break;
  1202. case AssignmentOp::BitwiseXorAssignment:
  1203. op_string = "^=";
  1204. break;
  1205. case AssignmentOp::LeftShiftAssignment:
  1206. op_string = "<<=";
  1207. break;
  1208. case AssignmentOp::RightShiftAssignment:
  1209. op_string = ">>=";
  1210. break;
  1211. case AssignmentOp::UnsignedRightShiftAssignment:
  1212. op_string = ">>>=";
  1213. break;
  1214. }
  1215. ASTNode::dump(indent);
  1216. print_indent(indent + 1);
  1217. printf("%s\n", op_string);
  1218. m_lhs->dump(indent + 1);
  1219. m_rhs->dump(indent + 1);
  1220. }
  1221. void UpdateExpression::dump(int indent) const
  1222. {
  1223. const char* op_string = nullptr;
  1224. switch (m_op) {
  1225. case UpdateOp::Increment:
  1226. op_string = "++";
  1227. break;
  1228. case UpdateOp::Decrement:
  1229. op_string = "--";
  1230. break;
  1231. }
  1232. ASTNode::dump(indent);
  1233. print_indent(indent + 1);
  1234. if (m_prefixed)
  1235. printf("%s\n", op_string);
  1236. m_argument->dump(indent + 1);
  1237. if (!m_prefixed) {
  1238. print_indent(indent + 1);
  1239. printf("%s\n", op_string);
  1240. }
  1241. }
  1242. Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1243. {
  1244. for (auto& declarator : m_declarations) {
  1245. if (auto* init = declarator.init()) {
  1246. auto initalizer_result = init->execute(interpreter, global_object);
  1247. if (interpreter.exception())
  1248. return {};
  1249. auto variable_name = declarator.id().string();
  1250. update_function_name(initalizer_result, variable_name);
  1251. interpreter.set_variable(variable_name, initalizer_result, global_object, true);
  1252. }
  1253. }
  1254. return js_undefined();
  1255. }
  1256. Value VariableDeclarator::execute(Interpreter&, GlobalObject&) const
  1257. {
  1258. // NOTE: This node is handled by VariableDeclaration.
  1259. ASSERT_NOT_REACHED();
  1260. }
  1261. void VariableDeclaration::dump(int indent) const
  1262. {
  1263. const char* declaration_kind_string = nullptr;
  1264. switch (m_declaration_kind) {
  1265. case DeclarationKind::Let:
  1266. declaration_kind_string = "Let";
  1267. break;
  1268. case DeclarationKind::Var:
  1269. declaration_kind_string = "Var";
  1270. break;
  1271. case DeclarationKind::Const:
  1272. declaration_kind_string = "Const";
  1273. break;
  1274. }
  1275. ASTNode::dump(indent);
  1276. print_indent(indent + 1);
  1277. printf("%s\n", declaration_kind_string);
  1278. for (auto& declarator : m_declarations)
  1279. declarator.dump(indent + 1);
  1280. }
  1281. void VariableDeclarator::dump(int indent) const
  1282. {
  1283. ASTNode::dump(indent);
  1284. m_id->dump(indent + 1);
  1285. if (m_init)
  1286. m_init->dump(indent + 1);
  1287. }
  1288. void ObjectProperty::dump(int indent) const
  1289. {
  1290. ASTNode::dump(indent);
  1291. m_key->dump(indent + 1);
  1292. m_value->dump(indent + 1);
  1293. }
  1294. void ObjectExpression::dump(int indent) const
  1295. {
  1296. ASTNode::dump(indent);
  1297. for (auto& property : m_properties) {
  1298. property.dump(indent + 1);
  1299. }
  1300. }
  1301. void ExpressionStatement::dump(int indent) const
  1302. {
  1303. ASTNode::dump(indent);
  1304. m_expression->dump(indent + 1);
  1305. }
  1306. Value ObjectProperty::execute(Interpreter&, GlobalObject&) const
  1307. {
  1308. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  1309. ASSERT_NOT_REACHED();
  1310. }
  1311. Value ObjectExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1312. {
  1313. auto* object = Object::create_empty(global_object);
  1314. for (auto& property : m_properties) {
  1315. auto key = property.key().execute(interpreter, global_object);
  1316. if (interpreter.exception())
  1317. return {};
  1318. if (property.type() == ObjectProperty::Type::Spread) {
  1319. if (key.is_array()) {
  1320. auto& array_to_spread = static_cast<Array&>(key.as_object());
  1321. for (auto& entry : array_to_spread.indexed_properties()) {
  1322. object->indexed_properties().append(entry.value_and_attributes(&array_to_spread).value);
  1323. if (interpreter.exception())
  1324. return {};
  1325. }
  1326. } else if (key.is_object()) {
  1327. auto& obj_to_spread = key.as_object();
  1328. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  1329. if (it.value.attributes.is_enumerable()) {
  1330. object->define_property(it.key, obj_to_spread.get(it.key));
  1331. if (interpreter.exception())
  1332. return {};
  1333. }
  1334. }
  1335. } else if (key.is_string()) {
  1336. auto& str_to_spread = key.as_string().string();
  1337. for (size_t i = 0; i < str_to_spread.length(); i++) {
  1338. object->define_property(i, js_string(interpreter, str_to_spread.substring(i, 1)));
  1339. if (interpreter.exception())
  1340. return {};
  1341. }
  1342. }
  1343. continue;
  1344. }
  1345. if (interpreter.exception())
  1346. return {};
  1347. auto value = property.value().execute(interpreter, global_object);
  1348. if (interpreter.exception())
  1349. return {};
  1350. if (value.is_function() && property.is_method())
  1351. value.as_function().set_home_object(object);
  1352. String name = get_function_name(interpreter, key);
  1353. if (property.type() == ObjectProperty::Type::Getter) {
  1354. name = String::format("get %s", name.characters());
  1355. } else if (property.type() == ObjectProperty::Type::Setter) {
  1356. name = String::format("set %s", name.characters());
  1357. }
  1358. update_function_name(value, name);
  1359. if (property.type() == ObjectProperty::Type::Getter || property.type() == ObjectProperty::Type::Setter) {
  1360. ASSERT(value.is_function());
  1361. object->define_accessor(PropertyName::from_value(interpreter, key), value.as_function(), property.type() == ObjectProperty::Type::Getter, Attribute::Configurable | Attribute::Enumerable);
  1362. if (interpreter.exception())
  1363. return {};
  1364. } else {
  1365. object->define_property(PropertyName::from_value(interpreter, key), value);
  1366. if (interpreter.exception())
  1367. return {};
  1368. }
  1369. }
  1370. return object;
  1371. }
  1372. void MemberExpression::dump(int indent) const
  1373. {
  1374. print_indent(indent);
  1375. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  1376. m_object->dump(indent + 1);
  1377. m_property->dump(indent + 1);
  1378. }
  1379. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter, GlobalObject& global_object) const
  1380. {
  1381. if (!is_computed()) {
  1382. ASSERT(m_property->is_identifier());
  1383. return static_cast<const Identifier&>(*m_property).string();
  1384. }
  1385. auto index = m_property->execute(interpreter, global_object);
  1386. if (interpreter.exception())
  1387. return {};
  1388. ASSERT(!index.is_empty());
  1389. if (index.is_integer() && index.as_i32() >= 0)
  1390. return index.as_i32();
  1391. if (index.is_symbol())
  1392. return &index.as_symbol();
  1393. auto index_string = index.to_string(interpreter);
  1394. if (interpreter.exception())
  1395. return {};
  1396. return index_string;
  1397. }
  1398. String MemberExpression::to_string_approximation() const
  1399. {
  1400. String object_string = "<object>";
  1401. if (m_object->is_identifier())
  1402. object_string = static_cast<const Identifier&>(*m_object).string();
  1403. if (is_computed())
  1404. return String::format("%s[<computed>]", object_string.characters());
  1405. ASSERT(m_property->is_identifier());
  1406. return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
  1407. }
  1408. Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1409. {
  1410. auto object_value = m_object->execute(interpreter, global_object);
  1411. if (interpreter.exception())
  1412. return {};
  1413. auto* object_result = object_value.to_object(interpreter, global_object);
  1414. if (interpreter.exception())
  1415. return {};
  1416. return object_result->get(computed_property_name(interpreter, global_object)).value_or(js_undefined());
  1417. }
  1418. Value StringLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1419. {
  1420. return js_string(interpreter, m_value);
  1421. }
  1422. Value NumericLiteral::execute(Interpreter&, GlobalObject&) const
  1423. {
  1424. return Value(m_value);
  1425. }
  1426. Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1427. {
  1428. return js_bigint(interpreter, Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
  1429. }
  1430. Value BooleanLiteral::execute(Interpreter&, GlobalObject&) const
  1431. {
  1432. return Value(m_value);
  1433. }
  1434. Value NullLiteral::execute(Interpreter&, GlobalObject&) const
  1435. {
  1436. return js_null();
  1437. }
  1438. void RegExpLiteral::dump(int indent) const
  1439. {
  1440. print_indent(indent);
  1441. printf("%s (/%s/%s)\n", class_name(), content().characters(), flags().characters());
  1442. }
  1443. Value RegExpLiteral::execute(Interpreter&, GlobalObject& global_object) const
  1444. {
  1445. return RegExpObject::create(global_object, content(), flags());
  1446. }
  1447. void ArrayExpression::dump(int indent) const
  1448. {
  1449. ASTNode::dump(indent);
  1450. for (auto& element : m_elements) {
  1451. if (element) {
  1452. element->dump(indent + 1);
  1453. } else {
  1454. print_indent(indent + 1);
  1455. printf("<empty>\n");
  1456. }
  1457. }
  1458. }
  1459. Value ArrayExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1460. {
  1461. auto* array = Array::create(global_object);
  1462. for (auto& element : m_elements) {
  1463. auto value = Value();
  1464. if (element) {
  1465. value = element->execute(interpreter, global_object);
  1466. if (interpreter.exception())
  1467. return {};
  1468. if (element->is_spread_expression()) {
  1469. get_iterator_values(global_object, value, [&](Value& iterator_value) {
  1470. array->indexed_properties().append(iterator_value);
  1471. return IterationDecision::Continue;
  1472. });
  1473. if (interpreter.exception())
  1474. return {};
  1475. continue;
  1476. }
  1477. }
  1478. array->indexed_properties().append(value);
  1479. }
  1480. return array;
  1481. }
  1482. void TemplateLiteral::dump(int indent) const
  1483. {
  1484. ASTNode::dump(indent);
  1485. for (auto& expression : m_expressions)
  1486. expression.dump(indent + 1);
  1487. }
  1488. Value TemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1489. {
  1490. StringBuilder string_builder;
  1491. for (auto& expression : m_expressions) {
  1492. auto expr = expression.execute(interpreter, global_object);
  1493. if (interpreter.exception())
  1494. return {};
  1495. auto string = expr.to_string(interpreter);
  1496. if (interpreter.exception())
  1497. return {};
  1498. string_builder.append(string);
  1499. }
  1500. return js_string(interpreter, string_builder.build());
  1501. }
  1502. void TaggedTemplateLiteral::dump(int indent) const
  1503. {
  1504. ASTNode::dump(indent);
  1505. print_indent(indent + 1);
  1506. printf("(Tag)\n");
  1507. m_tag->dump(indent + 2);
  1508. print_indent(indent + 1);
  1509. printf("(Template Literal)\n");
  1510. m_template_literal->dump(indent + 2);
  1511. }
  1512. Value TaggedTemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1513. {
  1514. auto tag = m_tag->execute(interpreter, global_object);
  1515. if (interpreter.exception())
  1516. return {};
  1517. if (!tag.is_function()) {
  1518. interpreter.throw_exception<TypeError>(ErrorType::NotAFunction, tag.to_string_without_side_effects().characters());
  1519. return {};
  1520. }
  1521. auto& tag_function = tag.as_function();
  1522. auto& expressions = m_template_literal->expressions();
  1523. auto* strings = Array::create(global_object);
  1524. MarkedValueList arguments(interpreter.heap());
  1525. arguments.append(strings);
  1526. for (size_t i = 0; i < expressions.size(); ++i) {
  1527. auto value = expressions[i].execute(interpreter, global_object);
  1528. if (interpreter.exception())
  1529. return {};
  1530. // tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
  1531. // tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
  1532. if (i % 2 == 0) {
  1533. strings->indexed_properties().append(value);
  1534. } else {
  1535. arguments.append(value);
  1536. }
  1537. }
  1538. auto* raw_strings = Array::create(global_object);
  1539. for (auto& raw_string : m_template_literal->raw_strings()) {
  1540. auto value = raw_string.execute(interpreter, global_object);
  1541. if (interpreter.exception())
  1542. return {};
  1543. raw_strings->indexed_properties().append(value);
  1544. }
  1545. strings->define_property("raw", raw_strings, 0);
  1546. return interpreter.call(tag_function, js_undefined(), move(arguments));
  1547. }
  1548. void TryStatement::dump(int indent) const
  1549. {
  1550. ASTNode::dump(indent);
  1551. print_indent(indent);
  1552. printf("(Block)\n");
  1553. block().dump(indent + 1);
  1554. if (handler()) {
  1555. print_indent(indent);
  1556. printf("(Handler)\n");
  1557. handler()->dump(indent + 1);
  1558. }
  1559. if (finalizer()) {
  1560. print_indent(indent);
  1561. printf("(Finalizer)\n");
  1562. finalizer()->dump(indent + 1);
  1563. }
  1564. }
  1565. void CatchClause::dump(int indent) const
  1566. {
  1567. print_indent(indent);
  1568. printf("CatchClause");
  1569. if (!m_parameter.is_null())
  1570. printf(" (%s)", m_parameter.characters());
  1571. printf("\n");
  1572. body().dump(indent + 1);
  1573. }
  1574. void ThrowStatement::dump(int indent) const
  1575. {
  1576. ASTNode::dump(indent);
  1577. argument().dump(indent + 1);
  1578. }
  1579. Value TryStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1580. {
  1581. interpreter.run(global_object, block(), {}, ScopeType::Try);
  1582. if (auto* exception = interpreter.exception()) {
  1583. if (m_handler) {
  1584. interpreter.clear_exception();
  1585. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  1586. interpreter.run(global_object, m_handler->body(), move(arguments));
  1587. }
  1588. }
  1589. if (m_finalizer)
  1590. m_finalizer->execute(interpreter, global_object);
  1591. return js_undefined();
  1592. }
  1593. Value CatchClause::execute(Interpreter&, GlobalObject&) const
  1594. {
  1595. // NOTE: CatchClause execution is handled by TryStatement.
  1596. ASSERT_NOT_REACHED();
  1597. return {};
  1598. }
  1599. Value ThrowStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1600. {
  1601. auto value = m_argument->execute(interpreter, global_object);
  1602. if (interpreter.exception())
  1603. return {};
  1604. return interpreter.throw_exception(value);
  1605. }
  1606. Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1607. {
  1608. auto discriminant_result = m_discriminant->execute(interpreter, global_object);
  1609. if (interpreter.exception())
  1610. return {};
  1611. bool falling_through = false;
  1612. for (auto& switch_case : m_cases) {
  1613. if (!falling_through && switch_case.test()) {
  1614. auto test_result = switch_case.test()->execute(interpreter, global_object);
  1615. if (interpreter.exception())
  1616. return {};
  1617. if (!strict_eq(interpreter, discriminant_result, test_result))
  1618. continue;
  1619. }
  1620. falling_through = true;
  1621. for (auto& statement : switch_case.consequent()) {
  1622. statement.execute(interpreter, global_object);
  1623. if (interpreter.exception())
  1624. return {};
  1625. if (interpreter.should_unwind()) {
  1626. if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  1627. interpreter.stop_unwind();
  1628. return {};
  1629. }
  1630. return {};
  1631. }
  1632. }
  1633. }
  1634. return js_undefined();
  1635. }
  1636. Value SwitchCase::execute(Interpreter&, GlobalObject&) const
  1637. {
  1638. return {};
  1639. }
  1640. Value BreakStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1641. {
  1642. interpreter.unwind(ScopeType::Breakable, m_target_label);
  1643. return js_undefined();
  1644. }
  1645. Value ContinueStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1646. {
  1647. interpreter.unwind(ScopeType::Continuable, m_target_label);
  1648. return js_undefined();
  1649. }
  1650. void SwitchStatement::dump(int indent) const
  1651. {
  1652. ASTNode::dump(indent);
  1653. m_discriminant->dump(indent + 1);
  1654. for (auto& switch_case : m_cases) {
  1655. switch_case.dump(indent + 1);
  1656. }
  1657. }
  1658. void SwitchCase::dump(int indent) const
  1659. {
  1660. ASTNode::dump(indent);
  1661. print_indent(indent + 1);
  1662. if (m_test) {
  1663. printf("(Test)\n");
  1664. m_test->dump(indent + 2);
  1665. } else {
  1666. printf("(Default)\n");
  1667. }
  1668. print_indent(indent + 1);
  1669. printf("(Consequent)\n");
  1670. for (auto& statement : m_consequent)
  1671. statement.dump(indent + 2);
  1672. }
  1673. Value ConditionalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1674. {
  1675. auto test_result = m_test->execute(interpreter, global_object);
  1676. if (interpreter.exception())
  1677. return {};
  1678. Value result;
  1679. if (test_result.to_boolean()) {
  1680. result = m_consequent->execute(interpreter, global_object);
  1681. } else {
  1682. result = m_alternate->execute(interpreter, global_object);
  1683. }
  1684. if (interpreter.exception())
  1685. return {};
  1686. return result;
  1687. }
  1688. void ConditionalExpression::dump(int indent) const
  1689. {
  1690. ASTNode::dump(indent);
  1691. print_indent(indent + 1);
  1692. printf("(Test)\n");
  1693. m_test->dump(indent + 2);
  1694. print_indent(indent + 1);
  1695. printf("(Consequent)\n");
  1696. m_consequent->dump(indent + 2);
  1697. print_indent(indent + 1);
  1698. printf("(Alternate)\n");
  1699. m_alternate->dump(indent + 2);
  1700. }
  1701. void SequenceExpression::dump(int indent) const
  1702. {
  1703. ASTNode::dump(indent);
  1704. for (auto& expression : m_expressions)
  1705. expression.dump(indent + 1);
  1706. }
  1707. Value SequenceExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1708. {
  1709. Value last_value;
  1710. for (auto& expression : m_expressions) {
  1711. last_value = expression.execute(interpreter, global_object);
  1712. if (interpreter.exception())
  1713. return {};
  1714. }
  1715. return last_value;
  1716. }
  1717. Value DebuggerStatement::execute(Interpreter&, GlobalObject&) const
  1718. {
  1719. // Sorry, no JavaScript debugger available (yet)!
  1720. return js_undefined();
  1721. }
  1722. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1723. {
  1724. m_variables.append(move(variables));
  1725. }
  1726. void ScopeNode::add_functions(NonnullRefPtrVector<FunctionDeclaration> functions)
  1727. {
  1728. m_functions.append(move(functions));
  1729. }
  1730. }