AST.cpp 54 KB

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