AST.cpp 72 KB

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