AST.cpp 72 KB

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