AST.cpp 71 KB

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