AST.cpp 65 KB

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