AST.cpp 82 KB

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