AST.cpp 70 KB

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