AST.cpp 64 KB

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