AST.cpp 72 KB

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