AST.cpp 70 KB

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