AST.cpp 70 KB

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