AST.cpp 62 KB

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