AST.cpp 72 KB

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