AST.cpp 73 KB

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