AST.cpp 65 KB

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