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