AST.cpp 66 KB

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