AST.cpp 54 KB

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