AST.cpp 73 KB

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