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. #include <stdio.h>
  51. namespace JS {
  52. static void update_function_name(Value& value, const FlyString& name, HashTable<const JS::Cell*>& visited)
  53. {
  54. if (!value.is_object())
  55. return;
  56. if (visited.contains(value.as_cell()))
  57. return;
  58. visited.set(value.as_cell());
  59. auto& object = value.as_object();
  60. if (object.is_function()) {
  61. auto& function = static_cast<Function&>(object);
  62. if (function.is_script_function() && function.name().is_empty())
  63. static_cast<ScriptFunction&>(function).set_name(name);
  64. } else if (object.is_array()) {
  65. auto& array = static_cast<Array&>(object);
  66. for (auto& entry : array.indexed_properties().values_unordered())
  67. update_function_name(entry.value, name, visited);
  68. }
  69. }
  70. static void update_function_name(Value& value, const FlyString& name)
  71. {
  72. HashTable<const JS::Cell*> visited;
  73. update_function_name(value, name, visited);
  74. }
  75. static String get_function_name(GlobalObject& global_object, Value value)
  76. {
  77. if (value.is_symbol())
  78. return String::formatted("[{}]", value.as_symbol().description());
  79. if (value.is_string())
  80. return value.as_string().string();
  81. return value.to_string(global_object);
  82. }
  83. Value ScopeNode::execute(Interpreter& interpreter, GlobalObject& global_object) const
  84. {
  85. return interpreter.execute_statement(global_object, *this);
  86. }
  87. Value Program::execute(Interpreter& interpreter, GlobalObject& global_object) const
  88. {
  89. return interpreter.execute_statement(global_object, *this, {}, ScopeType::Block);
  90. }
  91. Value FunctionDeclaration::execute(Interpreter&, GlobalObject&) const
  92. {
  93. return js_undefined();
  94. }
  95. Value FunctionExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  96. {
  97. 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);
  98. }
  99. Value ExpressionStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  100. {
  101. return m_expression->execute(interpreter, global_object);
  102. }
  103. CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter, GlobalObject& global_object) const
  104. {
  105. auto& vm = interpreter.vm();
  106. if (is_new_expression()) {
  107. // Computing |this| is irrelevant for "new" expression.
  108. return { js_undefined(), m_callee->execute(interpreter, global_object) };
  109. }
  110. if (m_callee->is_super_expression()) {
  111. // If we are calling super, |this| has not been initialized yet, and would not be meaningful to provide.
  112. auto new_target = vm.get_new_target();
  113. ASSERT(new_target.is_function());
  114. return { js_undefined(), new_target };
  115. }
  116. if (m_callee->is_member_expression()) {
  117. auto& member_expression = static_cast<const MemberExpression&>(*m_callee);
  118. bool is_super_property_lookup = member_expression.object().is_super_expression();
  119. auto lookup_target = is_super_property_lookup ? interpreter.current_environment()->get_super_base() : member_expression.object().execute(interpreter, global_object);
  120. if (vm.exception())
  121. return {};
  122. if (is_super_property_lookup && lookup_target.is_nullish()) {
  123. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeNullOrUndefinedOnSuperPropertyAccess, lookup_target.to_string_without_side_effects());
  124. return {};
  125. }
  126. auto* this_value = is_super_property_lookup ? &vm.this_value(global_object).as_object() : lookup_target.to_object(global_object);
  127. if (vm.exception())
  128. return {};
  129. auto property_name = member_expression.computed_property_name(interpreter, global_object);
  130. if (!property_name.is_valid())
  131. return {};
  132. auto callee = lookup_target.to_object(global_object)->get(property_name).value_or(js_undefined());
  133. return { this_value, callee };
  134. }
  135. return { &global_object, m_callee->execute(interpreter, global_object) };
  136. }
  137. Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  138. {
  139. auto& vm = interpreter.vm();
  140. auto [this_value, callee] = compute_this_and_callee(interpreter, global_object);
  141. if (vm.exception())
  142. return {};
  143. ASSERT(!callee.is_empty());
  144. if (!callee.is_function()
  145. || (is_new_expression() && (callee.as_object().is_native_function() && !static_cast<NativeFunction&>(callee.as_object()).has_constructor()))) {
  146. String error_message;
  147. auto call_type = is_new_expression() ? "constructor" : "function";
  148. if (m_callee->is_identifier() || m_callee->is_member_expression()) {
  149. String expression_string;
  150. if (m_callee->is_identifier()) {
  151. expression_string = static_cast<const Identifier&>(*m_callee).string();
  152. } else {
  153. expression_string = static_cast<const MemberExpression&>(*m_callee).to_string_approximation();
  154. }
  155. vm.throw_exception<TypeError>(global_object, ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects(), call_type, expression_string);
  156. } else {
  157. vm.throw_exception<TypeError>(global_object, ErrorType::IsNotA, callee.to_string_without_side_effects(), call_type);
  158. }
  159. return {};
  160. }
  161. auto& function = callee.as_function();
  162. MarkedValueList arguments(vm.heap());
  163. arguments.ensure_capacity(m_arguments.size());
  164. for (size_t i = 0; i < m_arguments.size(); ++i) {
  165. auto value = m_arguments[i].value->execute(interpreter, global_object);
  166. if (vm.exception())
  167. return {};
  168. if (m_arguments[i].is_spread) {
  169. get_iterator_values(global_object, value, [&](Value iterator_value) {
  170. if (vm.exception())
  171. return IterationDecision::Break;
  172. arguments.append(iterator_value);
  173. return IterationDecision::Continue;
  174. });
  175. if (vm.exception())
  176. return {};
  177. } else {
  178. arguments.append(value);
  179. }
  180. }
  181. Object* new_object = nullptr;
  182. Value result;
  183. if (is_new_expression()) {
  184. result = vm.construct(function, function, move(arguments), global_object);
  185. if (result.is_object())
  186. new_object = &result.as_object();
  187. } else if (m_callee->is_super_expression()) {
  188. auto* super_constructor = interpreter.current_environment()->current_function()->prototype();
  189. // FIXME: Functions should track their constructor kind.
  190. if (!super_constructor || !super_constructor->is_function()) {
  191. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, "Super constructor");
  192. return {};
  193. }
  194. result = vm.construct(static_cast<Function&>(*super_constructor), function, move(arguments), global_object);
  195. if (vm.exception())
  196. return {};
  197. interpreter.current_environment()->bind_this_value(global_object, result);
  198. } else {
  199. result = vm.call(function, this_value, move(arguments));
  200. }
  201. if (vm.exception())
  202. return {};
  203. if (is_new_expression()) {
  204. if (result.is_object())
  205. return result;
  206. return new_object;
  207. }
  208. return result;
  209. }
  210. Value ReturnStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  211. {
  212. auto value = argument() ? argument()->execute(interpreter, global_object) : js_undefined();
  213. if (interpreter.exception())
  214. return {};
  215. interpreter.vm().unwind(ScopeType::Function);
  216. return value;
  217. }
  218. Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  219. {
  220. auto predicate_result = m_predicate->execute(interpreter, global_object);
  221. if (interpreter.exception())
  222. return {};
  223. if (predicate_result.to_boolean())
  224. return interpreter.execute_statement(global_object, *m_consequent);
  225. if (m_alternate)
  226. return interpreter.execute_statement(global_object, *m_alternate);
  227. return js_undefined();
  228. }
  229. Value WithStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  230. {
  231. auto object_value = m_object->execute(interpreter, global_object);
  232. if (interpreter.exception())
  233. return {};
  234. auto* object = object_value.to_object(global_object);
  235. if (interpreter.exception())
  236. return {};
  237. ASSERT(object);
  238. auto* with_scope = interpreter.heap().allocate<WithScope>(global_object, *object, interpreter.vm().call_frame().scope);
  239. TemporaryChange<ScopeObject*> scope_change(interpreter.vm().call_frame().scope, with_scope);
  240. interpreter.execute_statement(global_object, m_body);
  241. return {};
  242. }
  243. Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  244. {
  245. Value last_value = js_undefined();
  246. for (;;) {
  247. auto test_result = m_test->execute(interpreter, global_object);
  248. if (interpreter.exception())
  249. return {};
  250. if (!test_result.to_boolean())
  251. break;
  252. last_value = interpreter.execute_statement(global_object, *m_body);
  253. if (interpreter.exception())
  254. return {};
  255. if (interpreter.vm().should_unwind()) {
  256. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  257. interpreter.vm().stop_unwind();
  258. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  259. interpreter.vm().stop_unwind();
  260. break;
  261. } else {
  262. return last_value;
  263. }
  264. }
  265. }
  266. return last_value;
  267. }
  268. Value DoWhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  269. {
  270. Value last_value = js_undefined();
  271. for (;;) {
  272. if (interpreter.exception())
  273. return {};
  274. last_value = interpreter.execute_statement(global_object, *m_body);
  275. if (interpreter.exception())
  276. return {};
  277. if (interpreter.vm().should_unwind()) {
  278. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  279. interpreter.vm().stop_unwind();
  280. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  281. interpreter.vm().stop_unwind();
  282. break;
  283. } else {
  284. return last_value;
  285. }
  286. }
  287. auto test_result = m_test->execute(interpreter, global_object);
  288. if (interpreter.exception())
  289. return {};
  290. if (!test_result.to_boolean())
  291. break;
  292. }
  293. return last_value;
  294. }
  295. Value ForStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  296. {
  297. RefPtr<BlockStatement> wrapper;
  298. if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_kind() != DeclarationKind::Var) {
  299. wrapper = create_ast_node<BlockStatement>();
  300. NonnullRefPtrVector<VariableDeclaration> decls;
  301. decls.append(*static_cast<const VariableDeclaration*>(m_init.ptr()));
  302. wrapper->add_variables(decls);
  303. interpreter.enter_scope(*wrapper, {}, ScopeType::Block, global_object);
  304. }
  305. auto wrapper_cleanup = ScopeGuard([&] {
  306. if (wrapper)
  307. interpreter.exit_scope(*wrapper);
  308. });
  309. Value last_value = js_undefined();
  310. if (m_init) {
  311. m_init->execute(interpreter, global_object);
  312. if (interpreter.exception())
  313. return {};
  314. }
  315. if (m_test) {
  316. while (true) {
  317. auto test_result = m_test->execute(interpreter, global_object);
  318. if (interpreter.exception())
  319. return {};
  320. if (!test_result.to_boolean())
  321. break;
  322. last_value = interpreter.execute_statement(global_object, *m_body);
  323. if (interpreter.exception())
  324. return {};
  325. if (interpreter.vm().should_unwind()) {
  326. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  327. interpreter.vm().stop_unwind();
  328. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  329. interpreter.vm().stop_unwind();
  330. break;
  331. } else {
  332. return last_value;
  333. }
  334. }
  335. if (m_update) {
  336. m_update->execute(interpreter, global_object);
  337. if (interpreter.exception())
  338. return {};
  339. }
  340. }
  341. } else {
  342. while (true) {
  343. last_value = interpreter.execute_statement(global_object, *m_body);
  344. if (interpreter.exception())
  345. return {};
  346. if (interpreter.vm().should_unwind()) {
  347. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  348. interpreter.vm().stop_unwind();
  349. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  350. interpreter.vm().stop_unwind();
  351. break;
  352. } else {
  353. return last_value;
  354. }
  355. }
  356. if (m_update) {
  357. m_update->execute(interpreter, global_object);
  358. if (interpreter.exception())
  359. return {};
  360. }
  361. }
  362. }
  363. return last_value;
  364. }
  365. static FlyString variable_from_for_declaration(Interpreter& interpreter, GlobalObject& global_object, NonnullRefPtr<ASTNode> node, RefPtr<BlockStatement> wrapper)
  366. {
  367. FlyString variable_name;
  368. if (node->is_variable_declaration()) {
  369. auto* variable_declaration = static_cast<const VariableDeclaration*>(node.ptr());
  370. ASSERT(!variable_declaration->declarations().is_empty());
  371. if (variable_declaration->declaration_kind() != DeclarationKind::Var) {
  372. wrapper = create_ast_node<BlockStatement>();
  373. interpreter.enter_scope(*wrapper, {}, ScopeType::Block, global_object);
  374. }
  375. variable_declaration->execute(interpreter, global_object);
  376. variable_name = variable_declaration->declarations().first().id().string();
  377. } else if (node->is_identifier()) {
  378. variable_name = static_cast<const Identifier&>(*node).string();
  379. } else {
  380. ASSERT_NOT_REACHED();
  381. }
  382. return variable_name;
  383. }
  384. Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  385. {
  386. if (!m_lhs->is_variable_declaration() && !m_lhs->is_identifier()) {
  387. // FIXME: Implement "for (foo.bar in baz)", "for (foo[0] in bar)"
  388. ASSERT_NOT_REACHED();
  389. }
  390. RefPtr<BlockStatement> wrapper;
  391. auto variable_name = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  392. auto wrapper_cleanup = ScopeGuard([&] {
  393. if (wrapper)
  394. interpreter.exit_scope(*wrapper);
  395. });
  396. auto last_value = js_undefined();
  397. auto rhs_result = m_rhs->execute(interpreter, global_object);
  398. if (interpreter.exception())
  399. return {};
  400. auto* object = rhs_result.to_object(global_object);
  401. while (object) {
  402. auto property_names = object->get_own_properties(*object, Object::PropertyKind::Key, true);
  403. for (auto& property_name : property_names.as_object().indexed_properties()) {
  404. interpreter.vm().set_variable(variable_name, property_name.value_and_attributes(object).value, global_object);
  405. if (interpreter.exception())
  406. return {};
  407. last_value = interpreter.execute_statement(global_object, *m_body);
  408. if (interpreter.exception())
  409. return {};
  410. if (interpreter.vm().should_unwind()) {
  411. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  412. interpreter.vm().stop_unwind();
  413. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  414. interpreter.vm().stop_unwind();
  415. break;
  416. } else {
  417. return last_value;
  418. }
  419. }
  420. }
  421. object = object->prototype();
  422. if (interpreter.exception())
  423. return {};
  424. }
  425. return last_value;
  426. }
  427. Value ForOfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  428. {
  429. if (!m_lhs->is_variable_declaration() && !m_lhs->is_identifier()) {
  430. // FIXME: Implement "for (foo.bar of baz)", "for (foo[0] of bar)"
  431. ASSERT_NOT_REACHED();
  432. }
  433. RefPtr<BlockStatement> wrapper;
  434. auto variable_name = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  435. auto wrapper_cleanup = ScopeGuard([&] {
  436. if (wrapper)
  437. interpreter.exit_scope(*wrapper);
  438. });
  439. auto last_value = js_undefined();
  440. auto rhs_result = m_rhs->execute(interpreter, global_object);
  441. if (interpreter.exception())
  442. return {};
  443. get_iterator_values(global_object, rhs_result, [&](Value value) {
  444. interpreter.vm().set_variable(variable_name, value, global_object);
  445. last_value = interpreter.execute_statement(global_object, *m_body);
  446. if (interpreter.exception())
  447. return IterationDecision::Break;
  448. if (interpreter.vm().should_unwind()) {
  449. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  450. interpreter.vm().stop_unwind();
  451. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  452. interpreter.vm().stop_unwind();
  453. return IterationDecision::Break;
  454. } else {
  455. return IterationDecision::Break;
  456. }
  457. }
  458. return IterationDecision::Continue;
  459. });
  460. if (interpreter.exception())
  461. return {};
  462. return last_value;
  463. }
  464. Value BinaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  465. {
  466. auto lhs_result = m_lhs->execute(interpreter, global_object);
  467. if (interpreter.exception())
  468. return {};
  469. auto rhs_result = m_rhs->execute(interpreter, global_object);
  470. if (interpreter.exception())
  471. return {};
  472. switch (m_op) {
  473. case BinaryOp::Addition:
  474. return add(global_object, lhs_result, rhs_result);
  475. case BinaryOp::Subtraction:
  476. return sub(global_object, lhs_result, rhs_result);
  477. case BinaryOp::Multiplication:
  478. return mul(global_object, lhs_result, rhs_result);
  479. case BinaryOp::Division:
  480. return div(global_object, lhs_result, rhs_result);
  481. case BinaryOp::Modulo:
  482. return mod(global_object, lhs_result, rhs_result);
  483. case BinaryOp::Exponentiation:
  484. return exp(global_object, lhs_result, rhs_result);
  485. case BinaryOp::TypedEquals:
  486. return Value(strict_eq(lhs_result, rhs_result));
  487. case BinaryOp::TypedInequals:
  488. return Value(!strict_eq(lhs_result, rhs_result));
  489. case BinaryOp::AbstractEquals:
  490. return Value(abstract_eq(global_object, lhs_result, rhs_result));
  491. case BinaryOp::AbstractInequals:
  492. return Value(!abstract_eq(global_object, lhs_result, rhs_result));
  493. case BinaryOp::GreaterThan:
  494. return greater_than(global_object, lhs_result, rhs_result);
  495. case BinaryOp::GreaterThanEquals:
  496. return greater_than_equals(global_object, lhs_result, rhs_result);
  497. case BinaryOp::LessThan:
  498. return less_than(global_object, lhs_result, rhs_result);
  499. case BinaryOp::LessThanEquals:
  500. return less_than_equals(global_object, lhs_result, rhs_result);
  501. case BinaryOp::BitwiseAnd:
  502. return bitwise_and(global_object, lhs_result, rhs_result);
  503. case BinaryOp::BitwiseOr:
  504. return bitwise_or(global_object, lhs_result, rhs_result);
  505. case BinaryOp::BitwiseXor:
  506. return bitwise_xor(global_object, lhs_result, rhs_result);
  507. case BinaryOp::LeftShift:
  508. return left_shift(global_object, lhs_result, rhs_result);
  509. case BinaryOp::RightShift:
  510. return right_shift(global_object, lhs_result, rhs_result);
  511. case BinaryOp::UnsignedRightShift:
  512. return unsigned_right_shift(global_object, lhs_result, rhs_result);
  513. case BinaryOp::In:
  514. return in(global_object, lhs_result, rhs_result);
  515. case BinaryOp::InstanceOf:
  516. return instance_of(global_object, lhs_result, rhs_result);
  517. }
  518. ASSERT_NOT_REACHED();
  519. }
  520. Value LogicalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  521. {
  522. auto lhs_result = m_lhs->execute(interpreter, global_object);
  523. if (interpreter.exception())
  524. return {};
  525. switch (m_op) {
  526. case LogicalOp::And:
  527. if (lhs_result.to_boolean()) {
  528. auto rhs_result = m_rhs->execute(interpreter, global_object);
  529. if (interpreter.exception())
  530. return {};
  531. return rhs_result;
  532. }
  533. return lhs_result;
  534. case LogicalOp::Or: {
  535. if (lhs_result.to_boolean())
  536. return lhs_result;
  537. auto rhs_result = m_rhs->execute(interpreter, global_object);
  538. if (interpreter.exception())
  539. return {};
  540. return rhs_result;
  541. }
  542. case LogicalOp::NullishCoalescing:
  543. if (lhs_result.is_nullish()) {
  544. auto rhs_result = m_rhs->execute(interpreter, global_object);
  545. if (interpreter.exception())
  546. return {};
  547. return rhs_result;
  548. }
  549. return lhs_result;
  550. }
  551. ASSERT_NOT_REACHED();
  552. }
  553. Reference Expression::to_reference(Interpreter&, GlobalObject&) const
  554. {
  555. return {};
  556. }
  557. Reference Identifier::to_reference(Interpreter& interpreter, GlobalObject&) const
  558. {
  559. return interpreter.vm().get_reference(string());
  560. }
  561. Reference MemberExpression::to_reference(Interpreter& interpreter, GlobalObject& global_object) const
  562. {
  563. auto object_value = m_object->execute(interpreter, global_object);
  564. if (interpreter.exception())
  565. return {};
  566. auto property_name = computed_property_name(interpreter, global_object);
  567. if (!property_name.is_valid())
  568. return {};
  569. return { object_value, property_name };
  570. }
  571. Value UnaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  572. {
  573. auto& vm = interpreter.vm();
  574. if (m_op == UnaryOp::Delete) {
  575. auto reference = m_lhs->to_reference(interpreter, global_object);
  576. if (interpreter.exception())
  577. return {};
  578. if (reference.is_unresolvable())
  579. return Value(true);
  580. // FIXME: Support deleting locals
  581. ASSERT(!reference.is_local_variable());
  582. if (reference.is_global_variable())
  583. return global_object.delete_property(reference.name());
  584. auto* base_object = reference.base().to_object(global_object);
  585. if (!base_object)
  586. return {};
  587. return base_object->delete_property(reference.name());
  588. }
  589. Value lhs_result;
  590. if (m_op == UnaryOp::Typeof && m_lhs->is_identifier()) {
  591. auto reference = m_lhs->to_reference(interpreter, global_object);
  592. if (interpreter.exception()) {
  593. return {};
  594. }
  595. // FIXME: standard recommends checking with is_unresolvable but it ALWAYS return false here
  596. if (reference.is_local_variable() || reference.is_global_variable()) {
  597. auto name = reference.name();
  598. lhs_result = interpreter.vm().get_variable(name.to_string(), global_object).value_or(js_undefined());
  599. if (interpreter.exception())
  600. return {};
  601. }
  602. } else {
  603. lhs_result = m_lhs->execute(interpreter, global_object);
  604. if (interpreter.exception())
  605. return {};
  606. }
  607. switch (m_op) {
  608. case UnaryOp::BitwiseNot:
  609. return bitwise_not(global_object, lhs_result);
  610. case UnaryOp::Not:
  611. return Value(!lhs_result.to_boolean());
  612. case UnaryOp::Plus:
  613. return unary_plus(global_object, lhs_result);
  614. case UnaryOp::Minus:
  615. return unary_minus(global_object, lhs_result);
  616. case UnaryOp::Typeof:
  617. switch (lhs_result.type()) {
  618. case Value::Type::Empty:
  619. ASSERT_NOT_REACHED();
  620. return {};
  621. case Value::Type::Undefined:
  622. return js_string(vm, "undefined");
  623. case Value::Type::Null:
  624. // yes, this is on purpose. yes, this is how javascript works.
  625. // yes, it's silly.
  626. return js_string(vm, "object");
  627. case Value::Type::Number:
  628. return js_string(vm, "number");
  629. case Value::Type::String:
  630. return js_string(vm, "string");
  631. case Value::Type::Object:
  632. if (lhs_result.is_function())
  633. return js_string(vm, "function");
  634. return js_string(vm, "object");
  635. case Value::Type::Boolean:
  636. return js_string(vm, "boolean");
  637. case Value::Type::Symbol:
  638. return js_string(vm, "symbol");
  639. case Value::Type::BigInt:
  640. return js_string(vm, "bigint");
  641. default:
  642. ASSERT_NOT_REACHED();
  643. }
  644. case UnaryOp::Void:
  645. return js_undefined();
  646. case UnaryOp::Delete:
  647. ASSERT_NOT_REACHED();
  648. }
  649. ASSERT_NOT_REACHED();
  650. }
  651. Value SuperExpression::execute(Interpreter&, GlobalObject&) const
  652. {
  653. // The semantics for SuperExpressions are handled in CallExpression::compute_this_and_callee()
  654. ASSERT_NOT_REACHED();
  655. }
  656. Value ClassMethod::execute(Interpreter& interpreter, GlobalObject& global_object) const
  657. {
  658. return m_function->execute(interpreter, global_object);
  659. }
  660. Value ClassExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  661. {
  662. auto& vm = interpreter.vm();
  663. Value class_constructor_value = m_constructor->execute(interpreter, global_object);
  664. if (interpreter.exception())
  665. return {};
  666. update_function_name(class_constructor_value, m_name);
  667. ASSERT(class_constructor_value.is_function() && class_constructor_value.as_function().is_script_function());
  668. ScriptFunction* class_constructor = static_cast<ScriptFunction*>(&class_constructor_value.as_function());
  669. class_constructor->set_is_class_constructor();
  670. Value super_constructor = js_undefined();
  671. if (!m_super_class.is_null()) {
  672. super_constructor = m_super_class->execute(interpreter, global_object);
  673. if (interpreter.exception())
  674. return {};
  675. if (!super_constructor.is_function() && !super_constructor.is_null()) {
  676. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ClassDoesNotExtendAConstructorOrNull, super_constructor.to_string_without_side_effects());
  677. return {};
  678. }
  679. class_constructor->set_constructor_kind(Function::ConstructorKind::Derived);
  680. Object* prototype = Object::create_empty(global_object);
  681. Object* super_constructor_prototype = nullptr;
  682. if (!super_constructor.is_null()) {
  683. super_constructor_prototype = &super_constructor.as_object().get(vm.names.prototype).as_object();
  684. if (interpreter.exception())
  685. return {};
  686. }
  687. prototype->set_prototype(super_constructor_prototype);
  688. prototype->define_property(vm.names.constructor, class_constructor, 0);
  689. if (interpreter.exception())
  690. return {};
  691. class_constructor->define_property(vm.names.prototype, prototype, Attribute::Writable);
  692. if (interpreter.exception())
  693. return {};
  694. class_constructor->set_prototype(super_constructor.is_null() ? global_object.function_prototype() : &super_constructor.as_object());
  695. }
  696. auto class_prototype = class_constructor->get(vm.names.prototype);
  697. if (interpreter.exception())
  698. return {};
  699. if (!class_prototype.is_object()) {
  700. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Class prototype");
  701. return {};
  702. }
  703. for (const auto& method : m_methods) {
  704. auto method_value = method.execute(interpreter, global_object);
  705. if (interpreter.exception())
  706. return {};
  707. auto& method_function = method_value.as_function();
  708. auto key = method.key().execute(interpreter, global_object);
  709. if (interpreter.exception())
  710. return {};
  711. auto& target = method.is_static() ? *class_constructor : class_prototype.as_object();
  712. method_function.set_home_object(&target);
  713. if (method.kind() == ClassMethod::Kind::Method) {
  714. target.define_property(StringOrSymbol::from_value(global_object, key), method_value);
  715. } else {
  716. String accessor_name = [&] {
  717. switch (method.kind()) {
  718. case ClassMethod::Kind::Getter:
  719. return String::formatted("get {}", get_function_name(global_object, key));
  720. case ClassMethod::Kind::Setter:
  721. return String::formatted("set {}", get_function_name(global_object, key));
  722. default:
  723. ASSERT_NOT_REACHED();
  724. }
  725. }();
  726. update_function_name(method_value, accessor_name);
  727. target.define_accessor(StringOrSymbol::from_value(global_object, key), method_function, method.kind() == ClassMethod::Kind::Getter, Attribute::Configurable | Attribute::Enumerable);
  728. }
  729. if (interpreter.exception())
  730. return {};
  731. }
  732. return class_constructor;
  733. }
  734. Value ClassDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  735. {
  736. Value class_constructor = m_class_expression->execute(interpreter, global_object);
  737. if (interpreter.exception())
  738. return {};
  739. interpreter.current_scope()->put_to_scope(m_class_expression->name(), { class_constructor, DeclarationKind::Let });
  740. return js_undefined();
  741. }
  742. static void print_indent(int indent)
  743. {
  744. for (int i = 0; i < indent * 2; ++i)
  745. putchar(' ');
  746. }
  747. void ASTNode::dump(int indent) const
  748. {
  749. print_indent(indent);
  750. printf("%s\n", class_name());
  751. }
  752. void ScopeNode::dump(int indent) const
  753. {
  754. ASTNode::dump(indent);
  755. if (!m_variables.is_empty()) {
  756. print_indent(indent + 1);
  757. printf("(Variables)\n");
  758. for (auto& variable : m_variables)
  759. variable.dump(indent + 2);
  760. }
  761. if (!m_children.is_empty()) {
  762. print_indent(indent + 1);
  763. printf("(Children)\n");
  764. for (auto& child : children())
  765. child.dump(indent + 2);
  766. }
  767. }
  768. void BinaryExpression::dump(int indent) const
  769. {
  770. const char* op_string = nullptr;
  771. switch (m_op) {
  772. case BinaryOp::Addition:
  773. op_string = "+";
  774. break;
  775. case BinaryOp::Subtraction:
  776. op_string = "-";
  777. break;
  778. case BinaryOp::Multiplication:
  779. op_string = "*";
  780. break;
  781. case BinaryOp::Division:
  782. op_string = "/";
  783. break;
  784. case BinaryOp::Modulo:
  785. op_string = "%";
  786. break;
  787. case BinaryOp::Exponentiation:
  788. op_string = "**";
  789. break;
  790. case BinaryOp::TypedEquals:
  791. op_string = "===";
  792. break;
  793. case BinaryOp::TypedInequals:
  794. op_string = "!==";
  795. break;
  796. case BinaryOp::AbstractEquals:
  797. op_string = "==";
  798. break;
  799. case BinaryOp::AbstractInequals:
  800. op_string = "!=";
  801. break;
  802. case BinaryOp::GreaterThan:
  803. op_string = ">";
  804. break;
  805. case BinaryOp::GreaterThanEquals:
  806. op_string = ">=";
  807. break;
  808. case BinaryOp::LessThan:
  809. op_string = "<";
  810. break;
  811. case BinaryOp::LessThanEquals:
  812. op_string = "<=";
  813. break;
  814. case BinaryOp::BitwiseAnd:
  815. op_string = "&";
  816. break;
  817. case BinaryOp::BitwiseOr:
  818. op_string = "|";
  819. break;
  820. case BinaryOp::BitwiseXor:
  821. op_string = "^";
  822. break;
  823. case BinaryOp::LeftShift:
  824. op_string = "<<";
  825. break;
  826. case BinaryOp::RightShift:
  827. op_string = ">>";
  828. break;
  829. case BinaryOp::UnsignedRightShift:
  830. op_string = ">>>";
  831. break;
  832. case BinaryOp::In:
  833. op_string = "in";
  834. break;
  835. case BinaryOp::InstanceOf:
  836. op_string = "instanceof";
  837. break;
  838. }
  839. print_indent(indent);
  840. printf("%s\n", class_name());
  841. m_lhs->dump(indent + 1);
  842. print_indent(indent + 1);
  843. printf("%s\n", op_string);
  844. m_rhs->dump(indent + 1);
  845. }
  846. void LogicalExpression::dump(int indent) const
  847. {
  848. const char* op_string = nullptr;
  849. switch (m_op) {
  850. case LogicalOp::And:
  851. op_string = "&&";
  852. break;
  853. case LogicalOp::Or:
  854. op_string = "||";
  855. break;
  856. case LogicalOp::NullishCoalescing:
  857. op_string = "??";
  858. break;
  859. }
  860. print_indent(indent);
  861. printf("%s\n", class_name());
  862. m_lhs->dump(indent + 1);
  863. print_indent(indent + 1);
  864. printf("%s\n", op_string);
  865. m_rhs->dump(indent + 1);
  866. }
  867. void UnaryExpression::dump(int indent) const
  868. {
  869. const char* op_string = nullptr;
  870. switch (m_op) {
  871. case UnaryOp::BitwiseNot:
  872. op_string = "~";
  873. break;
  874. case UnaryOp::Not:
  875. op_string = "!";
  876. break;
  877. case UnaryOp::Plus:
  878. op_string = "+";
  879. break;
  880. case UnaryOp::Minus:
  881. op_string = "-";
  882. break;
  883. case UnaryOp::Typeof:
  884. op_string = "typeof ";
  885. break;
  886. case UnaryOp::Void:
  887. op_string = "void ";
  888. break;
  889. case UnaryOp::Delete:
  890. op_string = "delete ";
  891. break;
  892. }
  893. print_indent(indent);
  894. printf("%s\n", class_name());
  895. print_indent(indent + 1);
  896. printf("%s\n", op_string);
  897. m_lhs->dump(indent + 1);
  898. }
  899. void CallExpression::dump(int indent) const
  900. {
  901. print_indent(indent);
  902. printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
  903. m_callee->dump(indent + 1);
  904. for (auto& argument : m_arguments)
  905. argument.value->dump(indent + 1);
  906. }
  907. void ClassDeclaration::dump(int indent) const
  908. {
  909. ASTNode::dump(indent);
  910. m_class_expression->dump(indent + 1);
  911. }
  912. void ClassExpression::dump(int indent) const
  913. {
  914. print_indent(indent);
  915. ASSERT(m_name.characters());
  916. printf("ClassExpression: \"%s\"\n", m_name.characters());
  917. print_indent(indent);
  918. printf("(Constructor)\n");
  919. m_constructor->dump(indent + 1);
  920. if (!m_super_class.is_null()) {
  921. print_indent(indent);
  922. printf("(Super Class)\n");
  923. m_super_class->dump(indent + 1);
  924. }
  925. print_indent(indent);
  926. printf("(Methods)\n");
  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. printf("(Key)\n");
  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. printf("Kind: %s\n", kind_string);
  950. print_indent(indent);
  951. printf("Static: %s\n", m_is_static ? "true" : "false");
  952. print_indent(indent);
  953. printf("(Function)\n");
  954. m_function->dump(indent + 1);
  955. }
  956. void StringLiteral::dump(int indent) const
  957. {
  958. print_indent(indent);
  959. printf("StringLiteral \"%s\"\n", m_value.characters());
  960. }
  961. void SuperExpression::dump(int indent) const
  962. {
  963. print_indent(indent);
  964. printf("super\n");
  965. }
  966. void NumericLiteral::dump(int indent) const
  967. {
  968. print_indent(indent);
  969. printf("NumericLiteral %g\n", m_value);
  970. }
  971. void BigIntLiteral::dump(int indent) const
  972. {
  973. print_indent(indent);
  974. printf("BigIntLiteral %s\n", m_value.characters());
  975. }
  976. void BooleanLiteral::dump(int indent) const
  977. {
  978. print_indent(indent);
  979. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  980. }
  981. void NullLiteral::dump(int indent) const
  982. {
  983. print_indent(indent);
  984. printf("null\n");
  985. }
  986. void FunctionNode::dump(int indent, const char* class_name) const
  987. {
  988. print_indent(indent);
  989. printf("%s '%s'\n", class_name, name().characters());
  990. if (!m_parameters.is_empty()) {
  991. print_indent(indent + 1);
  992. printf("(Parameters)\n");
  993. for (auto& parameter : m_parameters) {
  994. print_indent(indent + 2);
  995. if (parameter.is_rest)
  996. printf("...");
  997. printf("%s\n", parameter.name.characters());
  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. printf("(Variables)\n");
  1005. for (auto& variable : m_variables)
  1006. variable.dump(indent + 2);
  1007. }
  1008. print_indent(indent + 1);
  1009. printf("(Body)\n");
  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. printf("If\n");
  1031. predicate().dump(indent + 1);
  1032. consequent().dump(indent + 1);
  1033. if (alternate()) {
  1034. print_indent(indent);
  1035. printf("Else\n");
  1036. alternate()->dump(indent + 1);
  1037. }
  1038. }
  1039. void WhileStatement::dump(int indent) const
  1040. {
  1041. ASTNode::dump(indent);
  1042. print_indent(indent);
  1043. printf("While\n");
  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. printf("Object\n");
  1052. object().dump(indent + 2);
  1053. print_indent(indent + 1);
  1054. printf("Body\n");
  1055. body().dump(indent + 2);
  1056. }
  1057. void DoWhileStatement::dump(int indent) const
  1058. {
  1059. ASTNode::dump(indent);
  1060. print_indent(indent);
  1061. printf("DoWhile\n");
  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. printf("For\n");
  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. printf("ForIn\n");
  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. printf("ForOf\n");
  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. printf("Identifier \"%s\"\n", m_string.characters());
  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. printf("%s\n", 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. printf("%s\n", op_string);
  1343. }
  1344. m_argument->dump(indent + 1);
  1345. if (!m_prefixed) {
  1346. print_indent(indent + 1);
  1347. printf("%s\n", 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. printf("%s\n", 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. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  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. printf("%s %s\n", class_name(), name.characters());
  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. printf("%s (/%s/%s)\n", class_name(), content().characters(), flags().characters());
  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. printf("<empty>\n");
  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. printf("(Tag)\n");
  1629. m_tag->dump(indent + 2);
  1630. print_indent(indent + 1);
  1631. printf("(Template Literal)\n");
  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. printf("(Block)\n");
  1676. block().dump(indent + 1);
  1677. if (handler()) {
  1678. print_indent(indent);
  1679. printf("(Handler)\n");
  1680. handler()->dump(indent + 1);
  1681. }
  1682. if (finalizer()) {
  1683. print_indent(indent);
  1684. printf("(Finalizer)\n");
  1685. finalizer()->dump(indent + 1);
  1686. }
  1687. }
  1688. void CatchClause::dump(int indent) const
  1689. {
  1690. print_indent(indent);
  1691. printf("CatchClause");
  1692. if (!m_parameter.is_null())
  1693. printf(" (%s)", m_parameter.characters());
  1694. printf("\n");
  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. printf("(Test)\n");
  1806. m_test->dump(indent + 2);
  1807. } else {
  1808. printf("(Default)\n");
  1809. }
  1810. print_indent(indent + 1);
  1811. printf("(Consequent)\n");
  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. printf("(Test)\n");
  1835. m_test->dump(indent + 2);
  1836. print_indent(indent + 1);
  1837. printf("(Consequent)\n");
  1838. m_consequent->dump(indent + 2);
  1839. print_indent(indent + 1);
  1840. printf("(Alternate)\n");
  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. }