AST.cpp 73 KB

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