AST.cpp 73 KB

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