AST.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255
  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::ClassDoesNotExtendAConstructorOrNull, 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. super_constructor_prototype = &super_constructor.as_object().get(vm.names.prototype).as_object();
  731. if (interpreter.exception())
  732. return {};
  733. }
  734. prototype->set_prototype(super_constructor_prototype);
  735. prototype->define_property(vm.names.constructor, class_constructor, 0);
  736. if (interpreter.exception())
  737. return {};
  738. class_constructor->define_property(vm.names.prototype, prototype, Attribute::Writable);
  739. if (interpreter.exception())
  740. return {};
  741. class_constructor->set_prototype(super_constructor.is_null() ? global_object.function_prototype() : &super_constructor.as_object());
  742. }
  743. auto class_prototype = class_constructor->get(vm.names.prototype);
  744. if (interpreter.exception())
  745. return {};
  746. if (!class_prototype.is_object()) {
  747. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Class prototype");
  748. return {};
  749. }
  750. for (const auto& method : m_methods) {
  751. auto method_value = method.execute(interpreter, global_object);
  752. if (interpreter.exception())
  753. return {};
  754. auto& method_function = method_value.as_function();
  755. auto key = method.key().execute(interpreter, global_object);
  756. if (interpreter.exception())
  757. return {};
  758. auto& target = method.is_static() ? *class_constructor : class_prototype.as_object();
  759. method_function.set_home_object(&target);
  760. if (method.kind() == ClassMethod::Kind::Method) {
  761. target.define_property(StringOrSymbol::from_value(global_object, key), method_value);
  762. } else {
  763. String accessor_name = [&] {
  764. switch (method.kind()) {
  765. case ClassMethod::Kind::Getter:
  766. return String::formatted("get {}", get_function_name(global_object, key));
  767. case ClassMethod::Kind::Setter:
  768. return String::formatted("set {}", get_function_name(global_object, key));
  769. default:
  770. ASSERT_NOT_REACHED();
  771. }
  772. }();
  773. update_function_name(method_value, accessor_name);
  774. target.define_accessor(StringOrSymbol::from_value(global_object, key), method_function, method.kind() == ClassMethod::Kind::Getter, Attribute::Configurable | Attribute::Enumerable);
  775. }
  776. if (interpreter.exception())
  777. return {};
  778. }
  779. return class_constructor;
  780. }
  781. Value ClassDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  782. {
  783. interpreter.enter_node(*this);
  784. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  785. Value class_constructor = m_class_expression->execute(interpreter, global_object);
  786. if (interpreter.exception())
  787. return {};
  788. interpreter.current_scope()->put_to_scope(m_class_expression->name(), { class_constructor, DeclarationKind::Let });
  789. return js_undefined();
  790. }
  791. static void print_indent(int indent)
  792. {
  793. out("{}", String::repeated(' ', indent * 2));
  794. }
  795. void ASTNode::dump(int indent) const
  796. {
  797. print_indent(indent);
  798. outln("{}", class_name());
  799. }
  800. void ScopeNode::dump(int indent) const
  801. {
  802. ASTNode::dump(indent);
  803. if (!m_variables.is_empty()) {
  804. print_indent(indent + 1);
  805. outln("(Variables)");
  806. for (auto& variable : m_variables)
  807. variable.dump(indent + 2);
  808. }
  809. if (!m_children.is_empty()) {
  810. print_indent(indent + 1);
  811. outln("(Children)");
  812. for (auto& child : children())
  813. child.dump(indent + 2);
  814. }
  815. }
  816. void BinaryExpression::dump(int indent) const
  817. {
  818. const char* op_string = nullptr;
  819. switch (m_op) {
  820. case BinaryOp::Addition:
  821. op_string = "+";
  822. break;
  823. case BinaryOp::Subtraction:
  824. op_string = "-";
  825. break;
  826. case BinaryOp::Multiplication:
  827. op_string = "*";
  828. break;
  829. case BinaryOp::Division:
  830. op_string = "/";
  831. break;
  832. case BinaryOp::Modulo:
  833. op_string = "%";
  834. break;
  835. case BinaryOp::Exponentiation:
  836. op_string = "**";
  837. break;
  838. case BinaryOp::TypedEquals:
  839. op_string = "===";
  840. break;
  841. case BinaryOp::TypedInequals:
  842. op_string = "!==";
  843. break;
  844. case BinaryOp::AbstractEquals:
  845. op_string = "==";
  846. break;
  847. case BinaryOp::AbstractInequals:
  848. op_string = "!=";
  849. break;
  850. case BinaryOp::GreaterThan:
  851. op_string = ">";
  852. break;
  853. case BinaryOp::GreaterThanEquals:
  854. op_string = ">=";
  855. break;
  856. case BinaryOp::LessThan:
  857. op_string = "<";
  858. break;
  859. case BinaryOp::LessThanEquals:
  860. op_string = "<=";
  861. break;
  862. case BinaryOp::BitwiseAnd:
  863. op_string = "&";
  864. break;
  865. case BinaryOp::BitwiseOr:
  866. op_string = "|";
  867. break;
  868. case BinaryOp::BitwiseXor:
  869. op_string = "^";
  870. break;
  871. case BinaryOp::LeftShift:
  872. op_string = "<<";
  873. break;
  874. case BinaryOp::RightShift:
  875. op_string = ">>";
  876. break;
  877. case BinaryOp::UnsignedRightShift:
  878. op_string = ">>>";
  879. break;
  880. case BinaryOp::In:
  881. op_string = "in";
  882. break;
  883. case BinaryOp::InstanceOf:
  884. op_string = "instanceof";
  885. break;
  886. }
  887. print_indent(indent);
  888. outln("{}", class_name());
  889. m_lhs->dump(indent + 1);
  890. print_indent(indent + 1);
  891. outln("{}", op_string);
  892. m_rhs->dump(indent + 1);
  893. }
  894. void LogicalExpression::dump(int indent) const
  895. {
  896. const char* op_string = nullptr;
  897. switch (m_op) {
  898. case LogicalOp::And:
  899. op_string = "&&";
  900. break;
  901. case LogicalOp::Or:
  902. op_string = "||";
  903. break;
  904. case LogicalOp::NullishCoalescing:
  905. op_string = "??";
  906. break;
  907. }
  908. print_indent(indent);
  909. outln("{}", class_name());
  910. m_lhs->dump(indent + 1);
  911. print_indent(indent + 1);
  912. outln("{}", op_string);
  913. m_rhs->dump(indent + 1);
  914. }
  915. void UnaryExpression::dump(int indent) const
  916. {
  917. const char* op_string = nullptr;
  918. switch (m_op) {
  919. case UnaryOp::BitwiseNot:
  920. op_string = "~";
  921. break;
  922. case UnaryOp::Not:
  923. op_string = "!";
  924. break;
  925. case UnaryOp::Plus:
  926. op_string = "+";
  927. break;
  928. case UnaryOp::Minus:
  929. op_string = "-";
  930. break;
  931. case UnaryOp::Typeof:
  932. op_string = "typeof ";
  933. break;
  934. case UnaryOp::Void:
  935. op_string = "void ";
  936. break;
  937. case UnaryOp::Delete:
  938. op_string = "delete ";
  939. break;
  940. }
  941. print_indent(indent);
  942. outln("{}", class_name());
  943. print_indent(indent + 1);
  944. outln("{}", op_string);
  945. m_lhs->dump(indent + 1);
  946. }
  947. void CallExpression::dump(int indent) const
  948. {
  949. print_indent(indent);
  950. if (is<NewExpression>(*this))
  951. outln("CallExpression [new]");
  952. else
  953. outln("CallExpression");
  954. m_callee->dump(indent + 1);
  955. for (auto& argument : m_arguments)
  956. argument.value->dump(indent + 1);
  957. }
  958. void ClassDeclaration::dump(int indent) const
  959. {
  960. ASTNode::dump(indent);
  961. m_class_expression->dump(indent + 1);
  962. }
  963. void ClassExpression::dump(int indent) const
  964. {
  965. print_indent(indent);
  966. outln("ClassExpression: \"{}\"", m_name);
  967. print_indent(indent);
  968. outln("(Constructor)");
  969. m_constructor->dump(indent + 1);
  970. if (!m_super_class.is_null()) {
  971. print_indent(indent);
  972. outln("(Super Class)");
  973. m_super_class->dump(indent + 1);
  974. }
  975. print_indent(indent);
  976. outln("(Methods)");
  977. for (auto& method : m_methods)
  978. method.dump(indent + 1);
  979. }
  980. void ClassMethod::dump(int indent) const
  981. {
  982. ASTNode::dump(indent);
  983. print_indent(indent);
  984. outln("(Key)");
  985. m_key->dump(indent + 1);
  986. const char* kind_string = nullptr;
  987. switch (m_kind) {
  988. case Kind::Method:
  989. kind_string = "Method";
  990. break;
  991. case Kind::Getter:
  992. kind_string = "Getter";
  993. break;
  994. case Kind::Setter:
  995. kind_string = "Setter";
  996. break;
  997. }
  998. print_indent(indent);
  999. outln("Kind: {}", kind_string);
  1000. print_indent(indent);
  1001. outln("Static: {}", m_is_static);
  1002. print_indent(indent);
  1003. outln("(Function)");
  1004. m_function->dump(indent + 1);
  1005. }
  1006. void StringLiteral::dump(int indent) const
  1007. {
  1008. print_indent(indent);
  1009. outln("StringLiteral \"{}\"", m_value);
  1010. }
  1011. void SuperExpression::dump(int indent) const
  1012. {
  1013. print_indent(indent);
  1014. outln("super");
  1015. }
  1016. void NumericLiteral::dump(int indent) const
  1017. {
  1018. print_indent(indent);
  1019. outln("NumericLiteral {}", m_value);
  1020. }
  1021. void BigIntLiteral::dump(int indent) const
  1022. {
  1023. print_indent(indent);
  1024. outln("BigIntLiteral {}", m_value);
  1025. }
  1026. void BooleanLiteral::dump(int indent) const
  1027. {
  1028. print_indent(indent);
  1029. outln("BooleanLiteral {}", m_value);
  1030. }
  1031. void NullLiteral::dump(int indent) const
  1032. {
  1033. print_indent(indent);
  1034. outln("null");
  1035. }
  1036. void FunctionNode::dump(int indent, const String& class_name) const
  1037. {
  1038. print_indent(indent);
  1039. outln("{} '{}'", class_name, name());
  1040. if (!m_parameters.is_empty()) {
  1041. print_indent(indent + 1);
  1042. outln("(Parameters)\n");
  1043. for (auto& parameter : m_parameters) {
  1044. print_indent(indent + 2);
  1045. if (parameter.is_rest)
  1046. out("...");
  1047. outln("{}", parameter.name);
  1048. if (parameter.default_value)
  1049. parameter.default_value->dump(indent + 3);
  1050. }
  1051. }
  1052. if (!m_variables.is_empty()) {
  1053. print_indent(indent + 1);
  1054. outln("(Variables)");
  1055. for (auto& variable : m_variables)
  1056. variable.dump(indent + 2);
  1057. }
  1058. print_indent(indent + 1);
  1059. outln("(Body)");
  1060. body().dump(indent + 2);
  1061. }
  1062. void FunctionDeclaration::dump(int indent) const
  1063. {
  1064. FunctionNode::dump(indent, class_name());
  1065. }
  1066. void FunctionExpression::dump(int indent) const
  1067. {
  1068. FunctionNode::dump(indent, class_name());
  1069. }
  1070. void ReturnStatement::dump(int indent) const
  1071. {
  1072. ASTNode::dump(indent);
  1073. if (argument())
  1074. argument()->dump(indent + 1);
  1075. }
  1076. void IfStatement::dump(int indent) const
  1077. {
  1078. ASTNode::dump(indent);
  1079. print_indent(indent);
  1080. outln("If");
  1081. predicate().dump(indent + 1);
  1082. consequent().dump(indent + 1);
  1083. if (alternate()) {
  1084. print_indent(indent);
  1085. outln("Else");
  1086. alternate()->dump(indent + 1);
  1087. }
  1088. }
  1089. void WhileStatement::dump(int indent) const
  1090. {
  1091. ASTNode::dump(indent);
  1092. print_indent(indent);
  1093. outln("While");
  1094. test().dump(indent + 1);
  1095. body().dump(indent + 1);
  1096. }
  1097. void WithStatement::dump(int indent) const
  1098. {
  1099. ASTNode::dump(indent);
  1100. print_indent(indent + 1);
  1101. outln("Object");
  1102. object().dump(indent + 2);
  1103. print_indent(indent + 1);
  1104. outln("Body");
  1105. body().dump(indent + 2);
  1106. }
  1107. void DoWhileStatement::dump(int indent) const
  1108. {
  1109. ASTNode::dump(indent);
  1110. print_indent(indent);
  1111. outln("DoWhile");
  1112. test().dump(indent + 1);
  1113. body().dump(indent + 1);
  1114. }
  1115. void ForStatement::dump(int indent) const
  1116. {
  1117. ASTNode::dump(indent);
  1118. print_indent(indent);
  1119. outln("For");
  1120. if (init())
  1121. init()->dump(indent + 1);
  1122. if (test())
  1123. test()->dump(indent + 1);
  1124. if (update())
  1125. update()->dump(indent + 1);
  1126. body().dump(indent + 1);
  1127. }
  1128. void ForInStatement::dump(int indent) const
  1129. {
  1130. ASTNode::dump(indent);
  1131. print_indent(indent);
  1132. outln("ForIn");
  1133. lhs().dump(indent + 1);
  1134. rhs().dump(indent + 1);
  1135. body().dump(indent + 1);
  1136. }
  1137. void ForOfStatement::dump(int indent) const
  1138. {
  1139. ASTNode::dump(indent);
  1140. print_indent(indent);
  1141. outln("ForOf");
  1142. lhs().dump(indent + 1);
  1143. rhs().dump(indent + 1);
  1144. body().dump(indent + 1);
  1145. }
  1146. Value Identifier::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1147. {
  1148. interpreter.enter_node(*this);
  1149. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1150. auto value = interpreter.vm().get_variable(string(), global_object);
  1151. if (value.is_empty()) {
  1152. interpreter.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, string());
  1153. return {};
  1154. }
  1155. return value;
  1156. }
  1157. void Identifier::dump(int indent) const
  1158. {
  1159. print_indent(indent);
  1160. outln("Identifier \"{}\"", m_string);
  1161. }
  1162. void SpreadExpression::dump(int indent) const
  1163. {
  1164. ASTNode::dump(indent);
  1165. m_target->dump(indent + 1);
  1166. }
  1167. Value SpreadExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1168. {
  1169. interpreter.enter_node(*this);
  1170. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1171. return m_target->execute(interpreter, global_object);
  1172. }
  1173. Value ThisExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1174. {
  1175. interpreter.enter_node(*this);
  1176. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1177. return interpreter.vm().resolve_this_binding(global_object);
  1178. }
  1179. void ThisExpression::dump(int indent) const
  1180. {
  1181. ASTNode::dump(indent);
  1182. }
  1183. Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1184. {
  1185. interpreter.enter_node(*this);
  1186. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1187. #define EXECUTE_LHS_AND_RHS() \
  1188. do { \
  1189. lhs_result = m_lhs->execute(interpreter, global_object); \
  1190. if (interpreter.exception()) \
  1191. return {}; \
  1192. rhs_result = m_rhs->execute(interpreter, global_object); \
  1193. if (interpreter.exception()) \
  1194. return {}; \
  1195. } while (0)
  1196. Value lhs_result;
  1197. Value rhs_result;
  1198. switch (m_op) {
  1199. case AssignmentOp::Assignment:
  1200. break;
  1201. case AssignmentOp::AdditionAssignment:
  1202. EXECUTE_LHS_AND_RHS();
  1203. rhs_result = add(global_object, lhs_result, rhs_result);
  1204. break;
  1205. case AssignmentOp::SubtractionAssignment:
  1206. EXECUTE_LHS_AND_RHS();
  1207. rhs_result = sub(global_object, lhs_result, rhs_result);
  1208. break;
  1209. case AssignmentOp::MultiplicationAssignment:
  1210. EXECUTE_LHS_AND_RHS();
  1211. rhs_result = mul(global_object, lhs_result, rhs_result);
  1212. break;
  1213. case AssignmentOp::DivisionAssignment:
  1214. EXECUTE_LHS_AND_RHS();
  1215. rhs_result = div(global_object, lhs_result, rhs_result);
  1216. break;
  1217. case AssignmentOp::ModuloAssignment:
  1218. EXECUTE_LHS_AND_RHS();
  1219. rhs_result = mod(global_object, lhs_result, rhs_result);
  1220. break;
  1221. case AssignmentOp::ExponentiationAssignment:
  1222. EXECUTE_LHS_AND_RHS();
  1223. rhs_result = exp(global_object, lhs_result, rhs_result);
  1224. break;
  1225. case AssignmentOp::BitwiseAndAssignment:
  1226. EXECUTE_LHS_AND_RHS();
  1227. rhs_result = bitwise_and(global_object, lhs_result, rhs_result);
  1228. break;
  1229. case AssignmentOp::BitwiseOrAssignment:
  1230. EXECUTE_LHS_AND_RHS();
  1231. rhs_result = bitwise_or(global_object, lhs_result, rhs_result);
  1232. break;
  1233. case AssignmentOp::BitwiseXorAssignment:
  1234. EXECUTE_LHS_AND_RHS();
  1235. rhs_result = bitwise_xor(global_object, lhs_result, rhs_result);
  1236. break;
  1237. case AssignmentOp::LeftShiftAssignment:
  1238. EXECUTE_LHS_AND_RHS();
  1239. rhs_result = left_shift(global_object, lhs_result, rhs_result);
  1240. break;
  1241. case AssignmentOp::RightShiftAssignment:
  1242. EXECUTE_LHS_AND_RHS();
  1243. rhs_result = right_shift(global_object, lhs_result, rhs_result);
  1244. break;
  1245. case AssignmentOp::UnsignedRightShiftAssignment:
  1246. EXECUTE_LHS_AND_RHS();
  1247. rhs_result = unsigned_right_shift(global_object, lhs_result, rhs_result);
  1248. break;
  1249. case AssignmentOp::AndAssignment:
  1250. lhs_result = m_lhs->execute(interpreter, global_object);
  1251. if (interpreter.exception())
  1252. return {};
  1253. if (!lhs_result.to_boolean())
  1254. return lhs_result;
  1255. rhs_result = m_rhs->execute(interpreter, global_object);
  1256. break;
  1257. case AssignmentOp::OrAssignment:
  1258. lhs_result = m_lhs->execute(interpreter, global_object);
  1259. if (interpreter.exception())
  1260. return {};
  1261. if (lhs_result.to_boolean())
  1262. return lhs_result;
  1263. rhs_result = m_rhs->execute(interpreter, global_object);
  1264. break;
  1265. case AssignmentOp::NullishAssignment:
  1266. lhs_result = m_lhs->execute(interpreter, global_object);
  1267. if (interpreter.exception())
  1268. return {};
  1269. if (!lhs_result.is_nullish())
  1270. return lhs_result;
  1271. rhs_result = m_rhs->execute(interpreter, global_object);
  1272. break;
  1273. }
  1274. if (interpreter.exception())
  1275. return {};
  1276. auto reference = m_lhs->to_reference(interpreter, global_object);
  1277. if (interpreter.exception())
  1278. return {};
  1279. if (m_op == AssignmentOp::Assignment) {
  1280. rhs_result = m_rhs->execute(interpreter, global_object);
  1281. if (interpreter.exception())
  1282. return {};
  1283. }
  1284. if (reference.is_unresolvable()) {
  1285. interpreter.vm().throw_exception<ReferenceError>(global_object, ErrorType::InvalidLeftHandAssignment);
  1286. return {};
  1287. }
  1288. update_function_name(rhs_result, get_function_name(global_object, reference.name().to_value(interpreter.vm())));
  1289. reference.put(global_object, rhs_result);
  1290. if (interpreter.exception())
  1291. return {};
  1292. return rhs_result;
  1293. }
  1294. Value UpdateExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1295. {
  1296. interpreter.enter_node(*this);
  1297. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1298. auto reference = m_argument->to_reference(interpreter, global_object);
  1299. if (interpreter.exception())
  1300. return {};
  1301. auto old_value = reference.get(global_object);
  1302. if (interpreter.exception())
  1303. return {};
  1304. old_value = old_value.to_numeric(global_object);
  1305. if (interpreter.exception())
  1306. return {};
  1307. Value new_value;
  1308. switch (m_op) {
  1309. case UpdateOp::Increment:
  1310. if (old_value.is_number())
  1311. new_value = Value(old_value.as_double() + 1);
  1312. else
  1313. new_value = js_bigint(interpreter.heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  1314. break;
  1315. case UpdateOp::Decrement:
  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().minus(Crypto::SignedBigInteger { 1 }));
  1320. break;
  1321. default:
  1322. ASSERT_NOT_REACHED();
  1323. }
  1324. reference.put(global_object, new_value);
  1325. if (interpreter.exception())
  1326. return {};
  1327. return m_prefixed ? new_value : old_value;
  1328. }
  1329. void AssignmentExpression::dump(int indent) const
  1330. {
  1331. const char* op_string = nullptr;
  1332. switch (m_op) {
  1333. case AssignmentOp::Assignment:
  1334. op_string = "=";
  1335. break;
  1336. case AssignmentOp::AdditionAssignment:
  1337. op_string = "+=";
  1338. break;
  1339. case AssignmentOp::SubtractionAssignment:
  1340. op_string = "-=";
  1341. break;
  1342. case AssignmentOp::MultiplicationAssignment:
  1343. op_string = "*=";
  1344. break;
  1345. case AssignmentOp::DivisionAssignment:
  1346. op_string = "/=";
  1347. break;
  1348. case AssignmentOp::ModuloAssignment:
  1349. op_string = "%=";
  1350. break;
  1351. case AssignmentOp::ExponentiationAssignment:
  1352. op_string = "**=";
  1353. break;
  1354. case AssignmentOp::BitwiseAndAssignment:
  1355. op_string = "&=";
  1356. break;
  1357. case AssignmentOp::BitwiseOrAssignment:
  1358. op_string = "|=";
  1359. break;
  1360. case AssignmentOp::BitwiseXorAssignment:
  1361. op_string = "^=";
  1362. break;
  1363. case AssignmentOp::LeftShiftAssignment:
  1364. op_string = "<<=";
  1365. break;
  1366. case AssignmentOp::RightShiftAssignment:
  1367. op_string = ">>=";
  1368. break;
  1369. case AssignmentOp::UnsignedRightShiftAssignment:
  1370. op_string = ">>>=";
  1371. break;
  1372. case AssignmentOp::AndAssignment:
  1373. op_string = "&&=";
  1374. break;
  1375. case AssignmentOp::OrAssignment:
  1376. op_string = "||=";
  1377. break;
  1378. case AssignmentOp::NullishAssignment:
  1379. op_string = "\?\?=";
  1380. break;
  1381. }
  1382. ASTNode::dump(indent);
  1383. print_indent(indent + 1);
  1384. outln("{}", op_string);
  1385. m_lhs->dump(indent + 1);
  1386. m_rhs->dump(indent + 1);
  1387. }
  1388. void UpdateExpression::dump(int indent) const
  1389. {
  1390. const char* op_string = nullptr;
  1391. switch (m_op) {
  1392. case UpdateOp::Increment:
  1393. op_string = "++";
  1394. break;
  1395. case UpdateOp::Decrement:
  1396. op_string = "--";
  1397. break;
  1398. }
  1399. ASTNode::dump(indent);
  1400. if (m_prefixed) {
  1401. print_indent(indent + 1);
  1402. outln("{}", op_string);
  1403. }
  1404. m_argument->dump(indent + 1);
  1405. if (!m_prefixed) {
  1406. print_indent(indent + 1);
  1407. outln("{}", op_string);
  1408. }
  1409. }
  1410. Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1411. {
  1412. interpreter.enter_node(*this);
  1413. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1414. for (auto& declarator : m_declarations) {
  1415. if (auto* init = declarator.init()) {
  1416. auto initalizer_result = init->execute(interpreter, global_object);
  1417. if (interpreter.exception())
  1418. return {};
  1419. auto variable_name = declarator.id().string();
  1420. update_function_name(initalizer_result, variable_name);
  1421. interpreter.vm().set_variable(variable_name, initalizer_result, global_object, true);
  1422. }
  1423. }
  1424. return js_undefined();
  1425. }
  1426. Value VariableDeclarator::execute(Interpreter& interpreter, GlobalObject&) const
  1427. {
  1428. interpreter.enter_node(*this);
  1429. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1430. // NOTE: VariableDeclarator execution is handled by VariableDeclaration.
  1431. ASSERT_NOT_REACHED();
  1432. }
  1433. void VariableDeclaration::dump(int indent) const
  1434. {
  1435. const char* declaration_kind_string = nullptr;
  1436. switch (m_declaration_kind) {
  1437. case DeclarationKind::Let:
  1438. declaration_kind_string = "Let";
  1439. break;
  1440. case DeclarationKind::Var:
  1441. declaration_kind_string = "Var";
  1442. break;
  1443. case DeclarationKind::Const:
  1444. declaration_kind_string = "Const";
  1445. break;
  1446. }
  1447. ASTNode::dump(indent);
  1448. print_indent(indent + 1);
  1449. outln("{}", declaration_kind_string);
  1450. for (auto& declarator : m_declarations)
  1451. declarator.dump(indent + 1);
  1452. }
  1453. void VariableDeclarator::dump(int indent) const
  1454. {
  1455. ASTNode::dump(indent);
  1456. m_id->dump(indent + 1);
  1457. if (m_init)
  1458. m_init->dump(indent + 1);
  1459. }
  1460. void ObjectProperty::dump(int indent) const
  1461. {
  1462. ASTNode::dump(indent);
  1463. m_key->dump(indent + 1);
  1464. m_value->dump(indent + 1);
  1465. }
  1466. void ObjectExpression::dump(int indent) const
  1467. {
  1468. ASTNode::dump(indent);
  1469. for (auto& property : m_properties) {
  1470. property.dump(indent + 1);
  1471. }
  1472. }
  1473. void ExpressionStatement::dump(int indent) const
  1474. {
  1475. ASTNode::dump(indent);
  1476. m_expression->dump(indent + 1);
  1477. }
  1478. Value ObjectProperty::execute(Interpreter& interpreter, GlobalObject&) const
  1479. {
  1480. interpreter.enter_node(*this);
  1481. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1482. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  1483. ASSERT_NOT_REACHED();
  1484. }
  1485. Value ObjectExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1486. {
  1487. interpreter.enter_node(*this);
  1488. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1489. auto* object = Object::create_empty(global_object);
  1490. for (auto& property : m_properties) {
  1491. auto key = property.key().execute(interpreter, global_object);
  1492. if (interpreter.exception())
  1493. return {};
  1494. if (property.type() == ObjectProperty::Type::Spread) {
  1495. if (key.is_array()) {
  1496. auto& array_to_spread = static_cast<Array&>(key.as_object());
  1497. for (auto& entry : array_to_spread.indexed_properties()) {
  1498. object->indexed_properties().put(object, entry.index(), entry.value_and_attributes(&array_to_spread).value);
  1499. if (interpreter.exception())
  1500. return {};
  1501. }
  1502. } else if (key.is_object()) {
  1503. auto& obj_to_spread = key.as_object();
  1504. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  1505. if (it.value.attributes.is_enumerable()) {
  1506. object->define_property(it.key, obj_to_spread.get(it.key));
  1507. if (interpreter.exception())
  1508. return {};
  1509. }
  1510. }
  1511. } else if (key.is_string()) {
  1512. auto& str_to_spread = key.as_string().string();
  1513. for (size_t i = 0; i < str_to_spread.length(); i++) {
  1514. object->define_property(i, js_string(interpreter.heap(), str_to_spread.substring(i, 1)));
  1515. if (interpreter.exception())
  1516. return {};
  1517. }
  1518. }
  1519. continue;
  1520. }
  1521. auto value = property.value().execute(interpreter, global_object);
  1522. if (interpreter.exception())
  1523. return {};
  1524. if (value.is_function() && property.is_method())
  1525. value.as_function().set_home_object(object);
  1526. String name = get_function_name(global_object, key);
  1527. if (property.type() == ObjectProperty::Type::Getter) {
  1528. name = String::formatted("get {}", name);
  1529. } else if (property.type() == ObjectProperty::Type::Setter) {
  1530. name = String::formatted("set {}", name);
  1531. }
  1532. update_function_name(value, name);
  1533. if (property.type() == ObjectProperty::Type::Getter || property.type() == ObjectProperty::Type::Setter) {
  1534. ASSERT(value.is_function());
  1535. object->define_accessor(PropertyName::from_value(global_object, key), value.as_function(), property.type() == ObjectProperty::Type::Getter, Attribute::Configurable | Attribute::Enumerable);
  1536. if (interpreter.exception())
  1537. return {};
  1538. } else {
  1539. object->define_property(PropertyName::from_value(global_object, key), value);
  1540. if (interpreter.exception())
  1541. return {};
  1542. }
  1543. }
  1544. return object;
  1545. }
  1546. void MemberExpression::dump(int indent) const
  1547. {
  1548. print_indent(indent);
  1549. outln("%{}(computed={})", class_name(), is_computed());
  1550. m_object->dump(indent + 1);
  1551. m_property->dump(indent + 1);
  1552. }
  1553. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter, GlobalObject& global_object) const
  1554. {
  1555. if (!is_computed()) {
  1556. ASSERT(is<Identifier>(*m_property));
  1557. return static_cast<const Identifier&>(*m_property).string();
  1558. }
  1559. auto value = m_property->execute(interpreter, global_object);
  1560. if (interpreter.exception())
  1561. return {};
  1562. ASSERT(!value.is_empty());
  1563. return PropertyName::from_value(global_object, value);
  1564. }
  1565. String MemberExpression::to_string_approximation() const
  1566. {
  1567. String object_string = "<object>";
  1568. if (is<Identifier>(*m_object))
  1569. object_string = static_cast<const Identifier&>(*m_object).string();
  1570. if (is_computed())
  1571. return String::formatted("{}[<computed>]", object_string);
  1572. ASSERT(is<Identifier>(*m_property));
  1573. return String::formatted("{}.{}", object_string, static_cast<const Identifier&>(*m_property).string());
  1574. }
  1575. Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1576. {
  1577. interpreter.enter_node(*this);
  1578. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1579. auto object_value = m_object->execute(interpreter, global_object);
  1580. if (interpreter.exception())
  1581. return {};
  1582. auto* object_result = object_value.to_object(global_object);
  1583. if (interpreter.exception())
  1584. return {};
  1585. auto property_name = computed_property_name(interpreter, global_object);
  1586. if (!property_name.is_valid())
  1587. return {};
  1588. return object_result->get(property_name).value_or(js_undefined());
  1589. }
  1590. void MetaProperty::dump(int indent) const
  1591. {
  1592. String name;
  1593. if (m_type == MetaProperty::Type::NewTarget)
  1594. name = "new.target";
  1595. else if (m_type == MetaProperty::Type::ImportMeta)
  1596. name = "import.meta";
  1597. else
  1598. ASSERT_NOT_REACHED();
  1599. print_indent(indent);
  1600. outln("{} {}", class_name(), name);
  1601. }
  1602. Value MetaProperty::execute(Interpreter& interpreter, GlobalObject&) const
  1603. {
  1604. interpreter.enter_node(*this);
  1605. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1606. if (m_type == MetaProperty::Type::NewTarget)
  1607. return interpreter.vm().get_new_target().value_or(js_undefined());
  1608. if (m_type == MetaProperty::Type::ImportMeta)
  1609. TODO();
  1610. ASSERT_NOT_REACHED();
  1611. }
  1612. Value StringLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1613. {
  1614. interpreter.enter_node(*this);
  1615. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1616. return js_string(interpreter.heap(), m_value);
  1617. }
  1618. Value NumericLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1619. {
  1620. interpreter.enter_node(*this);
  1621. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1622. return Value(m_value);
  1623. }
  1624. Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1625. {
  1626. interpreter.enter_node(*this);
  1627. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1628. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
  1629. }
  1630. Value BooleanLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1631. {
  1632. interpreter.enter_node(*this);
  1633. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1634. return Value(m_value);
  1635. }
  1636. Value NullLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1637. {
  1638. interpreter.enter_node(*this);
  1639. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1640. return js_null();
  1641. }
  1642. void RegExpLiteral::dump(int indent) const
  1643. {
  1644. print_indent(indent);
  1645. outln("{} (/{}/{})", class_name(), content(), flags());
  1646. }
  1647. Value RegExpLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1648. {
  1649. interpreter.enter_node(*this);
  1650. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1651. return RegExpObject::create(global_object, content(), flags());
  1652. }
  1653. void ArrayExpression::dump(int indent) const
  1654. {
  1655. ASTNode::dump(indent);
  1656. for (auto& element : m_elements) {
  1657. if (element) {
  1658. element->dump(indent + 1);
  1659. } else {
  1660. print_indent(indent + 1);
  1661. outln("<empty>");
  1662. }
  1663. }
  1664. }
  1665. Value ArrayExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1666. {
  1667. interpreter.enter_node(*this);
  1668. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1669. auto* array = Array::create(global_object);
  1670. for (auto& element : m_elements) {
  1671. auto value = Value();
  1672. if (element) {
  1673. value = element->execute(interpreter, global_object);
  1674. if (interpreter.exception())
  1675. return {};
  1676. if (is<SpreadExpression>(*element)) {
  1677. get_iterator_values(global_object, value, [&](Value iterator_value) {
  1678. array->indexed_properties().append(iterator_value);
  1679. return IterationDecision::Continue;
  1680. });
  1681. if (interpreter.exception())
  1682. return {};
  1683. continue;
  1684. }
  1685. }
  1686. array->indexed_properties().append(value);
  1687. }
  1688. return array;
  1689. }
  1690. void TemplateLiteral::dump(int indent) const
  1691. {
  1692. ASTNode::dump(indent);
  1693. for (auto& expression : m_expressions)
  1694. expression.dump(indent + 1);
  1695. }
  1696. Value TemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1697. {
  1698. interpreter.enter_node(*this);
  1699. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1700. StringBuilder string_builder;
  1701. for (auto& expression : m_expressions) {
  1702. auto expr = expression.execute(interpreter, global_object);
  1703. if (interpreter.exception())
  1704. return {};
  1705. auto string = expr.to_string(global_object);
  1706. if (interpreter.exception())
  1707. return {};
  1708. string_builder.append(string);
  1709. }
  1710. return js_string(interpreter.heap(), string_builder.build());
  1711. }
  1712. void TaggedTemplateLiteral::dump(int indent) const
  1713. {
  1714. ASTNode::dump(indent);
  1715. print_indent(indent + 1);
  1716. outln("(Tag)");
  1717. m_tag->dump(indent + 2);
  1718. print_indent(indent + 1);
  1719. outln("(Template Literal)");
  1720. m_template_literal->dump(indent + 2);
  1721. }
  1722. Value TaggedTemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1723. {
  1724. interpreter.enter_node(*this);
  1725. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1726. auto& vm = interpreter.vm();
  1727. auto tag = m_tag->execute(interpreter, global_object);
  1728. if (vm.exception())
  1729. return {};
  1730. if (!tag.is_function()) {
  1731. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, tag.to_string_without_side_effects());
  1732. return {};
  1733. }
  1734. auto& tag_function = tag.as_function();
  1735. auto& expressions = m_template_literal->expressions();
  1736. auto* strings = Array::create(global_object);
  1737. MarkedValueList arguments(vm.heap());
  1738. arguments.append(strings);
  1739. for (size_t i = 0; i < expressions.size(); ++i) {
  1740. auto value = expressions[i].execute(interpreter, global_object);
  1741. if (vm.exception())
  1742. return {};
  1743. // tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
  1744. // tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
  1745. if (i % 2 == 0) {
  1746. strings->indexed_properties().append(value);
  1747. } else {
  1748. arguments.append(value);
  1749. }
  1750. }
  1751. auto* raw_strings = Array::create(global_object);
  1752. for (auto& raw_string : m_template_literal->raw_strings()) {
  1753. auto value = raw_string.execute(interpreter, global_object);
  1754. if (vm.exception())
  1755. return {};
  1756. raw_strings->indexed_properties().append(value);
  1757. }
  1758. strings->define_property(vm.names.raw, raw_strings, 0);
  1759. return vm.call(tag_function, js_undefined(), move(arguments));
  1760. }
  1761. void TryStatement::dump(int indent) const
  1762. {
  1763. ASTNode::dump(indent);
  1764. print_indent(indent);
  1765. outln("(Block)");
  1766. block().dump(indent + 1);
  1767. if (handler()) {
  1768. print_indent(indent);
  1769. outln("(Handler)");
  1770. handler()->dump(indent + 1);
  1771. }
  1772. if (finalizer()) {
  1773. print_indent(indent);
  1774. outln("(Finalizer)");
  1775. finalizer()->dump(indent + 1);
  1776. }
  1777. }
  1778. void CatchClause::dump(int indent) const
  1779. {
  1780. print_indent(indent);
  1781. if (m_parameter.is_null())
  1782. outln("CatchClause");
  1783. else
  1784. outln("CatchClause ({})", m_parameter);
  1785. body().dump(indent + 1);
  1786. }
  1787. void ThrowStatement::dump(int indent) const
  1788. {
  1789. ASTNode::dump(indent);
  1790. argument().dump(indent + 1);
  1791. }
  1792. Value TryStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1793. {
  1794. interpreter.enter_node(*this);
  1795. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1796. interpreter.execute_statement(global_object, m_block, ScopeType::Try);
  1797. if (auto* exception = interpreter.exception()) {
  1798. if (m_handler) {
  1799. interpreter.vm().clear_exception();
  1800. HashMap<FlyString, Variable> parameters;
  1801. parameters.set(m_handler->parameter(), Variable { exception->value(), DeclarationKind::Var });
  1802. auto* catch_scope = interpreter.heap().allocate<LexicalEnvironment>(global_object, move(parameters), interpreter.vm().call_frame().scope);
  1803. TemporaryChange<ScopeObject*> scope_change(interpreter.vm().call_frame().scope, catch_scope);
  1804. interpreter.execute_statement(global_object, m_handler->body());
  1805. }
  1806. }
  1807. if (m_finalizer) {
  1808. // Keep, if any, and then clear the current exception so we can
  1809. // execute() the finalizer without an exception in our way.
  1810. auto* previous_exception = interpreter.exception();
  1811. interpreter.vm().clear_exception();
  1812. interpreter.vm().stop_unwind();
  1813. m_finalizer->execute(interpreter, global_object);
  1814. // If we previously had an exception and the finalizer didn't
  1815. // throw a new one, restore the old one.
  1816. // FIXME: This will print debug output in throw_exception() for
  1817. // a seconds time with m_should_log_exceptions enabled.
  1818. if (previous_exception && !interpreter.exception())
  1819. interpreter.vm().throw_exception(previous_exception);
  1820. }
  1821. return js_undefined();
  1822. }
  1823. Value CatchClause::execute(Interpreter& interpreter, GlobalObject&) const
  1824. {
  1825. interpreter.enter_node(*this);
  1826. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1827. // NOTE: CatchClause execution is handled by TryStatement.
  1828. ASSERT_NOT_REACHED();
  1829. return {};
  1830. }
  1831. Value ThrowStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1832. {
  1833. interpreter.enter_node(*this);
  1834. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1835. auto value = m_argument->execute(interpreter, global_object);
  1836. if (interpreter.vm().exception())
  1837. return {};
  1838. interpreter.vm().throw_exception(global_object, value);
  1839. return {};
  1840. }
  1841. Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1842. {
  1843. interpreter.enter_node(*this);
  1844. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1845. auto discriminant_result = m_discriminant->execute(interpreter, global_object);
  1846. if (interpreter.exception())
  1847. return {};
  1848. bool falling_through = false;
  1849. for (auto& switch_case : m_cases) {
  1850. if (!falling_through && switch_case.test()) {
  1851. auto test_result = switch_case.test()->execute(interpreter, global_object);
  1852. if (interpreter.exception())
  1853. return {};
  1854. if (!strict_eq(discriminant_result, test_result))
  1855. continue;
  1856. }
  1857. falling_through = true;
  1858. for (auto& statement : switch_case.consequent()) {
  1859. auto last_value = statement.execute(interpreter, global_object);
  1860. if (interpreter.exception())
  1861. return {};
  1862. if (interpreter.vm().should_unwind()) {
  1863. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  1864. // No stop_unwind(), the outer loop will handle that - we just need to break out of the switch/case.
  1865. return {};
  1866. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  1867. interpreter.vm().stop_unwind();
  1868. return {};
  1869. } else {
  1870. return last_value;
  1871. }
  1872. }
  1873. }
  1874. }
  1875. return js_undefined();
  1876. }
  1877. Value SwitchCase::execute(Interpreter& interpreter, GlobalObject&) const
  1878. {
  1879. interpreter.enter_node(*this);
  1880. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1881. // NOTE: SwitchCase execution is handled by SwitchStatement.
  1882. ASSERT_NOT_REACHED();
  1883. return {};
  1884. }
  1885. Value BreakStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1886. {
  1887. interpreter.enter_node(*this);
  1888. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1889. interpreter.vm().unwind(ScopeType::Breakable, m_target_label);
  1890. return js_undefined();
  1891. }
  1892. Value ContinueStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1893. {
  1894. interpreter.enter_node(*this);
  1895. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1896. interpreter.vm().unwind(ScopeType::Continuable, m_target_label);
  1897. return js_undefined();
  1898. }
  1899. void SwitchStatement::dump(int indent) const
  1900. {
  1901. ASTNode::dump(indent);
  1902. m_discriminant->dump(indent + 1);
  1903. for (auto& switch_case : m_cases) {
  1904. switch_case.dump(indent + 1);
  1905. }
  1906. }
  1907. void SwitchCase::dump(int indent) const
  1908. {
  1909. ASTNode::dump(indent);
  1910. print_indent(indent + 1);
  1911. if (m_test) {
  1912. outln("(Test)");
  1913. m_test->dump(indent + 2);
  1914. } else {
  1915. outln("(Default)");
  1916. }
  1917. print_indent(indent + 1);
  1918. outln("(Consequent)");
  1919. for (auto& statement : m_consequent)
  1920. statement.dump(indent + 2);
  1921. }
  1922. Value ConditionalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1923. {
  1924. interpreter.enter_node(*this);
  1925. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1926. auto test_result = m_test->execute(interpreter, global_object);
  1927. if (interpreter.exception())
  1928. return {};
  1929. Value result;
  1930. if (test_result.to_boolean()) {
  1931. result = m_consequent->execute(interpreter, global_object);
  1932. } else {
  1933. result = m_alternate->execute(interpreter, global_object);
  1934. }
  1935. if (interpreter.exception())
  1936. return {};
  1937. return result;
  1938. }
  1939. void ConditionalExpression::dump(int indent) const
  1940. {
  1941. ASTNode::dump(indent);
  1942. print_indent(indent + 1);
  1943. outln("(Test)");
  1944. m_test->dump(indent + 2);
  1945. print_indent(indent + 1);
  1946. outln("(Consequent)");
  1947. m_consequent->dump(indent + 2);
  1948. print_indent(indent + 1);
  1949. outln("(Alternate)");
  1950. m_alternate->dump(indent + 2);
  1951. }
  1952. void SequenceExpression::dump(int indent) const
  1953. {
  1954. ASTNode::dump(indent);
  1955. for (auto& expression : m_expressions)
  1956. expression.dump(indent + 1);
  1957. }
  1958. Value SequenceExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1959. {
  1960. interpreter.enter_node(*this);
  1961. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1962. Value last_value;
  1963. for (auto& expression : m_expressions) {
  1964. last_value = expression.execute(interpreter, global_object);
  1965. if (interpreter.exception())
  1966. return {};
  1967. }
  1968. return last_value;
  1969. }
  1970. Value DebuggerStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1971. {
  1972. interpreter.enter_node(*this);
  1973. ScopeGuard exit_node { [&] { interpreter.exit_node(*this); } };
  1974. // Sorry, no JavaScript debugger available (yet)!
  1975. return js_undefined();
  1976. }
  1977. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1978. {
  1979. m_variables.append(move(variables));
  1980. }
  1981. void ScopeNode::add_functions(NonnullRefPtrVector<FunctionDeclaration> functions)
  1982. {
  1983. m_functions.append(move(functions));
  1984. }
  1985. }