AST.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/HashMap.h>
  28. #include <AK/HashTable.h>
  29. #include <AK/ScopeGuard.h>
  30. #include <AK/StringBuilder.h>
  31. #include <LibCrypto/BigInt/SignedBigInteger.h>
  32. #include <LibJS/AST.h>
  33. #include <LibJS/Interpreter.h>
  34. #include <LibJS/Runtime/Accessor.h>
  35. #include <LibJS/Runtime/Array.h>
  36. #include <LibJS/Runtime/BigInt.h>
  37. #include <LibJS/Runtime/Error.h>
  38. #include <LibJS/Runtime/GlobalObject.h>
  39. #include <LibJS/Runtime/IteratorOperations.h>
  40. #include <LibJS/Runtime/MarkedValueList.h>
  41. #include <LibJS/Runtime/NativeFunction.h>
  42. #include <LibJS/Runtime/PrimitiveString.h>
  43. #include <LibJS/Runtime/Reference.h>
  44. #include <LibJS/Runtime/RegExpObject.h>
  45. #include <LibJS/Runtime/ScriptFunction.h>
  46. #include <LibJS/Runtime/Shape.h>
  47. #include <LibJS/Runtime/StringObject.h>
  48. #include <stdio.h>
  49. namespace JS {
  50. static void update_function_name(Value& value, const FlyString& name, HashTable<const JS::Cell*>& visited)
  51. {
  52. if (!value.is_object())
  53. return;
  54. if (visited.contains(value.as_cell()))
  55. return;
  56. visited.set(value.as_cell());
  57. auto& object = value.as_object();
  58. if (object.is_function()) {
  59. auto& function = static_cast<Function&>(object);
  60. if (function.is_script_function() && function.name().is_empty())
  61. static_cast<ScriptFunction&>(function).set_name(name);
  62. } else if (object.is_array()) {
  63. auto& array = static_cast<Array&>(object);
  64. for (auto& entry : array.indexed_properties().values_unordered())
  65. update_function_name(entry.value, name, visited);
  66. }
  67. }
  68. static void update_function_name(Value& value, const FlyString& name)
  69. {
  70. HashTable<const JS::Cell*> visited;
  71. update_function_name(value, name, visited);
  72. }
  73. static String get_function_name(GlobalObject& global_object, Value value)
  74. {
  75. if (value.is_symbol())
  76. return String::format("[%s]", value.as_symbol().description().characters());
  77. if (value.is_string())
  78. return value.as_string().string();
  79. return value.to_string(global_object);
  80. }
  81. Value ScopeNode::execute(Interpreter& interpreter, GlobalObject& global_object) const
  82. {
  83. return interpreter.execute_statement(global_object, *this);
  84. }
  85. Value Program::execute(Interpreter& interpreter, GlobalObject& global_object) const
  86. {
  87. return interpreter.execute_statement(global_object, *this, {}, ScopeType::Block);
  88. }
  89. Value FunctionDeclaration::execute(Interpreter&, GlobalObject&) const
  90. {
  91. return js_undefined();
  92. }
  93. Value FunctionExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  94. {
  95. return ScriptFunction::create(global_object, name(), body(), parameters(), function_length(), interpreter.current_environment(), is_strict_mode() || interpreter.vm().in_strict_mode(), m_is_arrow_function);
  96. }
  97. Value ExpressionStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  98. {
  99. return m_expression->execute(interpreter, global_object);
  100. }
  101. CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter, GlobalObject& global_object) const
  102. {
  103. auto& vm = interpreter.vm();
  104. if (is_new_expression()) {
  105. // Computing |this| is irrelevant for "new" expression.
  106. return { js_undefined(), m_callee->execute(interpreter, global_object) };
  107. }
  108. if (m_callee->is_super_expression()) {
  109. // If we are calling super, |this| has not been initialized yet, and would not be meaningful to provide.
  110. auto new_target = interpreter.vm().get_new_target();
  111. ASSERT(new_target.is_function());
  112. return { js_undefined(), new_target };
  113. }
  114. if (m_callee->is_member_expression()) {
  115. auto& member_expression = static_cast<const MemberExpression&>(*m_callee);
  116. bool is_super_property_lookup = member_expression.object().is_super_expression();
  117. auto lookup_target = is_super_property_lookup ? interpreter.current_environment()->get_super_base() : member_expression.object().execute(interpreter, global_object);
  118. if (interpreter.exception())
  119. return {};
  120. if (is_super_property_lookup && lookup_target.is_nullish()) {
  121. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeNullOrUndefinedOnSuperPropertyAccess, lookup_target.to_string_without_side_effects().characters());
  122. return {};
  123. }
  124. auto* this_value = is_super_property_lookup ? &vm.this_value(global_object).as_object() : lookup_target.to_object(global_object);
  125. if (interpreter.exception())
  126. return {};
  127. auto property_name = member_expression.computed_property_name(interpreter, global_object);
  128. if (!property_name.is_valid())
  129. return {};
  130. auto callee = lookup_target.to_object(global_object)->get(property_name).value_or(js_undefined());
  131. return { this_value, callee };
  132. }
  133. return { &global_object, m_callee->execute(interpreter, global_object) };
  134. }
  135. Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  136. {
  137. auto [this_value, callee] = compute_this_and_callee(interpreter, global_object);
  138. if (interpreter.exception())
  139. return {};
  140. ASSERT(!callee.is_empty());
  141. if (!callee.is_function()
  142. || (is_new_expression() && (callee.as_object().is_native_function() && !static_cast<NativeFunction&>(callee.as_object()).has_constructor()))) {
  143. String error_message;
  144. auto call_type = is_new_expression() ? "constructor" : "function";
  145. if (m_callee->is_identifier() || m_callee->is_member_expression()) {
  146. String expression_string;
  147. if (m_callee->is_identifier()) {
  148. expression_string = static_cast<const Identifier&>(*m_callee).string();
  149. } else {
  150. expression_string = static_cast<const MemberExpression&>(*m_callee).to_string_approximation();
  151. }
  152. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects().characters(), call_type, expression_string.characters());
  153. } else {
  154. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::IsNotA, callee.to_string_without_side_effects().characters(), call_type);
  155. }
  156. return {};
  157. }
  158. auto& function = callee.as_function();
  159. MarkedValueList arguments(interpreter.heap());
  160. for (size_t i = 0; i < m_arguments.size(); ++i) {
  161. auto value = m_arguments[i].value->execute(interpreter, global_object);
  162. if (interpreter.exception())
  163. return {};
  164. if (m_arguments[i].is_spread) {
  165. get_iterator_values(global_object, value, [&](Value iterator_value) {
  166. if (interpreter.exception())
  167. return IterationDecision::Break;
  168. arguments.append(iterator_value);
  169. return IterationDecision::Continue;
  170. });
  171. if (interpreter.exception())
  172. return {};
  173. } else {
  174. arguments.append(value);
  175. }
  176. }
  177. Object* new_object = nullptr;
  178. Value result;
  179. if (is_new_expression()) {
  180. result = interpreter.vm().construct(function, function, move(arguments), global_object);
  181. if (result.is_object())
  182. new_object = &result.as_object();
  183. } else if (m_callee->is_super_expression()) {
  184. auto* super_constructor = interpreter.current_environment()->current_function()->prototype();
  185. // FIXME: Functions should track their constructor kind.
  186. if (!super_constructor || !super_constructor->is_function()) {
  187. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, "Super constructor");
  188. return {};
  189. }
  190. result = interpreter.vm().construct(static_cast<Function&>(*super_constructor), function, move(arguments), global_object);
  191. if (interpreter.exception())
  192. return {};
  193. interpreter.current_environment()->bind_this_value(global_object, result);
  194. } else {
  195. result = interpreter.call(function, this_value, move(arguments));
  196. }
  197. if (interpreter.exception())
  198. return {};
  199. if (is_new_expression()) {
  200. if (result.is_object())
  201. return result;
  202. return new_object;
  203. }
  204. return result;
  205. }
  206. Value ReturnStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  207. {
  208. auto value = argument() ? argument()->execute(interpreter, global_object) : js_undefined();
  209. if (interpreter.exception())
  210. return {};
  211. interpreter.vm().unwind(ScopeType::Function);
  212. return value;
  213. }
  214. Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  215. {
  216. auto predicate_result = m_predicate->execute(interpreter, global_object);
  217. if (interpreter.exception())
  218. return {};
  219. if (predicate_result.to_boolean())
  220. return interpreter.execute_statement(global_object, *m_consequent);
  221. if (m_alternate)
  222. return interpreter.execute_statement(global_object, *m_alternate);
  223. return js_undefined();
  224. }
  225. Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  226. {
  227. Value last_value = js_undefined();
  228. while (m_test->execute(interpreter, global_object).to_boolean()) {
  229. if (interpreter.exception())
  230. return {};
  231. last_value = interpreter.execute_statement(global_object, *m_body);
  232. if (interpreter.exception())
  233. return {};
  234. }
  235. return last_value;
  236. }
  237. Value DoWhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  238. {
  239. Value last_value = js_undefined();
  240. do {
  241. if (interpreter.exception())
  242. return {};
  243. last_value = interpreter.execute_statement(global_object, *m_body);
  244. if (interpreter.exception())
  245. return {};
  246. } while (m_test->execute(interpreter, global_object).to_boolean());
  247. return last_value;
  248. }
  249. Value ForStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  250. {
  251. RefPtr<BlockStatement> wrapper;
  252. if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_kind() != DeclarationKind::Var) {
  253. wrapper = create_ast_node<BlockStatement>();
  254. NonnullRefPtrVector<VariableDeclaration> decls;
  255. decls.append(*static_cast<const VariableDeclaration*>(m_init.ptr()));
  256. wrapper->add_variables(decls);
  257. interpreter.enter_scope(*wrapper, {}, ScopeType::Block, global_object);
  258. }
  259. auto wrapper_cleanup = ScopeGuard([&] {
  260. if (wrapper)
  261. interpreter.exit_scope(*wrapper);
  262. });
  263. Value last_value = js_undefined();
  264. if (m_init) {
  265. m_init->execute(interpreter, global_object);
  266. if (interpreter.exception())
  267. return {};
  268. }
  269. if (m_test) {
  270. while (true) {
  271. auto test_result = m_test->execute(interpreter, global_object);
  272. if (interpreter.exception())
  273. return {};
  274. if (!test_result.to_boolean())
  275. break;
  276. last_value = interpreter.execute_statement(global_object, *m_body);
  277. if (interpreter.exception())
  278. return {};
  279. if (interpreter.vm().should_unwind()) {
  280. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  281. interpreter.vm().stop_unwind();
  282. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  283. interpreter.vm().stop_unwind();
  284. break;
  285. } else {
  286. return js_undefined();
  287. }
  288. }
  289. if (m_update) {
  290. m_update->execute(interpreter, global_object);
  291. if (interpreter.exception())
  292. return {};
  293. }
  294. }
  295. } else {
  296. while (true) {
  297. last_value = interpreter.execute_statement(global_object, *m_body);
  298. if (interpreter.exception())
  299. return {};
  300. if (interpreter.vm().should_unwind()) {
  301. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  302. interpreter.vm().stop_unwind();
  303. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  304. interpreter.vm().stop_unwind();
  305. break;
  306. } else {
  307. return js_undefined();
  308. }
  309. }
  310. if (m_update) {
  311. m_update->execute(interpreter, global_object);
  312. if (interpreter.exception())
  313. return {};
  314. }
  315. }
  316. }
  317. return last_value;
  318. }
  319. static FlyString variable_from_for_declaration(Interpreter& interpreter, GlobalObject& global_object, NonnullRefPtr<ASTNode> node, RefPtr<BlockStatement> wrapper)
  320. {
  321. FlyString variable_name;
  322. if (node->is_variable_declaration()) {
  323. auto* variable_declaration = static_cast<const VariableDeclaration*>(node.ptr());
  324. ASSERT(!variable_declaration->declarations().is_empty());
  325. if (variable_declaration->declaration_kind() != DeclarationKind::Var) {
  326. wrapper = create_ast_node<BlockStatement>();
  327. interpreter.enter_scope(*wrapper, {}, ScopeType::Block, global_object);
  328. }
  329. variable_declaration->execute(interpreter, global_object);
  330. variable_name = variable_declaration->declarations().first().id().string();
  331. } else if (node->is_identifier()) {
  332. variable_name = static_cast<const Identifier&>(*node).string();
  333. } else {
  334. ASSERT_NOT_REACHED();
  335. }
  336. return variable_name;
  337. }
  338. Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  339. {
  340. if (!m_lhs->is_variable_declaration() && !m_lhs->is_identifier()) {
  341. // FIXME: Implement "for (foo.bar in baz)", "for (foo[0] in bar)"
  342. ASSERT_NOT_REACHED();
  343. }
  344. RefPtr<BlockStatement> wrapper;
  345. auto variable_name = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  346. auto wrapper_cleanup = ScopeGuard([&] {
  347. if (wrapper)
  348. interpreter.exit_scope(*wrapper);
  349. });
  350. auto last_value = js_undefined();
  351. auto rhs_result = m_rhs->execute(interpreter, global_object);
  352. if (interpreter.exception())
  353. return {};
  354. auto* object = rhs_result.to_object(global_object);
  355. while (object) {
  356. auto property_names = object->get_own_properties(*object, Object::PropertyKind::Key, true);
  357. for (auto& property_name : property_names.as_object().indexed_properties()) {
  358. interpreter.vm().set_variable(variable_name, property_name.value_and_attributes(object).value, global_object);
  359. if (interpreter.exception())
  360. return {};
  361. last_value = interpreter.execute_statement(global_object, *m_body);
  362. if (interpreter.exception())
  363. return {};
  364. if (interpreter.vm().should_unwind()) {
  365. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  366. interpreter.vm().stop_unwind();
  367. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  368. interpreter.vm().stop_unwind();
  369. break;
  370. } else {
  371. return js_undefined();
  372. }
  373. }
  374. }
  375. object = object->prototype();
  376. if (interpreter.exception())
  377. return {};
  378. }
  379. return last_value;
  380. }
  381. Value ForOfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  382. {
  383. if (!m_lhs->is_variable_declaration() && !m_lhs->is_identifier()) {
  384. // FIXME: Implement "for (foo.bar of baz)", "for (foo[0] of bar)"
  385. ASSERT_NOT_REACHED();
  386. }
  387. RefPtr<BlockStatement> wrapper;
  388. auto variable_name = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  389. auto wrapper_cleanup = ScopeGuard([&] {
  390. if (wrapper)
  391. interpreter.exit_scope(*wrapper);
  392. });
  393. auto last_value = js_undefined();
  394. auto rhs_result = m_rhs->execute(interpreter, global_object);
  395. if (interpreter.exception())
  396. return {};
  397. get_iterator_values(global_object, rhs_result, [&](Value value) {
  398. interpreter.vm().set_variable(variable_name, value, global_object);
  399. last_value = interpreter.execute_statement(global_object, *m_body);
  400. if (interpreter.exception())
  401. return IterationDecision::Break;
  402. if (interpreter.vm().should_unwind()) {
  403. if (interpreter.vm().should_unwind_until(ScopeType::Continuable, m_label)) {
  404. interpreter.vm().stop_unwind();
  405. } else if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  406. interpreter.vm().stop_unwind();
  407. return IterationDecision::Break;
  408. } else {
  409. return IterationDecision::Break;
  410. }
  411. }
  412. return IterationDecision::Continue;
  413. });
  414. if (interpreter.exception())
  415. return {};
  416. if (interpreter.vm().should_unwind())
  417. return js_undefined();
  418. return last_value;
  419. }
  420. Value BinaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  421. {
  422. auto lhs_result = m_lhs->execute(interpreter, global_object);
  423. if (interpreter.exception())
  424. return {};
  425. auto rhs_result = m_rhs->execute(interpreter, global_object);
  426. if (interpreter.exception())
  427. return {};
  428. switch (m_op) {
  429. case BinaryOp::Addition:
  430. return add(global_object, lhs_result, rhs_result);
  431. case BinaryOp::Subtraction:
  432. return sub(global_object, lhs_result, rhs_result);
  433. case BinaryOp::Multiplication:
  434. return mul(global_object, lhs_result, rhs_result);
  435. case BinaryOp::Division:
  436. return div(global_object, lhs_result, rhs_result);
  437. case BinaryOp::Modulo:
  438. return mod(global_object, lhs_result, rhs_result);
  439. case BinaryOp::Exponentiation:
  440. return exp(global_object, lhs_result, rhs_result);
  441. case BinaryOp::TypedEquals:
  442. return Value(strict_eq(lhs_result, rhs_result));
  443. case BinaryOp::TypedInequals:
  444. return Value(!strict_eq(lhs_result, rhs_result));
  445. case BinaryOp::AbstractEquals:
  446. return Value(abstract_eq(global_object, lhs_result, rhs_result));
  447. case BinaryOp::AbstractInequals:
  448. return Value(!abstract_eq(global_object, lhs_result, rhs_result));
  449. case BinaryOp::GreaterThan:
  450. return greater_than(global_object, lhs_result, rhs_result);
  451. case BinaryOp::GreaterThanEquals:
  452. return greater_than_equals(global_object, lhs_result, rhs_result);
  453. case BinaryOp::LessThan:
  454. return less_than(global_object, lhs_result, rhs_result);
  455. case BinaryOp::LessThanEquals:
  456. return less_than_equals(global_object, lhs_result, rhs_result);
  457. case BinaryOp::BitwiseAnd:
  458. return bitwise_and(global_object, lhs_result, rhs_result);
  459. case BinaryOp::BitwiseOr:
  460. return bitwise_or(global_object, lhs_result, rhs_result);
  461. case BinaryOp::BitwiseXor:
  462. return bitwise_xor(global_object, lhs_result, rhs_result);
  463. case BinaryOp::LeftShift:
  464. return left_shift(global_object, lhs_result, rhs_result);
  465. case BinaryOp::RightShift:
  466. return right_shift(global_object, lhs_result, rhs_result);
  467. case BinaryOp::UnsignedRightShift:
  468. return unsigned_right_shift(global_object, lhs_result, rhs_result);
  469. case BinaryOp::In:
  470. return in(global_object, lhs_result, rhs_result);
  471. case BinaryOp::InstanceOf:
  472. return instance_of(global_object, lhs_result, rhs_result);
  473. }
  474. ASSERT_NOT_REACHED();
  475. }
  476. Value LogicalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  477. {
  478. auto lhs_result = m_lhs->execute(interpreter, global_object);
  479. if (interpreter.exception())
  480. return {};
  481. switch (m_op) {
  482. case LogicalOp::And:
  483. if (lhs_result.to_boolean()) {
  484. auto rhs_result = m_rhs->execute(interpreter, global_object);
  485. if (interpreter.exception())
  486. return {};
  487. return rhs_result;
  488. }
  489. return lhs_result;
  490. case LogicalOp::Or: {
  491. if (lhs_result.to_boolean())
  492. return lhs_result;
  493. auto rhs_result = m_rhs->execute(interpreter, global_object);
  494. if (interpreter.exception())
  495. return {};
  496. return rhs_result;
  497. }
  498. case LogicalOp::NullishCoalescing:
  499. if (lhs_result.is_nullish()) {
  500. auto rhs_result = m_rhs->execute(interpreter, global_object);
  501. if (interpreter.exception())
  502. return {};
  503. return rhs_result;
  504. }
  505. return lhs_result;
  506. }
  507. ASSERT_NOT_REACHED();
  508. }
  509. Reference Expression::to_reference(Interpreter&, GlobalObject&) const
  510. {
  511. return {};
  512. }
  513. Reference Identifier::to_reference(Interpreter& interpreter, GlobalObject&) const
  514. {
  515. return interpreter.vm().get_reference(string());
  516. }
  517. Reference MemberExpression::to_reference(Interpreter& interpreter, GlobalObject& global_object) const
  518. {
  519. auto object_value = m_object->execute(interpreter, global_object);
  520. if (interpreter.exception())
  521. return {};
  522. auto property_name = computed_property_name(interpreter, global_object);
  523. if (!property_name.is_valid())
  524. return {};
  525. return { object_value, property_name };
  526. }
  527. Value UnaryExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  528. {
  529. auto& vm = interpreter.vm();
  530. if (m_op == UnaryOp::Delete) {
  531. auto reference = m_lhs->to_reference(interpreter, global_object);
  532. if (interpreter.exception())
  533. return {};
  534. if (reference.is_unresolvable())
  535. return Value(true);
  536. // FIXME: Support deleting locals
  537. ASSERT(!reference.is_local_variable());
  538. if (reference.is_global_variable())
  539. return global_object.delete_property(reference.name());
  540. auto* base_object = reference.base().to_object(global_object);
  541. if (!base_object)
  542. return {};
  543. return base_object->delete_property(reference.name());
  544. }
  545. Value lhs_result;
  546. if (m_op == UnaryOp::Typeof && m_lhs->is_identifier()) {
  547. auto reference = m_lhs->to_reference(interpreter, global_object);
  548. if (interpreter.exception()) {
  549. return {};
  550. }
  551. // FIXME: standard recommends checking with is_unresolvable but it ALWAYS return false here
  552. if (reference.is_local_variable() || reference.is_global_variable()) {
  553. auto name = reference.name();
  554. lhs_result = interpreter.vm().get_variable(name.to_string(), global_object).value_or(js_undefined());
  555. if (interpreter.exception())
  556. return {};
  557. }
  558. } else {
  559. lhs_result = m_lhs->execute(interpreter, global_object);
  560. if (interpreter.exception())
  561. return {};
  562. }
  563. switch (m_op) {
  564. case UnaryOp::BitwiseNot:
  565. return bitwise_not(global_object, lhs_result);
  566. case UnaryOp::Not:
  567. return Value(!lhs_result.to_boolean());
  568. case UnaryOp::Plus:
  569. return unary_plus(global_object, lhs_result);
  570. case UnaryOp::Minus:
  571. return unary_minus(global_object, lhs_result);
  572. case UnaryOp::Typeof:
  573. switch (lhs_result.type()) {
  574. case Value::Type::Empty:
  575. ASSERT_NOT_REACHED();
  576. return {};
  577. case Value::Type::Undefined:
  578. return js_string(vm, "undefined");
  579. case Value::Type::Null:
  580. // yes, this is on purpose. yes, this is how javascript works.
  581. // yes, it's silly.
  582. return js_string(vm, "object");
  583. case Value::Type::Number:
  584. return js_string(vm, "number");
  585. case Value::Type::String:
  586. return js_string(vm, "string");
  587. case Value::Type::Object:
  588. if (lhs_result.is_function())
  589. return js_string(vm, "function");
  590. return js_string(vm, "object");
  591. case Value::Type::Boolean:
  592. return js_string(vm, "boolean");
  593. case Value::Type::Symbol:
  594. return js_string(vm, "symbol");
  595. case Value::Type::BigInt:
  596. return js_string(vm, "bigint");
  597. default:
  598. ASSERT_NOT_REACHED();
  599. }
  600. case UnaryOp::Void:
  601. return js_undefined();
  602. case UnaryOp::Delete:
  603. ASSERT_NOT_REACHED();
  604. }
  605. ASSERT_NOT_REACHED();
  606. }
  607. Value SuperExpression::execute(Interpreter&, GlobalObject&) const
  608. {
  609. // The semantics for SuperExpressions are handled in CallExpression::compute_this_and_callee()
  610. ASSERT_NOT_REACHED();
  611. }
  612. Value ClassMethod::execute(Interpreter& interpreter, GlobalObject& global_object) const
  613. {
  614. return m_function->execute(interpreter, global_object);
  615. }
  616. Value ClassExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  617. {
  618. Value class_constructor_value = m_constructor->execute(interpreter, global_object);
  619. if (interpreter.exception())
  620. return {};
  621. update_function_name(class_constructor_value, m_name);
  622. ASSERT(class_constructor_value.is_function() && class_constructor_value.as_function().is_script_function());
  623. ScriptFunction* class_constructor = static_cast<ScriptFunction*>(&class_constructor_value.as_function());
  624. Value super_constructor = js_undefined();
  625. if (!m_super_class.is_null()) {
  626. super_constructor = m_super_class->execute(interpreter, global_object);
  627. if (interpreter.exception())
  628. return {};
  629. if (!super_constructor.is_function() && !super_constructor.is_null()) {
  630. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ClassDoesNotExtendAConstructorOrNull, super_constructor.to_string_without_side_effects().characters());
  631. return {};
  632. }
  633. class_constructor->set_constructor_kind(Function::ConstructorKind::Derived);
  634. Object* prototype = Object::create_empty(global_object);
  635. Object* super_constructor_prototype = nullptr;
  636. if (!super_constructor.is_null()) {
  637. super_constructor_prototype = &super_constructor.as_object().get("prototype").as_object();
  638. if (interpreter.exception())
  639. return {};
  640. }
  641. prototype->set_prototype(super_constructor_prototype);
  642. prototype->define_property("constructor", class_constructor, 0);
  643. if (interpreter.exception())
  644. return {};
  645. class_constructor->define_property("prototype", prototype, 0);
  646. if (interpreter.exception())
  647. return {};
  648. class_constructor->set_prototype(super_constructor.is_null() ? global_object.function_prototype() : &super_constructor.as_object());
  649. }
  650. auto class_prototype = class_constructor->get("prototype");
  651. if (interpreter.exception())
  652. return {};
  653. if (!class_prototype.is_object()) {
  654. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Class prototype");
  655. return {};
  656. }
  657. for (const auto& method : m_methods) {
  658. auto method_value = method.execute(interpreter, global_object);
  659. if (interpreter.exception())
  660. return {};
  661. auto& method_function = method_value.as_function();
  662. auto key = method.key().execute(interpreter, global_object);
  663. if (interpreter.exception())
  664. return {};
  665. auto& target = method.is_static() ? *class_constructor : class_prototype.as_object();
  666. method_function.set_home_object(&target);
  667. if (method.kind() == ClassMethod::Kind::Method) {
  668. target.define_property(StringOrSymbol::from_value(global_object, key), method_value);
  669. } else {
  670. String accessor_name = [&] {
  671. switch (method.kind()) {
  672. case ClassMethod::Kind::Getter:
  673. return String::format("get %s", get_function_name(global_object, key).characters());
  674. case ClassMethod::Kind::Setter:
  675. return String::format("set %s", get_function_name(global_object, key).characters());
  676. default:
  677. ASSERT_NOT_REACHED();
  678. }
  679. }();
  680. update_function_name(method_value, accessor_name);
  681. target.define_accessor(StringOrSymbol::from_value(global_object, key), method_function, method.kind() == ClassMethod::Kind::Getter, Attribute::Configurable | Attribute::Enumerable);
  682. }
  683. if (interpreter.exception())
  684. return {};
  685. }
  686. return class_constructor;
  687. }
  688. Value ClassDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  689. {
  690. Value class_constructor = m_class_expression->execute(interpreter, global_object);
  691. if (interpreter.exception())
  692. return {};
  693. interpreter.current_environment()->set(global_object, m_class_expression->name(), { class_constructor, DeclarationKind::Let });
  694. return js_undefined();
  695. }
  696. static void print_indent(int indent)
  697. {
  698. for (int i = 0; i < indent * 2; ++i)
  699. putchar(' ');
  700. }
  701. void ASTNode::dump(int indent) const
  702. {
  703. print_indent(indent);
  704. printf("%s\n", class_name());
  705. }
  706. void ScopeNode::dump(int indent) const
  707. {
  708. ASTNode::dump(indent);
  709. if (!m_variables.is_empty()) {
  710. print_indent(indent + 1);
  711. printf("(Variables)\n");
  712. for (auto& variable : m_variables)
  713. variable.dump(indent + 2);
  714. }
  715. if (!m_children.is_empty()) {
  716. print_indent(indent + 1);
  717. printf("(Children)\n");
  718. for (auto& child : children())
  719. child.dump(indent + 2);
  720. }
  721. }
  722. void BinaryExpression::dump(int indent) const
  723. {
  724. const char* op_string = nullptr;
  725. switch (m_op) {
  726. case BinaryOp::Addition:
  727. op_string = "+";
  728. break;
  729. case BinaryOp::Subtraction:
  730. op_string = "-";
  731. break;
  732. case BinaryOp::Multiplication:
  733. op_string = "*";
  734. break;
  735. case BinaryOp::Division:
  736. op_string = "/";
  737. break;
  738. case BinaryOp::Modulo:
  739. op_string = "%";
  740. break;
  741. case BinaryOp::Exponentiation:
  742. op_string = "**";
  743. break;
  744. case BinaryOp::TypedEquals:
  745. op_string = "===";
  746. break;
  747. case BinaryOp::TypedInequals:
  748. op_string = "!==";
  749. break;
  750. case BinaryOp::AbstractEquals:
  751. op_string = "==";
  752. break;
  753. case BinaryOp::AbstractInequals:
  754. op_string = "!=";
  755. break;
  756. case BinaryOp::GreaterThan:
  757. op_string = ">";
  758. break;
  759. case BinaryOp::GreaterThanEquals:
  760. op_string = ">=";
  761. break;
  762. case BinaryOp::LessThan:
  763. op_string = "<";
  764. break;
  765. case BinaryOp::LessThanEquals:
  766. op_string = "<=";
  767. break;
  768. case BinaryOp::BitwiseAnd:
  769. op_string = "&";
  770. break;
  771. case BinaryOp::BitwiseOr:
  772. op_string = "|";
  773. break;
  774. case BinaryOp::BitwiseXor:
  775. op_string = "^";
  776. break;
  777. case BinaryOp::LeftShift:
  778. op_string = "<<";
  779. break;
  780. case BinaryOp::RightShift:
  781. op_string = ">>";
  782. break;
  783. case BinaryOp::UnsignedRightShift:
  784. op_string = ">>>";
  785. break;
  786. case BinaryOp::In:
  787. op_string = "in";
  788. break;
  789. case BinaryOp::InstanceOf:
  790. op_string = "instanceof";
  791. break;
  792. }
  793. print_indent(indent);
  794. printf("%s\n", class_name());
  795. m_lhs->dump(indent + 1);
  796. print_indent(indent + 1);
  797. printf("%s\n", op_string);
  798. m_rhs->dump(indent + 1);
  799. }
  800. void LogicalExpression::dump(int indent) const
  801. {
  802. const char* op_string = nullptr;
  803. switch (m_op) {
  804. case LogicalOp::And:
  805. op_string = "&&";
  806. break;
  807. case LogicalOp::Or:
  808. op_string = "||";
  809. break;
  810. case LogicalOp::NullishCoalescing:
  811. op_string = "??";
  812. break;
  813. }
  814. print_indent(indent);
  815. printf("%s\n", class_name());
  816. m_lhs->dump(indent + 1);
  817. print_indent(indent + 1);
  818. printf("%s\n", op_string);
  819. m_rhs->dump(indent + 1);
  820. }
  821. void UnaryExpression::dump(int indent) const
  822. {
  823. const char* op_string = nullptr;
  824. switch (m_op) {
  825. case UnaryOp::BitwiseNot:
  826. op_string = "~";
  827. break;
  828. case UnaryOp::Not:
  829. op_string = "!";
  830. break;
  831. case UnaryOp::Plus:
  832. op_string = "+";
  833. break;
  834. case UnaryOp::Minus:
  835. op_string = "-";
  836. break;
  837. case UnaryOp::Typeof:
  838. op_string = "typeof ";
  839. break;
  840. case UnaryOp::Void:
  841. op_string = "void ";
  842. break;
  843. case UnaryOp::Delete:
  844. op_string = "delete ";
  845. break;
  846. }
  847. print_indent(indent);
  848. printf("%s\n", class_name());
  849. print_indent(indent + 1);
  850. printf("%s\n", op_string);
  851. m_lhs->dump(indent + 1);
  852. }
  853. void CallExpression::dump(int indent) const
  854. {
  855. print_indent(indent);
  856. printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
  857. m_callee->dump(indent + 1);
  858. for (auto& argument : m_arguments)
  859. argument.value->dump(indent + 1);
  860. }
  861. void ClassDeclaration::dump(int indent) const
  862. {
  863. ASTNode::dump(indent);
  864. m_class_expression->dump(indent + 1);
  865. }
  866. void ClassExpression::dump(int indent) const
  867. {
  868. print_indent(indent);
  869. ASSERT(m_name.characters());
  870. printf("ClassExpression: \"%s\"\n", m_name.characters());
  871. print_indent(indent);
  872. printf("(Constructor)\n");
  873. m_constructor->dump(indent + 1);
  874. if (!m_super_class.is_null()) {
  875. print_indent(indent);
  876. printf("(Super Class)\n");
  877. m_super_class->dump(indent + 1);
  878. }
  879. print_indent(indent);
  880. printf("(Methods)\n");
  881. for (auto& method : m_methods)
  882. method.dump(indent + 1);
  883. }
  884. void ClassMethod::dump(int indent) const
  885. {
  886. ASTNode::dump(indent);
  887. print_indent(indent);
  888. printf("(Key)\n");
  889. m_key->dump(indent + 1);
  890. const char* kind_string = nullptr;
  891. switch (m_kind) {
  892. case Kind::Method:
  893. kind_string = "Method";
  894. break;
  895. case Kind::Getter:
  896. kind_string = "Getter";
  897. break;
  898. case Kind::Setter:
  899. kind_string = "Setter";
  900. break;
  901. }
  902. print_indent(indent);
  903. printf("Kind: %s\n", kind_string);
  904. print_indent(indent);
  905. printf("Static: %s\n", m_is_static ? "true" : "false");
  906. print_indent(indent);
  907. printf("(Function)\n");
  908. m_function->dump(indent + 1);
  909. }
  910. void StringLiteral::dump(int indent) const
  911. {
  912. print_indent(indent);
  913. printf("StringLiteral \"%s\"\n", m_value.characters());
  914. }
  915. void SuperExpression::dump(int indent) const
  916. {
  917. print_indent(indent);
  918. printf("super\n");
  919. }
  920. void NumericLiteral::dump(int indent) const
  921. {
  922. print_indent(indent);
  923. printf("NumericLiteral %g\n", m_value);
  924. }
  925. void BigIntLiteral::dump(int indent) const
  926. {
  927. print_indent(indent);
  928. printf("BigIntLiteral %s\n", m_value.characters());
  929. }
  930. void BooleanLiteral::dump(int indent) const
  931. {
  932. print_indent(indent);
  933. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  934. }
  935. void NullLiteral::dump(int indent) const
  936. {
  937. print_indent(indent);
  938. printf("null\n");
  939. }
  940. void FunctionNode::dump(int indent, const char* class_name) const
  941. {
  942. print_indent(indent);
  943. printf("%s '%s'\n", class_name, name().characters());
  944. if (!m_parameters.is_empty()) {
  945. print_indent(indent + 1);
  946. printf("(Parameters)\n");
  947. for (auto& parameter : m_parameters) {
  948. print_indent(indent + 2);
  949. if (parameter.is_rest)
  950. printf("...");
  951. printf("%s\n", parameter.name.characters());
  952. if (parameter.default_value)
  953. parameter.default_value->dump(indent + 3);
  954. }
  955. }
  956. if (!m_variables.is_empty()) {
  957. print_indent(indent + 1);
  958. printf("(Variables)\n");
  959. for (auto& variable : m_variables)
  960. variable.dump(indent + 2);
  961. }
  962. print_indent(indent + 1);
  963. printf("(Body)\n");
  964. body().dump(indent + 2);
  965. }
  966. void FunctionDeclaration::dump(int indent) const
  967. {
  968. FunctionNode::dump(indent, class_name());
  969. }
  970. void FunctionExpression::dump(int indent) const
  971. {
  972. FunctionNode::dump(indent, class_name());
  973. }
  974. void ReturnStatement::dump(int indent) const
  975. {
  976. ASTNode::dump(indent);
  977. if (argument())
  978. argument()->dump(indent + 1);
  979. }
  980. void IfStatement::dump(int indent) const
  981. {
  982. ASTNode::dump(indent);
  983. print_indent(indent);
  984. printf("If\n");
  985. predicate().dump(indent + 1);
  986. consequent().dump(indent + 1);
  987. if (alternate()) {
  988. print_indent(indent);
  989. printf("Else\n");
  990. alternate()->dump(indent + 1);
  991. }
  992. }
  993. void WhileStatement::dump(int indent) const
  994. {
  995. ASTNode::dump(indent);
  996. print_indent(indent);
  997. printf("While\n");
  998. test().dump(indent + 1);
  999. body().dump(indent + 1);
  1000. }
  1001. void DoWhileStatement::dump(int indent) const
  1002. {
  1003. ASTNode::dump(indent);
  1004. print_indent(indent);
  1005. printf("DoWhile\n");
  1006. test().dump(indent + 1);
  1007. body().dump(indent + 1);
  1008. }
  1009. void ForStatement::dump(int indent) const
  1010. {
  1011. ASTNode::dump(indent);
  1012. print_indent(indent);
  1013. printf("For\n");
  1014. if (init())
  1015. init()->dump(indent + 1);
  1016. if (test())
  1017. test()->dump(indent + 1);
  1018. if (update())
  1019. update()->dump(indent + 1);
  1020. body().dump(indent + 1);
  1021. }
  1022. void ForInStatement::dump(int indent) const
  1023. {
  1024. ASTNode::dump(indent);
  1025. print_indent(indent);
  1026. printf("ForIn\n");
  1027. lhs().dump(indent + 1);
  1028. rhs().dump(indent + 1);
  1029. body().dump(indent + 1);
  1030. }
  1031. void ForOfStatement::dump(int indent) const
  1032. {
  1033. ASTNode::dump(indent);
  1034. print_indent(indent);
  1035. printf("ForOf\n");
  1036. lhs().dump(indent + 1);
  1037. rhs().dump(indent + 1);
  1038. body().dump(indent + 1);
  1039. }
  1040. Value Identifier::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1041. {
  1042. auto value = interpreter.vm().get_variable(string(), global_object);
  1043. if (value.is_empty()) {
  1044. interpreter.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, string().characters());
  1045. return {};
  1046. }
  1047. return value;
  1048. }
  1049. void Identifier::dump(int indent) const
  1050. {
  1051. print_indent(indent);
  1052. printf("Identifier \"%s\"\n", m_string.characters());
  1053. }
  1054. void SpreadExpression::dump(int indent) const
  1055. {
  1056. ASTNode::dump(indent);
  1057. m_target->dump(indent + 1);
  1058. }
  1059. Value SpreadExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1060. {
  1061. return m_target->execute(interpreter, global_object);
  1062. }
  1063. Value ThisExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1064. {
  1065. return interpreter.vm().resolve_this_binding(global_object);
  1066. }
  1067. void ThisExpression::dump(int indent) const
  1068. {
  1069. ASTNode::dump(indent);
  1070. }
  1071. Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1072. {
  1073. auto rhs_result = m_rhs->execute(interpreter, global_object);
  1074. if (interpreter.exception())
  1075. return {};
  1076. Value lhs_result;
  1077. switch (m_op) {
  1078. case AssignmentOp::Assignment:
  1079. break;
  1080. case AssignmentOp::AdditionAssignment:
  1081. lhs_result = m_lhs->execute(interpreter, global_object);
  1082. if (interpreter.exception())
  1083. return {};
  1084. rhs_result = add(global_object, lhs_result, rhs_result);
  1085. break;
  1086. case AssignmentOp::SubtractionAssignment:
  1087. lhs_result = m_lhs->execute(interpreter, global_object);
  1088. if (interpreter.exception())
  1089. return {};
  1090. rhs_result = sub(global_object, lhs_result, rhs_result);
  1091. break;
  1092. case AssignmentOp::MultiplicationAssignment:
  1093. lhs_result = m_lhs->execute(interpreter, global_object);
  1094. if (interpreter.exception())
  1095. return {};
  1096. rhs_result = mul(global_object, lhs_result, rhs_result);
  1097. break;
  1098. case AssignmentOp::DivisionAssignment:
  1099. lhs_result = m_lhs->execute(interpreter, global_object);
  1100. if (interpreter.exception())
  1101. return {};
  1102. rhs_result = div(global_object, lhs_result, rhs_result);
  1103. break;
  1104. case AssignmentOp::ModuloAssignment:
  1105. lhs_result = m_lhs->execute(interpreter, global_object);
  1106. if (interpreter.exception())
  1107. return {};
  1108. rhs_result = mod(global_object, lhs_result, rhs_result);
  1109. break;
  1110. case AssignmentOp::ExponentiationAssignment:
  1111. lhs_result = m_lhs->execute(interpreter, global_object);
  1112. if (interpreter.exception())
  1113. return {};
  1114. rhs_result = exp(global_object, lhs_result, rhs_result);
  1115. break;
  1116. case AssignmentOp::BitwiseAndAssignment:
  1117. lhs_result = m_lhs->execute(interpreter, global_object);
  1118. if (interpreter.exception())
  1119. return {};
  1120. rhs_result = bitwise_and(global_object, lhs_result, rhs_result);
  1121. break;
  1122. case AssignmentOp::BitwiseOrAssignment:
  1123. lhs_result = m_lhs->execute(interpreter, global_object);
  1124. if (interpreter.exception())
  1125. return {};
  1126. rhs_result = bitwise_or(global_object, lhs_result, rhs_result);
  1127. break;
  1128. case AssignmentOp::BitwiseXorAssignment:
  1129. lhs_result = m_lhs->execute(interpreter, global_object);
  1130. if (interpreter.exception())
  1131. return {};
  1132. rhs_result = bitwise_xor(global_object, lhs_result, rhs_result);
  1133. break;
  1134. case AssignmentOp::LeftShiftAssignment:
  1135. lhs_result = m_lhs->execute(interpreter, global_object);
  1136. if (interpreter.exception())
  1137. return {};
  1138. rhs_result = left_shift(global_object, lhs_result, rhs_result);
  1139. break;
  1140. case AssignmentOp::RightShiftAssignment:
  1141. lhs_result = m_lhs->execute(interpreter, global_object);
  1142. if (interpreter.exception())
  1143. return {};
  1144. rhs_result = right_shift(global_object, lhs_result, rhs_result);
  1145. break;
  1146. case AssignmentOp::UnsignedRightShiftAssignment:
  1147. lhs_result = m_lhs->execute(interpreter, global_object);
  1148. if (interpreter.exception())
  1149. return {};
  1150. rhs_result = unsigned_right_shift(global_object, lhs_result, rhs_result);
  1151. break;
  1152. }
  1153. if (interpreter.exception())
  1154. return {};
  1155. auto reference = m_lhs->to_reference(interpreter, global_object);
  1156. if (interpreter.exception())
  1157. return {};
  1158. if (reference.is_unresolvable()) {
  1159. interpreter.vm().throw_exception<ReferenceError>(global_object, ErrorType::InvalidLeftHandAssignment);
  1160. return {};
  1161. }
  1162. update_function_name(rhs_result, get_function_name(global_object, reference.name().to_value(interpreter.vm())));
  1163. reference.put(global_object, rhs_result);
  1164. if (interpreter.exception())
  1165. return {};
  1166. return rhs_result;
  1167. }
  1168. Value UpdateExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1169. {
  1170. auto reference = m_argument->to_reference(interpreter, global_object);
  1171. if (interpreter.exception())
  1172. return {};
  1173. auto old_value = reference.get(global_object);
  1174. if (interpreter.exception())
  1175. return {};
  1176. old_value = old_value.to_numeric(global_object);
  1177. if (interpreter.exception())
  1178. return {};
  1179. Value new_value;
  1180. switch (m_op) {
  1181. case UpdateOp::Increment:
  1182. if (old_value.is_number())
  1183. new_value = Value(old_value.as_double() + 1);
  1184. else
  1185. new_value = js_bigint(interpreter.heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  1186. break;
  1187. case UpdateOp::Decrement:
  1188. if (old_value.is_number())
  1189. new_value = Value(old_value.as_double() - 1);
  1190. else
  1191. new_value = js_bigint(interpreter.heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  1192. break;
  1193. default:
  1194. ASSERT_NOT_REACHED();
  1195. }
  1196. reference.put(global_object, new_value);
  1197. if (interpreter.exception())
  1198. return {};
  1199. return m_prefixed ? new_value : old_value;
  1200. }
  1201. void AssignmentExpression::dump(int indent) const
  1202. {
  1203. const char* op_string = nullptr;
  1204. switch (m_op) {
  1205. case AssignmentOp::Assignment:
  1206. op_string = "=";
  1207. break;
  1208. case AssignmentOp::AdditionAssignment:
  1209. op_string = "+=";
  1210. break;
  1211. case AssignmentOp::SubtractionAssignment:
  1212. op_string = "-=";
  1213. break;
  1214. case AssignmentOp::MultiplicationAssignment:
  1215. op_string = "*=";
  1216. break;
  1217. case AssignmentOp::DivisionAssignment:
  1218. op_string = "/=";
  1219. break;
  1220. case AssignmentOp::ModuloAssignment:
  1221. op_string = "%=";
  1222. break;
  1223. case AssignmentOp::ExponentiationAssignment:
  1224. op_string = "**=";
  1225. break;
  1226. case AssignmentOp::BitwiseAndAssignment:
  1227. op_string = "&=";
  1228. break;
  1229. case AssignmentOp::BitwiseOrAssignment:
  1230. op_string = "|=";
  1231. break;
  1232. case AssignmentOp::BitwiseXorAssignment:
  1233. op_string = "^=";
  1234. break;
  1235. case AssignmentOp::LeftShiftAssignment:
  1236. op_string = "<<=";
  1237. break;
  1238. case AssignmentOp::RightShiftAssignment:
  1239. op_string = ">>=";
  1240. break;
  1241. case AssignmentOp::UnsignedRightShiftAssignment:
  1242. op_string = ">>>=";
  1243. break;
  1244. }
  1245. ASTNode::dump(indent);
  1246. print_indent(indent + 1);
  1247. printf("%s\n", op_string);
  1248. m_lhs->dump(indent + 1);
  1249. m_rhs->dump(indent + 1);
  1250. }
  1251. void UpdateExpression::dump(int indent) const
  1252. {
  1253. const char* op_string = nullptr;
  1254. switch (m_op) {
  1255. case UpdateOp::Increment:
  1256. op_string = "++";
  1257. break;
  1258. case UpdateOp::Decrement:
  1259. op_string = "--";
  1260. break;
  1261. }
  1262. ASTNode::dump(indent);
  1263. print_indent(indent + 1);
  1264. if (m_prefixed)
  1265. printf("%s\n", op_string);
  1266. m_argument->dump(indent + 1);
  1267. if (!m_prefixed) {
  1268. print_indent(indent + 1);
  1269. printf("%s\n", op_string);
  1270. }
  1271. }
  1272. Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1273. {
  1274. for (auto& declarator : m_declarations) {
  1275. if (auto* init = declarator.init()) {
  1276. auto initalizer_result = init->execute(interpreter, global_object);
  1277. if (interpreter.exception())
  1278. return {};
  1279. auto variable_name = declarator.id().string();
  1280. update_function_name(initalizer_result, variable_name);
  1281. interpreter.vm().set_variable(variable_name, initalizer_result, global_object, true);
  1282. }
  1283. }
  1284. return js_undefined();
  1285. }
  1286. Value VariableDeclarator::execute(Interpreter&, GlobalObject&) const
  1287. {
  1288. // NOTE: This node is handled by VariableDeclaration.
  1289. ASSERT_NOT_REACHED();
  1290. }
  1291. void VariableDeclaration::dump(int indent) const
  1292. {
  1293. const char* declaration_kind_string = nullptr;
  1294. switch (m_declaration_kind) {
  1295. case DeclarationKind::Let:
  1296. declaration_kind_string = "Let";
  1297. break;
  1298. case DeclarationKind::Var:
  1299. declaration_kind_string = "Var";
  1300. break;
  1301. case DeclarationKind::Const:
  1302. declaration_kind_string = "Const";
  1303. break;
  1304. }
  1305. ASTNode::dump(indent);
  1306. print_indent(indent + 1);
  1307. printf("%s\n", declaration_kind_string);
  1308. for (auto& declarator : m_declarations)
  1309. declarator.dump(indent + 1);
  1310. }
  1311. void VariableDeclarator::dump(int indent) const
  1312. {
  1313. ASTNode::dump(indent);
  1314. m_id->dump(indent + 1);
  1315. if (m_init)
  1316. m_init->dump(indent + 1);
  1317. }
  1318. void ObjectProperty::dump(int indent) const
  1319. {
  1320. ASTNode::dump(indent);
  1321. m_key->dump(indent + 1);
  1322. m_value->dump(indent + 1);
  1323. }
  1324. void ObjectExpression::dump(int indent) const
  1325. {
  1326. ASTNode::dump(indent);
  1327. for (auto& property : m_properties) {
  1328. property.dump(indent + 1);
  1329. }
  1330. }
  1331. void ExpressionStatement::dump(int indent) const
  1332. {
  1333. ASTNode::dump(indent);
  1334. m_expression->dump(indent + 1);
  1335. }
  1336. Value ObjectProperty::execute(Interpreter&, GlobalObject&) const
  1337. {
  1338. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  1339. ASSERT_NOT_REACHED();
  1340. }
  1341. Value ObjectExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1342. {
  1343. auto* object = Object::create_empty(global_object);
  1344. for (auto& property : m_properties) {
  1345. auto key = property.key().execute(interpreter, global_object);
  1346. if (interpreter.exception())
  1347. return {};
  1348. if (property.type() == ObjectProperty::Type::Spread) {
  1349. if (key.is_array()) {
  1350. auto& array_to_spread = static_cast<Array&>(key.as_object());
  1351. for (auto& entry : array_to_spread.indexed_properties()) {
  1352. object->indexed_properties().append(entry.value_and_attributes(&array_to_spread).value);
  1353. if (interpreter.exception())
  1354. return {};
  1355. }
  1356. } else if (key.is_object()) {
  1357. auto& obj_to_spread = key.as_object();
  1358. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  1359. if (it.value.attributes.is_enumerable()) {
  1360. object->define_property(it.key, obj_to_spread.get(it.key));
  1361. if (interpreter.exception())
  1362. return {};
  1363. }
  1364. }
  1365. } else if (key.is_string()) {
  1366. auto& str_to_spread = key.as_string().string();
  1367. for (size_t i = 0; i < str_to_spread.length(); i++) {
  1368. object->define_property(i, js_string(interpreter.heap(), str_to_spread.substring(i, 1)));
  1369. if (interpreter.exception())
  1370. return {};
  1371. }
  1372. }
  1373. continue;
  1374. }
  1375. if (interpreter.exception())
  1376. return {};
  1377. auto value = property.value().execute(interpreter, global_object);
  1378. if (interpreter.exception())
  1379. return {};
  1380. if (value.is_function() && property.is_method())
  1381. value.as_function().set_home_object(object);
  1382. String name = get_function_name(global_object, key);
  1383. if (property.type() == ObjectProperty::Type::Getter) {
  1384. name = String::format("get %s", name.characters());
  1385. } else if (property.type() == ObjectProperty::Type::Setter) {
  1386. name = String::format("set %s", name.characters());
  1387. }
  1388. update_function_name(value, name);
  1389. if (property.type() == ObjectProperty::Type::Getter || property.type() == ObjectProperty::Type::Setter) {
  1390. ASSERT(value.is_function());
  1391. object->define_accessor(PropertyName::from_value(global_object, key), value.as_function(), property.type() == ObjectProperty::Type::Getter, Attribute::Configurable | Attribute::Enumerable);
  1392. if (interpreter.exception())
  1393. return {};
  1394. } else {
  1395. object->define_property(PropertyName::from_value(global_object, key), value);
  1396. if (interpreter.exception())
  1397. return {};
  1398. }
  1399. }
  1400. return object;
  1401. }
  1402. void MemberExpression::dump(int indent) const
  1403. {
  1404. print_indent(indent);
  1405. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  1406. m_object->dump(indent + 1);
  1407. m_property->dump(indent + 1);
  1408. }
  1409. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter, GlobalObject& global_object) const
  1410. {
  1411. if (!is_computed()) {
  1412. ASSERT(m_property->is_identifier());
  1413. return static_cast<const Identifier&>(*m_property).string();
  1414. }
  1415. auto index = m_property->execute(interpreter, global_object);
  1416. if (interpreter.exception())
  1417. return {};
  1418. ASSERT(!index.is_empty());
  1419. if (index.is_integer() && index.as_i32() >= 0)
  1420. return index.as_i32();
  1421. if (index.is_symbol())
  1422. return &index.as_symbol();
  1423. auto index_string = index.to_string(global_object);
  1424. if (interpreter.exception())
  1425. return {};
  1426. return index_string;
  1427. }
  1428. String MemberExpression::to_string_approximation() const
  1429. {
  1430. String object_string = "<object>";
  1431. if (m_object->is_identifier())
  1432. object_string = static_cast<const Identifier&>(*m_object).string();
  1433. if (is_computed())
  1434. return String::format("%s[<computed>]", object_string.characters());
  1435. ASSERT(m_property->is_identifier());
  1436. return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
  1437. }
  1438. Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1439. {
  1440. auto object_value = m_object->execute(interpreter, global_object);
  1441. if (interpreter.exception())
  1442. return {};
  1443. auto* object_result = object_value.to_object(global_object);
  1444. if (interpreter.exception())
  1445. return {};
  1446. auto property_name = computed_property_name(interpreter, global_object);
  1447. if (!property_name.is_valid())
  1448. return {};
  1449. return object_result->get(property_name).value_or(js_undefined());
  1450. }
  1451. Value StringLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1452. {
  1453. return js_string(interpreter.heap(), m_value);
  1454. }
  1455. Value NumericLiteral::execute(Interpreter&, GlobalObject&) const
  1456. {
  1457. return Value(m_value);
  1458. }
  1459. Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1460. {
  1461. return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
  1462. }
  1463. Value BooleanLiteral::execute(Interpreter&, GlobalObject&) const
  1464. {
  1465. return Value(m_value);
  1466. }
  1467. Value NullLiteral::execute(Interpreter&, GlobalObject&) const
  1468. {
  1469. return js_null();
  1470. }
  1471. void RegExpLiteral::dump(int indent) const
  1472. {
  1473. print_indent(indent);
  1474. printf("%s (/%s/%s)\n", class_name(), content().characters(), flags().characters());
  1475. }
  1476. Value RegExpLiteral::execute(Interpreter&, GlobalObject& global_object) const
  1477. {
  1478. return RegExpObject::create(global_object, content(), flags());
  1479. }
  1480. void ArrayExpression::dump(int indent) const
  1481. {
  1482. ASTNode::dump(indent);
  1483. for (auto& element : m_elements) {
  1484. if (element) {
  1485. element->dump(indent + 1);
  1486. } else {
  1487. print_indent(indent + 1);
  1488. printf("<empty>\n");
  1489. }
  1490. }
  1491. }
  1492. Value ArrayExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1493. {
  1494. auto* array = Array::create(global_object);
  1495. for (auto& element : m_elements) {
  1496. auto value = Value();
  1497. if (element) {
  1498. value = element->execute(interpreter, global_object);
  1499. if (interpreter.exception())
  1500. return {};
  1501. if (element->is_spread_expression()) {
  1502. get_iterator_values(global_object, value, [&](Value iterator_value) {
  1503. array->indexed_properties().append(iterator_value);
  1504. return IterationDecision::Continue;
  1505. });
  1506. if (interpreter.exception())
  1507. return {};
  1508. continue;
  1509. }
  1510. }
  1511. array->indexed_properties().append(value);
  1512. }
  1513. return array;
  1514. }
  1515. void TemplateLiteral::dump(int indent) const
  1516. {
  1517. ASTNode::dump(indent);
  1518. for (auto& expression : m_expressions)
  1519. expression.dump(indent + 1);
  1520. }
  1521. Value TemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1522. {
  1523. StringBuilder string_builder;
  1524. for (auto& expression : m_expressions) {
  1525. auto expr = expression.execute(interpreter, global_object);
  1526. if (interpreter.exception())
  1527. return {};
  1528. auto string = expr.to_string(global_object);
  1529. if (interpreter.exception())
  1530. return {};
  1531. string_builder.append(string);
  1532. }
  1533. return js_string(interpreter.heap(), string_builder.build());
  1534. }
  1535. void TaggedTemplateLiteral::dump(int indent) const
  1536. {
  1537. ASTNode::dump(indent);
  1538. print_indent(indent + 1);
  1539. printf("(Tag)\n");
  1540. m_tag->dump(indent + 2);
  1541. print_indent(indent + 1);
  1542. printf("(Template Literal)\n");
  1543. m_template_literal->dump(indent + 2);
  1544. }
  1545. Value TaggedTemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1546. {
  1547. auto tag = m_tag->execute(interpreter, global_object);
  1548. if (interpreter.exception())
  1549. return {};
  1550. if (!tag.is_function()) {
  1551. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::NotAFunction, tag.to_string_without_side_effects().characters());
  1552. return {};
  1553. }
  1554. auto& tag_function = tag.as_function();
  1555. auto& expressions = m_template_literal->expressions();
  1556. auto* strings = Array::create(global_object);
  1557. MarkedValueList arguments(interpreter.heap());
  1558. arguments.append(strings);
  1559. for (size_t i = 0; i < expressions.size(); ++i) {
  1560. auto value = expressions[i].execute(interpreter, global_object);
  1561. if (interpreter.exception())
  1562. return {};
  1563. // tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
  1564. // tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
  1565. if (i % 2 == 0) {
  1566. strings->indexed_properties().append(value);
  1567. } else {
  1568. arguments.append(value);
  1569. }
  1570. }
  1571. auto* raw_strings = Array::create(global_object);
  1572. for (auto& raw_string : m_template_literal->raw_strings()) {
  1573. auto value = raw_string.execute(interpreter, global_object);
  1574. if (interpreter.exception())
  1575. return {};
  1576. raw_strings->indexed_properties().append(value);
  1577. }
  1578. strings->define_property("raw", raw_strings, 0);
  1579. return interpreter.call(tag_function, js_undefined(), move(arguments));
  1580. }
  1581. void TryStatement::dump(int indent) const
  1582. {
  1583. ASTNode::dump(indent);
  1584. print_indent(indent);
  1585. printf("(Block)\n");
  1586. block().dump(indent + 1);
  1587. if (handler()) {
  1588. print_indent(indent);
  1589. printf("(Handler)\n");
  1590. handler()->dump(indent + 1);
  1591. }
  1592. if (finalizer()) {
  1593. print_indent(indent);
  1594. printf("(Finalizer)\n");
  1595. finalizer()->dump(indent + 1);
  1596. }
  1597. }
  1598. void CatchClause::dump(int indent) const
  1599. {
  1600. print_indent(indent);
  1601. printf("CatchClause");
  1602. if (!m_parameter.is_null())
  1603. printf(" (%s)", m_parameter.characters());
  1604. printf("\n");
  1605. body().dump(indent + 1);
  1606. }
  1607. void ThrowStatement::dump(int indent) const
  1608. {
  1609. ASTNode::dump(indent);
  1610. argument().dump(indent + 1);
  1611. }
  1612. Value TryStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1613. {
  1614. interpreter.execute_statement(global_object, m_block, {}, ScopeType::Try);
  1615. if (auto* exception = interpreter.exception()) {
  1616. if (m_handler) {
  1617. interpreter.vm().clear_exception();
  1618. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  1619. interpreter.execute_statement(global_object, m_handler->body(), move(arguments));
  1620. }
  1621. }
  1622. if (m_finalizer) {
  1623. // Keep, if any, and then clear the current exception so we can
  1624. // execute() the finalizer without an exception in our way.
  1625. auto* previous_exception = interpreter.exception();
  1626. interpreter.vm().clear_exception();
  1627. interpreter.vm().stop_unwind();
  1628. m_finalizer->execute(interpreter, global_object);
  1629. // If we previously had an exception and the finalizer didn't
  1630. // throw a new one, restore the old one.
  1631. // FIXME: This will print debug output in throw_exception() for
  1632. // a seconds time with INTERPRETER_DEBUG enabled.
  1633. if (previous_exception && !interpreter.exception())
  1634. interpreter.vm().throw_exception(previous_exception);
  1635. }
  1636. return js_undefined();
  1637. }
  1638. Value CatchClause::execute(Interpreter&, GlobalObject&) const
  1639. {
  1640. // NOTE: CatchClause execution is handled by TryStatement.
  1641. ASSERT_NOT_REACHED();
  1642. return {};
  1643. }
  1644. Value ThrowStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1645. {
  1646. auto value = m_argument->execute(interpreter, global_object);
  1647. if (interpreter.vm().exception())
  1648. return {};
  1649. interpreter.vm().throw_exception(global_object, value);
  1650. return {};
  1651. }
  1652. Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1653. {
  1654. auto discriminant_result = m_discriminant->execute(interpreter, global_object);
  1655. if (interpreter.exception())
  1656. return {};
  1657. bool falling_through = false;
  1658. for (auto& switch_case : m_cases) {
  1659. if (!falling_through && switch_case.test()) {
  1660. auto test_result = switch_case.test()->execute(interpreter, global_object);
  1661. if (interpreter.exception())
  1662. return {};
  1663. if (!strict_eq(discriminant_result, test_result))
  1664. continue;
  1665. }
  1666. falling_through = true;
  1667. for (auto& statement : switch_case.consequent()) {
  1668. statement.execute(interpreter, global_object);
  1669. if (interpreter.exception())
  1670. return {};
  1671. if (interpreter.vm().should_unwind()) {
  1672. if (interpreter.vm().should_unwind_until(ScopeType::Breakable, m_label)) {
  1673. interpreter.vm().stop_unwind();
  1674. return {};
  1675. }
  1676. return {};
  1677. }
  1678. }
  1679. }
  1680. return js_undefined();
  1681. }
  1682. Value SwitchCase::execute(Interpreter&, GlobalObject&) const
  1683. {
  1684. return {};
  1685. }
  1686. Value BreakStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1687. {
  1688. interpreter.vm().unwind(ScopeType::Breakable, m_target_label);
  1689. return js_undefined();
  1690. }
  1691. Value ContinueStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1692. {
  1693. interpreter.vm().unwind(ScopeType::Continuable, m_target_label);
  1694. return js_undefined();
  1695. }
  1696. void SwitchStatement::dump(int indent) const
  1697. {
  1698. ASTNode::dump(indent);
  1699. m_discriminant->dump(indent + 1);
  1700. for (auto& switch_case : m_cases) {
  1701. switch_case.dump(indent + 1);
  1702. }
  1703. }
  1704. void SwitchCase::dump(int indent) const
  1705. {
  1706. ASTNode::dump(indent);
  1707. print_indent(indent + 1);
  1708. if (m_test) {
  1709. printf("(Test)\n");
  1710. m_test->dump(indent + 2);
  1711. } else {
  1712. printf("(Default)\n");
  1713. }
  1714. print_indent(indent + 1);
  1715. printf("(Consequent)\n");
  1716. for (auto& statement : m_consequent)
  1717. statement.dump(indent + 2);
  1718. }
  1719. Value ConditionalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1720. {
  1721. auto test_result = m_test->execute(interpreter, global_object);
  1722. if (interpreter.exception())
  1723. return {};
  1724. Value result;
  1725. if (test_result.to_boolean()) {
  1726. result = m_consequent->execute(interpreter, global_object);
  1727. } else {
  1728. result = m_alternate->execute(interpreter, global_object);
  1729. }
  1730. if (interpreter.exception())
  1731. return {};
  1732. return result;
  1733. }
  1734. void ConditionalExpression::dump(int indent) const
  1735. {
  1736. ASTNode::dump(indent);
  1737. print_indent(indent + 1);
  1738. printf("(Test)\n");
  1739. m_test->dump(indent + 2);
  1740. print_indent(indent + 1);
  1741. printf("(Consequent)\n");
  1742. m_consequent->dump(indent + 2);
  1743. print_indent(indent + 1);
  1744. printf("(Alternate)\n");
  1745. m_alternate->dump(indent + 2);
  1746. }
  1747. void SequenceExpression::dump(int indent) const
  1748. {
  1749. ASTNode::dump(indent);
  1750. for (auto& expression : m_expressions)
  1751. expression.dump(indent + 1);
  1752. }
  1753. Value SequenceExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1754. {
  1755. Value last_value;
  1756. for (auto& expression : m_expressions) {
  1757. last_value = expression.execute(interpreter, global_object);
  1758. if (interpreter.exception())
  1759. return {};
  1760. }
  1761. return last_value;
  1762. }
  1763. Value DebuggerStatement::execute(Interpreter&, GlobalObject&) const
  1764. {
  1765. // Sorry, no JavaScript debugger available (yet)!
  1766. return js_undefined();
  1767. }
  1768. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1769. {
  1770. m_variables.append(move(variables));
  1771. }
  1772. void ScopeNode::add_functions(NonnullRefPtrVector<FunctionDeclaration> functions)
  1773. {
  1774. m_functions.append(move(functions));
  1775. }
  1776. }