AST.cpp 71 KB

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