AST.cpp 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  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, move(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. // 7. Set the running execution context's LexicalEnvironment to oldEnv.
  319. interpreter.vm().running_execution_context().lexical_environment = old_environment;
  320. if (interpreter.exception())
  321. return {};
  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, move(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. }