AST.cpp 64 KB

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