AST.cpp 71 KB

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