AST.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  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/Accessor.h>
  17. #include <LibJS/Runtime/Array.h>
  18. #include <LibJS/Runtime/BigInt.h>
  19. #include <LibJS/Runtime/Error.h>
  20. #include <LibJS/Runtime/GlobalObject.h>
  21. #include <LibJS/Runtime/IteratorOperations.h>
  22. #include <LibJS/Runtime/MarkedValueList.h>
  23. #include <LibJS/Runtime/NativeFunction.h>
  24. #include <LibJS/Runtime/ObjectEnvironmentRecord.h>
  25. #include <LibJS/Runtime/PrimitiveString.h>
  26. #include <LibJS/Runtime/Reference.h>
  27. #include <LibJS/Runtime/RegExpObject.h>
  28. #include <LibJS/Runtime/ScriptFunction.h>
  29. #include <LibJS/Runtime/Shape.h>
  30. #include <typeinfo>
  31. namespace JS {
  32. class InterpreterNodeScope {
  33. AK_MAKE_NONCOPYABLE(InterpreterNodeScope);
  34. AK_MAKE_NONMOVABLE(InterpreterNodeScope);
  35. public:
  36. InterpreterNodeScope(Interpreter& interpreter, ASTNode const& node)
  37. : m_interpreter(interpreter)
  38. , m_chain_node { nullptr, node }
  39. {
  40. m_interpreter.vm().call_frame().current_node = &node;
  41. m_interpreter.push_ast_node(m_chain_node);
  42. }
  43. ~InterpreterNodeScope()
  44. {
  45. m_interpreter.pop_ast_node();
  46. }
  47. private:
  48. Interpreter& m_interpreter;
  49. ExecutingASTNodeChain m_chain_node;
  50. };
  51. String ASTNode::class_name() const
  52. {
  53. // NOTE: We strip the "JS::" prefix.
  54. return demangle(typeid(*this).name()).substring(4);
  55. }
  56. static void update_function_name(Value value, FlyString const& name)
  57. {
  58. if (!value.is_function())
  59. return;
  60. auto& function = value.as_function();
  61. if (is<ScriptFunction>(function) && function.name().is_empty())
  62. static_cast<ScriptFunction&>(function).set_name(name);
  63. }
  64. static String get_function_name(GlobalObject& global_object, Value value)
  65. {
  66. if (value.is_symbol())
  67. return String::formatted("[{}]", value.as_symbol().description());
  68. if (value.is_string())
  69. return value.as_string().string();
  70. return value.to_string(global_object);
  71. }
  72. Value ScopeNode::execute(Interpreter& interpreter, GlobalObject& global_object) const
  73. {
  74. InterpreterNodeScope node_scope { interpreter, *this };
  75. return interpreter.execute_statement(global_object, *this);
  76. }
  77. Value Program::execute(Interpreter& interpreter, GlobalObject& global_object) const
  78. {
  79. InterpreterNodeScope node_scope { interpreter, *this };
  80. return interpreter.execute_statement(global_object, *this, ScopeType::Block);
  81. }
  82. Value FunctionDeclaration::execute(Interpreter& interpreter, GlobalObject&) const
  83. {
  84. InterpreterNodeScope node_scope { interpreter, *this };
  85. return {};
  86. }
  87. Value FunctionExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  88. {
  89. InterpreterNodeScope node_scope { interpreter, *this };
  90. return ScriptFunction::create(global_object, name(), body(), parameters(), function_length(), interpreter.current_scope(), kind(), is_strict_mode() || interpreter.vm().in_strict_mode(), is_arrow_function());
  91. }
  92. Value ExpressionStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  93. {
  94. InterpreterNodeScope node_scope { interpreter, *this };
  95. return m_expression->execute(interpreter, global_object);
  96. }
  97. CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter, GlobalObject& global_object) const
  98. {
  99. auto& vm = interpreter.vm();
  100. if (is<NewExpression>(*this)) {
  101. // Computing |this| is irrelevant for "new" expression.
  102. return { js_undefined(), m_callee->execute(interpreter, global_object) };
  103. }
  104. if (is<SuperExpression>(*m_callee)) {
  105. // If we are calling super, |this| has not been initialized yet, and would not be meaningful to provide.
  106. auto new_target = vm.get_new_target();
  107. VERIFY(new_target.is_function());
  108. return { js_undefined(), new_target };
  109. }
  110. if (is<MemberExpression>(*m_callee)) {
  111. auto& member_expression = static_cast<MemberExpression const&>(*m_callee);
  112. Value callee;
  113. Object* this_value = nullptr;
  114. if (is<SuperExpression>(member_expression.object())) {
  115. auto super_base = interpreter.current_environment()->get_super_base();
  116. if (super_base.is_nullish()) {
  117. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeNullOrUndefinedOnSuperPropertyAccess, super_base.to_string_without_side_effects());
  118. return {};
  119. }
  120. auto property_name = member_expression.computed_property_name(interpreter, global_object);
  121. if (!property_name.is_valid())
  122. return {};
  123. auto reference = Reference(super_base, property_name);
  124. callee = reference.get(global_object);
  125. if (vm.exception())
  126. return {};
  127. this_value = &vm.this_value(global_object).as_object();
  128. } else {
  129. auto reference = member_expression.to_reference(interpreter, global_object);
  130. if (vm.exception())
  131. return {};
  132. callee = reference.get(global_object);
  133. if (vm.exception())
  134. return {};
  135. this_value = reference.base().to_object(global_object);
  136. if (vm.exception())
  137. return {};
  138. }
  139. return { this_value, callee };
  140. }
  141. if (interpreter.vm().in_strict_mode()) {
  142. // If we are in strict mode, |this| should never be bound to global object by default.
  143. return { js_undefined(), m_callee->execute(interpreter, global_object) };
  144. }
  145. return { &global_object, m_callee->execute(interpreter, global_object) };
  146. }
  147. Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  148. {
  149. InterpreterNodeScope node_scope { interpreter, *this };
  150. auto& vm = interpreter.vm();
  151. auto [this_value, callee] = compute_this_and_callee(interpreter, global_object);
  152. if (vm.exception())
  153. return {};
  154. VERIFY(!callee.is_empty());
  155. if (!callee.is_function()
  156. || (is<NewExpression>(*this) && (is<NativeFunction>(callee.as_object()) && !static_cast<NativeFunction&>(callee.as_object()).has_constructor()))) {
  157. String error_message;
  158. auto call_type = is<NewExpression>(*this) ? "constructor" : "function";
  159. if (is<Identifier>(*m_callee) || is<MemberExpression>(*m_callee)) {
  160. String expression_string;
  161. if (is<Identifier>(*m_callee)) {
  162. expression_string = static_cast<Identifier const&>(*m_callee).string();
  163. } else {
  164. expression_string = static_cast<MemberExpression const&>(*m_callee).to_string_approximation();
  165. }
  166. vm.throw_exception<TypeError>(global_object, ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects(), call_type, expression_string);
  167. } else {
  168. vm.throw_exception<TypeError>(global_object, ErrorType::IsNotA, callee.to_string_without_side_effects(), call_type);
  169. }
  170. return {};
  171. }
  172. auto& function = callee.as_function();
  173. MarkedValueList arguments(vm.heap());
  174. arguments.ensure_capacity(m_arguments.size());
  175. for (auto& argument : m_arguments) {
  176. auto value = argument.value->execute(interpreter, global_object);
  177. if (vm.exception())
  178. return {};
  179. if (argument.is_spread) {
  180. get_iterator_values(global_object, value, [&](Value iterator_value) {
  181. if (vm.exception())
  182. return IterationDecision::Break;
  183. arguments.append(iterator_value);
  184. return IterationDecision::Continue;
  185. });
  186. if (vm.exception())
  187. return {};
  188. } else {
  189. arguments.append(value);
  190. }
  191. }
  192. vm.call_frame().current_node = interpreter.current_node();
  193. Object* new_object = nullptr;
  194. Value result;
  195. if (is<NewExpression>(*this)) {
  196. result = vm.construct(function, function, move(arguments));
  197. if (result.is_object())
  198. new_object = &result.as_object();
  199. } else if (is<SuperExpression>(*m_callee)) {
  200. // FIXME: This is merely a band-aid to make super() inside catch {} work (which constructs
  201. // a new DeclarativeEnvironmentRecord without current function). Implement GetSuperConstructor()
  202. // and subsequently GetThisEnvironment() instead.
  203. auto* function_environment = interpreter.current_environment();
  204. if (!function_environment->current_function())
  205. function_environment = static_cast<DeclarativeEnvironmentRecord*>(function_environment->outer_environment());
  206. auto* super_constructor = function_environment->current_function()->prototype();
  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. function_environment->bind_this_value(global_object, result);
  216. } else {
  217. result = vm.call(function, this_value, move(arguments));
  218. }
  219. if (vm.exception())
  220. return {};
  221. if (is<NewExpression>(*this)) {
  222. if (result.is_object())
  223. return result;
  224. return new_object;
  225. }
  226. return result;
  227. }
  228. Value YieldExpression::execute(Interpreter&, GlobalObject&) const
  229. {
  230. // This should be transformed to a return.
  231. VERIFY_NOT_REACHED();
  232. }
  233. Value ReturnStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  234. {
  235. InterpreterNodeScope node_scope { interpreter, *this };
  236. auto value = argument() ? argument()->execute(interpreter, global_object) : js_undefined();
  237. if (interpreter.exception())
  238. return {};
  239. interpreter.vm().unwind(ScopeType::Function);
  240. return value;
  241. }
  242. Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  243. {
  244. InterpreterNodeScope node_scope { interpreter, *this };
  245. auto predicate_result = m_predicate->execute(interpreter, global_object);
  246. if (interpreter.exception())
  247. return {};
  248. if (predicate_result.to_boolean())
  249. return interpreter.execute_statement(global_object, *m_consequent);
  250. if (m_alternate)
  251. return interpreter.execute_statement(global_object, *m_alternate);
  252. return js_undefined();
  253. }
  254. Value WithStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  255. {
  256. InterpreterNodeScope node_scope { interpreter, *this };
  257. auto object_value = m_object->execute(interpreter, global_object);
  258. if (interpreter.exception())
  259. return {};
  260. auto* object = object_value.to_object(global_object);
  261. if (interpreter.exception())
  262. return {};
  263. VERIFY(object);
  264. auto* object_environment_record = interpreter.heap().allocate<ObjectEnvironmentRecord>(global_object, *object, interpreter.vm().call_frame().environment_record);
  265. TemporaryChange<EnvironmentRecord*> scope_change(interpreter.vm().call_frame().environment_record, object_environment_record);
  266. return interpreter.execute_statement(global_object, m_body).value_or(js_undefined());
  267. }
  268. Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  269. {
  270. InterpreterNodeScope node_scope { interpreter, *this };
  271. auto last_value = js_undefined();
  272. for (;;) {
  273. auto test_result = m_test->execute(interpreter, global_object);
  274. if (interpreter.exception())
  275. return {};
  276. if (!test_result.to_boolean())
  277. break;
  278. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  279. if (interpreter.exception())
  280. return {};
  281. if (interpreter.vm().should_unwind()) {
  282. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  283. interpreter.vm().stop_unwind();
  284. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  285. interpreter.vm().stop_unwind();
  286. break;
  287. } else {
  288. return last_value;
  289. }
  290. }
  291. }
  292. return last_value;
  293. }
  294. Value DoWhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  295. {
  296. InterpreterNodeScope node_scope { interpreter, *this };
  297. auto last_value = js_undefined();
  298. for (;;) {
  299. if (interpreter.exception())
  300. return {};
  301. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  302. if (interpreter.exception())
  303. return {};
  304. if (interpreter.vm().should_unwind()) {
  305. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  306. interpreter.vm().stop_unwind();
  307. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  308. interpreter.vm().stop_unwind();
  309. break;
  310. } else {
  311. return last_value;
  312. }
  313. }
  314. auto test_result = m_test->execute(interpreter, global_object);
  315. if (interpreter.exception())
  316. return {};
  317. if (!test_result.to_boolean())
  318. break;
  319. }
  320. return last_value;
  321. }
  322. Value ForStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  323. {
  324. InterpreterNodeScope node_scope { interpreter, *this };
  325. RefPtr<BlockStatement> wrapper;
  326. if (m_init && is<VariableDeclaration>(*m_init) && static_cast<VariableDeclaration const&>(*m_init).declaration_kind() != DeclarationKind::Var) {
  327. wrapper = create_ast_node<BlockStatement>(source_range());
  328. NonnullRefPtrVector<VariableDeclaration> decls;
  329. decls.append(*static_cast<VariableDeclaration const*>(m_init.ptr()));
  330. wrapper->add_variables(decls);
  331. interpreter.enter_scope(*wrapper, ScopeType::Block, global_object);
  332. }
  333. auto wrapper_cleanup = ScopeGuard([&] {
  334. if (wrapper)
  335. interpreter.exit_scope(*wrapper);
  336. });
  337. auto last_value = js_undefined();
  338. if (m_init) {
  339. m_init->execute(interpreter, global_object);
  340. if (interpreter.exception())
  341. return {};
  342. }
  343. if (m_test) {
  344. while (true) {
  345. auto test_result = m_test->execute(interpreter, global_object);
  346. if (interpreter.exception())
  347. return {};
  348. if (!test_result.to_boolean())
  349. break;
  350. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  351. if (interpreter.exception())
  352. return {};
  353. if (interpreter.vm().should_unwind()) {
  354. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  355. interpreter.vm().stop_unwind();
  356. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  357. interpreter.vm().stop_unwind();
  358. break;
  359. } else {
  360. return last_value;
  361. }
  362. }
  363. if (m_update) {
  364. m_update->execute(interpreter, global_object);
  365. if (interpreter.exception())
  366. return {};
  367. }
  368. }
  369. } else {
  370. while (true) {
  371. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  372. if (interpreter.exception())
  373. return {};
  374. if (interpreter.vm().should_unwind()) {
  375. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  376. interpreter.vm().stop_unwind();
  377. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  378. interpreter.vm().stop_unwind();
  379. break;
  380. } else {
  381. return last_value;
  382. }
  383. }
  384. if (m_update) {
  385. m_update->execute(interpreter, global_object);
  386. if (interpreter.exception())
  387. return {};
  388. }
  389. }
  390. }
  391. return last_value;
  392. }
  393. static Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>> variable_from_for_declaration(Interpreter& interpreter, GlobalObject& global_object, ASTNode const& node, RefPtr<BlockStatement> wrapper)
  394. {
  395. if (is<VariableDeclaration>(node)) {
  396. auto& variable_declaration = static_cast<VariableDeclaration const&>(node);
  397. VERIFY(!variable_declaration.declarations().is_empty());
  398. if (variable_declaration.declaration_kind() != DeclarationKind::Var) {
  399. wrapper = create_ast_node<BlockStatement>(node.source_range());
  400. interpreter.enter_scope(*wrapper, ScopeType::Block, global_object);
  401. }
  402. variable_declaration.execute(interpreter, global_object);
  403. return variable_declaration.declarations().first().target();
  404. }
  405. if (is<Identifier>(node)) {
  406. return NonnullRefPtr(static_cast<Identifier const&>(node));
  407. }
  408. VERIFY_NOT_REACHED();
  409. }
  410. Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  411. {
  412. InterpreterNodeScope node_scope { interpreter, *this };
  413. bool has_declaration = is<VariableDeclaration>(*m_lhs);
  414. if (!has_declaration && !is<Identifier>(*m_lhs)) {
  415. // FIXME: Implement "for (foo.bar in baz)", "for (foo[0] in bar)"
  416. VERIFY_NOT_REACHED();
  417. }
  418. RefPtr<BlockStatement> wrapper;
  419. auto target = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  420. auto wrapper_cleanup = ScopeGuard([&] {
  421. if (wrapper)
  422. interpreter.exit_scope(*wrapper);
  423. });
  424. auto last_value = js_undefined();
  425. auto rhs_result = m_rhs->execute(interpreter, global_object);
  426. if (interpreter.exception())
  427. return {};
  428. if (rhs_result.is_nullish())
  429. return {};
  430. auto* object = rhs_result.to_object(global_object);
  431. while (object) {
  432. auto property_names = object->get_enumerable_own_property_names(Object::PropertyKind::Key);
  433. for (auto& value : property_names) {
  434. interpreter.vm().assign(target, value, global_object, has_declaration);
  435. if (interpreter.exception())
  436. return {};
  437. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  438. if (interpreter.exception())
  439. return {};
  440. if (interpreter.vm().should_unwind()) {
  441. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  442. interpreter.vm().stop_unwind();
  443. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  444. interpreter.vm().stop_unwind();
  445. break;
  446. } else {
  447. return last_value;
  448. }
  449. }
  450. }
  451. object = object->prototype();
  452. if (interpreter.exception())
  453. return {};
  454. }
  455. return last_value;
  456. }
  457. Value ForOfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  458. {
  459. InterpreterNodeScope node_scope { interpreter, *this };
  460. bool has_declaration = is<VariableDeclaration>(*m_lhs);
  461. if (!has_declaration && !is<Identifier>(*m_lhs)) {
  462. // FIXME: Implement "for (foo.bar of baz)", "for (foo[0] of bar)"
  463. VERIFY_NOT_REACHED();
  464. }
  465. RefPtr<BlockStatement> wrapper;
  466. auto target = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  467. auto wrapper_cleanup = ScopeGuard([&] {
  468. if (wrapper)
  469. interpreter.exit_scope(*wrapper);
  470. });
  471. auto last_value = js_undefined();
  472. auto rhs_result = m_rhs->execute(interpreter, global_object);
  473. if (interpreter.exception())
  474. return {};
  475. get_iterator_values(global_object, rhs_result, [&](Value value) {
  476. interpreter.vm().assign(target, value, global_object, has_declaration);
  477. last_value = interpreter.execute_statement(global_object, *m_body).value_or(last_value);
  478. if (interpreter.exception())
  479. return IterationDecision::Break;
  480. if (interpreter.vm().should_unwind()) {
  481. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  482. interpreter.vm().stop_unwind();
  483. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  484. interpreter.vm().stop_unwind();
  485. return IterationDecision::Break;
  486. } else {
  487. return IterationDecision::Break;
  488. }
  489. }
  490. return IterationDecision::Continue;
  491. });
  492. if (interpreter.exception())
  493. return {};
  494. return last_value;
  495. }
  496. Value BinaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  497. {
  498. InterpreterNodeScope node_scope { interpreter, *this };
  499. auto lhs_result = m_lhs->execute(interpreter, global_object);
  500. if (interpreter.exception())
  501. return {};
  502. auto rhs_result = m_rhs->execute(interpreter, global_object);
  503. if (interpreter.exception())
  504. return {};
  505. switch (m_op) {
  506. case BinaryOp::Addition:
  507. return add(global_object, lhs_result, rhs_result);
  508. case BinaryOp::Subtraction:
  509. return sub(global_object, lhs_result, rhs_result);
  510. case BinaryOp::Multiplication:
  511. return mul(global_object, lhs_result, rhs_result);
  512. case BinaryOp::Division:
  513. return div(global_object, lhs_result, rhs_result);
  514. case BinaryOp::Modulo:
  515. return mod(global_object, lhs_result, rhs_result);
  516. case BinaryOp::Exponentiation:
  517. return exp(global_object, lhs_result, rhs_result);
  518. case BinaryOp::TypedEquals:
  519. return Value(strict_eq(lhs_result, rhs_result));
  520. case BinaryOp::TypedInequals:
  521. return Value(!strict_eq(lhs_result, rhs_result));
  522. case BinaryOp::AbstractEquals:
  523. return Value(abstract_eq(global_object, lhs_result, rhs_result));
  524. case BinaryOp::AbstractInequals:
  525. return Value(!abstract_eq(global_object, lhs_result, rhs_result));
  526. case BinaryOp::GreaterThan:
  527. return greater_than(global_object, lhs_result, rhs_result);
  528. case BinaryOp::GreaterThanEquals:
  529. return greater_than_equals(global_object, lhs_result, rhs_result);
  530. case BinaryOp::LessThan:
  531. return less_than(global_object, lhs_result, rhs_result);
  532. case BinaryOp::LessThanEquals:
  533. return less_than_equals(global_object, lhs_result, rhs_result);
  534. case BinaryOp::BitwiseAnd:
  535. return bitwise_and(global_object, lhs_result, rhs_result);
  536. case BinaryOp::BitwiseOr:
  537. return bitwise_or(global_object, lhs_result, rhs_result);
  538. case BinaryOp::BitwiseXor:
  539. return bitwise_xor(global_object, lhs_result, rhs_result);
  540. case BinaryOp::LeftShift:
  541. return left_shift(global_object, lhs_result, rhs_result);
  542. case BinaryOp::RightShift:
  543. return right_shift(global_object, lhs_result, rhs_result);
  544. case BinaryOp::UnsignedRightShift:
  545. return unsigned_right_shift(global_object, lhs_result, rhs_result);
  546. case BinaryOp::In:
  547. return in(global_object, lhs_result, rhs_result);
  548. case BinaryOp::InstanceOf:
  549. return instance_of(global_object, lhs_result, rhs_result);
  550. }
  551. VERIFY_NOT_REACHED();
  552. }
  553. Value LogicalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  554. {
  555. InterpreterNodeScope node_scope { interpreter, *this };
  556. auto lhs_result = m_lhs->execute(interpreter, global_object);
  557. if (interpreter.exception())
  558. return {};
  559. switch (m_op) {
  560. case LogicalOp::And:
  561. if (lhs_result.to_boolean()) {
  562. auto rhs_result = m_rhs->execute(interpreter, global_object);
  563. if (interpreter.exception())
  564. return {};
  565. return rhs_result;
  566. }
  567. return lhs_result;
  568. case LogicalOp::Or: {
  569. if (lhs_result.to_boolean())
  570. return lhs_result;
  571. auto rhs_result = m_rhs->execute(interpreter, global_object);
  572. if (interpreter.exception())
  573. return {};
  574. return rhs_result;
  575. }
  576. case LogicalOp::NullishCoalescing:
  577. if (lhs_result.is_nullish()) {
  578. auto rhs_result = m_rhs->execute(interpreter, global_object);
  579. if (interpreter.exception())
  580. return {};
  581. return rhs_result;
  582. }
  583. return lhs_result;
  584. }
  585. VERIFY_NOT_REACHED();
  586. }
  587. Reference Expression::to_reference(Interpreter&, GlobalObject&) const
  588. {
  589. return {};
  590. }
  591. Reference Identifier::to_reference(Interpreter& interpreter, GlobalObject&) const
  592. {
  593. if (m_argument_index.has_value())
  594. return Reference(Reference::CallFrameArgument, m_argument_index.value(), string());
  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.current_scope()->put_to_scope(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. if (m_argument_index.has_value())
  1163. return interpreter.vm().argument(m_argument_index.value());
  1164. auto value = interpreter.vm().get_variable(string(), global_object);
  1165. if (value.is_empty()) {
  1166. if (!interpreter.exception())
  1167. interpreter.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, string());
  1168. return {};
  1169. }
  1170. return value;
  1171. }
  1172. void Identifier::dump(int indent) const
  1173. {
  1174. print_indent(indent);
  1175. if (m_argument_index.has_value())
  1176. outln("Identifier \"{}\" (argument #{})", m_string, m_argument_index.value());
  1177. else
  1178. outln("Identifier \"{}\"", m_string);
  1179. }
  1180. void SpreadExpression::dump(int indent) const
  1181. {
  1182. ASTNode::dump(indent);
  1183. m_target->dump(indent + 1);
  1184. }
  1185. Value SpreadExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1186. {
  1187. InterpreterNodeScope node_scope { interpreter, *this };
  1188. return m_target->execute(interpreter, global_object);
  1189. }
  1190. Value ThisExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1191. {
  1192. InterpreterNodeScope node_scope { interpreter, *this };
  1193. return interpreter.vm().resolve_this_binding(global_object);
  1194. }
  1195. void ThisExpression::dump(int indent) const
  1196. {
  1197. ASTNode::dump(indent);
  1198. }
  1199. Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1200. {
  1201. InterpreterNodeScope node_scope { interpreter, *this };
  1202. #define EXECUTE_LHS_AND_RHS() \
  1203. do { \
  1204. lhs_result = m_lhs->execute(interpreter, global_object); \
  1205. if (interpreter.exception()) \
  1206. return {}; \
  1207. rhs_result = m_rhs->execute(interpreter, global_object); \
  1208. if (interpreter.exception()) \
  1209. return {}; \
  1210. } while (0)
  1211. Value lhs_result;
  1212. Value rhs_result;
  1213. switch (m_op) {
  1214. case AssignmentOp::Assignment:
  1215. break;
  1216. case AssignmentOp::AdditionAssignment:
  1217. EXECUTE_LHS_AND_RHS();
  1218. rhs_result = add(global_object, lhs_result, rhs_result);
  1219. break;
  1220. case AssignmentOp::SubtractionAssignment:
  1221. EXECUTE_LHS_AND_RHS();
  1222. rhs_result = sub(global_object, lhs_result, rhs_result);
  1223. break;
  1224. case AssignmentOp::MultiplicationAssignment:
  1225. EXECUTE_LHS_AND_RHS();
  1226. rhs_result = mul(global_object, lhs_result, rhs_result);
  1227. break;
  1228. case AssignmentOp::DivisionAssignment:
  1229. EXECUTE_LHS_AND_RHS();
  1230. rhs_result = div(global_object, lhs_result, rhs_result);
  1231. break;
  1232. case AssignmentOp::ModuloAssignment:
  1233. EXECUTE_LHS_AND_RHS();
  1234. rhs_result = mod(global_object, lhs_result, rhs_result);
  1235. break;
  1236. case AssignmentOp::ExponentiationAssignment:
  1237. EXECUTE_LHS_AND_RHS();
  1238. rhs_result = exp(global_object, lhs_result, rhs_result);
  1239. break;
  1240. case AssignmentOp::BitwiseAndAssignment:
  1241. EXECUTE_LHS_AND_RHS();
  1242. rhs_result = bitwise_and(global_object, lhs_result, rhs_result);
  1243. break;
  1244. case AssignmentOp::BitwiseOrAssignment:
  1245. EXECUTE_LHS_AND_RHS();
  1246. rhs_result = bitwise_or(global_object, lhs_result, rhs_result);
  1247. break;
  1248. case AssignmentOp::BitwiseXorAssignment:
  1249. EXECUTE_LHS_AND_RHS();
  1250. rhs_result = bitwise_xor(global_object, lhs_result, rhs_result);
  1251. break;
  1252. case AssignmentOp::LeftShiftAssignment:
  1253. EXECUTE_LHS_AND_RHS();
  1254. rhs_result = left_shift(global_object, lhs_result, rhs_result);
  1255. break;
  1256. case AssignmentOp::RightShiftAssignment:
  1257. EXECUTE_LHS_AND_RHS();
  1258. rhs_result = right_shift(global_object, lhs_result, rhs_result);
  1259. break;
  1260. case AssignmentOp::UnsignedRightShiftAssignment:
  1261. EXECUTE_LHS_AND_RHS();
  1262. rhs_result = unsigned_right_shift(global_object, lhs_result, rhs_result);
  1263. break;
  1264. case AssignmentOp::AndAssignment:
  1265. lhs_result = m_lhs->execute(interpreter, global_object);
  1266. if (interpreter.exception())
  1267. return {};
  1268. if (!lhs_result.to_boolean())
  1269. return lhs_result;
  1270. rhs_result = m_rhs->execute(interpreter, global_object);
  1271. break;
  1272. case AssignmentOp::OrAssignment:
  1273. lhs_result = m_lhs->execute(interpreter, global_object);
  1274. if (interpreter.exception())
  1275. return {};
  1276. if (lhs_result.to_boolean())
  1277. return lhs_result;
  1278. rhs_result = m_rhs->execute(interpreter, global_object);
  1279. break;
  1280. case AssignmentOp::NullishAssignment:
  1281. lhs_result = m_lhs->execute(interpreter, global_object);
  1282. if (interpreter.exception())
  1283. return {};
  1284. if (!lhs_result.is_nullish())
  1285. return lhs_result;
  1286. rhs_result = m_rhs->execute(interpreter, global_object);
  1287. break;
  1288. }
  1289. if (interpreter.exception())
  1290. return {};
  1291. auto reference = m_lhs->to_reference(interpreter, global_object);
  1292. if (interpreter.exception())
  1293. return {};
  1294. if (m_op == AssignmentOp::Assignment) {
  1295. rhs_result = m_rhs->execute(interpreter, global_object);
  1296. if (interpreter.exception())
  1297. return {};
  1298. }
  1299. if (reference.is_unresolvable()) {
  1300. interpreter.vm().throw_exception<ReferenceError>(global_object, ErrorType::InvalidLeftHandAssignment);
  1301. return {};
  1302. }
  1303. reference.put(global_object, rhs_result);
  1304. if (interpreter.exception())
  1305. return {};
  1306. return rhs_result;
  1307. }
  1308. Value UpdateExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1309. {
  1310. InterpreterNodeScope node_scope { interpreter, *this };
  1311. auto reference = m_argument->to_reference(interpreter, global_object);
  1312. if (interpreter.exception())
  1313. return {};
  1314. auto old_value = reference.get(global_object);
  1315. if (interpreter.exception())
  1316. return {};
  1317. old_value = old_value.to_numeric(global_object);
  1318. if (interpreter.exception())
  1319. return {};
  1320. Value new_value;
  1321. switch (m_op) {
  1322. case UpdateOp::Increment:
  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().plus(Crypto::SignedBigInteger { 1 }));
  1327. break;
  1328. case UpdateOp::Decrement:
  1329. if (old_value.is_number())
  1330. new_value = Value(old_value.as_double() - 1);
  1331. else
  1332. new_value = js_bigint(interpreter.heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  1333. break;
  1334. default:
  1335. VERIFY_NOT_REACHED();
  1336. }
  1337. reference.put(global_object, new_value);
  1338. if (interpreter.exception())
  1339. return {};
  1340. return m_prefixed ? new_value : old_value;
  1341. }
  1342. void AssignmentExpression::dump(int indent) const
  1343. {
  1344. const char* op_string = nullptr;
  1345. switch (m_op) {
  1346. case AssignmentOp::Assignment:
  1347. op_string = "=";
  1348. break;
  1349. case AssignmentOp::AdditionAssignment:
  1350. op_string = "+=";
  1351. break;
  1352. case AssignmentOp::SubtractionAssignment:
  1353. op_string = "-=";
  1354. break;
  1355. case AssignmentOp::MultiplicationAssignment:
  1356. op_string = "*=";
  1357. break;
  1358. case AssignmentOp::DivisionAssignment:
  1359. op_string = "/=";
  1360. break;
  1361. case AssignmentOp::ModuloAssignment:
  1362. op_string = "%=";
  1363. break;
  1364. case AssignmentOp::ExponentiationAssignment:
  1365. op_string = "**=";
  1366. break;
  1367. case AssignmentOp::BitwiseAndAssignment:
  1368. op_string = "&=";
  1369. break;
  1370. case AssignmentOp::BitwiseOrAssignment:
  1371. op_string = "|=";
  1372. break;
  1373. case AssignmentOp::BitwiseXorAssignment:
  1374. op_string = "^=";
  1375. break;
  1376. case AssignmentOp::LeftShiftAssignment:
  1377. op_string = "<<=";
  1378. break;
  1379. case AssignmentOp::RightShiftAssignment:
  1380. op_string = ">>=";
  1381. break;
  1382. case AssignmentOp::UnsignedRightShiftAssignment:
  1383. op_string = ">>>=";
  1384. break;
  1385. case AssignmentOp::AndAssignment:
  1386. op_string = "&&=";
  1387. break;
  1388. case AssignmentOp::OrAssignment:
  1389. op_string = "||=";
  1390. break;
  1391. case AssignmentOp::NullishAssignment:
  1392. op_string = "\?\?=";
  1393. break;
  1394. }
  1395. ASTNode::dump(indent);
  1396. print_indent(indent + 1);
  1397. outln("{}", op_string);
  1398. m_lhs->dump(indent + 1);
  1399. m_rhs->dump(indent + 1);
  1400. }
  1401. void UpdateExpression::dump(int indent) const
  1402. {
  1403. const char* op_string = nullptr;
  1404. switch (m_op) {
  1405. case UpdateOp::Increment:
  1406. op_string = "++";
  1407. break;
  1408. case UpdateOp::Decrement:
  1409. op_string = "--";
  1410. break;
  1411. }
  1412. ASTNode::dump(indent);
  1413. if (m_prefixed) {
  1414. print_indent(indent + 1);
  1415. outln("{}", op_string);
  1416. }
  1417. m_argument->dump(indent + 1);
  1418. if (!m_prefixed) {
  1419. print_indent(indent + 1);
  1420. outln("{}", op_string);
  1421. }
  1422. }
  1423. Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1424. {
  1425. InterpreterNodeScope node_scope { interpreter, *this };
  1426. for (auto& declarator : m_declarations) {
  1427. if (auto* init = declarator.init()) {
  1428. auto initializer_result = init->execute(interpreter, global_object);
  1429. if (interpreter.exception())
  1430. return {};
  1431. declarator.target().visit(
  1432. [&](NonnullRefPtr<Identifier> const& id) {
  1433. auto variable_name = id->string();
  1434. if (is<ClassExpression>(*init))
  1435. update_function_name(initializer_result, variable_name);
  1436. interpreter.vm().set_variable(variable_name, initializer_result, global_object, true);
  1437. },
  1438. [&](NonnullRefPtr<BindingPattern> const& pattern) {
  1439. interpreter.vm().assign(pattern, initializer_result, global_object, true);
  1440. });
  1441. }
  1442. }
  1443. return {};
  1444. }
  1445. Value VariableDeclarator::execute(Interpreter& interpreter, GlobalObject&) const
  1446. {
  1447. InterpreterNodeScope node_scope { interpreter, *this };
  1448. // NOTE: VariableDeclarator execution is handled by VariableDeclaration.
  1449. VERIFY_NOT_REACHED();
  1450. }
  1451. void VariableDeclaration::dump(int indent) const
  1452. {
  1453. const char* declaration_kind_string = nullptr;
  1454. switch (m_declaration_kind) {
  1455. case DeclarationKind::Let:
  1456. declaration_kind_string = "Let";
  1457. break;
  1458. case DeclarationKind::Var:
  1459. declaration_kind_string = "Var";
  1460. break;
  1461. case DeclarationKind::Const:
  1462. declaration_kind_string = "Const";
  1463. break;
  1464. }
  1465. ASTNode::dump(indent);
  1466. print_indent(indent + 1);
  1467. outln("{}", declaration_kind_string);
  1468. for (auto& declarator : m_declarations)
  1469. declarator.dump(indent + 1);
  1470. }
  1471. void VariableDeclarator::dump(int indent) const
  1472. {
  1473. ASTNode::dump(indent);
  1474. m_target.visit([indent](const auto& value) { value->dump(indent + 1); });
  1475. if (m_init)
  1476. m_init->dump(indent + 1);
  1477. }
  1478. void ObjectProperty::dump(int indent) const
  1479. {
  1480. ASTNode::dump(indent);
  1481. m_key->dump(indent + 1);
  1482. m_value->dump(indent + 1);
  1483. }
  1484. void ObjectExpression::dump(int indent) const
  1485. {
  1486. ASTNode::dump(indent);
  1487. for (auto& property : m_properties) {
  1488. property.dump(indent + 1);
  1489. }
  1490. }
  1491. void ExpressionStatement::dump(int indent) const
  1492. {
  1493. ASTNode::dump(indent);
  1494. m_expression->dump(indent + 1);
  1495. }
  1496. Value ObjectProperty::execute(Interpreter& interpreter, GlobalObject&) const
  1497. {
  1498. InterpreterNodeScope node_scope { interpreter, *this };
  1499. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  1500. VERIFY_NOT_REACHED();
  1501. }
  1502. Value ObjectExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1503. {
  1504. InterpreterNodeScope node_scope { interpreter, *this };
  1505. auto* object = Object::create(global_object, global_object.object_prototype());
  1506. for (auto& property : m_properties) {
  1507. auto key = property.key().execute(interpreter, global_object);
  1508. if (interpreter.exception())
  1509. return {};
  1510. if (property.type() == ObjectProperty::Type::Spread) {
  1511. if (key.is_object() && key.as_object().is_array()) {
  1512. auto& array_to_spread = static_cast<Array&>(key.as_object());
  1513. for (auto& entry : array_to_spread.indexed_properties()) {
  1514. object->indexed_properties().put(object, entry.index(), entry.value_and_attributes(&array_to_spread).value);
  1515. if (interpreter.exception())
  1516. return {};
  1517. }
  1518. } else if (key.is_object()) {
  1519. auto& obj_to_spread = key.as_object();
  1520. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  1521. if (it.value.attributes.is_enumerable()) {
  1522. object->define_property(it.key, obj_to_spread.get(it.key));
  1523. if (interpreter.exception())
  1524. return {};
  1525. }
  1526. }
  1527. } else if (key.is_string()) {
  1528. auto& str_to_spread = key.as_string().string();
  1529. for (size_t i = 0; i < str_to_spread.length(); i++) {
  1530. object->define_property(i, js_string(interpreter.heap(), str_to_spread.substring(i, 1)));
  1531. if (interpreter.exception())
  1532. return {};
  1533. }
  1534. }
  1535. continue;
  1536. }
  1537. auto value = property.value().execute(interpreter, global_object);
  1538. if (interpreter.exception())
  1539. return {};
  1540. if (value.is_function() && property.is_method())
  1541. value.as_function().set_home_object(object);
  1542. String name = get_function_name(global_object, key);
  1543. if (property.type() == ObjectProperty::Type::Getter) {
  1544. name = String::formatted("get {}", name);
  1545. } else if (property.type() == ObjectProperty::Type::Setter) {
  1546. name = String::formatted("set {}", name);
  1547. }
  1548. update_function_name(value, name);
  1549. switch (property.type()) {
  1550. case ObjectProperty::Type::Getter:
  1551. VERIFY(value.is_function());
  1552. object->define_accessor(PropertyName::from_value(global_object, key), &value.as_function(), nullptr, Attribute::Configurable | Attribute::Enumerable);
  1553. break;
  1554. case ObjectProperty::Type::Setter:
  1555. VERIFY(value.is_function());
  1556. object->define_accessor(PropertyName::from_value(global_object, key), nullptr, &value.as_function(), Attribute::Configurable | Attribute::Enumerable);
  1557. break;
  1558. case ObjectProperty::Type::KeyValue:
  1559. object->define_property(PropertyName::from_value(global_object, key), value);
  1560. break;
  1561. case ObjectProperty::Type::Spread:
  1562. default:
  1563. VERIFY_NOT_REACHED();
  1564. }
  1565. if (interpreter.exception())
  1566. return {};
  1567. }
  1568. return object;
  1569. }
  1570. void MemberExpression::dump(int indent) const
  1571. {
  1572. print_indent(indent);
  1573. outln("{}(computed={})", class_name(), is_computed());
  1574. m_object->dump(indent + 1);
  1575. m_property->dump(indent + 1);
  1576. }
  1577. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter, GlobalObject& global_object) const
  1578. {
  1579. if (!is_computed()) {
  1580. VERIFY(is<Identifier>(*m_property));
  1581. return static_cast<Identifier const&>(*m_property).string();
  1582. }
  1583. auto value = m_property->execute(interpreter, global_object);
  1584. if (interpreter.exception())
  1585. return {};
  1586. VERIFY(!value.is_empty());
  1587. return PropertyName::from_value(global_object, value);
  1588. }
  1589. String MemberExpression::to_string_approximation() const
  1590. {
  1591. String object_string = "<object>";
  1592. if (is<Identifier>(*m_object))
  1593. object_string = static_cast<Identifier const&>(*m_object).string();
  1594. if (is_computed())
  1595. return String::formatted("{}[<computed>]", object_string);
  1596. VERIFY(is<Identifier>(*m_property));
  1597. return String::formatted("{}.{}", object_string, static_cast<Identifier const&>(*m_property).string());
  1598. }
  1599. Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1600. {
  1601. InterpreterNodeScope node_scope { interpreter, *this };
  1602. auto reference = to_reference(interpreter, global_object);
  1603. if (interpreter.exception())
  1604. return {};
  1605. return reference.get(global_object);
  1606. }
  1607. void MetaProperty::dump(int indent) const
  1608. {
  1609. String name;
  1610. if (m_type == MetaProperty::Type::NewTarget)
  1611. name = "new.target";
  1612. else if (m_type == MetaProperty::Type::ImportMeta)
  1613. name = "import.meta";
  1614. else
  1615. VERIFY_NOT_REACHED();
  1616. print_indent(indent);
  1617. outln("{} {}", class_name(), name);
  1618. }
  1619. Value MetaProperty::execute(Interpreter& interpreter, GlobalObject&) const
  1620. {
  1621. InterpreterNodeScope node_scope { interpreter, *this };
  1622. if (m_type == MetaProperty::Type::NewTarget)
  1623. return interpreter.vm().get_new_target().value_or(js_undefined());
  1624. if (m_type == MetaProperty::Type::ImportMeta)
  1625. TODO();
  1626. VERIFY_NOT_REACHED();
  1627. }
  1628. Value StringLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1629. {
  1630. InterpreterNodeScope node_scope { interpreter, *this };
  1631. return js_string(interpreter.heap(), m_value);
  1632. }
  1633. Value NumericLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1634. {
  1635. InterpreterNodeScope node_scope { interpreter, *this };
  1636. return Value(m_value);
  1637. }
  1638. Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1639. {
  1640. InterpreterNodeScope node_scope { interpreter, *this };
  1641. Crypto::SignedBigInteger integer;
  1642. if (m_value[0] == '0' && m_value.length() >= 3) {
  1643. if (m_value[1] == 'x' || m_value[1] == 'X') {
  1644. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base16(m_value.substring(2, m_value.length() - 3)));
  1645. } else if (m_value[1] == 'o' || m_value[1] == 'O') {
  1646. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base8(m_value.substring(2, m_value.length() - 3)));
  1647. } else if (m_value[1] == 'b' || m_value[1] == 'B') {
  1648. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base2(m_value.substring(2, m_value.length() - 3)));
  1649. }
  1650. }
  1651. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
  1652. }
  1653. Value BooleanLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1654. {
  1655. InterpreterNodeScope node_scope { interpreter, *this };
  1656. return Value(m_value);
  1657. }
  1658. Value NullLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1659. {
  1660. InterpreterNodeScope node_scope { interpreter, *this };
  1661. return js_null();
  1662. }
  1663. void RegExpLiteral::dump(int indent) const
  1664. {
  1665. print_indent(indent);
  1666. outln("{} (/{}/{})", class_name(), pattern(), flags());
  1667. }
  1668. Value RegExpLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1669. {
  1670. InterpreterNodeScope node_scope { interpreter, *this };
  1671. return RegExpObject::create(global_object, pattern(), flags());
  1672. }
  1673. void ArrayExpression::dump(int indent) const
  1674. {
  1675. ASTNode::dump(indent);
  1676. for (auto& element : m_elements) {
  1677. if (element) {
  1678. element->dump(indent + 1);
  1679. } else {
  1680. print_indent(indent + 1);
  1681. outln("<empty>");
  1682. }
  1683. }
  1684. }
  1685. Value ArrayExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1686. {
  1687. InterpreterNodeScope node_scope { interpreter, *this };
  1688. auto* array = Array::create(global_object);
  1689. for (auto& element : m_elements) {
  1690. auto value = Value();
  1691. if (element) {
  1692. value = element->execute(interpreter, global_object);
  1693. if (interpreter.exception())
  1694. return {};
  1695. if (is<SpreadExpression>(*element)) {
  1696. get_iterator_values(global_object, value, [&](Value iterator_value) {
  1697. array->indexed_properties().append(iterator_value);
  1698. return IterationDecision::Continue;
  1699. });
  1700. if (interpreter.exception())
  1701. return {};
  1702. continue;
  1703. }
  1704. }
  1705. array->indexed_properties().append(value);
  1706. }
  1707. return array;
  1708. }
  1709. void TemplateLiteral::dump(int indent) const
  1710. {
  1711. ASTNode::dump(indent);
  1712. for (auto& expression : m_expressions)
  1713. expression.dump(indent + 1);
  1714. }
  1715. Value TemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1716. {
  1717. InterpreterNodeScope node_scope { interpreter, *this };
  1718. StringBuilder string_builder;
  1719. for (auto& expression : m_expressions) {
  1720. auto expr = expression.execute(interpreter, global_object);
  1721. if (interpreter.exception())
  1722. return {};
  1723. auto string = expr.to_string(global_object);
  1724. if (interpreter.exception())
  1725. return {};
  1726. string_builder.append(string);
  1727. }
  1728. return js_string(interpreter.heap(), string_builder.build());
  1729. }
  1730. void TaggedTemplateLiteral::dump(int indent) const
  1731. {
  1732. ASTNode::dump(indent);
  1733. print_indent(indent + 1);
  1734. outln("(Tag)");
  1735. m_tag->dump(indent + 2);
  1736. print_indent(indent + 1);
  1737. outln("(Template Literal)");
  1738. m_template_literal->dump(indent + 2);
  1739. }
  1740. Value TaggedTemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1741. {
  1742. InterpreterNodeScope node_scope { interpreter, *this };
  1743. auto& vm = interpreter.vm();
  1744. auto tag = m_tag->execute(interpreter, global_object);
  1745. if (vm.exception())
  1746. return {};
  1747. if (!tag.is_function()) {
  1748. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, tag.to_string_without_side_effects());
  1749. return {};
  1750. }
  1751. auto& tag_function = tag.as_function();
  1752. auto& expressions = m_template_literal->expressions();
  1753. auto* strings = Array::create(global_object);
  1754. MarkedValueList arguments(vm.heap());
  1755. arguments.append(strings);
  1756. for (size_t i = 0; i < expressions.size(); ++i) {
  1757. auto value = expressions[i].execute(interpreter, global_object);
  1758. if (vm.exception())
  1759. return {};
  1760. // tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
  1761. // tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
  1762. if (i % 2 == 0) {
  1763. strings->indexed_properties().append(value);
  1764. } else {
  1765. arguments.append(value);
  1766. }
  1767. }
  1768. auto* raw_strings = Array::create(global_object);
  1769. for (auto& raw_string : m_template_literal->raw_strings()) {
  1770. auto value = raw_string.execute(interpreter, global_object);
  1771. if (vm.exception())
  1772. return {};
  1773. raw_strings->indexed_properties().append(value);
  1774. }
  1775. strings->define_property(vm.names.raw, raw_strings, 0);
  1776. return vm.call(tag_function, js_undefined(), move(arguments));
  1777. }
  1778. void TryStatement::dump(int indent) const
  1779. {
  1780. ASTNode::dump(indent);
  1781. print_indent(indent);
  1782. outln("(Block)");
  1783. block().dump(indent + 1);
  1784. if (handler()) {
  1785. print_indent(indent);
  1786. outln("(Handler)");
  1787. handler()->dump(indent + 1);
  1788. }
  1789. if (finalizer()) {
  1790. print_indent(indent);
  1791. outln("(Finalizer)");
  1792. finalizer()->dump(indent + 1);
  1793. }
  1794. }
  1795. void CatchClause::dump(int indent) const
  1796. {
  1797. print_indent(indent);
  1798. if (m_parameter.is_null())
  1799. outln("CatchClause");
  1800. else
  1801. outln("CatchClause ({})", m_parameter);
  1802. body().dump(indent + 1);
  1803. }
  1804. void ThrowStatement::dump(int indent) const
  1805. {
  1806. ASTNode::dump(indent);
  1807. argument().dump(indent + 1);
  1808. }
  1809. Value TryStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1810. {
  1811. InterpreterNodeScope node_scope { interpreter, *this };
  1812. auto result = interpreter.execute_statement(global_object, m_block, ScopeType::Try);
  1813. if (auto* exception = interpreter.exception()) {
  1814. if (m_handler) {
  1815. interpreter.vm().clear_exception();
  1816. HashMap<FlyString, Variable> parameters;
  1817. parameters.set(m_handler->parameter(), Variable { exception->value(), DeclarationKind::Var });
  1818. auto* catch_scope = interpreter.heap().allocate<DeclarativeEnvironmentRecord>(global_object, move(parameters), interpreter.vm().call_frame().environment_record);
  1819. TemporaryChange<EnvironmentRecord*> scope_change(interpreter.vm().call_frame().environment_record, catch_scope);
  1820. result = interpreter.execute_statement(global_object, m_handler->body());
  1821. }
  1822. }
  1823. if (m_finalizer) {
  1824. // Keep, if any, and then clear the current exception so we can
  1825. // execute() the finalizer without an exception in our way.
  1826. auto* previous_exception = interpreter.exception();
  1827. interpreter.vm().clear_exception();
  1828. // Remember what scope type we were unwinding to, and temporarily
  1829. // clear it as well (e.g. return from handler).
  1830. auto unwind_until = interpreter.vm().unwind_until();
  1831. interpreter.vm().stop_unwind();
  1832. auto finalizer_result = m_finalizer->execute(interpreter, global_object);
  1833. if (interpreter.vm().should_unwind()) {
  1834. // This was NOT a 'normal' completion (e.g. return from finalizer).
  1835. result = finalizer_result;
  1836. } else {
  1837. // Continue unwinding to whatever we found ourselves unwinding
  1838. // to when the finalizer was entered (e.g. return from handler,
  1839. // which is unaffected by normal completion from finalizer).
  1840. interpreter.vm().unwind(unwind_until);
  1841. // If we previously had an exception and the finalizer didn't
  1842. // throw a new one, restore the old one.
  1843. if (previous_exception && !interpreter.exception())
  1844. interpreter.vm().set_exception(*previous_exception);
  1845. }
  1846. }
  1847. return result.value_or(js_undefined());
  1848. }
  1849. Value CatchClause::execute(Interpreter& interpreter, GlobalObject&) const
  1850. {
  1851. InterpreterNodeScope node_scope { interpreter, *this };
  1852. // NOTE: CatchClause execution is handled by TryStatement.
  1853. VERIFY_NOT_REACHED();
  1854. return {};
  1855. }
  1856. Value ThrowStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1857. {
  1858. InterpreterNodeScope node_scope { interpreter, *this };
  1859. auto value = m_argument->execute(interpreter, global_object);
  1860. if (interpreter.vm().exception())
  1861. return {};
  1862. interpreter.vm().throw_exception(global_object, value);
  1863. return {};
  1864. }
  1865. Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1866. {
  1867. InterpreterNodeScope node_scope { interpreter, *this };
  1868. auto discriminant_result = m_discriminant->execute(interpreter, global_object);
  1869. if (interpreter.exception())
  1870. return {};
  1871. bool falling_through = false;
  1872. auto last_value = js_undefined();
  1873. for (auto& switch_case : m_cases) {
  1874. if (!falling_through && switch_case.test()) {
  1875. auto test_result = switch_case.test()->execute(interpreter, global_object);
  1876. if (interpreter.exception())
  1877. return {};
  1878. if (!strict_eq(discriminant_result, test_result))
  1879. continue;
  1880. }
  1881. falling_through = true;
  1882. for (auto& statement : switch_case.consequent()) {
  1883. auto value = statement.execute(interpreter, global_object);
  1884. if (!value.is_empty())
  1885. last_value = value;
  1886. if (interpreter.exception())
  1887. return {};
  1888. if (interpreter.vm().should_unwind()) {
  1889. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  1890. // No stop_unwind(), the outer loop will handle that - we just need to break out of the switch/case.
  1891. return last_value;
  1892. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  1893. interpreter.vm().stop_unwind();
  1894. return last_value;
  1895. } else {
  1896. return last_value;
  1897. }
  1898. }
  1899. }
  1900. }
  1901. return last_value;
  1902. }
  1903. Value SwitchCase::execute(Interpreter& interpreter, GlobalObject&) const
  1904. {
  1905. InterpreterNodeScope node_scope { interpreter, *this };
  1906. // NOTE: SwitchCase execution is handled by SwitchStatement.
  1907. VERIFY_NOT_REACHED();
  1908. return {};
  1909. }
  1910. Value BreakStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1911. {
  1912. InterpreterNodeScope node_scope { interpreter, *this };
  1913. interpreter.vm().unwind(ScopeType::Breakable, m_target_label);
  1914. return {};
  1915. }
  1916. Value ContinueStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1917. {
  1918. InterpreterNodeScope node_scope { interpreter, *this };
  1919. interpreter.vm().unwind(ScopeType::Continuable, m_target_label);
  1920. return {};
  1921. }
  1922. void SwitchStatement::dump(int indent) const
  1923. {
  1924. ASTNode::dump(indent);
  1925. m_discriminant->dump(indent + 1);
  1926. for (auto& switch_case : m_cases) {
  1927. switch_case.dump(indent + 1);
  1928. }
  1929. }
  1930. void SwitchCase::dump(int indent) const
  1931. {
  1932. ASTNode::dump(indent);
  1933. print_indent(indent + 1);
  1934. if (m_test) {
  1935. outln("(Test)");
  1936. m_test->dump(indent + 2);
  1937. } else {
  1938. outln("(Default)");
  1939. }
  1940. print_indent(indent + 1);
  1941. outln("(Consequent)");
  1942. for (auto& statement : m_consequent)
  1943. statement.dump(indent + 2);
  1944. }
  1945. Value ConditionalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1946. {
  1947. InterpreterNodeScope node_scope { interpreter, *this };
  1948. auto test_result = m_test->execute(interpreter, global_object);
  1949. if (interpreter.exception())
  1950. return {};
  1951. Value result;
  1952. if (test_result.to_boolean()) {
  1953. result = m_consequent->execute(interpreter, global_object);
  1954. } else {
  1955. result = m_alternate->execute(interpreter, global_object);
  1956. }
  1957. if (interpreter.exception())
  1958. return {};
  1959. return result;
  1960. }
  1961. void ConditionalExpression::dump(int indent) const
  1962. {
  1963. ASTNode::dump(indent);
  1964. print_indent(indent + 1);
  1965. outln("(Test)");
  1966. m_test->dump(indent + 2);
  1967. print_indent(indent + 1);
  1968. outln("(Consequent)");
  1969. m_consequent->dump(indent + 2);
  1970. print_indent(indent + 1);
  1971. outln("(Alternate)");
  1972. m_alternate->dump(indent + 2);
  1973. }
  1974. void SequenceExpression::dump(int indent) const
  1975. {
  1976. ASTNode::dump(indent);
  1977. for (auto& expression : m_expressions)
  1978. expression.dump(indent + 1);
  1979. }
  1980. Value SequenceExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1981. {
  1982. InterpreterNodeScope node_scope { interpreter, *this };
  1983. Value last_value;
  1984. for (auto& expression : m_expressions) {
  1985. last_value = expression.execute(interpreter, global_object);
  1986. if (interpreter.exception())
  1987. return {};
  1988. }
  1989. return last_value;
  1990. }
  1991. Value DebuggerStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1992. {
  1993. InterpreterNodeScope node_scope { interpreter, *this };
  1994. // Sorry, no JavaScript debugger available (yet)!
  1995. return {};
  1996. }
  1997. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1998. {
  1999. m_variables.extend(move(variables));
  2000. }
  2001. void ScopeNode::add_functions(NonnullRefPtrVector<FunctionDeclaration> functions)
  2002. {
  2003. m_functions.extend(move(functions));
  2004. }
  2005. }