AST.cpp 53 KB

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