AST.cpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  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/Function.h>
  28. #include <AK/HashMap.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/MarkedValueList.h>
  40. #include <LibJS/Runtime/NativeFunction.h>
  41. #include <LibJS/Runtime/PrimitiveString.h>
  42. #include <LibJS/Runtime/Reference.h>
  43. #include <LibJS/Runtime/RegExpObject.h>
  44. #include <LibJS/Runtime/ScriptFunction.h>
  45. #include <LibJS/Runtime/Shape.h>
  46. #include <LibJS/Runtime/StringObject.h>
  47. #include <stdio.h>
  48. namespace JS {
  49. static void update_function_name(Value& value, const FlyString& name)
  50. {
  51. if (!value.is_object())
  52. return;
  53. auto& object = value.as_object();
  54. if (object.is_function()) {
  55. auto& function = static_cast<Function&>(object);
  56. if (function.is_script_function() && function.name().is_empty())
  57. static_cast<ScriptFunction&>(function).set_name(name);
  58. } else if (object.is_array()) {
  59. auto& array = static_cast<Array&>(object);
  60. for (auto& entry : array.indexed_properties().values_unordered())
  61. update_function_name(entry.value, name);
  62. }
  63. }
  64. Value ScopeNode::execute(Interpreter& interpreter, GlobalObject& global_object) const
  65. {
  66. return interpreter.run(global_object, *this);
  67. }
  68. Value FunctionDeclaration::execute(Interpreter&, GlobalObject&) const
  69. {
  70. return js_undefined();
  71. }
  72. Value FunctionExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  73. {
  74. return ScriptFunction::create(global_object, name(), body(), parameters(), function_length(), interpreter.current_environment(), m_is_arrow_function);
  75. }
  76. Value ExpressionStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  77. {
  78. return m_expression->execute(interpreter, global_object);
  79. }
  80. CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interpreter& interpreter, GlobalObject& global_object) const
  81. {
  82. if (is_new_expression()) {
  83. // Computing |this| is irrelevant for "new" expression.
  84. return { js_undefined(), m_callee->execute(interpreter, global_object) };
  85. }
  86. if (m_callee->is_member_expression()) {
  87. auto& member_expression = static_cast<const MemberExpression&>(*m_callee);
  88. auto object_value = member_expression.object().execute(interpreter, global_object);
  89. if (interpreter.exception())
  90. return {};
  91. auto* this_value = object_value.to_object(interpreter);
  92. if (interpreter.exception())
  93. return {};
  94. auto callee = this_value->get(member_expression.computed_property_name(interpreter, global_object)).value_or(js_undefined());
  95. return { this_value, callee };
  96. }
  97. return { &interpreter.global_object(), m_callee->execute(interpreter, global_object) };
  98. }
  99. Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  100. {
  101. auto [this_value, callee] = compute_this_and_callee(interpreter, global_object);
  102. if (interpreter.exception())
  103. return {};
  104. ASSERT(!callee.is_empty());
  105. if (!callee.is_function()
  106. || (is_new_expression() && (callee.as_object().is_native_function() && !static_cast<NativeFunction&>(callee.as_object()).has_constructor()))) {
  107. String error_message;
  108. auto call_type = is_new_expression() ? "constructor" : "function";
  109. if (m_callee->is_identifier() || m_callee->is_member_expression()) {
  110. String expression_string;
  111. if (m_callee->is_identifier()) {
  112. expression_string = static_cast<const Identifier&>(*m_callee).string();
  113. } else {
  114. expression_string = static_cast<const MemberExpression&>(*m_callee).to_string_approximation();
  115. }
  116. return interpreter.throw_exception<TypeError>(ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects().characters(), call_type, expression_string.characters());
  117. } else {
  118. return interpreter.throw_exception<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects().characters(), call_type);
  119. }
  120. }
  121. auto& function = callee.as_function();
  122. MarkedValueList arguments(interpreter.heap());
  123. arguments.values().append(function.bound_arguments());
  124. for (size_t i = 0; i < m_arguments.size(); ++i) {
  125. auto value = m_arguments[i].value->execute(interpreter, global_object);
  126. if (interpreter.exception())
  127. return {};
  128. if (m_arguments[i].is_spread) {
  129. // FIXME: Support generic iterables
  130. if (value.is_string()) {
  131. for (auto ch : value.as_string().string())
  132. arguments.append(Value(js_string(interpreter, String::format("%c", ch))));
  133. } else if (value.is_object() && value.as_object().is_array()) {
  134. auto& array = static_cast<Array&>(value.as_object());
  135. for (auto& entry : array.indexed_properties()) {
  136. arguments.append(entry.value_and_attributes(&array).value);
  137. if (interpreter.exception())
  138. return {};
  139. }
  140. } else if (value.is_object() && value.as_object().is_string_object()) {
  141. for (auto ch : static_cast<const StringObject&>(value.as_object()).primitive_string().string())
  142. arguments.append(Value(js_string(interpreter, String::format("%c", ch))));
  143. } else {
  144. interpreter.throw_exception<TypeError>(ErrorType::NotIterable, value.to_string_without_side_effects().characters());
  145. }
  146. } else {
  147. arguments.append(value);
  148. }
  149. }
  150. auto& call_frame = interpreter.push_call_frame();
  151. call_frame.function_name = function.name();
  152. call_frame.arguments = arguments.values();
  153. call_frame.environment = function.create_environment();
  154. Object* new_object = nullptr;
  155. Value result;
  156. if (is_new_expression()) {
  157. new_object = Object::create_empty(interpreter, interpreter.global_object());
  158. auto prototype = function.get("prototype");
  159. if (interpreter.exception())
  160. return {};
  161. if (prototype.is_object()) {
  162. new_object->set_prototype(&prototype.as_object());
  163. if (interpreter.exception())
  164. return {};
  165. }
  166. call_frame.this_value = new_object;
  167. result = function.construct(interpreter);
  168. } else {
  169. call_frame.this_value = function.bound_this().value_or(this_value);
  170. result = function.call(interpreter);
  171. }
  172. interpreter.pop_call_frame();
  173. if (interpreter.exception())
  174. return {};
  175. if (is_new_expression()) {
  176. if (result.is_object())
  177. return result;
  178. return new_object;
  179. }
  180. return result;
  181. }
  182. Value ReturnStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  183. {
  184. auto value = argument() ? argument()->execute(interpreter, global_object) : js_undefined();
  185. if (interpreter.exception())
  186. return {};
  187. interpreter.unwind(ScopeType::Function);
  188. return value;
  189. }
  190. Value IfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  191. {
  192. auto predicate_result = m_predicate->execute(interpreter, global_object);
  193. if (interpreter.exception())
  194. return {};
  195. if (predicate_result.to_boolean())
  196. return interpreter.run(global_object, *m_consequent);
  197. if (m_alternate)
  198. return interpreter.run(global_object, *m_alternate);
  199. return js_undefined();
  200. }
  201. Value WhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  202. {
  203. Value last_value = js_undefined();
  204. while (m_test->execute(interpreter, global_object).to_boolean()) {
  205. if (interpreter.exception())
  206. return {};
  207. last_value = interpreter.run(global_object, *m_body);
  208. if (interpreter.exception())
  209. return {};
  210. }
  211. return last_value;
  212. }
  213. Value DoWhileStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  214. {
  215. Value last_value = js_undefined();
  216. do {
  217. if (interpreter.exception())
  218. return {};
  219. last_value = interpreter.run(global_object, *m_body);
  220. if (interpreter.exception())
  221. return {};
  222. } while (m_test->execute(interpreter, global_object).to_boolean());
  223. return last_value;
  224. }
  225. Value ForStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  226. {
  227. RefPtr<BlockStatement> wrapper;
  228. if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_kind() != DeclarationKind::Var) {
  229. wrapper = create_ast_node<BlockStatement>();
  230. NonnullRefPtrVector<VariableDeclaration> decls;
  231. decls.append(*static_cast<const VariableDeclaration*>(m_init.ptr()));
  232. wrapper->add_variables(decls);
  233. interpreter.enter_scope(*wrapper, {}, ScopeType::Block, global_object);
  234. }
  235. auto wrapper_cleanup = ScopeGuard([&] {
  236. if (wrapper)
  237. interpreter.exit_scope(*wrapper);
  238. });
  239. Value last_value = js_undefined();
  240. if (m_init) {
  241. m_init->execute(interpreter, global_object);
  242. if (interpreter.exception())
  243. return {};
  244. }
  245. if (m_test) {
  246. while (true) {
  247. auto test_result = m_test->execute(interpreter, global_object);
  248. if (interpreter.exception())
  249. return {};
  250. if (!test_result.to_boolean())
  251. break;
  252. last_value = interpreter.run(global_object, *m_body);
  253. if (interpreter.exception())
  254. return {};
  255. if (interpreter.should_unwind()) {
  256. if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
  257. interpreter.stop_unwind();
  258. } else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  259. interpreter.stop_unwind();
  260. break;
  261. } else {
  262. return js_undefined();
  263. }
  264. }
  265. if (m_update) {
  266. m_update->execute(interpreter, global_object);
  267. if (interpreter.exception())
  268. return {};
  269. }
  270. }
  271. } else {
  272. while (true) {
  273. last_value = interpreter.run(global_object, *m_body);
  274. if (interpreter.exception())
  275. return {};
  276. if (interpreter.should_unwind()) {
  277. if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
  278. interpreter.stop_unwind();
  279. } else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  280. interpreter.stop_unwind();
  281. break;
  282. } else {
  283. return js_undefined();
  284. }
  285. }
  286. if (m_update) {
  287. m_update->execute(interpreter, global_object);
  288. if (interpreter.exception())
  289. return {};
  290. }
  291. }
  292. }
  293. return last_value;
  294. }
  295. static FlyString variable_from_for_declaration(Interpreter& interpreter, GlobalObject& global_object, NonnullRefPtr<ASTNode> node, RefPtr<BlockStatement> wrapper)
  296. {
  297. FlyString variable_name;
  298. if (node->is_variable_declaration()) {
  299. auto* variable_declaration = static_cast<const VariableDeclaration*>(node.ptr());
  300. ASSERT(!variable_declaration->declarations().is_empty());
  301. if (variable_declaration->declaration_kind() != DeclarationKind::Var) {
  302. wrapper = create_ast_node<BlockStatement>();
  303. interpreter.enter_scope(*wrapper, {}, ScopeType::Block, global_object);
  304. }
  305. variable_declaration->execute(interpreter, global_object);
  306. variable_name = variable_declaration->declarations().first().id().string();
  307. } else if (node->is_identifier()) {
  308. variable_name = static_cast<const Identifier&>(*node).string();
  309. } else {
  310. ASSERT_NOT_REACHED();
  311. }
  312. return variable_name;
  313. }
  314. Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  315. {
  316. if (!m_lhs->is_variable_declaration() && !m_lhs->is_identifier()) {
  317. // FIXME: Implement "for (foo.bar in baz)", "for (foo[0] in bar)"
  318. ASSERT_NOT_REACHED();
  319. }
  320. RefPtr<BlockStatement> wrapper;
  321. auto variable_name = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  322. auto wrapper_cleanup = ScopeGuard([&] {
  323. if (wrapper)
  324. interpreter.exit_scope(*wrapper);
  325. });
  326. auto last_value = js_undefined();
  327. auto rhs_result = m_rhs->execute(interpreter, global_object);
  328. if (interpreter.exception())
  329. return {};
  330. auto* object = rhs_result.to_object(interpreter);
  331. while (object) {
  332. auto property_names = object->get_own_properties(*object, Object::GetOwnPropertyMode::Key, true);
  333. for (auto& property_name : property_names.as_object().indexed_properties()) {
  334. interpreter.set_variable(variable_name, property_name.value_and_attributes(object).value, global_object);
  335. if (interpreter.exception())
  336. return {};
  337. last_value = interpreter.run(global_object, *m_body);
  338. if (interpreter.exception())
  339. return {};
  340. if (interpreter.should_unwind()) {
  341. if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
  342. interpreter.stop_unwind();
  343. } else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  344. interpreter.stop_unwind();
  345. break;
  346. } else {
  347. return js_undefined();
  348. }
  349. }
  350. }
  351. object = object->prototype();
  352. if (interpreter.exception())
  353. return {};
  354. }
  355. return last_value;
  356. }
  357. Value ForOfStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  358. {
  359. if (!m_lhs->is_variable_declaration() && !m_lhs->is_identifier()) {
  360. // FIXME: Implement "for (foo.bar of baz)", "for (foo[0] of bar)"
  361. ASSERT_NOT_REACHED();
  362. }
  363. RefPtr<BlockStatement> wrapper;
  364. auto variable_name = variable_from_for_declaration(interpreter, global_object, m_lhs, wrapper);
  365. auto wrapper_cleanup = ScopeGuard([&] {
  366. if (wrapper)
  367. interpreter.exit_scope(*wrapper);
  368. });
  369. auto last_value = js_undefined();
  370. auto rhs_result = m_rhs->execute(interpreter, global_object);
  371. if (interpreter.exception())
  372. return {};
  373. // FIXME: We need to properly implement the iterator protocol
  374. auto is_iterable = rhs_result.is_array() || rhs_result.is_string() || (rhs_result.is_object() && rhs_result.as_object().is_string_object());
  375. if (!is_iterable)
  376. return interpreter.throw_exception<TypeError>(ErrorType::ForOfNotIterable);
  377. size_t index = 0;
  378. auto next = [&]() -> Optional<Value> {
  379. if (rhs_result.is_array()) {
  380. auto& array_elements = rhs_result.as_object().indexed_properties();
  381. if (index < array_elements.array_like_size()) {
  382. auto result = array_elements.get(&rhs_result.as_object(), index);
  383. if (interpreter.exception())
  384. return {};
  385. return result.value().value;
  386. }
  387. } else if (rhs_result.is_string()) {
  388. auto string = rhs_result.as_string().string();
  389. if (index < string.length())
  390. return js_string(interpreter, string.substring(index, 1));
  391. } else if (rhs_result.is_object() && rhs_result.as_object().is_string_object()) {
  392. auto string = static_cast<StringObject*>(&rhs_result.as_object())->primitive_string().string();
  393. if (index < string.length())
  394. return js_string(interpreter, string.substring(index, 1));
  395. }
  396. return {};
  397. };
  398. for (;;) {
  399. auto next_item = next();
  400. if (!next_item.has_value())
  401. break;
  402. interpreter.set_variable(variable_name, next_item.value(), global_object);
  403. last_value = interpreter.run(global_object, *m_body);
  404. if (interpreter.exception())
  405. return {};
  406. if (interpreter.should_unwind()) {
  407. if (interpreter.should_unwind_until(ScopeType::Continuable, m_label)) {
  408. interpreter.stop_unwind();
  409. } else if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  410. interpreter.stop_unwind();
  411. break;
  412. } else {
  413. return js_undefined();
  414. }
  415. }
  416. ++index;
  417. }
  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(interpreter, lhs_result, rhs_result);
  431. case BinaryOp::Subtraction:
  432. return sub(interpreter, lhs_result, rhs_result);
  433. case BinaryOp::Multiplication:
  434. return mul(interpreter, lhs_result, rhs_result);
  435. case BinaryOp::Division:
  436. return div(interpreter, lhs_result, rhs_result);
  437. case BinaryOp::Modulo:
  438. return mod(interpreter, lhs_result, rhs_result);
  439. case BinaryOp::Exponentiation:
  440. return exp(interpreter, lhs_result, rhs_result);
  441. case BinaryOp::TypedEquals:
  442. return Value(strict_eq(interpreter, lhs_result, rhs_result));
  443. case BinaryOp::TypedInequals:
  444. return Value(!strict_eq(interpreter, lhs_result, rhs_result));
  445. case BinaryOp::AbstractEquals:
  446. return Value(abstract_eq(interpreter, lhs_result, rhs_result));
  447. case BinaryOp::AbstractInequals:
  448. return Value(!abstract_eq(interpreter, lhs_result, rhs_result));
  449. case BinaryOp::GreaterThan:
  450. return greater_than(interpreter, lhs_result, rhs_result);
  451. case BinaryOp::GreaterThanEquals:
  452. return greater_than_equals(interpreter, lhs_result, rhs_result);
  453. case BinaryOp::LessThan:
  454. return less_than(interpreter, lhs_result, rhs_result);
  455. case BinaryOp::LessThanEquals:
  456. return less_than_equals(interpreter, lhs_result, rhs_result);
  457. case BinaryOp::BitwiseAnd:
  458. return bitwise_and(interpreter, lhs_result, rhs_result);
  459. case BinaryOp::BitwiseOr:
  460. return bitwise_or(interpreter, lhs_result, rhs_result);
  461. case BinaryOp::BitwiseXor:
  462. return bitwise_xor(interpreter, lhs_result, rhs_result);
  463. case BinaryOp::LeftShift:
  464. return left_shift(interpreter, lhs_result, rhs_result);
  465. case BinaryOp::RightShift:
  466. return right_shift(interpreter, lhs_result, rhs_result);
  467. case BinaryOp::UnsignedRightShift:
  468. return unsigned_right_shift(interpreter, lhs_result, rhs_result);
  469. case BinaryOp::In:
  470. return in(interpreter, lhs_result, rhs_result);
  471. case BinaryOp::InstanceOf:
  472. return instance_of(interpreter, 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_null() || lhs_result.is_undefined()) {
  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.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. if (m_op == UnaryOp::Delete) {
  530. auto reference = m_lhs->to_reference(interpreter, global_object);
  531. if (interpreter.exception())
  532. return {};
  533. if (reference.is_unresolvable())
  534. return Value(true);
  535. // FIXME: Support deleting locals
  536. ASSERT(!reference.is_local_variable());
  537. if (reference.is_global_variable())
  538. return interpreter.global_object().delete_property(reference.name());
  539. auto* base_object = reference.base().to_object(interpreter);
  540. if (!base_object)
  541. return {};
  542. return base_object->delete_property(reference.name());
  543. }
  544. Value lhs_result;
  545. if (m_op == UnaryOp::Typeof && m_lhs->is_identifier()) {
  546. auto reference = m_lhs->to_reference(interpreter, global_object);
  547. if (interpreter.exception()) {
  548. return {};
  549. }
  550. // FIXME: standard recommends checking with is_unresolvable but it ALWAYS return false here
  551. if (reference.is_local_variable() || reference.is_global_variable()) {
  552. auto name = reference.name();
  553. lhs_result = interpreter.get_variable(name.to_string(), global_object).value_or(js_undefined());
  554. if (interpreter.exception())
  555. return {};
  556. }
  557. } else {
  558. lhs_result = m_lhs->execute(interpreter, global_object);
  559. if (interpreter.exception())
  560. return {};
  561. }
  562. switch (m_op) {
  563. case UnaryOp::BitwiseNot:
  564. return bitwise_not(interpreter, lhs_result);
  565. case UnaryOp::Not:
  566. return Value(!lhs_result.to_boolean());
  567. case UnaryOp::Plus:
  568. return unary_plus(interpreter, lhs_result);
  569. case UnaryOp::Minus:
  570. return unary_minus(interpreter, lhs_result);
  571. case UnaryOp::Typeof:
  572. switch (lhs_result.type()) {
  573. case Value::Type::Empty:
  574. ASSERT_NOT_REACHED();
  575. return {};
  576. case Value::Type::Undefined:
  577. return js_string(interpreter, "undefined");
  578. case Value::Type::Null:
  579. // yes, this is on purpose. yes, this is how javascript works.
  580. // yes, it's silly.
  581. return js_string(interpreter, "object");
  582. case Value::Type::Number:
  583. return js_string(interpreter, "number");
  584. case Value::Type::String:
  585. return js_string(interpreter, "string");
  586. case Value::Type::Object:
  587. if (lhs_result.is_function())
  588. return js_string(interpreter, "function");
  589. return js_string(interpreter, "object");
  590. case Value::Type::Boolean:
  591. return js_string(interpreter, "boolean");
  592. case Value::Type::Symbol:
  593. return js_string(interpreter, "symbol");
  594. case Value::Type::BigInt:
  595. return js_string(interpreter, "bigint");
  596. default:
  597. ASSERT_NOT_REACHED();
  598. }
  599. case UnaryOp::Void:
  600. return js_undefined();
  601. case UnaryOp::Delete:
  602. ASSERT_NOT_REACHED();
  603. }
  604. ASSERT_NOT_REACHED();
  605. }
  606. static void print_indent(int indent)
  607. {
  608. for (int i = 0; i < indent * 2; ++i)
  609. putchar(' ');
  610. }
  611. void ASTNode::dump(int indent) const
  612. {
  613. print_indent(indent);
  614. printf("%s\n", class_name());
  615. }
  616. void ScopeNode::dump(int indent) const
  617. {
  618. ASTNode::dump(indent);
  619. if (!m_variables.is_empty()) {
  620. print_indent(indent + 1);
  621. printf("(Variables)\n");
  622. for (auto& variable : m_variables)
  623. variable.dump(indent + 2);
  624. }
  625. if (!m_children.is_empty()) {
  626. print_indent(indent + 1);
  627. printf("(Children)\n");
  628. for (auto& child : children())
  629. child.dump(indent + 2);
  630. }
  631. }
  632. void BinaryExpression::dump(int indent) const
  633. {
  634. const char* op_string = nullptr;
  635. switch (m_op) {
  636. case BinaryOp::Addition:
  637. op_string = "+";
  638. break;
  639. case BinaryOp::Subtraction:
  640. op_string = "-";
  641. break;
  642. case BinaryOp::Multiplication:
  643. op_string = "*";
  644. break;
  645. case BinaryOp::Division:
  646. op_string = "/";
  647. break;
  648. case BinaryOp::Modulo:
  649. op_string = "%";
  650. break;
  651. case BinaryOp::Exponentiation:
  652. op_string = "**";
  653. break;
  654. case BinaryOp::TypedEquals:
  655. op_string = "===";
  656. break;
  657. case BinaryOp::TypedInequals:
  658. op_string = "!==";
  659. break;
  660. case BinaryOp::AbstractEquals:
  661. op_string = "==";
  662. break;
  663. case BinaryOp::AbstractInequals:
  664. op_string = "!=";
  665. break;
  666. case BinaryOp::GreaterThan:
  667. op_string = ">";
  668. break;
  669. case BinaryOp::GreaterThanEquals:
  670. op_string = ">=";
  671. break;
  672. case BinaryOp::LessThan:
  673. op_string = "<";
  674. break;
  675. case BinaryOp::LessThanEquals:
  676. op_string = "<=";
  677. break;
  678. case BinaryOp::BitwiseAnd:
  679. op_string = "&";
  680. break;
  681. case BinaryOp::BitwiseOr:
  682. op_string = "|";
  683. break;
  684. case BinaryOp::BitwiseXor:
  685. op_string = "^";
  686. break;
  687. case BinaryOp::LeftShift:
  688. op_string = "<<";
  689. break;
  690. case BinaryOp::RightShift:
  691. op_string = ">>";
  692. break;
  693. case BinaryOp::UnsignedRightShift:
  694. op_string = ">>>";
  695. break;
  696. case BinaryOp::In:
  697. op_string = "in";
  698. break;
  699. case BinaryOp::InstanceOf:
  700. op_string = "instanceof";
  701. break;
  702. }
  703. print_indent(indent);
  704. printf("%s\n", class_name());
  705. m_lhs->dump(indent + 1);
  706. print_indent(indent + 1);
  707. printf("%s\n", op_string);
  708. m_rhs->dump(indent + 1);
  709. }
  710. void LogicalExpression::dump(int indent) const
  711. {
  712. const char* op_string = nullptr;
  713. switch (m_op) {
  714. case LogicalOp::And:
  715. op_string = "&&";
  716. break;
  717. case LogicalOp::Or:
  718. op_string = "||";
  719. break;
  720. case LogicalOp::NullishCoalescing:
  721. op_string = "??";
  722. break;
  723. }
  724. print_indent(indent);
  725. printf("%s\n", class_name());
  726. m_lhs->dump(indent + 1);
  727. print_indent(indent + 1);
  728. printf("%s\n", op_string);
  729. m_rhs->dump(indent + 1);
  730. }
  731. void UnaryExpression::dump(int indent) const
  732. {
  733. const char* op_string = nullptr;
  734. switch (m_op) {
  735. case UnaryOp::BitwiseNot:
  736. op_string = "~";
  737. break;
  738. case UnaryOp::Not:
  739. op_string = "!";
  740. break;
  741. case UnaryOp::Plus:
  742. op_string = "+";
  743. break;
  744. case UnaryOp::Minus:
  745. op_string = "-";
  746. break;
  747. case UnaryOp::Typeof:
  748. op_string = "typeof ";
  749. break;
  750. case UnaryOp::Void:
  751. op_string = "void ";
  752. break;
  753. case UnaryOp::Delete:
  754. op_string = "delete ";
  755. break;
  756. }
  757. print_indent(indent);
  758. printf("%s\n", class_name());
  759. print_indent(indent + 1);
  760. printf("%s\n", op_string);
  761. m_lhs->dump(indent + 1);
  762. }
  763. void CallExpression::dump(int indent) const
  764. {
  765. print_indent(indent);
  766. printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
  767. m_callee->dump(indent + 1);
  768. for (auto& argument : m_arguments)
  769. argument.value->dump(indent + 1);
  770. }
  771. void StringLiteral::dump(int indent) const
  772. {
  773. print_indent(indent);
  774. printf("StringLiteral \"%s\"\n", m_value.characters());
  775. }
  776. void NumericLiteral::dump(int indent) const
  777. {
  778. print_indent(indent);
  779. printf("NumericLiteral %g\n", m_value);
  780. }
  781. void BigIntLiteral::dump(int indent) const
  782. {
  783. print_indent(indent);
  784. printf("BigIntLiteral %s\n", m_value.characters());
  785. }
  786. void BooleanLiteral::dump(int indent) const
  787. {
  788. print_indent(indent);
  789. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  790. }
  791. void NullLiteral::dump(int indent) const
  792. {
  793. print_indent(indent);
  794. printf("null\n");
  795. }
  796. void FunctionNode::dump(int indent, const char* class_name) const
  797. {
  798. print_indent(indent);
  799. printf("%s '%s'\n", class_name, name().characters());
  800. if (!m_parameters.is_empty()) {
  801. print_indent(indent + 1);
  802. printf("(Parameters)\n");
  803. for (auto& parameter : m_parameters) {
  804. print_indent(indent + 2);
  805. if (parameter.is_rest)
  806. printf("...");
  807. printf("%s\n", parameter.name.characters());
  808. if (parameter.default_value)
  809. parameter.default_value->dump(indent + 3);
  810. }
  811. }
  812. if (!m_variables.is_empty()) {
  813. print_indent(indent + 1);
  814. printf("(Variables)\n");
  815. for (auto& variable : m_variables)
  816. variable.dump(indent + 2);
  817. }
  818. print_indent(indent + 1);
  819. printf("(Body)\n");
  820. body().dump(indent + 2);
  821. }
  822. void FunctionDeclaration::dump(int indent) const
  823. {
  824. FunctionNode::dump(indent, class_name());
  825. }
  826. void FunctionExpression::dump(int indent) const
  827. {
  828. FunctionNode::dump(indent, class_name());
  829. }
  830. void ReturnStatement::dump(int indent) const
  831. {
  832. ASTNode::dump(indent);
  833. if (argument())
  834. argument()->dump(indent + 1);
  835. }
  836. void IfStatement::dump(int indent) const
  837. {
  838. ASTNode::dump(indent);
  839. print_indent(indent);
  840. printf("If\n");
  841. predicate().dump(indent + 1);
  842. consequent().dump(indent + 1);
  843. if (alternate()) {
  844. print_indent(indent);
  845. printf("Else\n");
  846. alternate()->dump(indent + 1);
  847. }
  848. }
  849. void WhileStatement::dump(int indent) const
  850. {
  851. ASTNode::dump(indent);
  852. print_indent(indent);
  853. printf("While\n");
  854. test().dump(indent + 1);
  855. body().dump(indent + 1);
  856. }
  857. void DoWhileStatement::dump(int indent) const
  858. {
  859. ASTNode::dump(indent);
  860. print_indent(indent);
  861. printf("DoWhile\n");
  862. test().dump(indent + 1);
  863. body().dump(indent + 1);
  864. }
  865. void ForStatement::dump(int indent) const
  866. {
  867. ASTNode::dump(indent);
  868. print_indent(indent);
  869. printf("For\n");
  870. if (init())
  871. init()->dump(indent + 1);
  872. if (test())
  873. test()->dump(indent + 1);
  874. if (update())
  875. update()->dump(indent + 1);
  876. body().dump(indent + 1);
  877. }
  878. void ForInStatement::dump(int indent) const
  879. {
  880. ASTNode::dump(indent);
  881. print_indent(indent);
  882. printf("ForIn\n");
  883. lhs().dump(indent + 1);
  884. rhs().dump(indent + 1);
  885. body().dump(indent + 1);
  886. }
  887. void ForOfStatement::dump(int indent) const
  888. {
  889. ASTNode::dump(indent);
  890. print_indent(indent);
  891. printf("ForOf\n");
  892. lhs().dump(indent + 1);
  893. rhs().dump(indent + 1);
  894. body().dump(indent + 1);
  895. }
  896. Value Identifier::execute(Interpreter& interpreter, GlobalObject& global_object) const
  897. {
  898. auto value = interpreter.get_variable(string(), global_object);
  899. if (value.is_empty())
  900. return interpreter.throw_exception<ReferenceError>(ErrorType::UnknownIdentifier, string().characters());
  901. return value;
  902. }
  903. void Identifier::dump(int indent) const
  904. {
  905. print_indent(indent);
  906. printf("Identifier \"%s\"\n", m_string.characters());
  907. }
  908. void SpreadExpression::dump(int indent) const
  909. {
  910. ASTNode::dump(indent);
  911. m_target->dump(indent + 1);
  912. }
  913. Value SpreadExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  914. {
  915. return m_target->execute(interpreter, global_object);
  916. }
  917. Value ThisExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  918. {
  919. return interpreter.this_value(global_object);
  920. }
  921. void ThisExpression::dump(int indent) const
  922. {
  923. ASTNode::dump(indent);
  924. }
  925. Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  926. {
  927. auto rhs_result = m_rhs->execute(interpreter, global_object);
  928. if (interpreter.exception())
  929. return {};
  930. Value lhs_result;
  931. switch (m_op) {
  932. case AssignmentOp::Assignment:
  933. break;
  934. case AssignmentOp::AdditionAssignment:
  935. lhs_result = m_lhs->execute(interpreter, global_object);
  936. if (interpreter.exception())
  937. return {};
  938. rhs_result = add(interpreter, lhs_result, rhs_result);
  939. break;
  940. case AssignmentOp::SubtractionAssignment:
  941. lhs_result = m_lhs->execute(interpreter, global_object);
  942. if (interpreter.exception())
  943. return {};
  944. rhs_result = sub(interpreter, lhs_result, rhs_result);
  945. break;
  946. case AssignmentOp::MultiplicationAssignment:
  947. lhs_result = m_lhs->execute(interpreter, global_object);
  948. if (interpreter.exception())
  949. return {};
  950. rhs_result = mul(interpreter, lhs_result, rhs_result);
  951. break;
  952. case AssignmentOp::DivisionAssignment:
  953. lhs_result = m_lhs->execute(interpreter, global_object);
  954. if (interpreter.exception())
  955. return {};
  956. rhs_result = div(interpreter, lhs_result, rhs_result);
  957. break;
  958. case AssignmentOp::ModuloAssignment:
  959. lhs_result = m_lhs->execute(interpreter, global_object);
  960. if (interpreter.exception())
  961. return {};
  962. rhs_result = mod(interpreter, lhs_result, rhs_result);
  963. break;
  964. case AssignmentOp::ExponentiationAssignment:
  965. lhs_result = m_lhs->execute(interpreter, global_object);
  966. if (interpreter.exception())
  967. return {};
  968. rhs_result = exp(interpreter, lhs_result, rhs_result);
  969. break;
  970. case AssignmentOp::BitwiseAndAssignment:
  971. lhs_result = m_lhs->execute(interpreter, global_object);
  972. if (interpreter.exception())
  973. return {};
  974. rhs_result = bitwise_and(interpreter, lhs_result, rhs_result);
  975. break;
  976. case AssignmentOp::BitwiseOrAssignment:
  977. lhs_result = m_lhs->execute(interpreter, global_object);
  978. if (interpreter.exception())
  979. return {};
  980. rhs_result = bitwise_or(interpreter, lhs_result, rhs_result);
  981. break;
  982. case AssignmentOp::BitwiseXorAssignment:
  983. lhs_result = m_lhs->execute(interpreter, global_object);
  984. if (interpreter.exception())
  985. return {};
  986. rhs_result = bitwise_xor(interpreter, lhs_result, rhs_result);
  987. break;
  988. case AssignmentOp::LeftShiftAssignment:
  989. lhs_result = m_lhs->execute(interpreter, global_object);
  990. if (interpreter.exception())
  991. return {};
  992. rhs_result = left_shift(interpreter, lhs_result, rhs_result);
  993. break;
  994. case AssignmentOp::RightShiftAssignment:
  995. lhs_result = m_lhs->execute(interpreter, global_object);
  996. if (interpreter.exception())
  997. return {};
  998. rhs_result = right_shift(interpreter, lhs_result, rhs_result);
  999. break;
  1000. case AssignmentOp::UnsignedRightShiftAssignment:
  1001. lhs_result = m_lhs->execute(interpreter, global_object);
  1002. if (interpreter.exception())
  1003. return {};
  1004. rhs_result = unsigned_right_shift(interpreter, lhs_result, rhs_result);
  1005. break;
  1006. }
  1007. if (interpreter.exception())
  1008. return {};
  1009. auto reference = m_lhs->to_reference(interpreter, global_object);
  1010. if (interpreter.exception())
  1011. return {};
  1012. if (reference.is_unresolvable())
  1013. return interpreter.throw_exception<ReferenceError>(ErrorType::InvalidLeftHandAssignment);
  1014. update_function_name(rhs_result, reference.name().as_string());
  1015. reference.put(interpreter, rhs_result);
  1016. if (interpreter.exception())
  1017. return {};
  1018. return rhs_result;
  1019. }
  1020. Value UpdateExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1021. {
  1022. auto reference = m_argument->to_reference(interpreter, global_object);
  1023. if (interpreter.exception())
  1024. return {};
  1025. auto old_value = reference.get(interpreter);
  1026. if (interpreter.exception())
  1027. return {};
  1028. old_value = old_value.to_numeric(interpreter);
  1029. if (interpreter.exception())
  1030. return {};
  1031. Value new_value;
  1032. switch (m_op) {
  1033. case UpdateOp::Increment:
  1034. if (old_value.is_number())
  1035. new_value = Value(old_value.as_double() + 1);
  1036. else
  1037. new_value = js_bigint(interpreter, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
  1038. break;
  1039. case UpdateOp::Decrement:
  1040. if (old_value.is_number())
  1041. new_value = Value(old_value.as_double() - 1);
  1042. else
  1043. new_value = js_bigint(interpreter, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
  1044. break;
  1045. default:
  1046. ASSERT_NOT_REACHED();
  1047. }
  1048. reference.put(interpreter, new_value);
  1049. if (interpreter.exception())
  1050. return {};
  1051. return m_prefixed ? new_value : old_value;
  1052. }
  1053. void AssignmentExpression::dump(int indent) const
  1054. {
  1055. const char* op_string = nullptr;
  1056. switch (m_op) {
  1057. case AssignmentOp::Assignment:
  1058. op_string = "=";
  1059. break;
  1060. case AssignmentOp::AdditionAssignment:
  1061. op_string = "+=";
  1062. break;
  1063. case AssignmentOp::SubtractionAssignment:
  1064. op_string = "-=";
  1065. break;
  1066. case AssignmentOp::MultiplicationAssignment:
  1067. op_string = "*=";
  1068. break;
  1069. case AssignmentOp::DivisionAssignment:
  1070. op_string = "/=";
  1071. break;
  1072. case AssignmentOp::ModuloAssignment:
  1073. op_string = "%=";
  1074. break;
  1075. case AssignmentOp::ExponentiationAssignment:
  1076. op_string = "**=";
  1077. break;
  1078. case AssignmentOp::BitwiseAndAssignment:
  1079. op_string = "&=";
  1080. break;
  1081. case AssignmentOp::BitwiseOrAssignment:
  1082. op_string = "|=";
  1083. break;
  1084. case AssignmentOp::BitwiseXorAssignment:
  1085. op_string = "^=";
  1086. break;
  1087. case AssignmentOp::LeftShiftAssignment:
  1088. op_string = "<<=";
  1089. break;
  1090. case AssignmentOp::RightShiftAssignment:
  1091. op_string = ">>=";
  1092. break;
  1093. case AssignmentOp::UnsignedRightShiftAssignment:
  1094. op_string = ">>>=";
  1095. break;
  1096. }
  1097. ASTNode::dump(indent);
  1098. print_indent(indent + 1);
  1099. printf("%s\n", op_string);
  1100. m_lhs->dump(indent + 1);
  1101. m_rhs->dump(indent + 1);
  1102. }
  1103. void UpdateExpression::dump(int indent) const
  1104. {
  1105. const char* op_string = nullptr;
  1106. switch (m_op) {
  1107. case UpdateOp::Increment:
  1108. op_string = "++";
  1109. break;
  1110. case UpdateOp::Decrement:
  1111. op_string = "--";
  1112. break;
  1113. }
  1114. ASTNode::dump(indent);
  1115. print_indent(indent + 1);
  1116. if (m_prefixed)
  1117. printf("%s\n", op_string);
  1118. m_argument->dump(indent + 1);
  1119. if (!m_prefixed) {
  1120. print_indent(indent + 1);
  1121. printf("%s\n", op_string);
  1122. }
  1123. }
  1124. Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1125. {
  1126. for (auto& declarator : m_declarations) {
  1127. if (auto* init = declarator.init()) {
  1128. auto initalizer_result = init->execute(interpreter, global_object);
  1129. if (interpreter.exception())
  1130. return {};
  1131. auto variable_name = declarator.id().string();
  1132. update_function_name(initalizer_result, variable_name);
  1133. interpreter.set_variable(variable_name, initalizer_result, global_object, true);
  1134. }
  1135. }
  1136. return js_undefined();
  1137. }
  1138. Value VariableDeclarator::execute(Interpreter&, GlobalObject&) const
  1139. {
  1140. // NOTE: This node is handled by VariableDeclaration.
  1141. ASSERT_NOT_REACHED();
  1142. }
  1143. void VariableDeclaration::dump(int indent) const
  1144. {
  1145. const char* declaration_kind_string = nullptr;
  1146. switch (m_declaration_kind) {
  1147. case DeclarationKind::Let:
  1148. declaration_kind_string = "Let";
  1149. break;
  1150. case DeclarationKind::Var:
  1151. declaration_kind_string = "Var";
  1152. break;
  1153. case DeclarationKind::Const:
  1154. declaration_kind_string = "Const";
  1155. break;
  1156. }
  1157. ASTNode::dump(indent);
  1158. print_indent(indent + 1);
  1159. printf("%s\n", declaration_kind_string);
  1160. for (auto& declarator : m_declarations)
  1161. declarator.dump(indent + 1);
  1162. }
  1163. void VariableDeclarator::dump(int indent) const
  1164. {
  1165. ASTNode::dump(indent);
  1166. m_id->dump(indent + 1);
  1167. if (m_init)
  1168. m_init->dump(indent + 1);
  1169. }
  1170. void ObjectProperty::dump(int indent) const
  1171. {
  1172. ASTNode::dump(indent);
  1173. m_key->dump(indent + 1);
  1174. m_value->dump(indent + 1);
  1175. }
  1176. void ObjectExpression::dump(int indent) const
  1177. {
  1178. ASTNode::dump(indent);
  1179. for (auto& property : m_properties) {
  1180. property.dump(indent + 1);
  1181. }
  1182. }
  1183. void ExpressionStatement::dump(int indent) const
  1184. {
  1185. ASTNode::dump(indent);
  1186. m_expression->dump(indent + 1);
  1187. }
  1188. Value ObjectProperty::execute(Interpreter&, GlobalObject&) const
  1189. {
  1190. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  1191. ASSERT_NOT_REACHED();
  1192. }
  1193. Value ObjectExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1194. {
  1195. auto* object = Object::create_empty(interpreter, global_object);
  1196. for (auto& property : m_properties) {
  1197. auto key_result = property.key().execute(interpreter, global_object);
  1198. if (interpreter.exception())
  1199. return {};
  1200. if (property.type() == ObjectProperty::Type::Spread) {
  1201. if (key_result.is_array()) {
  1202. auto& array_to_spread = static_cast<Array&>(key_result.as_object());
  1203. for (auto& entry : array_to_spread.indexed_properties()) {
  1204. object->indexed_properties().append(entry.value_and_attributes(&array_to_spread).value);
  1205. if (interpreter.exception())
  1206. return {};
  1207. }
  1208. } else if (key_result.is_object()) {
  1209. auto& obj_to_spread = key_result.as_object();
  1210. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  1211. if (it.value.attributes.is_enumerable()) {
  1212. object->define_property(it.key, obj_to_spread.get(it.key));
  1213. if (interpreter.exception())
  1214. return {};
  1215. }
  1216. }
  1217. } else if (key_result.is_string()) {
  1218. auto& str_to_spread = key_result.as_string().string();
  1219. for (size_t i = 0; i < str_to_spread.length(); i++) {
  1220. object->define_property(i, js_string(interpreter, str_to_spread.substring(i, 1)));
  1221. if (interpreter.exception())
  1222. return {};
  1223. }
  1224. }
  1225. continue;
  1226. }
  1227. auto key = key_result.to_string(interpreter);
  1228. if (interpreter.exception())
  1229. return {};
  1230. auto value = property.value().execute(interpreter, global_object);
  1231. if (interpreter.exception())
  1232. return {};
  1233. String name = key;
  1234. if (property.type() == ObjectProperty::Type::Getter) {
  1235. name = String::format("get %s", key.characters());
  1236. } else if (property.type() == ObjectProperty::Type::Setter) {
  1237. name = String::format("set %s", key.characters());
  1238. }
  1239. update_function_name(value, name);
  1240. if (property.type() == ObjectProperty::Type::Getter || property.type() == ObjectProperty::Type::Setter) {
  1241. ASSERT(value.is_function());
  1242. Accessor* accessor { nullptr };
  1243. auto property_metadata = object->shape().lookup(key);
  1244. if (property_metadata.has_value()) {
  1245. auto existing_property = object->get_direct(property_metadata.value().offset);
  1246. if (existing_property.is_accessor())
  1247. accessor = &existing_property.as_accessor();
  1248. }
  1249. if (!accessor) {
  1250. accessor = Accessor::create(interpreter, nullptr, nullptr);
  1251. object->define_property(key, accessor, Attribute::Configurable | Attribute::Enumerable);
  1252. if (interpreter.exception())
  1253. return {};
  1254. }
  1255. if (property.type() == ObjectProperty::Type::Getter)
  1256. accessor->set_getter(&value.as_function());
  1257. else
  1258. accessor->set_setter(&value.as_function());
  1259. } else {
  1260. object->define_property(key, value);
  1261. if (interpreter.exception())
  1262. return {};
  1263. }
  1264. }
  1265. return object;
  1266. }
  1267. void MemberExpression::dump(int indent) const
  1268. {
  1269. print_indent(indent);
  1270. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  1271. m_object->dump(indent + 1);
  1272. m_property->dump(indent + 1);
  1273. }
  1274. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter, GlobalObject& global_object) const
  1275. {
  1276. if (!is_computed()) {
  1277. ASSERT(m_property->is_identifier());
  1278. return static_cast<const Identifier&>(*m_property).string();
  1279. }
  1280. auto index = m_property->execute(interpreter, global_object);
  1281. if (interpreter.exception())
  1282. return {};
  1283. ASSERT(!index.is_empty());
  1284. if (index.is_integer() && index.as_i32() >= 0)
  1285. return index.as_i32();
  1286. auto index_string = index.to_string(interpreter);
  1287. if (interpreter.exception())
  1288. return {};
  1289. return index_string;
  1290. }
  1291. String MemberExpression::to_string_approximation() const
  1292. {
  1293. String object_string = "<object>";
  1294. if (m_object->is_identifier())
  1295. object_string = static_cast<const Identifier&>(*m_object).string();
  1296. if (is_computed())
  1297. return String::format("%s[<computed>]", object_string.characters());
  1298. ASSERT(m_property->is_identifier());
  1299. return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
  1300. }
  1301. Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1302. {
  1303. auto object_value = m_object->execute(interpreter, global_object);
  1304. if (interpreter.exception())
  1305. return {};
  1306. auto* object_result = object_value.to_object(interpreter);
  1307. if (interpreter.exception())
  1308. return {};
  1309. return object_result->get(computed_property_name(interpreter, global_object)).value_or(js_undefined());
  1310. }
  1311. Value StringLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1312. {
  1313. return js_string(interpreter, m_value);
  1314. }
  1315. Value NumericLiteral::execute(Interpreter&, GlobalObject&) const
  1316. {
  1317. return Value(m_value);
  1318. }
  1319. Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1320. {
  1321. return js_bigint(interpreter, Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
  1322. }
  1323. Value BooleanLiteral::execute(Interpreter&, GlobalObject&) const
  1324. {
  1325. return Value(m_value);
  1326. }
  1327. Value NullLiteral::execute(Interpreter&, GlobalObject&) const
  1328. {
  1329. return js_null();
  1330. }
  1331. void RegExpLiteral::dump(int indent) const
  1332. {
  1333. print_indent(indent);
  1334. printf("%s (/%s/%s)\n", class_name(), content().characters(), flags().characters());
  1335. }
  1336. Value RegExpLiteral::execute(Interpreter&, GlobalObject& global_object) const
  1337. {
  1338. return RegExpObject::create(global_object, content(), flags());
  1339. }
  1340. void ArrayExpression::dump(int indent) const
  1341. {
  1342. ASTNode::dump(indent);
  1343. for (auto& element : m_elements) {
  1344. if (element) {
  1345. element->dump(indent + 1);
  1346. } else {
  1347. print_indent(indent + 1);
  1348. printf("<empty>\n");
  1349. }
  1350. }
  1351. }
  1352. Value ArrayExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1353. {
  1354. auto* array = Array::create(interpreter.global_object());
  1355. for (auto& element : m_elements) {
  1356. auto value = Value();
  1357. if (element) {
  1358. value = element->execute(interpreter, global_object);
  1359. if (interpreter.exception())
  1360. return {};
  1361. if (element->is_spread_expression()) {
  1362. // FIXME: Support arbitrary iterables
  1363. if (value.is_array()) {
  1364. auto& array_to_spread = static_cast<Array&>(value.as_object());
  1365. for (auto& entry : array_to_spread.indexed_properties()) {
  1366. array->indexed_properties().append(entry.value_and_attributes(&array_to_spread).value);
  1367. if (interpreter.exception())
  1368. return {};
  1369. }
  1370. continue;
  1371. }
  1372. if (value.is_string() || (value.is_object() && value.as_object().is_string_object())) {
  1373. String string_to_spread;
  1374. if (value.is_string()) {
  1375. string_to_spread = value.as_string().string();
  1376. } else {
  1377. string_to_spread = static_cast<const StringObject&>(value.as_object()).primitive_string().string();
  1378. }
  1379. for (size_t i = 0; i < string_to_spread.length(); ++i)
  1380. array->indexed_properties().append(js_string(interpreter, string_to_spread.substring(i, 1)));
  1381. continue;
  1382. }
  1383. interpreter.throw_exception<TypeError>(ErrorType::NotIterable, value.to_string_without_side_effects().characters());
  1384. return {};
  1385. }
  1386. }
  1387. array->indexed_properties().append(value);
  1388. }
  1389. return array;
  1390. }
  1391. void TemplateLiteral::dump(int indent) const
  1392. {
  1393. ASTNode::dump(indent);
  1394. for (auto& expression : m_expressions)
  1395. expression.dump(indent + 1);
  1396. }
  1397. Value TemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1398. {
  1399. StringBuilder string_builder;
  1400. for (auto& expression : m_expressions) {
  1401. auto expr = expression.execute(interpreter, global_object);
  1402. if (interpreter.exception())
  1403. return {};
  1404. auto string = expr.to_string(interpreter);
  1405. if (interpreter.exception())
  1406. return {};
  1407. string_builder.append(string);
  1408. }
  1409. return js_string(interpreter, string_builder.build());
  1410. }
  1411. void TaggedTemplateLiteral::dump(int indent) const
  1412. {
  1413. ASTNode::dump(indent);
  1414. print_indent(indent + 1);
  1415. printf("(Tag)\n");
  1416. m_tag->dump(indent + 2);
  1417. print_indent(indent + 1);
  1418. printf("(Template Literal)\n");
  1419. m_template_literal->dump(indent + 2);
  1420. }
  1421. Value TaggedTemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1422. {
  1423. auto tag = m_tag->execute(interpreter, global_object);
  1424. if (interpreter.exception())
  1425. return {};
  1426. if (!tag.is_function()) {
  1427. interpreter.throw_exception<TypeError>(ErrorType::NotAFunction, tag.to_string_without_side_effects().characters());
  1428. return {};
  1429. }
  1430. auto& tag_function = tag.as_function();
  1431. auto& expressions = m_template_literal->expressions();
  1432. auto* strings = Array::create(interpreter.global_object());
  1433. MarkedValueList arguments(interpreter.heap());
  1434. arguments.append(strings);
  1435. for (size_t i = 0; i < expressions.size(); ++i) {
  1436. auto value = expressions[i].execute(interpreter, global_object);
  1437. if (interpreter.exception())
  1438. return {};
  1439. // tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
  1440. // tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
  1441. if (i % 2 == 0) {
  1442. strings->indexed_properties().append(value);
  1443. } else {
  1444. arguments.append(value);
  1445. }
  1446. }
  1447. auto* raw_strings = Array::create(global_object);
  1448. for (auto& raw_string : m_template_literal->raw_strings()) {
  1449. auto value = raw_string.execute(interpreter, global_object);
  1450. if (interpreter.exception())
  1451. return {};
  1452. raw_strings->indexed_properties().append(value);
  1453. }
  1454. strings->define_property("raw", raw_strings, 0);
  1455. if (interpreter.exception())
  1456. return {};
  1457. return interpreter.call(tag_function, js_undefined(), move(arguments));
  1458. }
  1459. void TryStatement::dump(int indent) const
  1460. {
  1461. ASTNode::dump(indent);
  1462. print_indent(indent);
  1463. printf("(Block)\n");
  1464. block().dump(indent + 1);
  1465. if (handler()) {
  1466. print_indent(indent);
  1467. printf("(Handler)\n");
  1468. handler()->dump(indent + 1);
  1469. }
  1470. if (finalizer()) {
  1471. print_indent(indent);
  1472. printf("(Finalizer)\n");
  1473. finalizer()->dump(indent + 1);
  1474. }
  1475. }
  1476. void CatchClause::dump(int indent) const
  1477. {
  1478. print_indent(indent);
  1479. printf("CatchClause");
  1480. if (!m_parameter.is_null())
  1481. printf(" (%s)", m_parameter.characters());
  1482. printf("\n");
  1483. body().dump(indent + 1);
  1484. }
  1485. void ThrowStatement::dump(int indent) const
  1486. {
  1487. ASTNode::dump(indent);
  1488. argument().dump(indent + 1);
  1489. }
  1490. Value TryStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1491. {
  1492. interpreter.run(global_object, block(), {}, ScopeType::Try);
  1493. if (auto* exception = interpreter.exception()) {
  1494. if (m_handler) {
  1495. interpreter.clear_exception();
  1496. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  1497. interpreter.run(global_object, m_handler->body(), move(arguments));
  1498. }
  1499. }
  1500. if (m_finalizer)
  1501. m_finalizer->execute(interpreter, global_object);
  1502. return js_undefined();
  1503. }
  1504. Value CatchClause::execute(Interpreter&, GlobalObject&) const
  1505. {
  1506. // NOTE: CatchClause execution is handled by TryStatement.
  1507. ASSERT_NOT_REACHED();
  1508. return {};
  1509. }
  1510. Value ThrowStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1511. {
  1512. auto value = m_argument->execute(interpreter, global_object);
  1513. if (interpreter.exception())
  1514. return {};
  1515. return interpreter.throw_exception(value);
  1516. }
  1517. Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1518. {
  1519. auto discriminant_result = m_discriminant->execute(interpreter, global_object);
  1520. if (interpreter.exception())
  1521. return {};
  1522. bool falling_through = false;
  1523. for (auto& switch_case : m_cases) {
  1524. if (!falling_through && switch_case.test()) {
  1525. auto test_result = switch_case.test()->execute(interpreter, global_object);
  1526. if (interpreter.exception())
  1527. return {};
  1528. if (!strict_eq(interpreter, discriminant_result, test_result))
  1529. continue;
  1530. }
  1531. falling_through = true;
  1532. for (auto& statement : switch_case.consequent()) {
  1533. statement.execute(interpreter, global_object);
  1534. if (interpreter.exception())
  1535. return {};
  1536. if (interpreter.should_unwind()) {
  1537. if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  1538. interpreter.stop_unwind();
  1539. return {};
  1540. }
  1541. return {};
  1542. }
  1543. }
  1544. }
  1545. return js_undefined();
  1546. }
  1547. Value SwitchCase::execute(Interpreter&, GlobalObject&) const
  1548. {
  1549. return {};
  1550. }
  1551. Value BreakStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1552. {
  1553. interpreter.unwind(ScopeType::Breakable, m_target_label);
  1554. return js_undefined();
  1555. }
  1556. Value ContinueStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1557. {
  1558. interpreter.unwind(ScopeType::Continuable, m_target_label);
  1559. return js_undefined();
  1560. }
  1561. void SwitchStatement::dump(int indent) const
  1562. {
  1563. ASTNode::dump(indent);
  1564. m_discriminant->dump(indent + 1);
  1565. for (auto& switch_case : m_cases) {
  1566. switch_case.dump(indent + 1);
  1567. }
  1568. }
  1569. void SwitchCase::dump(int indent) const
  1570. {
  1571. ASTNode::dump(indent);
  1572. print_indent(indent + 1);
  1573. if (m_test) {
  1574. printf("(Test)\n");
  1575. m_test->dump(indent + 2);
  1576. } else {
  1577. printf("(Default)\n");
  1578. }
  1579. print_indent(indent + 1);
  1580. printf("(Consequent)\n");
  1581. for (auto& statement : m_consequent)
  1582. statement.dump(indent + 2);
  1583. }
  1584. Value ConditionalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1585. {
  1586. auto test_result = m_test->execute(interpreter, global_object);
  1587. if (interpreter.exception())
  1588. return {};
  1589. Value result;
  1590. if (test_result.to_boolean()) {
  1591. result = m_consequent->execute(interpreter, global_object);
  1592. } else {
  1593. result = m_alternate->execute(interpreter, global_object);
  1594. }
  1595. if (interpreter.exception())
  1596. return {};
  1597. return result;
  1598. }
  1599. void ConditionalExpression::dump(int indent) const
  1600. {
  1601. ASTNode::dump(indent);
  1602. print_indent(indent + 1);
  1603. printf("(Test)\n");
  1604. m_test->dump(indent + 2);
  1605. print_indent(indent + 1);
  1606. printf("(Consequent)\n");
  1607. m_consequent->dump(indent + 2);
  1608. print_indent(indent + 1);
  1609. printf("(Alternate)\n");
  1610. m_alternate->dump(indent + 2);
  1611. }
  1612. void SequenceExpression::dump(int indent) const
  1613. {
  1614. ASTNode::dump(indent);
  1615. for (auto& expression : m_expressions)
  1616. expression.dump(indent + 1);
  1617. }
  1618. Value SequenceExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1619. {
  1620. Value last_value;
  1621. for (auto& expression : m_expressions) {
  1622. last_value = expression.execute(interpreter, global_object);
  1623. if (interpreter.exception())
  1624. return {};
  1625. }
  1626. return last_value;
  1627. }
  1628. Value DebuggerStatement::execute(Interpreter&, GlobalObject&) const
  1629. {
  1630. dbg() << "Sorry, no JavaScript debugger available (yet)!";
  1631. return js_undefined();
  1632. }
  1633. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1634. {
  1635. m_variables.append(move(variables));
  1636. }
  1637. void ScopeNode::add_functions(NonnullRefPtrVector<FunctionDeclaration> functions)
  1638. {
  1639. m_functions.append(move(functions));
  1640. }
  1641. }