AST.cpp 84 KB

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