AST.cpp 73 KB

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