AST.cpp 78 KB

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