AST.cpp 74 KB

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