AST.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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, global_object);
  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 { &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, 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, global_object);
  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 global_object.delete_property(reference.name());
  539. auto* base_object = reference.base().to_object(interpreter, global_object);
  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, global_object, 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, global_object);
  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, global_object, 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. object->define_accessor(key, value.as_function(), property.type() == ObjectProperty::Type::Getter, Attribute::Configurable | Attribute::Enumerable);
  1243. if (interpreter.exception())
  1244. return {};
  1245. } else {
  1246. object->define_property(key, value);
  1247. if (interpreter.exception())
  1248. return {};
  1249. }
  1250. }
  1251. return object;
  1252. }
  1253. void MemberExpression::dump(int indent) const
  1254. {
  1255. print_indent(indent);
  1256. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  1257. m_object->dump(indent + 1);
  1258. m_property->dump(indent + 1);
  1259. }
  1260. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter, GlobalObject& global_object) const
  1261. {
  1262. if (!is_computed()) {
  1263. ASSERT(m_property->is_identifier());
  1264. return static_cast<const Identifier&>(*m_property).string();
  1265. }
  1266. auto index = m_property->execute(interpreter, global_object);
  1267. if (interpreter.exception())
  1268. return {};
  1269. ASSERT(!index.is_empty());
  1270. if (index.is_integer() && index.as_i32() >= 0)
  1271. return index.as_i32();
  1272. auto index_string = index.to_string(interpreter);
  1273. if (interpreter.exception())
  1274. return {};
  1275. return index_string;
  1276. }
  1277. String MemberExpression::to_string_approximation() const
  1278. {
  1279. String object_string = "<object>";
  1280. if (m_object->is_identifier())
  1281. object_string = static_cast<const Identifier&>(*m_object).string();
  1282. if (is_computed())
  1283. return String::format("%s[<computed>]", object_string.characters());
  1284. ASSERT(m_property->is_identifier());
  1285. return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
  1286. }
  1287. Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1288. {
  1289. auto object_value = m_object->execute(interpreter, global_object);
  1290. if (interpreter.exception())
  1291. return {};
  1292. auto* object_result = object_value.to_object(interpreter, global_object);
  1293. if (interpreter.exception())
  1294. return {};
  1295. return object_result->get(computed_property_name(interpreter, global_object)).value_or(js_undefined());
  1296. }
  1297. Value StringLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1298. {
  1299. return js_string(interpreter, m_value);
  1300. }
  1301. Value NumericLiteral::execute(Interpreter&, GlobalObject&) const
  1302. {
  1303. return Value(m_value);
  1304. }
  1305. Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
  1306. {
  1307. return js_bigint(interpreter, Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
  1308. }
  1309. Value BooleanLiteral::execute(Interpreter&, GlobalObject&) const
  1310. {
  1311. return Value(m_value);
  1312. }
  1313. Value NullLiteral::execute(Interpreter&, GlobalObject&) const
  1314. {
  1315. return js_null();
  1316. }
  1317. void RegExpLiteral::dump(int indent) const
  1318. {
  1319. print_indent(indent);
  1320. printf("%s (/%s/%s)\n", class_name(), content().characters(), flags().characters());
  1321. }
  1322. Value RegExpLiteral::execute(Interpreter&, GlobalObject& global_object) const
  1323. {
  1324. return RegExpObject::create(global_object, content(), flags());
  1325. }
  1326. void ArrayExpression::dump(int indent) const
  1327. {
  1328. ASTNode::dump(indent);
  1329. for (auto& element : m_elements) {
  1330. if (element) {
  1331. element->dump(indent + 1);
  1332. } else {
  1333. print_indent(indent + 1);
  1334. printf("<empty>\n");
  1335. }
  1336. }
  1337. }
  1338. Value ArrayExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1339. {
  1340. auto* array = Array::create(global_object);
  1341. for (auto& element : m_elements) {
  1342. auto value = Value();
  1343. if (element) {
  1344. value = element->execute(interpreter, global_object);
  1345. if (interpreter.exception())
  1346. return {};
  1347. if (element->is_spread_expression()) {
  1348. // FIXME: Support arbitrary iterables
  1349. if (value.is_array()) {
  1350. auto& array_to_spread = static_cast<Array&>(value.as_object());
  1351. for (auto& entry : array_to_spread.indexed_properties()) {
  1352. array->indexed_properties().append(entry.value_and_attributes(&array_to_spread).value);
  1353. if (interpreter.exception())
  1354. return {};
  1355. }
  1356. continue;
  1357. }
  1358. if (value.is_string() || (value.is_object() && value.as_object().is_string_object())) {
  1359. String string_to_spread;
  1360. if (value.is_string()) {
  1361. string_to_spread = value.as_string().string();
  1362. } else {
  1363. string_to_spread = static_cast<const StringObject&>(value.as_object()).primitive_string().string();
  1364. }
  1365. for (size_t i = 0; i < string_to_spread.length(); ++i)
  1366. array->indexed_properties().append(js_string(interpreter, string_to_spread.substring(i, 1)));
  1367. continue;
  1368. }
  1369. interpreter.throw_exception<TypeError>(ErrorType::NotIterable, value.to_string_without_side_effects().characters());
  1370. return {};
  1371. }
  1372. }
  1373. array->indexed_properties().append(value);
  1374. }
  1375. return array;
  1376. }
  1377. void TemplateLiteral::dump(int indent) const
  1378. {
  1379. ASTNode::dump(indent);
  1380. for (auto& expression : m_expressions)
  1381. expression.dump(indent + 1);
  1382. }
  1383. Value TemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1384. {
  1385. StringBuilder string_builder;
  1386. for (auto& expression : m_expressions) {
  1387. auto expr = expression.execute(interpreter, global_object);
  1388. if (interpreter.exception())
  1389. return {};
  1390. auto string = expr.to_string(interpreter);
  1391. if (interpreter.exception())
  1392. return {};
  1393. string_builder.append(string);
  1394. }
  1395. return js_string(interpreter, string_builder.build());
  1396. }
  1397. void TaggedTemplateLiteral::dump(int indent) const
  1398. {
  1399. ASTNode::dump(indent);
  1400. print_indent(indent + 1);
  1401. printf("(Tag)\n");
  1402. m_tag->dump(indent + 2);
  1403. print_indent(indent + 1);
  1404. printf("(Template Literal)\n");
  1405. m_template_literal->dump(indent + 2);
  1406. }
  1407. Value TaggedTemplateLiteral::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1408. {
  1409. auto tag = m_tag->execute(interpreter, global_object);
  1410. if (interpreter.exception())
  1411. return {};
  1412. if (!tag.is_function()) {
  1413. interpreter.throw_exception<TypeError>(ErrorType::NotAFunction, tag.to_string_without_side_effects().characters());
  1414. return {};
  1415. }
  1416. auto& tag_function = tag.as_function();
  1417. auto& expressions = m_template_literal->expressions();
  1418. auto* strings = Array::create(global_object);
  1419. MarkedValueList arguments(interpreter.heap());
  1420. arguments.append(strings);
  1421. for (size_t i = 0; i < expressions.size(); ++i) {
  1422. auto value = expressions[i].execute(interpreter, global_object);
  1423. if (interpreter.exception())
  1424. return {};
  1425. // tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
  1426. // tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
  1427. if (i % 2 == 0) {
  1428. strings->indexed_properties().append(value);
  1429. } else {
  1430. arguments.append(value);
  1431. }
  1432. }
  1433. auto* raw_strings = Array::create(global_object);
  1434. for (auto& raw_string : m_template_literal->raw_strings()) {
  1435. auto value = raw_string.execute(interpreter, global_object);
  1436. if (interpreter.exception())
  1437. return {};
  1438. raw_strings->indexed_properties().append(value);
  1439. }
  1440. strings->define_property("raw", raw_strings, 0);
  1441. if (interpreter.exception())
  1442. return {};
  1443. return interpreter.call(tag_function, js_undefined(), move(arguments));
  1444. }
  1445. void TryStatement::dump(int indent) const
  1446. {
  1447. ASTNode::dump(indent);
  1448. print_indent(indent);
  1449. printf("(Block)\n");
  1450. block().dump(indent + 1);
  1451. if (handler()) {
  1452. print_indent(indent);
  1453. printf("(Handler)\n");
  1454. handler()->dump(indent + 1);
  1455. }
  1456. if (finalizer()) {
  1457. print_indent(indent);
  1458. printf("(Finalizer)\n");
  1459. finalizer()->dump(indent + 1);
  1460. }
  1461. }
  1462. void CatchClause::dump(int indent) const
  1463. {
  1464. print_indent(indent);
  1465. printf("CatchClause");
  1466. if (!m_parameter.is_null())
  1467. printf(" (%s)", m_parameter.characters());
  1468. printf("\n");
  1469. body().dump(indent + 1);
  1470. }
  1471. void ThrowStatement::dump(int indent) const
  1472. {
  1473. ASTNode::dump(indent);
  1474. argument().dump(indent + 1);
  1475. }
  1476. Value TryStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1477. {
  1478. interpreter.run(global_object, block(), {}, ScopeType::Try);
  1479. if (auto* exception = interpreter.exception()) {
  1480. if (m_handler) {
  1481. interpreter.clear_exception();
  1482. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  1483. interpreter.run(global_object, m_handler->body(), move(arguments));
  1484. }
  1485. }
  1486. if (m_finalizer)
  1487. m_finalizer->execute(interpreter, global_object);
  1488. return js_undefined();
  1489. }
  1490. Value CatchClause::execute(Interpreter&, GlobalObject&) const
  1491. {
  1492. // NOTE: CatchClause execution is handled by TryStatement.
  1493. ASSERT_NOT_REACHED();
  1494. return {};
  1495. }
  1496. Value ThrowStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1497. {
  1498. auto value = m_argument->execute(interpreter, global_object);
  1499. if (interpreter.exception())
  1500. return {};
  1501. return interpreter.throw_exception(value);
  1502. }
  1503. Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1504. {
  1505. auto discriminant_result = m_discriminant->execute(interpreter, global_object);
  1506. if (interpreter.exception())
  1507. return {};
  1508. bool falling_through = false;
  1509. for (auto& switch_case : m_cases) {
  1510. if (!falling_through && switch_case.test()) {
  1511. auto test_result = switch_case.test()->execute(interpreter, global_object);
  1512. if (interpreter.exception())
  1513. return {};
  1514. if (!strict_eq(interpreter, discriminant_result, test_result))
  1515. continue;
  1516. }
  1517. falling_through = true;
  1518. for (auto& statement : switch_case.consequent()) {
  1519. statement.execute(interpreter, global_object);
  1520. if (interpreter.exception())
  1521. return {};
  1522. if (interpreter.should_unwind()) {
  1523. if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  1524. interpreter.stop_unwind();
  1525. return {};
  1526. }
  1527. return {};
  1528. }
  1529. }
  1530. }
  1531. return js_undefined();
  1532. }
  1533. Value SwitchCase::execute(Interpreter&, GlobalObject&) const
  1534. {
  1535. return {};
  1536. }
  1537. Value BreakStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1538. {
  1539. interpreter.unwind(ScopeType::Breakable, m_target_label);
  1540. return js_undefined();
  1541. }
  1542. Value ContinueStatement::execute(Interpreter& interpreter, GlobalObject&) const
  1543. {
  1544. interpreter.unwind(ScopeType::Continuable, m_target_label);
  1545. return js_undefined();
  1546. }
  1547. void SwitchStatement::dump(int indent) const
  1548. {
  1549. ASTNode::dump(indent);
  1550. m_discriminant->dump(indent + 1);
  1551. for (auto& switch_case : m_cases) {
  1552. switch_case.dump(indent + 1);
  1553. }
  1554. }
  1555. void SwitchCase::dump(int indent) const
  1556. {
  1557. ASTNode::dump(indent);
  1558. print_indent(indent + 1);
  1559. if (m_test) {
  1560. printf("(Test)\n");
  1561. m_test->dump(indent + 2);
  1562. } else {
  1563. printf("(Default)\n");
  1564. }
  1565. print_indent(indent + 1);
  1566. printf("(Consequent)\n");
  1567. for (auto& statement : m_consequent)
  1568. statement.dump(indent + 2);
  1569. }
  1570. Value ConditionalExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1571. {
  1572. auto test_result = m_test->execute(interpreter, global_object);
  1573. if (interpreter.exception())
  1574. return {};
  1575. Value result;
  1576. if (test_result.to_boolean()) {
  1577. result = m_consequent->execute(interpreter, global_object);
  1578. } else {
  1579. result = m_alternate->execute(interpreter, global_object);
  1580. }
  1581. if (interpreter.exception())
  1582. return {};
  1583. return result;
  1584. }
  1585. void ConditionalExpression::dump(int indent) const
  1586. {
  1587. ASTNode::dump(indent);
  1588. print_indent(indent + 1);
  1589. printf("(Test)\n");
  1590. m_test->dump(indent + 2);
  1591. print_indent(indent + 1);
  1592. printf("(Consequent)\n");
  1593. m_consequent->dump(indent + 2);
  1594. print_indent(indent + 1);
  1595. printf("(Alternate)\n");
  1596. m_alternate->dump(indent + 2);
  1597. }
  1598. void SequenceExpression::dump(int indent) const
  1599. {
  1600. ASTNode::dump(indent);
  1601. for (auto& expression : m_expressions)
  1602. expression.dump(indent + 1);
  1603. }
  1604. Value SequenceExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
  1605. {
  1606. Value last_value;
  1607. for (auto& expression : m_expressions) {
  1608. last_value = expression.execute(interpreter, global_object);
  1609. if (interpreter.exception())
  1610. return {};
  1611. }
  1612. return last_value;
  1613. }
  1614. Value DebuggerStatement::execute(Interpreter&, GlobalObject&) const
  1615. {
  1616. dbg() << "Sorry, no JavaScript debugger available (yet)!";
  1617. return js_undefined();
  1618. }
  1619. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1620. {
  1621. m_variables.append(move(variables));
  1622. }
  1623. void ScopeNode::add_functions(NonnullRefPtrVector<FunctionDeclaration> functions)
  1624. {
  1625. m_functions.append(move(functions));
  1626. }
  1627. }