AST.cpp 75 KB

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