AST.cpp 71 KB

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