AST.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258
  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/PrimitiveString.h>
  25. #include <LibJS/Runtime/Reference.h>
  26. #include <LibJS/Runtime/RegExpObject.h>
  27. #include <LibJS/Runtime/ScriptFunction.h>
  28. #include <LibJS/Runtime/Shape.h>
  29. #include <LibJS/Runtime/WithScope.h>
  30. #include <typeinfo>
  31. namespace JS {
  32. class InterpreterNodeScope {
  33. AK_MAKE_NONCOPYABLE(InterpreterNodeScope);
  34. AK_MAKE_NONMOVABLE(InterpreterNodeScope);
  35. public:
  36. InterpreterNodeScope(Interpreter& interpreter, 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 LexicalEnvironment 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<LexicalEnvironment*>(function_environment->parent());
  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* with_scope = interpreter.heap().allocate<WithScope>(global_object, *object, interpreter.vm().call_frame().scope);
  265. TemporaryChange<ScopeObject*> scope_change(interpreter.vm().call_frame().scope, with_scope);
  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& target = method.is_static() ? *class_constructor : class_prototype.as_object();
  722. method_function.set_home_object(&target);
  723. switch (method.kind()) {
  724. case ClassMethod::Kind::Method:
  725. target.define_property(key.to_property_key(global_object), method_value);
  726. break;
  727. case ClassMethod::Kind::Getter:
  728. update_function_name(method_value, String::formatted("get {}", get_function_name(global_object, key)));
  729. target.define_accessor(key.to_property_key(global_object), &method_function, nullptr, Attribute::Configurable | Attribute::Enumerable);
  730. break;
  731. case ClassMethod::Kind::Setter:
  732. update_function_name(method_value, String::formatted("set {}", get_function_name(global_object, key)));
  733. target.define_accessor(key.to_property_key(global_object), nullptr, &method_function, Attribute::Configurable | Attribute::Enumerable);
  734. break;
  735. default:
  736. VERIFY_NOT_REACHED();
  737. }
  738. if (interpreter.exception())
  739. return {};
  740. }
  741. return class_constructor;
  742. }
  743. Value ClassDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  744. {
  745. InterpreterNodeScope node_scope { interpreter, *this };
  746. Value class_constructor = m_class_expression->execute(interpreter, global_object);
  747. if (interpreter.exception())
  748. return {};
  749. interpreter.current_scope()->put_to_scope(m_class_expression->name(), { class_constructor, DeclarationKind::Let });
  750. return {};
  751. }
  752. static void print_indent(int indent)
  753. {
  754. out("{}", String::repeated(' ', indent * 2));
  755. }
  756. void ASTNode::dump(int indent) const
  757. {
  758. print_indent(indent);
  759. outln("{}", class_name());
  760. }
  761. void ScopeNode::dump(int indent) const
  762. {
  763. ASTNode::dump(indent);
  764. if (!m_variables.is_empty()) {
  765. print_indent(indent + 1);
  766. outln("(Variables)");
  767. for (auto& variable : m_variables)
  768. variable.dump(indent + 2);
  769. }
  770. if (!m_children.is_empty()) {
  771. print_indent(indent + 1);
  772. outln("(Children)");
  773. for (auto& child : children())
  774. child.dump(indent + 2);
  775. }
  776. }
  777. void BinaryExpression::dump(int indent) const
  778. {
  779. const char* op_string = nullptr;
  780. switch (m_op) {
  781. case BinaryOp::Addition:
  782. op_string = "+";
  783. break;
  784. case BinaryOp::Subtraction:
  785. op_string = "-";
  786. break;
  787. case BinaryOp::Multiplication:
  788. op_string = "*";
  789. break;
  790. case BinaryOp::Division:
  791. op_string = "/";
  792. break;
  793. case BinaryOp::Modulo:
  794. op_string = "%";
  795. break;
  796. case BinaryOp::Exponentiation:
  797. op_string = "**";
  798. break;
  799. case BinaryOp::TypedEquals:
  800. op_string = "===";
  801. break;
  802. case BinaryOp::TypedInequals:
  803. op_string = "!==";
  804. break;
  805. case BinaryOp::AbstractEquals:
  806. op_string = "==";
  807. break;
  808. case BinaryOp::AbstractInequals:
  809. op_string = "!=";
  810. break;
  811. case BinaryOp::GreaterThan:
  812. op_string = ">";
  813. break;
  814. case BinaryOp::GreaterThanEquals:
  815. op_string = ">=";
  816. break;
  817. case BinaryOp::LessThan:
  818. op_string = "<";
  819. break;
  820. case BinaryOp::LessThanEquals:
  821. op_string = "<=";
  822. break;
  823. case BinaryOp::BitwiseAnd:
  824. op_string = "&";
  825. break;
  826. case BinaryOp::BitwiseOr:
  827. op_string = "|";
  828. break;
  829. case BinaryOp::BitwiseXor:
  830. op_string = "^";
  831. break;
  832. case BinaryOp::LeftShift:
  833. op_string = "<<";
  834. break;
  835. case BinaryOp::RightShift:
  836. op_string = ">>";
  837. break;
  838. case BinaryOp::UnsignedRightShift:
  839. op_string = ">>>";
  840. break;
  841. case BinaryOp::In:
  842. op_string = "in";
  843. break;
  844. case BinaryOp::InstanceOf:
  845. op_string = "instanceof";
  846. break;
  847. }
  848. print_indent(indent);
  849. outln("{}", class_name());
  850. m_lhs->dump(indent + 1);
  851. print_indent(indent + 1);
  852. outln("{}", op_string);
  853. m_rhs->dump(indent + 1);
  854. }
  855. void LogicalExpression::dump(int indent) const
  856. {
  857. const char* op_string = nullptr;
  858. switch (m_op) {
  859. case LogicalOp::And:
  860. op_string = "&&";
  861. break;
  862. case LogicalOp::Or:
  863. op_string = "||";
  864. break;
  865. case LogicalOp::NullishCoalescing:
  866. op_string = "??";
  867. break;
  868. }
  869. print_indent(indent);
  870. outln("{}", class_name());
  871. m_lhs->dump(indent + 1);
  872. print_indent(indent + 1);
  873. outln("{}", op_string);
  874. m_rhs->dump(indent + 1);
  875. }
  876. void UnaryExpression::dump(int indent) const
  877. {
  878. const char* op_string = nullptr;
  879. switch (m_op) {
  880. case UnaryOp::BitwiseNot:
  881. op_string = "~";
  882. break;
  883. case UnaryOp::Not:
  884. op_string = "!";
  885. break;
  886. case UnaryOp::Plus:
  887. op_string = "+";
  888. break;
  889. case UnaryOp::Minus:
  890. op_string = "-";
  891. break;
  892. case UnaryOp::Typeof:
  893. op_string = "typeof ";
  894. break;
  895. case UnaryOp::Void:
  896. op_string = "void ";
  897. break;
  898. case UnaryOp::Delete:
  899. op_string = "delete ";
  900. break;
  901. }
  902. print_indent(indent);
  903. outln("{}", class_name());
  904. print_indent(indent + 1);
  905. outln("{}", op_string);
  906. m_lhs->dump(indent + 1);
  907. }
  908. void CallExpression::dump(int indent) const
  909. {
  910. print_indent(indent);
  911. if (is<NewExpression>(*this))
  912. outln("CallExpression [new]");
  913. else
  914. outln("CallExpression");
  915. m_callee->dump(indent + 1);
  916. for (auto& argument : m_arguments)
  917. argument.value->dump(indent + 1);
  918. }
  919. void ClassDeclaration::dump(int indent) const
  920. {
  921. ASTNode::dump(indent);
  922. m_class_expression->dump(indent + 1);
  923. }
  924. void ClassExpression::dump(int indent) const
  925. {
  926. print_indent(indent);
  927. outln("ClassExpression: \"{}\"", m_name);
  928. print_indent(indent);
  929. outln("(Constructor)");
  930. m_constructor->dump(indent + 1);
  931. if (!m_super_class.is_null()) {
  932. print_indent(indent);
  933. outln("(Super Class)");
  934. m_super_class->dump(indent + 1);
  935. }
  936. print_indent(indent);
  937. outln("(Methods)");
  938. for (auto& method : m_methods)
  939. method.dump(indent + 1);
  940. }
  941. void ClassMethod::dump(int indent) const
  942. {
  943. ASTNode::dump(indent);
  944. print_indent(indent);
  945. outln("(Key)");
  946. m_key->dump(indent + 1);
  947. const char* kind_string = nullptr;
  948. switch (m_kind) {
  949. case Kind::Method:
  950. kind_string = "Method";
  951. break;
  952. case Kind::Getter:
  953. kind_string = "Getter";
  954. break;
  955. case Kind::Setter:
  956. kind_string = "Setter";
  957. break;
  958. }
  959. print_indent(indent);
  960. outln("Kind: {}", kind_string);
  961. print_indent(indent);
  962. outln("Static: {}", m_is_static);
  963. print_indent(indent);
  964. outln("(Function)");
  965. m_function->dump(indent + 1);
  966. }
  967. void StringLiteral::dump(int indent) const
  968. {
  969. print_indent(indent);
  970. outln("StringLiteral \"{}\"", m_value);
  971. }
  972. void SuperExpression::dump(int indent) const
  973. {
  974. print_indent(indent);
  975. outln("super");
  976. }
  977. void NumericLiteral::dump(int indent) const
  978. {
  979. print_indent(indent);
  980. outln("NumericLiteral {}", m_value);
  981. }
  982. void BigIntLiteral::dump(int indent) const
  983. {
  984. print_indent(indent);
  985. outln("BigIntLiteral {}", m_value);
  986. }
  987. void BooleanLiteral::dump(int indent) const
  988. {
  989. print_indent(indent);
  990. outln("BooleanLiteral {}", m_value);
  991. }
  992. void NullLiteral::dump(int indent) const
  993. {
  994. print_indent(indent);
  995. outln("null");
  996. }
  997. void BindingPattern::dump(int indent) const
  998. {
  999. print_indent(indent);
  1000. outln("BindingPattern {}", kind == Kind::Array ? "Array" : "Object");
  1001. print_indent(++indent);
  1002. outln("(Properties)");
  1003. for (auto& property : properties) {
  1004. print_indent(indent + 1);
  1005. outln("(Identifier)");
  1006. if (property.name) {
  1007. property.name->dump(indent + 2);
  1008. } else {
  1009. print_indent(indent + 2);
  1010. outln("(None)");
  1011. }
  1012. print_indent(indent + 1);
  1013. outln("(Pattern)");
  1014. if (property.pattern) {
  1015. property.pattern->dump(indent + 2);
  1016. } else {
  1017. print_indent(indent + 2);
  1018. outln("(None)");
  1019. }
  1020. print_indent(indent + 1);
  1021. outln("(Is Rest = {})", property.is_rest);
  1022. }
  1023. }
  1024. void FunctionNode::dump(int indent, String const& class_name) const
  1025. {
  1026. print_indent(indent);
  1027. outln("{}{} '{}'", class_name, m_kind == FunctionKind::Generator ? "*" : "", name());
  1028. if (!m_parameters.is_empty()) {
  1029. print_indent(indent + 1);
  1030. outln("(Parameters)");
  1031. for (auto& parameter : m_parameters) {
  1032. print_indent(indent + 2);
  1033. if (parameter.is_rest)
  1034. out("...");
  1035. parameter.binding.visit(
  1036. [&](FlyString const& name) {
  1037. outln("{}", name);
  1038. },
  1039. [&](BindingPattern const& pattern) {
  1040. pattern.dump(indent + 2);
  1041. });
  1042. if (parameter.default_value)
  1043. parameter.default_value->dump(indent + 3);
  1044. }
  1045. }
  1046. if (!m_variables.is_empty()) {
  1047. print_indent(indent + 1);
  1048. outln("(Variables)");
  1049. for (auto& variable : m_variables)
  1050. variable.dump(indent + 2);
  1051. }
  1052. print_indent(indent + 1);
  1053. outln("(Body)");
  1054. body().dump(indent + 2);
  1055. }
  1056. void FunctionDeclaration::dump(int indent) const
  1057. {
  1058. FunctionNode::dump(indent, class_name());
  1059. }
  1060. void FunctionExpression::dump(int indent) const
  1061. {
  1062. FunctionNode::dump(indent, class_name());
  1063. }
  1064. void YieldExpression::dump(int indent) const
  1065. {
  1066. ASTNode::dump(indent);
  1067. if (argument())
  1068. argument()->dump(indent + 1);
  1069. }
  1070. void ReturnStatement::dump(int indent) const
  1071. {
  1072. ASTNode::dump(indent);
  1073. if (argument())
  1074. argument()->dump(indent + 1);
  1075. }
  1076. void IfStatement::dump(int indent) const
  1077. {
  1078. ASTNode::dump(indent);
  1079. print_indent(indent);
  1080. outln("If");
  1081. predicate().dump(indent + 1);
  1082. consequent().dump(indent + 1);
  1083. if (alternate()) {
  1084. print_indent(indent);
  1085. outln("Else");
  1086. alternate()->dump(indent + 1);
  1087. }
  1088. }
  1089. void WhileStatement::dump(int indent) const
  1090. {
  1091. ASTNode::dump(indent);
  1092. print_indent(indent);
  1093. outln("While");
  1094. test().dump(indent + 1);
  1095. body().dump(indent + 1);
  1096. }
  1097. void WithStatement::dump(int indent) const
  1098. {
  1099. ASTNode::dump(indent);
  1100. print_indent(indent + 1);
  1101. outln("Object");
  1102. object().dump(indent + 2);
  1103. print_indent(indent + 1);
  1104. outln("Body");
  1105. body().dump(indent + 2);
  1106. }
  1107. void DoWhileStatement::dump(int indent) const
  1108. {
  1109. ASTNode::dump(indent);
  1110. print_indent(indent);
  1111. outln("DoWhile");
  1112. test().dump(indent + 1);
  1113. body().dump(indent + 1);
  1114. }
  1115. void ForStatement::dump(int indent) const
  1116. {
  1117. ASTNode::dump(indent);
  1118. print_indent(indent);
  1119. outln("For");
  1120. if (init())
  1121. init()->dump(indent + 1);
  1122. if (test())
  1123. test()->dump(indent + 1);
  1124. if (update())
  1125. update()->dump(indent + 1);
  1126. body().dump(indent + 1);
  1127. }
  1128. void ForInStatement::dump(int indent) const
  1129. {
  1130. ASTNode::dump(indent);
  1131. print_indent(indent);
  1132. outln("ForIn");
  1133. lhs().dump(indent + 1);
  1134. rhs().dump(indent + 1);
  1135. body().dump(indent + 1);
  1136. }
  1137. void ForOfStatement::dump(int indent) const
  1138. {
  1139. ASTNode::dump(indent);
  1140. print_indent(indent);
  1141. outln("ForOf");
  1142. lhs().dump(indent + 1);
  1143. rhs().dump(indent + 1);
  1144. body().dump(indent + 1);
  1145. }
  1146. Value Identifier::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1147. {
  1148. InterpreterNodeScope node_scope { interpreter, *this };
  1149. if (m_argument_index.has_value())
  1150. return interpreter.vm().argument(m_argument_index.value());
  1151. auto value = interpreter.vm().get_variable(string(), global_object);
  1152. if (value.is_empty()) {
  1153. if (!interpreter.exception())
  1154. interpreter.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, string());
  1155. return {};
  1156. }
  1157. return value;
  1158. }
  1159. void Identifier::dump(int indent) const
  1160. {
  1161. print_indent(indent);
  1162. if (m_argument_index.has_value())
  1163. outln("Identifier \"{}\" (argument #{})", m_string, m_argument_index.value());
  1164. else
  1165. outln("Identifier \"{}\"", m_string);
  1166. }
  1167. void SpreadExpression::dump(int indent) const
  1168. {
  1169. ASTNode::dump(indent);
  1170. m_target->dump(indent + 1);
  1171. }
  1172. Value SpreadExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1173. {
  1174. InterpreterNodeScope node_scope { interpreter, *this };
  1175. return m_target->execute(interpreter, global_object);
  1176. }
  1177. Value ThisExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1178. {
  1179. InterpreterNodeScope node_scope { interpreter, *this };
  1180. return interpreter.vm().resolve_this_binding(global_object);
  1181. }
  1182. void ThisExpression::dump(int indent) const
  1183. {
  1184. ASTNode::dump(indent);
  1185. }
  1186. Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1187. {
  1188. InterpreterNodeScope node_scope { interpreter, *this };
  1189. #define EXECUTE_LHS_AND_RHS() \
  1190. do { \
  1191. lhs_result = m_lhs->execute(interpreter, global_object); \
  1192. if (interpreter.exception()) \
  1193. return {}; \
  1194. rhs_result = m_rhs->execute(interpreter, global_object); \
  1195. if (interpreter.exception()) \
  1196. return {}; \
  1197. } while (0)
  1198. Value lhs_result;
  1199. Value rhs_result;
  1200. switch (m_op) {
  1201. case AssignmentOp::Assignment:
  1202. break;
  1203. case AssignmentOp::AdditionAssignment:
  1204. EXECUTE_LHS_AND_RHS();
  1205. rhs_result = add(global_object, lhs_result, rhs_result);
  1206. break;
  1207. case AssignmentOp::SubtractionAssignment:
  1208. EXECUTE_LHS_AND_RHS();
  1209. rhs_result = sub(global_object, lhs_result, rhs_result);
  1210. break;
  1211. case AssignmentOp::MultiplicationAssignment:
  1212. EXECUTE_LHS_AND_RHS();
  1213. rhs_result = mul(global_object, lhs_result, rhs_result);
  1214. break;
  1215. case AssignmentOp::DivisionAssignment:
  1216. EXECUTE_LHS_AND_RHS();
  1217. rhs_result = div(global_object, lhs_result, rhs_result);
  1218. break;
  1219. case AssignmentOp::ModuloAssignment:
  1220. EXECUTE_LHS_AND_RHS();
  1221. rhs_result = mod(global_object, lhs_result, rhs_result);
  1222. break;
  1223. case AssignmentOp::ExponentiationAssignment:
  1224. EXECUTE_LHS_AND_RHS();
  1225. rhs_result = exp(global_object, lhs_result, rhs_result);
  1226. break;
  1227. case AssignmentOp::BitwiseAndAssignment:
  1228. EXECUTE_LHS_AND_RHS();
  1229. rhs_result = bitwise_and(global_object, lhs_result, rhs_result);
  1230. break;
  1231. case AssignmentOp::BitwiseOrAssignment:
  1232. EXECUTE_LHS_AND_RHS();
  1233. rhs_result = bitwise_or(global_object, lhs_result, rhs_result);
  1234. break;
  1235. case AssignmentOp::BitwiseXorAssignment:
  1236. EXECUTE_LHS_AND_RHS();
  1237. rhs_result = bitwise_xor(global_object, lhs_result, rhs_result);
  1238. break;
  1239. case AssignmentOp::LeftShiftAssignment:
  1240. EXECUTE_LHS_AND_RHS();
  1241. rhs_result = left_shift(global_object, lhs_result, rhs_result);
  1242. break;
  1243. case AssignmentOp::RightShiftAssignment:
  1244. EXECUTE_LHS_AND_RHS();
  1245. rhs_result = right_shift(global_object, lhs_result, rhs_result);
  1246. break;
  1247. case AssignmentOp::UnsignedRightShiftAssignment:
  1248. EXECUTE_LHS_AND_RHS();
  1249. rhs_result = unsigned_right_shift(global_object, lhs_result, rhs_result);
  1250. break;
  1251. case AssignmentOp::AndAssignment:
  1252. lhs_result = m_lhs->execute(interpreter, global_object);
  1253. if (interpreter.exception())
  1254. return {};
  1255. if (!lhs_result.to_boolean())
  1256. return lhs_result;
  1257. rhs_result = m_rhs->execute(interpreter, global_object);
  1258. break;
  1259. case AssignmentOp::OrAssignment:
  1260. lhs_result = m_lhs->execute(interpreter, global_object);
  1261. if (interpreter.exception())
  1262. return {};
  1263. if (lhs_result.to_boolean())
  1264. return lhs_result;
  1265. rhs_result = m_rhs->execute(interpreter, global_object);
  1266. break;
  1267. case AssignmentOp::NullishAssignment:
  1268. lhs_result = m_lhs->execute(interpreter, global_object);
  1269. if (interpreter.exception())
  1270. return {};
  1271. if (!lhs_result.is_nullish())
  1272. return lhs_result;
  1273. rhs_result = m_rhs->execute(interpreter, global_object);
  1274. break;
  1275. }
  1276. if (interpreter.exception())
  1277. return {};
  1278. auto reference = m_lhs->to_reference(interpreter, global_object);
  1279. if (interpreter.exception())
  1280. return {};
  1281. if (m_op == AssignmentOp::Assignment) {
  1282. rhs_result = m_rhs->execute(interpreter, global_object);
  1283. if (interpreter.exception())
  1284. return {};
  1285. }
  1286. if (reference.is_unresolvable()) {
  1287. interpreter.vm().throw_exception<ReferenceError>(global_object, ErrorType::InvalidLeftHandAssignment);
  1288. return {};
  1289. }
  1290. reference.put(global_object, rhs_result);
  1291. if (interpreter.exception())
  1292. return {};
  1293. return rhs_result;
  1294. }
  1295. Value UpdateExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1296. {
  1297. InterpreterNodeScope node_scope { interpreter, *this };
  1298. auto reference = m_argument->to_reference(interpreter, global_object);
  1299. if (interpreter.exception())
  1300. return {};
  1301. auto old_value = reference.get(global_object);
  1302. if (interpreter.exception())
  1303. return {};
  1304. old_value = old_value.to_numeric(global_object);
  1305. if (interpreter.exception())
  1306. return {};
  1307. Value new_value;
  1308. switch (m_op) {
  1309. case UpdateOp::Increment:
  1310. if (old_value.is_number())
  1311. new_value = Value(old_value.as_double() + 1);
  1312. else
  1313. new_value = js_bigint(interpreter.heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  1314. break;
  1315. case UpdateOp::Decrement:
  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().minus(Crypto::SignedBigInteger { 1 }));
  1320. break;
  1321. default:
  1322. VERIFY_NOT_REACHED();
  1323. }
  1324. reference.put(global_object, new_value);
  1325. if (interpreter.exception())
  1326. return {};
  1327. return m_prefixed ? new_value : old_value;
  1328. }
  1329. void AssignmentExpression::dump(int indent) const
  1330. {
  1331. const char* op_string = nullptr;
  1332. switch (m_op) {
  1333. case AssignmentOp::Assignment:
  1334. op_string = "=";
  1335. break;
  1336. case AssignmentOp::AdditionAssignment:
  1337. op_string = "+=";
  1338. break;
  1339. case AssignmentOp::SubtractionAssignment:
  1340. op_string = "-=";
  1341. break;
  1342. case AssignmentOp::MultiplicationAssignment:
  1343. op_string = "*=";
  1344. break;
  1345. case AssignmentOp::DivisionAssignment:
  1346. op_string = "/=";
  1347. break;
  1348. case AssignmentOp::ModuloAssignment:
  1349. op_string = "%=";
  1350. break;
  1351. case AssignmentOp::ExponentiationAssignment:
  1352. op_string = "**=";
  1353. break;
  1354. case AssignmentOp::BitwiseAndAssignment:
  1355. op_string = "&=";
  1356. break;
  1357. case AssignmentOp::BitwiseOrAssignment:
  1358. op_string = "|=";
  1359. break;
  1360. case AssignmentOp::BitwiseXorAssignment:
  1361. op_string = "^=";
  1362. break;
  1363. case AssignmentOp::LeftShiftAssignment:
  1364. op_string = "<<=";
  1365. break;
  1366. case AssignmentOp::RightShiftAssignment:
  1367. op_string = ">>=";
  1368. break;
  1369. case AssignmentOp::UnsignedRightShiftAssignment:
  1370. op_string = ">>>=";
  1371. break;
  1372. case AssignmentOp::AndAssignment:
  1373. op_string = "&&=";
  1374. break;
  1375. case AssignmentOp::OrAssignment:
  1376. op_string = "||=";
  1377. break;
  1378. case AssignmentOp::NullishAssignment:
  1379. op_string = "\?\?=";
  1380. break;
  1381. }
  1382. ASTNode::dump(indent);
  1383. print_indent(indent + 1);
  1384. outln("{}", op_string);
  1385. m_lhs->dump(indent + 1);
  1386. m_rhs->dump(indent + 1);
  1387. }
  1388. void UpdateExpression::dump(int indent) const
  1389. {
  1390. const char* op_string = nullptr;
  1391. switch (m_op) {
  1392. case UpdateOp::Increment:
  1393. op_string = "++";
  1394. break;
  1395. case UpdateOp::Decrement:
  1396. op_string = "--";
  1397. break;
  1398. }
  1399. ASTNode::dump(indent);
  1400. if (m_prefixed) {
  1401. print_indent(indent + 1);
  1402. outln("{}", op_string);
  1403. }
  1404. m_argument->dump(indent + 1);
  1405. if (!m_prefixed) {
  1406. print_indent(indent + 1);
  1407. outln("{}", op_string);
  1408. }
  1409. }
  1410. Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1411. {
  1412. InterpreterNodeScope node_scope { interpreter, *this };
  1413. for (auto& declarator : m_declarations) {
  1414. if (auto* init = declarator.init()) {
  1415. auto initalizer_result = init->execute(interpreter, global_object);
  1416. if (interpreter.exception())
  1417. return {};
  1418. declarator.target().visit(
  1419. [&](NonnullRefPtr<Identifier> const& id) {
  1420. auto variable_name = id->string();
  1421. if (is<ClassExpression>(*init))
  1422. update_function_name(initalizer_result, variable_name);
  1423. interpreter.vm().set_variable(variable_name, initalizer_result, global_object, true);
  1424. },
  1425. [&](NonnullRefPtr<BindingPattern> const& pattern) {
  1426. interpreter.vm().assign(pattern, initalizer_result, global_object, true);
  1427. });
  1428. }
  1429. }
  1430. return {};
  1431. }
  1432. Value VariableDeclarator::execute(Interpreter& interpreter, GlobalObject&) const
  1433. {
  1434. InterpreterNodeScope node_scope { interpreter, *this };
  1435. // NOTE: VariableDeclarator execution is handled by VariableDeclaration.
  1436. VERIFY_NOT_REACHED();
  1437. }
  1438. void VariableDeclaration::dump(int indent) const
  1439. {
  1440. const char* declaration_kind_string = nullptr;
  1441. switch (m_declaration_kind) {
  1442. case DeclarationKind::Let:
  1443. declaration_kind_string = "Let";
  1444. break;
  1445. case DeclarationKind::Var:
  1446. declaration_kind_string = "Var";
  1447. break;
  1448. case DeclarationKind::Const:
  1449. declaration_kind_string = "Const";
  1450. break;
  1451. }
  1452. ASTNode::dump(indent);
  1453. print_indent(indent + 1);
  1454. outln("{}", declaration_kind_string);
  1455. for (auto& declarator : m_declarations)
  1456. declarator.dump(indent + 1);
  1457. }
  1458. void VariableDeclarator::dump(int indent) const
  1459. {
  1460. ASTNode::dump(indent);
  1461. m_target.visit([indent](const auto& value) { value->dump(indent + 1); });
  1462. if (m_init)
  1463. m_init->dump(indent + 1);
  1464. }
  1465. void ObjectProperty::dump(int indent) const
  1466. {
  1467. ASTNode::dump(indent);
  1468. m_key->dump(indent + 1);
  1469. m_value->dump(indent + 1);
  1470. }
  1471. void ObjectExpression::dump(int indent) const
  1472. {
  1473. ASTNode::dump(indent);
  1474. for (auto& property : m_properties) {
  1475. property.dump(indent + 1);
  1476. }
  1477. }
  1478. void ExpressionStatement::dump(int indent) const
  1479. {
  1480. ASTNode::dump(indent);
  1481. m_expression->dump(indent + 1);
  1482. }
  1483. Value ObjectProperty::execute(Interpreter& interpreter, GlobalObject&) const
  1484. {
  1485. InterpreterNodeScope node_scope { interpreter, *this };
  1486. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  1487. VERIFY_NOT_REACHED();
  1488. }
  1489. Value ObjectExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1490. {
  1491. InterpreterNodeScope node_scope { interpreter, *this };
  1492. auto* object = Object::create(global_object, global_object.object_prototype());
  1493. for (auto& property : m_properties) {
  1494. auto key = property.key().execute(interpreter, global_object);
  1495. if (interpreter.exception())
  1496. return {};
  1497. if (property.type() == ObjectProperty::Type::Spread) {
  1498. if (key.is_object() && key.as_object().is_array()) {
  1499. auto& array_to_spread = static_cast<Array&>(key.as_object());
  1500. for (auto& entry : array_to_spread.indexed_properties()) {
  1501. object->indexed_properties().put(object, entry.index(), entry.value_and_attributes(&array_to_spread).value);
  1502. if (interpreter.exception())
  1503. return {};
  1504. }
  1505. } else if (key.is_object()) {
  1506. auto& obj_to_spread = key.as_object();
  1507. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  1508. if (it.value.attributes.is_enumerable()) {
  1509. object->define_property(it.key, obj_to_spread.get(it.key));
  1510. if (interpreter.exception())
  1511. return {};
  1512. }
  1513. }
  1514. } else if (key.is_string()) {
  1515. auto& str_to_spread = key.as_string().string();
  1516. for (size_t i = 0; i < str_to_spread.length(); i++) {
  1517. object->define_property(i, js_string(interpreter.heap(), str_to_spread.substring(i, 1)));
  1518. if (interpreter.exception())
  1519. return {};
  1520. }
  1521. }
  1522. continue;
  1523. }
  1524. auto value = property.value().execute(interpreter, global_object);
  1525. if (interpreter.exception())
  1526. return {};
  1527. if (value.is_function() && property.is_method())
  1528. value.as_function().set_home_object(object);
  1529. String name = get_function_name(global_object, key);
  1530. if (property.type() == ObjectProperty::Type::Getter) {
  1531. name = String::formatted("get {}", name);
  1532. } else if (property.type() == ObjectProperty::Type::Setter) {
  1533. name = String::formatted("set {}", name);
  1534. }
  1535. update_function_name(value, name);
  1536. switch (property.type()) {
  1537. case ObjectProperty::Type::Getter:
  1538. VERIFY(value.is_function());
  1539. object->define_accessor(PropertyName::from_value(global_object, key), &value.as_function(), nullptr, Attribute::Configurable | Attribute::Enumerable);
  1540. break;
  1541. case ObjectProperty::Type::Setter:
  1542. VERIFY(value.is_function());
  1543. object->define_accessor(PropertyName::from_value(global_object, key), nullptr, &value.as_function(), Attribute::Configurable | Attribute::Enumerable);
  1544. break;
  1545. case ObjectProperty::Type::KeyValue:
  1546. object->define_property(PropertyName::from_value(global_object, key), value);
  1547. break;
  1548. case ObjectProperty::Type::Spread:
  1549. default:
  1550. VERIFY_NOT_REACHED();
  1551. }
  1552. if (interpreter.exception())
  1553. return {};
  1554. }
  1555. return object;
  1556. }
  1557. void MemberExpression::dump(int indent) const
  1558. {
  1559. print_indent(indent);
  1560. outln("{}(computed={})", class_name(), is_computed());
  1561. m_object->dump(indent + 1);
  1562. m_property->dump(indent + 1);
  1563. }
  1564. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter, GlobalObject& global_object) const
  1565. {
  1566. if (!is_computed()) {
  1567. VERIFY(is<Identifier>(*m_property));
  1568. return static_cast<Identifier const&>(*m_property).string();
  1569. }
  1570. auto value = m_property->execute(interpreter, global_object);
  1571. if (interpreter.exception())
  1572. return {};
  1573. VERIFY(!value.is_empty());
  1574. return PropertyName::from_value(global_object, value);
  1575. }
  1576. String MemberExpression::to_string_approximation() const
  1577. {
  1578. String object_string = "<object>";
  1579. if (is<Identifier>(*m_object))
  1580. object_string = static_cast<Identifier const&>(*m_object).string();
  1581. if (is_computed())
  1582. return String::formatted("{}[<computed>]", object_string);
  1583. VERIFY(is<Identifier>(*m_property));
  1584. return String::formatted("{}.{}", object_string, static_cast<Identifier const&>(*m_property).string());
  1585. }
  1586. Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1587. {
  1588. InterpreterNodeScope node_scope { interpreter, *this };
  1589. auto reference = to_reference(interpreter, global_object);
  1590. if (interpreter.exception())
  1591. return {};
  1592. return reference.get(global_object);
  1593. }
  1594. void MetaProperty::dump(int indent) const
  1595. {
  1596. String name;
  1597. if (m_type == MetaProperty::Type::NewTarget)
  1598. name = "new.target";
  1599. else if (m_type == MetaProperty::Type::ImportMeta)
  1600. name = "import.meta";
  1601. else
  1602. VERIFY_NOT_REACHED();
  1603. print_indent(indent);
  1604. outln("{} {}", class_name(), name);
  1605. }
  1606. Value MetaProperty::execute(Interpreter& interpreter, GlobalObject&) const
  1607. {
  1608. InterpreterNodeScope node_scope { interpreter, *this };
  1609. if (m_type == MetaProperty::Type::NewTarget)
  1610. return interpreter.vm().get_new_target().value_or(js_undefined());
  1611. if (m_type == MetaProperty::Type::ImportMeta)
  1612. TODO();
  1613. VERIFY_NOT_REACHED();
  1614. }
  1615. Value StringLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1616. {
  1617. InterpreterNodeScope node_scope { interpreter, *this };
  1618. return js_string(interpreter.heap(), m_value);
  1619. }
  1620. Value NumericLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1621. {
  1622. InterpreterNodeScope node_scope { interpreter, *this };
  1623. return Value(m_value);
  1624. }
  1625. Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1626. {
  1627. InterpreterNodeScope node_scope { interpreter, *this };
  1628. Crypto::SignedBigInteger integer;
  1629. if (m_value[0] == '0' && m_value.length() >= 3) {
  1630. if (m_value[1] == 'x' || m_value[1] == 'X') {
  1631. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base16(m_value.substring(2, m_value.length() - 3)));
  1632. } else if (m_value[1] == 'o' || m_value[1] == 'O') {
  1633. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base8(m_value.substring(2, m_value.length() - 3)));
  1634. } else if (m_value[1] == 'b' || m_value[1] == 'B') {
  1635. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base2(m_value.substring(2, m_value.length() - 3)));
  1636. }
  1637. }
  1638. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
  1639. }
  1640. Value BooleanLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1641. {
  1642. InterpreterNodeScope node_scope { interpreter, *this };
  1643. return Value(m_value);
  1644. }
  1645. Value NullLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1646. {
  1647. InterpreterNodeScope node_scope { interpreter, *this };
  1648. return js_null();
  1649. }
  1650. void RegExpLiteral::dump(int indent) const
  1651. {
  1652. print_indent(indent);
  1653. outln("{} (/{}/{})", class_name(), pattern(), flags());
  1654. }
  1655. Value RegExpLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1656. {
  1657. InterpreterNodeScope node_scope { interpreter, *this };
  1658. return RegExpObject::create(global_object, pattern(), flags());
  1659. }
  1660. void ArrayExpression::dump(int indent) const
  1661. {
  1662. ASTNode::dump(indent);
  1663. for (auto& element : m_elements) {
  1664. if (element) {
  1665. element->dump(indent + 1);
  1666. } else {
  1667. print_indent(indent + 1);
  1668. outln("<empty>");
  1669. }
  1670. }
  1671. }
  1672. Value ArrayExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1673. {
  1674. InterpreterNodeScope node_scope { interpreter, *this };
  1675. auto* array = Array::create(global_object);
  1676. for (auto& element : m_elements) {
  1677. auto value = Value();
  1678. if (element) {
  1679. value = element->execute(interpreter, global_object);
  1680. if (interpreter.exception())
  1681. return {};
  1682. if (is<SpreadExpression>(*element)) {
  1683. get_iterator_values(global_object, value, [&](Value iterator_value) {
  1684. array->indexed_properties().append(iterator_value);
  1685. return IterationDecision::Continue;
  1686. });
  1687. if (interpreter.exception())
  1688. return {};
  1689. continue;
  1690. }
  1691. }
  1692. array->indexed_properties().append(value);
  1693. }
  1694. return array;
  1695. }
  1696. void TemplateLiteral::dump(int indent) const
  1697. {
  1698. ASTNode::dump(indent);
  1699. for (auto& expression : m_expressions)
  1700. expression.dump(indent + 1);
  1701. }
  1702. Value TemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1703. {
  1704. InterpreterNodeScope node_scope { interpreter, *this };
  1705. StringBuilder string_builder;
  1706. for (auto& expression : m_expressions) {
  1707. auto expr = expression.execute(interpreter, global_object);
  1708. if (interpreter.exception())
  1709. return {};
  1710. auto string = expr.to_string(global_object);
  1711. if (interpreter.exception())
  1712. return {};
  1713. string_builder.append(string);
  1714. }
  1715. return js_string(interpreter.heap(), string_builder.build());
  1716. }
  1717. void TaggedTemplateLiteral::dump(int indent) const
  1718. {
  1719. ASTNode::dump(indent);
  1720. print_indent(indent + 1);
  1721. outln("(Tag)");
  1722. m_tag->dump(indent + 2);
  1723. print_indent(indent + 1);
  1724. outln("(Template Literal)");
  1725. m_template_literal->dump(indent + 2);
  1726. }
  1727. Value TaggedTemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1728. {
  1729. InterpreterNodeScope node_scope { interpreter, *this };
  1730. auto& vm = interpreter.vm();
  1731. auto tag = m_tag->execute(interpreter, global_object);
  1732. if (vm.exception())
  1733. return {};
  1734. if (!tag.is_function()) {
  1735. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, tag.to_string_without_side_effects());
  1736. return {};
  1737. }
  1738. auto& tag_function = tag.as_function();
  1739. auto& expressions = m_template_literal->expressions();
  1740. auto* strings = Array::create(global_object);
  1741. MarkedValueList arguments(vm.heap());
  1742. arguments.append(strings);
  1743. for (size_t i = 0; i < expressions.size(); ++i) {
  1744. auto value = expressions[i].execute(interpreter, global_object);
  1745. if (vm.exception())
  1746. return {};
  1747. // tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
  1748. // tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
  1749. if (i % 2 == 0) {
  1750. strings->indexed_properties().append(value);
  1751. } else {
  1752. arguments.append(value);
  1753. }
  1754. }
  1755. auto* raw_strings = Array::create(global_object);
  1756. for (auto& raw_string : m_template_literal->raw_strings()) {
  1757. auto value = raw_string.execute(interpreter, global_object);
  1758. if (vm.exception())
  1759. return {};
  1760. raw_strings->indexed_properties().append(value);
  1761. }
  1762. strings->define_property(vm.names.raw, raw_strings, 0);
  1763. return vm.call(tag_function, js_undefined(), move(arguments));
  1764. }
  1765. void TryStatement::dump(int indent) const
  1766. {
  1767. ASTNode::dump(indent);
  1768. print_indent(indent);
  1769. outln("(Block)");
  1770. block().dump(indent + 1);
  1771. if (handler()) {
  1772. print_indent(indent);
  1773. outln("(Handler)");
  1774. handler()->dump(indent + 1);
  1775. }
  1776. if (finalizer()) {
  1777. print_indent(indent);
  1778. outln("(Finalizer)");
  1779. finalizer()->dump(indent + 1);
  1780. }
  1781. }
  1782. void CatchClause::dump(int indent) const
  1783. {
  1784. print_indent(indent);
  1785. if (m_parameter.is_null())
  1786. outln("CatchClause");
  1787. else
  1788. outln("CatchClause ({})", m_parameter);
  1789. body().dump(indent + 1);
  1790. }
  1791. void ThrowStatement::dump(int indent) const
  1792. {
  1793. ASTNode::dump(indent);
  1794. argument().dump(indent + 1);
  1795. }
  1796. Value TryStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1797. {
  1798. InterpreterNodeScope node_scope { interpreter, *this };
  1799. auto result = interpreter.execute_statement(global_object, m_block, ScopeType::Try);
  1800. if (auto* exception = interpreter.exception()) {
  1801. if (m_handler) {
  1802. interpreter.vm().clear_exception();
  1803. HashMap<FlyString, Variable> parameters;
  1804. parameters.set(m_handler->parameter(), Variable { exception->value(), DeclarationKind::Var });
  1805. auto* catch_scope = interpreter.heap().allocate<LexicalEnvironment>(global_object, move(parameters), interpreter.vm().call_frame().scope);
  1806. TemporaryChange<ScopeObject*> scope_change(interpreter.vm().call_frame().scope, catch_scope);
  1807. result = interpreter.execute_statement(global_object, m_handler->body());
  1808. }
  1809. }
  1810. if (m_finalizer) {
  1811. // Keep, if any, and then clear the current exception so we can
  1812. // execute() the finalizer without an exception in our way.
  1813. auto* previous_exception = interpreter.exception();
  1814. interpreter.vm().clear_exception();
  1815. // Remember what scope type we were unwinding to, and temporarily
  1816. // clear it as well (e.g. return from handler).
  1817. auto unwind_until = interpreter.vm().unwind_until();
  1818. interpreter.vm().stop_unwind();
  1819. auto finalizer_result = m_finalizer->execute(interpreter, global_object);
  1820. if (interpreter.vm().should_unwind()) {
  1821. // This was NOT a 'normal' completion (e.g. return from finalizer).
  1822. result = finalizer_result;
  1823. } else {
  1824. // Continue unwinding to whatever we found ourselves unwinding
  1825. // to when the finalizer was entered (e.g. return from handler,
  1826. // which is unaffected by normal completion from finalizer).
  1827. interpreter.vm().unwind(unwind_until);
  1828. // If we previously had an exception and the finalizer didn't
  1829. // throw a new one, restore the old one.
  1830. if (previous_exception && !interpreter.exception())
  1831. interpreter.vm().set_exception(*previous_exception);
  1832. }
  1833. }
  1834. return result.value_or(js_undefined());
  1835. }
  1836. Value CatchClause::execute(Interpreter& interpreter, GlobalObject&) const
  1837. {
  1838. InterpreterNodeScope node_scope { interpreter, *this };
  1839. // NOTE: CatchClause execution is handled by TryStatement.
  1840. VERIFY_NOT_REACHED();
  1841. return {};
  1842. }
  1843. Value ThrowStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1844. {
  1845. InterpreterNodeScope node_scope { interpreter, *this };
  1846. auto value = m_argument->execute(interpreter, global_object);
  1847. if (interpreter.vm().exception())
  1848. return {};
  1849. interpreter.vm().throw_exception(global_object, value);
  1850. return {};
  1851. }
  1852. Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1853. {
  1854. InterpreterNodeScope node_scope { interpreter, *this };
  1855. auto discriminant_result = m_discriminant->execute(interpreter, global_object);
  1856. if (interpreter.exception())
  1857. return {};
  1858. bool falling_through = false;
  1859. auto last_value = js_undefined();
  1860. for (auto& switch_case : m_cases) {
  1861. if (!falling_through && switch_case.test()) {
  1862. auto test_result = switch_case.test()->execute(interpreter, global_object);
  1863. if (interpreter.exception())
  1864. return {};
  1865. if (!strict_eq(discriminant_result, test_result))
  1866. continue;
  1867. }
  1868. falling_through = true;
  1869. for (auto& statement : switch_case.consequent()) {
  1870. auto value = statement.execute(interpreter, global_object);
  1871. if (!value.is_empty())
  1872. last_value = value;
  1873. if (interpreter.exception())
  1874. return {};
  1875. if (interpreter.vm().should_unwind()) {
  1876. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  1877. // No stop_unwind(), the outer loop will handle that - we just need to break out of the switch/case.
  1878. return last_value;
  1879. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  1880. interpreter.vm().stop_unwind();
  1881. return last_value;
  1882. } else {
  1883. return last_value;
  1884. }
  1885. }
  1886. }
  1887. }
  1888. return last_value;
  1889. }
  1890. Value SwitchCase::execute(Interpreter& interpreter, GlobalObject&) const
  1891. {
  1892. InterpreterNodeScope node_scope { interpreter, *this };
  1893. // NOTE: SwitchCase execution is handled by SwitchStatement.
  1894. VERIFY_NOT_REACHED();
  1895. return {};
  1896. }
  1897. Value BreakStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1898. {
  1899. InterpreterNodeScope node_scope { interpreter, *this };
  1900. interpreter.vm().unwind(ScopeType::Breakable, m_target_label);
  1901. return {};
  1902. }
  1903. Value ContinueStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1904. {
  1905. InterpreterNodeScope node_scope { interpreter, *this };
  1906. interpreter.vm().unwind(ScopeType::Continuable, m_target_label);
  1907. return {};
  1908. }
  1909. void SwitchStatement::dump(int indent) const
  1910. {
  1911. ASTNode::dump(indent);
  1912. m_discriminant->dump(indent + 1);
  1913. for (auto& switch_case : m_cases) {
  1914. switch_case.dump(indent + 1);
  1915. }
  1916. }
  1917. void SwitchCase::dump(int indent) const
  1918. {
  1919. ASTNode::dump(indent);
  1920. print_indent(indent + 1);
  1921. if (m_test) {
  1922. outln("(Test)");
  1923. m_test->dump(indent + 2);
  1924. } else {
  1925. outln("(Default)");
  1926. }
  1927. print_indent(indent + 1);
  1928. outln("(Consequent)");
  1929. for (auto& statement : m_consequent)
  1930. statement.dump(indent + 2);
  1931. }
  1932. Value ConditionalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1933. {
  1934. InterpreterNodeScope node_scope { interpreter, *this };
  1935. auto test_result = m_test->execute(interpreter, global_object);
  1936. if (interpreter.exception())
  1937. return {};
  1938. Value result;
  1939. if (test_result.to_boolean()) {
  1940. result = m_consequent->execute(interpreter, global_object);
  1941. } else {
  1942. result = m_alternate->execute(interpreter, global_object);
  1943. }
  1944. if (interpreter.exception())
  1945. return {};
  1946. return result;
  1947. }
  1948. void ConditionalExpression::dump(int indent) const
  1949. {
  1950. ASTNode::dump(indent);
  1951. print_indent(indent + 1);
  1952. outln("(Test)");
  1953. m_test->dump(indent + 2);
  1954. print_indent(indent + 1);
  1955. outln("(Consequent)");
  1956. m_consequent->dump(indent + 2);
  1957. print_indent(indent + 1);
  1958. outln("(Alternate)");
  1959. m_alternate->dump(indent + 2);
  1960. }
  1961. void SequenceExpression::dump(int indent) const
  1962. {
  1963. ASTNode::dump(indent);
  1964. for (auto& expression : m_expressions)
  1965. expression.dump(indent + 1);
  1966. }
  1967. Value SequenceExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1968. {
  1969. InterpreterNodeScope node_scope { interpreter, *this };
  1970. Value last_value;
  1971. for (auto& expression : m_expressions) {
  1972. last_value = expression.execute(interpreter, global_object);
  1973. if (interpreter.exception())
  1974. return {};
  1975. }
  1976. return last_value;
  1977. }
  1978. Value DebuggerStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1979. {
  1980. InterpreterNodeScope node_scope { interpreter, *this };
  1981. // Sorry, no JavaScript debugger available (yet)!
  1982. return {};
  1983. }
  1984. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1985. {
  1986. m_variables.extend(move(variables));
  1987. }
  1988. void ScopeNode::add_functions(NonnullRefPtrVector<FunctionDeclaration> functions)
  1989. {
  1990. m_functions.extend(move(functions));
  1991. }
  1992. }