AST.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  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. auto lhs_result = m_lhs->execute(interpreter);
  537. if (interpreter.exception())
  538. return {};
  539. switch (m_op) {
  540. case UnaryOp::BitwiseNot:
  541. return bitwise_not(interpreter, lhs_result);
  542. case UnaryOp::Not:
  543. return Value(!lhs_result.to_boolean());
  544. case UnaryOp::Plus:
  545. return unary_plus(interpreter, lhs_result);
  546. case UnaryOp::Minus:
  547. return unary_minus(interpreter, lhs_result);
  548. case UnaryOp::Typeof:
  549. switch (lhs_result.type()) {
  550. case Value::Type::Empty:
  551. ASSERT_NOT_REACHED();
  552. return {};
  553. case Value::Type::Undefined:
  554. return js_string(interpreter, "undefined");
  555. case Value::Type::Null:
  556. // yes, this is on purpose. yes, this is how javascript works.
  557. // yes, it's silly.
  558. return js_string(interpreter, "object");
  559. case Value::Type::Number:
  560. return js_string(interpreter, "number");
  561. case Value::Type::String:
  562. return js_string(interpreter, "string");
  563. case Value::Type::Object:
  564. if (lhs_result.is_function())
  565. return js_string(interpreter, "function");
  566. return js_string(interpreter, "object");
  567. case Value::Type::Boolean:
  568. return js_string(interpreter, "boolean");
  569. case Value::Type::Symbol:
  570. return js_string(interpreter, "symbol");
  571. default:
  572. ASSERT_NOT_REACHED();
  573. }
  574. case UnaryOp::Void:
  575. return js_undefined();
  576. case UnaryOp::Delete:
  577. ASSERT_NOT_REACHED();
  578. }
  579. ASSERT_NOT_REACHED();
  580. }
  581. static void print_indent(int indent)
  582. {
  583. for (int i = 0; i < indent * 2; ++i)
  584. putchar(' ');
  585. }
  586. void ASTNode::dump(int indent) const
  587. {
  588. print_indent(indent);
  589. printf("%s\n", class_name());
  590. }
  591. void ScopeNode::dump(int indent) const
  592. {
  593. ASTNode::dump(indent);
  594. if (!m_variables.is_empty()) {
  595. print_indent(indent + 1);
  596. printf("(Variables)\n");
  597. for (auto& variable : m_variables)
  598. variable.dump(indent + 2);
  599. }
  600. if (!m_children.is_empty()) {
  601. print_indent(indent + 1);
  602. printf("(Children)\n");
  603. for (auto& child : children())
  604. child.dump(indent + 2);
  605. }
  606. }
  607. void BinaryExpression::dump(int indent) const
  608. {
  609. const char* op_string = nullptr;
  610. switch (m_op) {
  611. case BinaryOp::Addition:
  612. op_string = "+";
  613. break;
  614. case BinaryOp::Subtraction:
  615. op_string = "-";
  616. break;
  617. case BinaryOp::Multiplication:
  618. op_string = "*";
  619. break;
  620. case BinaryOp::Division:
  621. op_string = "/";
  622. break;
  623. case BinaryOp::Modulo:
  624. op_string = "%";
  625. break;
  626. case BinaryOp::Exponentiation:
  627. op_string = "**";
  628. break;
  629. case BinaryOp::TypedEquals:
  630. op_string = "===";
  631. break;
  632. case BinaryOp::TypedInequals:
  633. op_string = "!==";
  634. break;
  635. case BinaryOp::AbstractEquals:
  636. op_string = "==";
  637. break;
  638. case BinaryOp::AbstractInequals:
  639. op_string = "!=";
  640. break;
  641. case BinaryOp::GreaterThan:
  642. op_string = ">";
  643. break;
  644. case BinaryOp::GreaterThanEquals:
  645. op_string = ">=";
  646. break;
  647. case BinaryOp::LessThan:
  648. op_string = "<";
  649. break;
  650. case BinaryOp::LessThanEquals:
  651. op_string = "<=";
  652. break;
  653. case BinaryOp::BitwiseAnd:
  654. op_string = "&";
  655. break;
  656. case BinaryOp::BitwiseOr:
  657. op_string = "|";
  658. break;
  659. case BinaryOp::BitwiseXor:
  660. op_string = "^";
  661. break;
  662. case BinaryOp::LeftShift:
  663. op_string = "<<";
  664. break;
  665. case BinaryOp::RightShift:
  666. op_string = ">>";
  667. break;
  668. case BinaryOp::UnsignedRightShift:
  669. op_string = ">>>";
  670. break;
  671. case BinaryOp::In:
  672. op_string = "in";
  673. break;
  674. case BinaryOp::InstanceOf:
  675. op_string = "instanceof";
  676. break;
  677. }
  678. print_indent(indent);
  679. printf("%s\n", class_name());
  680. m_lhs->dump(indent + 1);
  681. print_indent(indent + 1);
  682. printf("%s\n", op_string);
  683. m_rhs->dump(indent + 1);
  684. }
  685. void LogicalExpression::dump(int indent) const
  686. {
  687. const char* op_string = nullptr;
  688. switch (m_op) {
  689. case LogicalOp::And:
  690. op_string = "&&";
  691. break;
  692. case LogicalOp::Or:
  693. op_string = "||";
  694. break;
  695. case LogicalOp::NullishCoalescing:
  696. op_string = "??";
  697. break;
  698. }
  699. print_indent(indent);
  700. printf("%s\n", class_name());
  701. m_lhs->dump(indent + 1);
  702. print_indent(indent + 1);
  703. printf("%s\n", op_string);
  704. m_rhs->dump(indent + 1);
  705. }
  706. void UnaryExpression::dump(int indent) const
  707. {
  708. const char* op_string = nullptr;
  709. switch (m_op) {
  710. case UnaryOp::BitwiseNot:
  711. op_string = "~";
  712. break;
  713. case UnaryOp::Not:
  714. op_string = "!";
  715. break;
  716. case UnaryOp::Plus:
  717. op_string = "+";
  718. break;
  719. case UnaryOp::Minus:
  720. op_string = "-";
  721. break;
  722. case UnaryOp::Typeof:
  723. op_string = "typeof ";
  724. break;
  725. case UnaryOp::Void:
  726. op_string = "void ";
  727. break;
  728. case UnaryOp::Delete:
  729. op_string = "delete ";
  730. break;
  731. }
  732. print_indent(indent);
  733. printf("%s\n", class_name());
  734. print_indent(indent + 1);
  735. printf("%s\n", op_string);
  736. m_lhs->dump(indent + 1);
  737. }
  738. void CallExpression::dump(int indent) const
  739. {
  740. print_indent(indent);
  741. printf("CallExpression %s\n", is_new_expression() ? "[new]" : "");
  742. m_callee->dump(indent + 1);
  743. for (auto& argument : m_arguments)
  744. argument.value->dump(indent + 1);
  745. }
  746. void StringLiteral::dump(int indent) const
  747. {
  748. print_indent(indent);
  749. printf("StringLiteral \"%s\"\n", m_value.characters());
  750. }
  751. void NumericLiteral::dump(int indent) const
  752. {
  753. print_indent(indent);
  754. printf("NumericLiteral %g\n", m_value);
  755. }
  756. void BooleanLiteral::dump(int indent) const
  757. {
  758. print_indent(indent);
  759. printf("BooleanLiteral %s\n", m_value ? "true" : "false");
  760. }
  761. void NullLiteral::dump(int indent) const
  762. {
  763. print_indent(indent);
  764. printf("null\n");
  765. }
  766. void FunctionNode::dump(int indent, const char* class_name) const
  767. {
  768. print_indent(indent);
  769. printf("%s '%s'\n", class_name, name().characters());
  770. if (!m_parameters.is_empty()) {
  771. print_indent(indent + 1);
  772. printf("(Parameters)\n");
  773. for (auto& parameter : m_parameters) {
  774. print_indent(indent + 2);
  775. if (parameter.is_rest)
  776. printf("...");
  777. printf("%s\n", parameter.name.characters());
  778. if (parameter.default_value)
  779. parameter.default_value->dump(indent + 3);
  780. }
  781. }
  782. if (!m_variables.is_empty()) {
  783. print_indent(indent + 1);
  784. printf("(Variables)\n");
  785. for (auto& variable : m_variables)
  786. variable.dump(indent + 2);
  787. }
  788. print_indent(indent + 1);
  789. printf("(Body)\n");
  790. body().dump(indent + 2);
  791. }
  792. void FunctionDeclaration::dump(int indent) const
  793. {
  794. FunctionNode::dump(indent, class_name());
  795. }
  796. void FunctionExpression::dump(int indent) const
  797. {
  798. FunctionNode::dump(indent, class_name());
  799. }
  800. void ReturnStatement::dump(int indent) const
  801. {
  802. ASTNode::dump(indent);
  803. if (argument())
  804. argument()->dump(indent + 1);
  805. }
  806. void IfStatement::dump(int indent) const
  807. {
  808. ASTNode::dump(indent);
  809. print_indent(indent);
  810. printf("If\n");
  811. predicate().dump(indent + 1);
  812. consequent().dump(indent + 1);
  813. if (alternate()) {
  814. print_indent(indent);
  815. printf("Else\n");
  816. alternate()->dump(indent + 1);
  817. }
  818. }
  819. void WhileStatement::dump(int indent) const
  820. {
  821. ASTNode::dump(indent);
  822. print_indent(indent);
  823. printf("While\n");
  824. test().dump(indent + 1);
  825. body().dump(indent + 1);
  826. }
  827. void DoWhileStatement::dump(int indent) const
  828. {
  829. ASTNode::dump(indent);
  830. print_indent(indent);
  831. printf("DoWhile\n");
  832. test().dump(indent + 1);
  833. body().dump(indent + 1);
  834. }
  835. void ForStatement::dump(int indent) const
  836. {
  837. ASTNode::dump(indent);
  838. print_indent(indent);
  839. printf("For\n");
  840. if (init())
  841. init()->dump(indent + 1);
  842. if (test())
  843. test()->dump(indent + 1);
  844. if (update())
  845. update()->dump(indent + 1);
  846. body().dump(indent + 1);
  847. }
  848. void ForInStatement::dump(int indent) const
  849. {
  850. ASTNode::dump(indent);
  851. print_indent(indent);
  852. printf("ForIn\n");
  853. lhs().dump(indent + 1);
  854. rhs().dump(indent + 1);
  855. body().dump(indent + 1);
  856. }
  857. void ForOfStatement::dump(int indent) const
  858. {
  859. ASTNode::dump(indent);
  860. print_indent(indent);
  861. printf("ForOf\n");
  862. lhs().dump(indent + 1);
  863. rhs().dump(indent + 1);
  864. body().dump(indent + 1);
  865. }
  866. Value Identifier::execute(Interpreter& interpreter) const
  867. {
  868. auto value = interpreter.get_variable(string());
  869. if (value.is_empty())
  870. return interpreter.throw_exception<ReferenceError>(String::format("'%s' not known", string().characters()));
  871. return value;
  872. }
  873. void Identifier::dump(int indent) const
  874. {
  875. print_indent(indent);
  876. printf("Identifier \"%s\"\n", m_string.characters());
  877. }
  878. void SpreadExpression::dump(int indent) const
  879. {
  880. ASTNode::dump(indent);
  881. m_target->dump(indent + 1);
  882. }
  883. Value SpreadExpression::execute(Interpreter& interpreter) const
  884. {
  885. return m_target->execute(interpreter);
  886. }
  887. Value ThisExpression::execute(Interpreter& interpreter) const
  888. {
  889. return interpreter.this_value();
  890. }
  891. void ThisExpression::dump(int indent) const
  892. {
  893. ASTNode::dump(indent);
  894. }
  895. Value AssignmentExpression::execute(Interpreter& interpreter) const
  896. {
  897. auto rhs_result = m_rhs->execute(interpreter);
  898. if (interpreter.exception())
  899. return {};
  900. Value lhs_result;
  901. switch (m_op) {
  902. case AssignmentOp::Assignment:
  903. break;
  904. case AssignmentOp::AdditionAssignment:
  905. lhs_result = m_lhs->execute(interpreter);
  906. if (interpreter.exception())
  907. return {};
  908. rhs_result = add(interpreter, lhs_result, rhs_result);
  909. break;
  910. case AssignmentOp::SubtractionAssignment:
  911. lhs_result = m_lhs->execute(interpreter);
  912. if (interpreter.exception())
  913. return {};
  914. rhs_result = sub(interpreter, lhs_result, rhs_result);
  915. break;
  916. case AssignmentOp::MultiplicationAssignment:
  917. lhs_result = m_lhs->execute(interpreter);
  918. if (interpreter.exception())
  919. return {};
  920. rhs_result = mul(interpreter, lhs_result, rhs_result);
  921. break;
  922. case AssignmentOp::DivisionAssignment:
  923. lhs_result = m_lhs->execute(interpreter);
  924. if (interpreter.exception())
  925. return {};
  926. rhs_result = div(interpreter, lhs_result, rhs_result);
  927. break;
  928. case AssignmentOp::ModuloAssignment:
  929. lhs_result = m_lhs->execute(interpreter);
  930. if (interpreter.exception())
  931. return {};
  932. rhs_result = mod(interpreter, lhs_result, rhs_result);
  933. break;
  934. case AssignmentOp::ExponentiationAssignment:
  935. lhs_result = m_lhs->execute(interpreter);
  936. if (interpreter.exception())
  937. return {};
  938. rhs_result = exp(interpreter, lhs_result, rhs_result);
  939. break;
  940. case AssignmentOp::BitwiseAndAssignment:
  941. lhs_result = m_lhs->execute(interpreter);
  942. if (interpreter.exception())
  943. return {};
  944. rhs_result = bitwise_and(interpreter, lhs_result, rhs_result);
  945. break;
  946. case AssignmentOp::BitwiseOrAssignment:
  947. lhs_result = m_lhs->execute(interpreter);
  948. if (interpreter.exception())
  949. return {};
  950. rhs_result = bitwise_or(interpreter, lhs_result, rhs_result);
  951. break;
  952. case AssignmentOp::BitwiseXorAssignment:
  953. lhs_result = m_lhs->execute(interpreter);
  954. if (interpreter.exception())
  955. return {};
  956. rhs_result = bitwise_xor(interpreter, lhs_result, rhs_result);
  957. break;
  958. case AssignmentOp::LeftShiftAssignment:
  959. lhs_result = m_lhs->execute(interpreter);
  960. if (interpreter.exception())
  961. return {};
  962. rhs_result = left_shift(interpreter, lhs_result, rhs_result);
  963. break;
  964. case AssignmentOp::RightShiftAssignment:
  965. lhs_result = m_lhs->execute(interpreter);
  966. if (interpreter.exception())
  967. return {};
  968. rhs_result = right_shift(interpreter, lhs_result, rhs_result);
  969. break;
  970. case AssignmentOp::UnsignedRightShiftAssignment:
  971. lhs_result = m_lhs->execute(interpreter);
  972. if (interpreter.exception())
  973. return {};
  974. rhs_result = unsigned_right_shift(interpreter, lhs_result, rhs_result);
  975. break;
  976. }
  977. if (interpreter.exception())
  978. return {};
  979. auto reference = m_lhs->to_reference(interpreter);
  980. if (interpreter.exception())
  981. return {};
  982. if (reference.is_unresolvable())
  983. return interpreter.throw_exception<ReferenceError>("Invalid left-hand side in assignment");
  984. update_function_name(rhs_result, reference.name().as_string());
  985. reference.put(interpreter, rhs_result);
  986. if (interpreter.exception())
  987. return {};
  988. return rhs_result;
  989. }
  990. Value UpdateExpression::execute(Interpreter& interpreter) const
  991. {
  992. auto reference = m_argument->to_reference(interpreter);
  993. if (interpreter.exception())
  994. return {};
  995. auto old_value = reference.get(interpreter);
  996. if (interpreter.exception())
  997. return {};
  998. old_value = old_value.to_number(interpreter);
  999. if (interpreter.exception())
  1000. return {};
  1001. int op_result = 0;
  1002. switch (m_op) {
  1003. case UpdateOp::Increment:
  1004. op_result = 1;
  1005. break;
  1006. case UpdateOp::Decrement:
  1007. op_result = -1;
  1008. break;
  1009. default:
  1010. ASSERT_NOT_REACHED();
  1011. }
  1012. auto new_value = Value(old_value.as_double() + op_result);
  1013. reference.put(interpreter, new_value);
  1014. if (interpreter.exception())
  1015. return {};
  1016. return m_prefixed ? new_value : old_value;
  1017. }
  1018. void AssignmentExpression::dump(int indent) const
  1019. {
  1020. const char* op_string = nullptr;
  1021. switch (m_op) {
  1022. case AssignmentOp::Assignment:
  1023. op_string = "=";
  1024. break;
  1025. case AssignmentOp::AdditionAssignment:
  1026. op_string = "+=";
  1027. break;
  1028. case AssignmentOp::SubtractionAssignment:
  1029. op_string = "-=";
  1030. break;
  1031. case AssignmentOp::MultiplicationAssignment:
  1032. op_string = "*=";
  1033. break;
  1034. case AssignmentOp::DivisionAssignment:
  1035. op_string = "/=";
  1036. break;
  1037. case AssignmentOp::ModuloAssignment:
  1038. op_string = "%=";
  1039. break;
  1040. case AssignmentOp::ExponentiationAssignment:
  1041. op_string = "**=";
  1042. break;
  1043. case AssignmentOp::BitwiseAndAssignment:
  1044. op_string = "&=";
  1045. break;
  1046. case AssignmentOp::BitwiseOrAssignment:
  1047. op_string = "|=";
  1048. break;
  1049. case AssignmentOp::BitwiseXorAssignment:
  1050. op_string = "^=";
  1051. break;
  1052. case AssignmentOp::LeftShiftAssignment:
  1053. op_string = "<<=";
  1054. break;
  1055. case AssignmentOp::RightShiftAssignment:
  1056. op_string = ">>=";
  1057. break;
  1058. case AssignmentOp::UnsignedRightShiftAssignment:
  1059. op_string = ">>>=";
  1060. break;
  1061. }
  1062. ASTNode::dump(indent);
  1063. print_indent(indent + 1);
  1064. printf("%s\n", op_string);
  1065. m_lhs->dump(indent + 1);
  1066. m_rhs->dump(indent + 1);
  1067. }
  1068. void UpdateExpression::dump(int indent) const
  1069. {
  1070. const char* op_string = nullptr;
  1071. switch (m_op) {
  1072. case UpdateOp::Increment:
  1073. op_string = "++";
  1074. break;
  1075. case UpdateOp::Decrement:
  1076. op_string = "--";
  1077. break;
  1078. }
  1079. ASTNode::dump(indent);
  1080. print_indent(indent + 1);
  1081. if (m_prefixed)
  1082. printf("%s\n", op_string);
  1083. m_argument->dump(indent + 1);
  1084. if (!m_prefixed) {
  1085. print_indent(indent + 1);
  1086. printf("%s\n", op_string);
  1087. }
  1088. }
  1089. Value VariableDeclaration::execute(Interpreter& interpreter) const
  1090. {
  1091. for (auto& declarator : m_declarations) {
  1092. if (auto* init = declarator.init()) {
  1093. auto initalizer_result = init->execute(interpreter);
  1094. if (interpreter.exception())
  1095. return {};
  1096. auto variable_name = declarator.id().string();
  1097. update_function_name(initalizer_result, variable_name);
  1098. interpreter.set_variable(variable_name, initalizer_result, true);
  1099. }
  1100. }
  1101. return js_undefined();
  1102. }
  1103. Value VariableDeclarator::execute(Interpreter&) const
  1104. {
  1105. // NOTE: This node is handled by VariableDeclaration.
  1106. ASSERT_NOT_REACHED();
  1107. }
  1108. void VariableDeclaration::dump(int indent) const
  1109. {
  1110. const char* declaration_kind_string = nullptr;
  1111. switch (m_declaration_kind) {
  1112. case DeclarationKind::Let:
  1113. declaration_kind_string = "Let";
  1114. break;
  1115. case DeclarationKind::Var:
  1116. declaration_kind_string = "Var";
  1117. break;
  1118. case DeclarationKind::Const:
  1119. declaration_kind_string = "Const";
  1120. break;
  1121. }
  1122. ASTNode::dump(indent);
  1123. print_indent(indent + 1);
  1124. printf("%s\n", declaration_kind_string);
  1125. for (auto& declarator : m_declarations)
  1126. declarator.dump(indent + 1);
  1127. }
  1128. void VariableDeclarator::dump(int indent) const
  1129. {
  1130. ASTNode::dump(indent);
  1131. m_id->dump(indent + 1);
  1132. if (m_init)
  1133. m_init->dump(indent + 1);
  1134. }
  1135. void ObjectProperty::dump(int indent) const
  1136. {
  1137. ASTNode::dump(indent);
  1138. m_key->dump(indent + 1);
  1139. m_value->dump(indent + 1);
  1140. }
  1141. void ObjectExpression::dump(int indent) const
  1142. {
  1143. ASTNode::dump(indent);
  1144. for (auto& property : m_properties) {
  1145. property.dump(indent + 1);
  1146. }
  1147. }
  1148. void ExpressionStatement::dump(int indent) const
  1149. {
  1150. ASTNode::dump(indent);
  1151. m_expression->dump(indent + 1);
  1152. }
  1153. Value ObjectProperty::execute(Interpreter&) const
  1154. {
  1155. // NOTE: ObjectProperty execution is handled by ObjectExpression.
  1156. ASSERT_NOT_REACHED();
  1157. }
  1158. Value ObjectExpression::execute(Interpreter& interpreter) const
  1159. {
  1160. auto* object = Object::create_empty(interpreter, interpreter.global_object());
  1161. for (auto& property : m_properties) {
  1162. auto key_result = property.key().execute(interpreter);
  1163. if (interpreter.exception())
  1164. return {};
  1165. if (property.type() == ObjectProperty::Type::Spread) {
  1166. if (key_result.is_array()) {
  1167. auto& array_to_spread = static_cast<Array&>(key_result.as_object());
  1168. for (auto& entry : array_to_spread.indexed_properties()) {
  1169. object->indexed_properties().append(entry.value_and_attributes(&array_to_spread).value);
  1170. if (interpreter.exception())
  1171. return {};
  1172. }
  1173. } else if (key_result.is_object()) {
  1174. auto& obj_to_spread = key_result.as_object();
  1175. for (auto& it : obj_to_spread.shape().property_table_ordered()) {
  1176. if (it.value.attributes & Attribute::Enumerable)
  1177. object->define_property(it.key, obj_to_spread.get(it.key));
  1178. }
  1179. } else if (key_result.is_string()) {
  1180. auto& str_to_spread = key_result.as_string().string();
  1181. for (size_t i = 0; i < str_to_spread.length(); i++) {
  1182. object->define_property(i, js_string(interpreter, str_to_spread.substring(i, 1)));
  1183. }
  1184. }
  1185. continue;
  1186. }
  1187. auto key = key_result.to_string(interpreter);
  1188. if (interpreter.exception())
  1189. return {};
  1190. auto value = property.value().execute(interpreter);
  1191. if (interpreter.exception())
  1192. return {};
  1193. String name = key;
  1194. if (property.type() == ObjectProperty::Type::Getter) {
  1195. name = String::format("get %s", key.characters());
  1196. } else if (property.type() == ObjectProperty::Type::Setter) {
  1197. name = String::format("set %s", key.characters());
  1198. }
  1199. update_function_name(value, name);
  1200. if (property.type() == ObjectProperty::Type::Getter || property.type() == ObjectProperty::Type::Setter) {
  1201. ASSERT(value.is_function());
  1202. Accessor* accessor { nullptr };
  1203. auto property_metadata = object->shape().lookup(key);
  1204. if (property_metadata.has_value()) {
  1205. auto existing_property = object->get_direct(property_metadata.value().offset);
  1206. if (existing_property.is_accessor())
  1207. accessor = &existing_property.as_accessor();
  1208. }
  1209. if (!accessor) {
  1210. accessor = Accessor::create(interpreter, nullptr, nullptr);
  1211. object->define_property(key, accessor, Attribute::Configurable | Attribute::Enumerable);
  1212. }
  1213. if (property.type() == ObjectProperty::Type::Getter)
  1214. accessor->set_getter(&value.as_function());
  1215. else
  1216. accessor->set_setter(&value.as_function());
  1217. } else {
  1218. object->define_property(key, value);
  1219. }
  1220. }
  1221. return object;
  1222. }
  1223. void MemberExpression::dump(int indent) const
  1224. {
  1225. print_indent(indent);
  1226. printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
  1227. m_object->dump(indent + 1);
  1228. m_property->dump(indent + 1);
  1229. }
  1230. PropertyName MemberExpression::computed_property_name(Interpreter& interpreter) const
  1231. {
  1232. if (!is_computed()) {
  1233. ASSERT(m_property->is_identifier());
  1234. return static_cast<const Identifier&>(*m_property).string();
  1235. }
  1236. auto index = m_property->execute(interpreter);
  1237. if (interpreter.exception())
  1238. return {};
  1239. ASSERT(!index.is_empty());
  1240. if (index.is_integer() && index.as_i32() >= 0)
  1241. return index.as_i32();
  1242. auto index_string = index.to_string(interpreter);
  1243. if (interpreter.exception())
  1244. return {};
  1245. return index_string;
  1246. }
  1247. String MemberExpression::to_string_approximation() const
  1248. {
  1249. String object_string = "<object>";
  1250. if (m_object->is_identifier())
  1251. object_string = static_cast<const Identifier&>(*m_object).string();
  1252. if (is_computed())
  1253. return String::format("%s[<computed>]", object_string.characters());
  1254. ASSERT(m_property->is_identifier());
  1255. return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
  1256. }
  1257. Value MemberExpression::execute(Interpreter& interpreter) const
  1258. {
  1259. auto object_value = m_object->execute(interpreter);
  1260. if (interpreter.exception())
  1261. return {};
  1262. auto* object_result = object_value.to_object(interpreter);
  1263. if (interpreter.exception())
  1264. return {};
  1265. return object_result->get(computed_property_name(interpreter)).value_or(js_undefined());
  1266. }
  1267. Value StringLiteral::execute(Interpreter& interpreter) const
  1268. {
  1269. return js_string(interpreter, m_value);
  1270. }
  1271. Value NumericLiteral::execute(Interpreter&) const
  1272. {
  1273. return Value(m_value);
  1274. }
  1275. Value BooleanLiteral::execute(Interpreter&) const
  1276. {
  1277. return Value(m_value);
  1278. }
  1279. Value NullLiteral::execute(Interpreter&) const
  1280. {
  1281. return js_null();
  1282. }
  1283. void ArrayExpression::dump(int indent) const
  1284. {
  1285. ASTNode::dump(indent);
  1286. for (auto& element : m_elements) {
  1287. if (element) {
  1288. element->dump(indent + 1);
  1289. } else {
  1290. print_indent(indent + 1);
  1291. printf("<empty>\n");
  1292. }
  1293. }
  1294. }
  1295. Value ArrayExpression::execute(Interpreter& interpreter) const
  1296. {
  1297. auto* array = Array::create(interpreter.global_object());
  1298. for (auto& element : m_elements) {
  1299. auto value = Value();
  1300. if (element) {
  1301. value = element->execute(interpreter);
  1302. if (interpreter.exception())
  1303. return {};
  1304. if (element->is_spread_expression()) {
  1305. // FIXME: Support arbitrary iterables
  1306. if (value.is_array()) {
  1307. auto& array_to_spread = static_cast<Array&>(value.as_object());
  1308. for (auto& entry : array_to_spread.indexed_properties()) {
  1309. array->indexed_properties().append(entry.value_and_attributes(&array_to_spread).value);
  1310. if (interpreter.exception())
  1311. return {};
  1312. }
  1313. continue;
  1314. }
  1315. if (value.is_string() || (value.is_object() && value.as_object().is_string_object())) {
  1316. String string_to_spread;
  1317. if (value.is_string()) {
  1318. string_to_spread = value.as_string().string();
  1319. } else {
  1320. string_to_spread = static_cast<const StringObject&>(value.as_object()).primitive_string().string();
  1321. }
  1322. for (size_t i = 0; i < string_to_spread.length(); ++i)
  1323. array->indexed_properties().append(js_string(interpreter, string_to_spread.substring(i, 1)));
  1324. continue;
  1325. }
  1326. interpreter.throw_exception<TypeError>(String::format("%s is not iterable", value.to_string_without_side_effects().characters()));
  1327. return {};
  1328. }
  1329. }
  1330. array->indexed_properties().append(value);
  1331. }
  1332. return array;
  1333. }
  1334. void TemplateLiteral::dump(int indent) const
  1335. {
  1336. ASTNode::dump(indent);
  1337. for (auto& expression : m_expressions)
  1338. expression.dump(indent + 1);
  1339. }
  1340. Value TemplateLiteral::execute(Interpreter& interpreter) const
  1341. {
  1342. StringBuilder string_builder;
  1343. for (auto& expression : m_expressions) {
  1344. auto expr = expression.execute(interpreter);
  1345. if (interpreter.exception())
  1346. return {};
  1347. auto string = expr.to_string(interpreter);
  1348. if (interpreter.exception())
  1349. return {};
  1350. string_builder.append(string);
  1351. }
  1352. return js_string(interpreter, string_builder.build());
  1353. }
  1354. void TaggedTemplateLiteral::dump(int indent) const
  1355. {
  1356. ASTNode::dump(indent);
  1357. print_indent(indent + 1);
  1358. printf("(Tag)\n");
  1359. m_tag->dump(indent + 2);
  1360. print_indent(indent + 1);
  1361. printf("(Template Literal)\n");
  1362. m_template_literal->dump(indent + 2);
  1363. }
  1364. Value TaggedTemplateLiteral::execute(Interpreter& interpreter) const
  1365. {
  1366. auto tag = m_tag->execute(interpreter);
  1367. if (interpreter.exception())
  1368. return {};
  1369. if (!tag.is_function()) {
  1370. interpreter.throw_exception<TypeError>(String::format("%s is not a function", tag.to_string_without_side_effects().characters()));
  1371. return {};
  1372. }
  1373. auto& tag_function = tag.as_function();
  1374. auto& expressions = m_template_literal->expressions();
  1375. auto* strings = Array::create(interpreter.global_object());
  1376. MarkedValueList arguments(interpreter.heap());
  1377. arguments.append(strings);
  1378. for (size_t i = 0; i < expressions.size(); ++i) {
  1379. auto value = expressions[i].execute(interpreter);
  1380. if (interpreter.exception())
  1381. return {};
  1382. // tag`${foo}` -> "", foo, "" -> tag(["", ""], foo)
  1383. // tag`foo${bar}baz${qux}` -> "foo", bar, "baz", qux, "" -> tag(["foo", "baz", ""], bar, qux)
  1384. if (i % 2 == 0) {
  1385. strings->indexed_properties().append(value);
  1386. } else {
  1387. arguments.append(value);
  1388. }
  1389. }
  1390. auto* raw_strings = Array::create(interpreter.global_object());
  1391. for (auto& raw_string : m_template_literal->raw_strings()) {
  1392. auto value = raw_string.execute(interpreter);
  1393. if (interpreter.exception())
  1394. return {};
  1395. raw_strings->indexed_properties().append(value);
  1396. }
  1397. strings->define_property("raw", raw_strings, 0);
  1398. return interpreter.call(tag_function, js_undefined(), move(arguments));
  1399. }
  1400. void TryStatement::dump(int indent) const
  1401. {
  1402. ASTNode::dump(indent);
  1403. print_indent(indent);
  1404. printf("(Block)\n");
  1405. block().dump(indent + 1);
  1406. if (handler()) {
  1407. print_indent(indent);
  1408. printf("(Handler)\n");
  1409. handler()->dump(indent + 1);
  1410. }
  1411. if (finalizer()) {
  1412. print_indent(indent);
  1413. printf("(Finalizer)\n");
  1414. finalizer()->dump(indent + 1);
  1415. }
  1416. }
  1417. void CatchClause::dump(int indent) const
  1418. {
  1419. print_indent(indent);
  1420. printf("CatchClause");
  1421. if (!m_parameter.is_null())
  1422. printf(" (%s)", m_parameter.characters());
  1423. printf("\n");
  1424. body().dump(indent + 1);
  1425. }
  1426. void ThrowStatement::dump(int indent) const
  1427. {
  1428. ASTNode::dump(indent);
  1429. argument().dump(indent + 1);
  1430. }
  1431. Value TryStatement::execute(Interpreter& interpreter) const
  1432. {
  1433. interpreter.run(block(), {}, ScopeType::Try);
  1434. if (auto* exception = interpreter.exception()) {
  1435. if (m_handler) {
  1436. interpreter.clear_exception();
  1437. ArgumentVector arguments { { m_handler->parameter(), exception->value() } };
  1438. interpreter.run(m_handler->body(), move(arguments));
  1439. }
  1440. }
  1441. if (m_finalizer)
  1442. m_finalizer->execute(interpreter);
  1443. return js_undefined();
  1444. }
  1445. Value CatchClause::execute(Interpreter&) const
  1446. {
  1447. // NOTE: CatchClause execution is handled by TryStatement.
  1448. ASSERT_NOT_REACHED();
  1449. return {};
  1450. }
  1451. Value ThrowStatement::execute(Interpreter& interpreter) const
  1452. {
  1453. auto value = m_argument->execute(interpreter);
  1454. if (interpreter.exception())
  1455. return {};
  1456. return interpreter.throw_exception(value);
  1457. }
  1458. Value SwitchStatement::execute(Interpreter& interpreter) const
  1459. {
  1460. auto discriminant_result = m_discriminant->execute(interpreter);
  1461. if (interpreter.exception())
  1462. return {};
  1463. bool falling_through = false;
  1464. for (auto& switch_case : m_cases) {
  1465. if (!falling_through && switch_case.test()) {
  1466. auto test_result = switch_case.test()->execute(interpreter);
  1467. if (interpreter.exception())
  1468. return {};
  1469. if (!strict_eq(interpreter, discriminant_result, test_result))
  1470. continue;
  1471. }
  1472. falling_through = true;
  1473. for (auto& statement : switch_case.consequent()) {
  1474. statement.execute(interpreter);
  1475. if (interpreter.exception())
  1476. return {};
  1477. if (interpreter.should_unwind()) {
  1478. if (interpreter.should_unwind_until(ScopeType::Breakable, m_label)) {
  1479. interpreter.stop_unwind();
  1480. return {};
  1481. }
  1482. return {};
  1483. }
  1484. }
  1485. }
  1486. return js_undefined();
  1487. }
  1488. Value SwitchCase::execute(Interpreter& interpreter) const
  1489. {
  1490. (void)interpreter;
  1491. return {};
  1492. }
  1493. Value BreakStatement::execute(Interpreter& interpreter) const
  1494. {
  1495. interpreter.unwind(ScopeType::Breakable, m_target_label);
  1496. return js_undefined();
  1497. }
  1498. Value ContinueStatement::execute(Interpreter& interpreter) const
  1499. {
  1500. interpreter.unwind(ScopeType::Continuable, m_target_label);
  1501. return js_undefined();
  1502. }
  1503. void SwitchStatement::dump(int indent) const
  1504. {
  1505. ASTNode::dump(indent);
  1506. m_discriminant->dump(indent + 1);
  1507. for (auto& switch_case : m_cases) {
  1508. switch_case.dump(indent + 1);
  1509. }
  1510. }
  1511. void SwitchCase::dump(int indent) const
  1512. {
  1513. ASTNode::dump(indent);
  1514. print_indent(indent + 1);
  1515. if (m_test) {
  1516. printf("(Test)\n");
  1517. m_test->dump(indent + 2);
  1518. } else {
  1519. printf("(Default)\n");
  1520. }
  1521. print_indent(indent + 1);
  1522. printf("(Consequent)\n");
  1523. for (auto& statement : m_consequent)
  1524. statement.dump(indent + 2);
  1525. }
  1526. Value ConditionalExpression::execute(Interpreter& interpreter) const
  1527. {
  1528. auto test_result = m_test->execute(interpreter);
  1529. if (interpreter.exception())
  1530. return {};
  1531. Value result;
  1532. if (test_result.to_boolean()) {
  1533. result = m_consequent->execute(interpreter);
  1534. } else {
  1535. result = m_alternate->execute(interpreter);
  1536. }
  1537. if (interpreter.exception())
  1538. return {};
  1539. return result;
  1540. }
  1541. void ConditionalExpression::dump(int indent) const
  1542. {
  1543. ASTNode::dump(indent);
  1544. print_indent(indent + 1);
  1545. printf("(Test)\n");
  1546. m_test->dump(indent + 2);
  1547. print_indent(indent + 1);
  1548. printf("(Consequent)\n");
  1549. m_consequent->dump(indent + 2);
  1550. print_indent(indent + 1);
  1551. printf("(Alternate)\n");
  1552. m_alternate->dump(indent + 2);
  1553. }
  1554. void SequenceExpression::dump(int indent) const
  1555. {
  1556. ASTNode::dump(indent);
  1557. for (auto& expression : m_expressions)
  1558. expression.dump(indent + 1);
  1559. }
  1560. Value SequenceExpression::execute(Interpreter& interpreter) const
  1561. {
  1562. Value last_value;
  1563. for (auto& expression : m_expressions) {
  1564. last_value = expression.execute(interpreter);
  1565. if (interpreter.exception())
  1566. return {};
  1567. }
  1568. return last_value;
  1569. }
  1570. Value DebuggerStatement::execute(Interpreter&) const
  1571. {
  1572. dbg() << "Sorry, no JavaScript debugger available (yet)!";
  1573. return js_undefined();
  1574. }
  1575. void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
  1576. {
  1577. m_variables.append(move(variables));
  1578. }
  1579. }