AST.cpp 67 KB

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