AST.cpp 73 KB

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