ExpressionIntepreter.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. using System;
  2. using System.Linq;
  3. using Jint.Native;
  4. using Jint.Native.Function;
  5. using Jint.Native.Number;
  6. using Jint.Native.Object;
  7. using Jint.Parser.Ast;
  8. using Jint.Runtime.Descriptors;
  9. using Jint.Runtime.Environments;
  10. using Jint.Runtime.References;
  11. namespace Jint.Runtime
  12. {
  13. public class ExpressionInterpreter
  14. {
  15. private readonly Engine _engine;
  16. public ExpressionInterpreter(Engine engine)
  17. {
  18. _engine = engine;
  19. }
  20. private object EvaluateExpression(Expression expression)
  21. {
  22. return _engine.EvaluateExpression(expression);
  23. }
  24. public object EvaluateConditionalExpression(ConditionalExpression conditionalExpression)
  25. {
  26. var test = _engine.EvaluateExpression(conditionalExpression.Test);
  27. var evaluate = TypeConverter.ToBoolean(test) ? conditionalExpression.Consequent : conditionalExpression.Alternate;
  28. return _engine.EvaluateExpression(evaluate);
  29. }
  30. public object EvaluateAssignmentExpression(AssignmentExpression assignmentExpression)
  31. {
  32. object rval = _engine.GetValue(EvaluateExpression(assignmentExpression.Right));
  33. var lref = EvaluateExpression(assignmentExpression.Left) as Reference;
  34. if (assignmentExpression.Operator == "=")
  35. {
  36. if(lref != null && lref.IsStrict() && lref.GetBase() is EnvironmentRecord && (lref.GetReferencedName() == "eval" || lref.GetReferencedName() == "arguments"))
  37. {
  38. throw new JavaScriptException(_engine.SyntaxError);
  39. }
  40. _engine.PutValue(lref, rval);
  41. return rval;
  42. }
  43. object lval = _engine.GetValue(lref);
  44. switch (assignmentExpression.Operator)
  45. {
  46. case "+=":
  47. var lprim = TypeConverter.ToPrimitive(lval);
  48. var rprim = TypeConverter.ToPrimitive(rval);
  49. if (TypeConverter.GetType(lprim) == Types.String ||
  50. TypeConverter.GetType(rprim) == Types.String)
  51. {
  52. lval = TypeConverter.ToString(lprim) + TypeConverter.ToString(rprim);
  53. }
  54. else
  55. {
  56. lval = TypeConverter.ToNumber(lprim) + TypeConverter.ToNumber(rprim);
  57. }
  58. break;
  59. case "-=":
  60. lval = TypeConverter.ToNumber(lval) - TypeConverter.ToNumber(rval);
  61. break;
  62. case "*=":
  63. if (lval == Undefined.Instance || rval == Undefined.Instance)
  64. {
  65. lval = Undefined.Instance;
  66. }
  67. else
  68. {
  69. lval = TypeConverter.ToNumber(lval) * TypeConverter.ToNumber(rval);
  70. }
  71. break;
  72. case "/=":
  73. lval = Divide(lval, rval);
  74. break;
  75. case "%=":
  76. if (lval == Undefined.Instance || rval == Undefined.Instance)
  77. {
  78. lval = Undefined.Instance;
  79. }
  80. else
  81. {
  82. lval = TypeConverter.ToNumber(lval) % TypeConverter.ToNumber(rval);
  83. }
  84. break;
  85. case "&=":
  86. lval = TypeConverter.ToInt32(lval) & TypeConverter.ToInt32(rval);
  87. break;
  88. case "|=":
  89. lval = TypeConverter.ToInt32(lval) | TypeConverter.ToInt32(rval);
  90. break;
  91. case "^=":
  92. lval = TypeConverter.ToInt32(lval) ^ TypeConverter.ToInt32(rval);
  93. break;
  94. case "<<=":
  95. lval = TypeConverter.ToInt32(lval) << (int)(TypeConverter.ToUint32(rval) & 0x1F);
  96. break;
  97. case ">>=":
  98. lval = TypeConverter.ToInt32(lval) >> (int)(TypeConverter.ToUint32(rval) & 0x1F);
  99. break;
  100. case ">>>=":
  101. lval = (uint)TypeConverter.ToInt32(lval) >> (int)(TypeConverter.ToUint32(rval) & 0x1F);
  102. break;
  103. default:
  104. throw new NotImplementedException();
  105. }
  106. _engine.PutValue(lref, lval);
  107. return lval;
  108. }
  109. private object Divide(object lval, object rval)
  110. {
  111. if (lval == Undefined.Instance || rval == Undefined.Instance)
  112. {
  113. return Undefined.Instance;
  114. }
  115. else
  116. {
  117. var rN = TypeConverter.ToNumber(rval);
  118. var lN = TypeConverter.ToNumber(lval);
  119. if (double.IsNaN(rN) || double.IsNaN(lN))
  120. {
  121. return double.NaN;
  122. }
  123. if (double.IsInfinity(lN) && double.IsInfinity(rN))
  124. {
  125. return double.NaN;
  126. }
  127. if (double.IsInfinity(lN) && rN == 0)
  128. {
  129. if (NumberInstance.IsNegativeZero(rN))
  130. {
  131. return -lN;
  132. }
  133. return lN;
  134. }
  135. if (lN == 0 && rN == 0)
  136. {
  137. return double.NaN;
  138. }
  139. if (rN == 0)
  140. {
  141. if (NumberInstance.IsNegativeZero(rN))
  142. {
  143. return lN > 0 ? -double.PositiveInfinity : -double.NegativeInfinity;
  144. }
  145. return lN > 0 ? double.PositiveInfinity : double.NegativeInfinity;
  146. }
  147. return lN/rN;
  148. }
  149. }
  150. public object EvaluateBinaryExpression(BinaryExpression expression)
  151. {
  152. object left = _engine.GetValue(EvaluateExpression(expression.Left));
  153. object right = _engine.GetValue(EvaluateExpression(expression.Right));
  154. object value;
  155. switch (expression.Operator)
  156. {
  157. case "+":
  158. var lprim = TypeConverter.ToPrimitive(left);
  159. var rprim = TypeConverter.ToPrimitive(right);
  160. if (TypeConverter.GetType(lprim) == Types.String || TypeConverter.GetType(rprim) == Types.String)
  161. {
  162. value = TypeConverter.ToString(lprim) + TypeConverter.ToString(rprim);
  163. }
  164. else
  165. {
  166. value = TypeConverter.ToNumber(lprim) + TypeConverter.ToNumber(rprim);
  167. }
  168. break;
  169. case "-":
  170. value = TypeConverter.ToNumber(left) - TypeConverter.ToNumber(right);
  171. break;
  172. case "*":
  173. if (left == Undefined.Instance || right == Undefined.Instance)
  174. {
  175. value = Undefined.Instance;
  176. }
  177. else
  178. {
  179. value = TypeConverter.ToNumber(left) * TypeConverter.ToNumber(right);
  180. }
  181. break;
  182. case "/":
  183. value = Divide(left, right);
  184. break;
  185. case "%":
  186. if (left == Undefined.Instance || right == Undefined.Instance)
  187. {
  188. value = Undefined.Instance;
  189. }
  190. else
  191. {
  192. value = TypeConverter.ToNumber(left) % TypeConverter.ToNumber(right);
  193. }
  194. break;
  195. case "==":
  196. value = Equal(left, right);
  197. break;
  198. case "!=":
  199. value = !Equal(left, right);
  200. break;
  201. case ">":
  202. value = Compare(right, left, false);
  203. if (value == Undefined.Instance)
  204. {
  205. value = false;
  206. }
  207. break;
  208. case ">=":
  209. value = Compare(left, right);
  210. if (value == Undefined.Instance || (bool) value == true)
  211. {
  212. value = false;
  213. }
  214. else
  215. {
  216. value = true;
  217. }
  218. break;
  219. case "<":
  220. value = Compare(left, right);
  221. if (value == Undefined.Instance)
  222. {
  223. value = false;
  224. }
  225. break;
  226. case "<=":
  227. value = Compare(right, left, false);
  228. if (value == Undefined.Instance || (bool) value == true)
  229. {
  230. value = false;
  231. }
  232. else
  233. {
  234. value = true;
  235. }
  236. break;
  237. case "===":
  238. return StriclyEqual(left, right);
  239. case "!==":
  240. return !StriclyEqual(left, right);
  241. case "&":
  242. return TypeConverter.ToInt32(left) & TypeConverter.ToInt32(right);
  243. case "|":
  244. return TypeConverter.ToInt32(left) | TypeConverter.ToInt32(right);
  245. case "^":
  246. return TypeConverter.ToInt32(left) ^ TypeConverter.ToInt32(right);
  247. case "<<":
  248. return TypeConverter.ToInt32(left) << (int)(TypeConverter.ToUint32(right) & 0x1F);
  249. case ">>":
  250. return TypeConverter.ToInt32(left) >> (int)(TypeConverter.ToUint32(right) & 0x1F);
  251. case ">>>":
  252. return (uint)TypeConverter.ToInt32(left) >> (int)(TypeConverter.ToUint32(right) & 0x1F);
  253. case "instanceof":
  254. var f = right as FunctionInstance;
  255. if (f == null)
  256. {
  257. throw new JavaScriptException(_engine.TypeError, "instanceof can only be used with a function object");
  258. }
  259. value = f.HasInstance(left);
  260. break;
  261. case "in":
  262. var o = right as ObjectInstance;
  263. if (o == null)
  264. {
  265. throw new JavaScriptException(_engine.TypeError, "in can only be used with an object");
  266. }
  267. value = o.HasProperty(TypeConverter.ToString(left));
  268. break;
  269. default:
  270. throw new NotImplementedException();
  271. }
  272. return value;
  273. }
  274. public object EvaluateLogicalExpression(LogicalExpression logicalExpression)
  275. {
  276. var left = _engine.GetValue(EvaluateExpression(logicalExpression.Left));
  277. switch (logicalExpression.Operator)
  278. {
  279. case "&&":
  280. if (!TypeConverter.ToBoolean(left))
  281. {
  282. return left;
  283. }
  284. return _engine.GetValue(EvaluateExpression(logicalExpression.Right));
  285. case "||":
  286. if (TypeConverter.ToBoolean(left))
  287. {
  288. return left;
  289. }
  290. return _engine.GetValue(EvaluateExpression(logicalExpression.Right));
  291. default:
  292. throw new NotImplementedException();
  293. }
  294. }
  295. public static bool Equal(object x, object y)
  296. {
  297. var typex = TypeConverter.GetType(x);
  298. var typey = TypeConverter.GetType(y);
  299. if (typex == typey)
  300. {
  301. if (typex == Types.Undefined || typex == Types.Null)
  302. {
  303. return true;
  304. }
  305. if (typex == Types.Number)
  306. {
  307. var nx = TypeConverter.ToNumber(x);
  308. var ny = TypeConverter.ToNumber(y);
  309. if (double.IsNaN(nx) || double.IsNaN(ny))
  310. {
  311. return false;
  312. }
  313. if (nx == ny)
  314. {
  315. return true;
  316. }
  317. return false;
  318. }
  319. if (typex == Types.String)
  320. {
  321. return TypeConverter.ToString(x) == TypeConverter.ToString(y);
  322. }
  323. if (typex == Types.Boolean)
  324. {
  325. return (bool) x == (bool) y;
  326. }
  327. return x == y;
  328. }
  329. if (x == Null.Instance && y == Undefined.Instance)
  330. {
  331. return true;
  332. }
  333. if (x == Undefined.Instance && y == Null.Instance)
  334. {
  335. return true;
  336. }
  337. if (typex == Types.Number && typey == Types.String)
  338. {
  339. return Equal(x, TypeConverter.ToNumber(y));
  340. }
  341. if (typex == Types.String && typey == Types.Number)
  342. {
  343. return Equal(TypeConverter.ToNumber(x), y);
  344. }
  345. if (typex == Types.Boolean)
  346. {
  347. return Equal(TypeConverter.ToNumber(x), y);
  348. }
  349. if (typey == Types.Boolean)
  350. {
  351. return Equal(x, TypeConverter.ToNumber(y));
  352. }
  353. if (typey == Types.Object && (typex == Types.String || typex == Types.Number))
  354. {
  355. return Equal(x, TypeConverter.ToPrimitive(y));
  356. }
  357. if (typex == Types.Object && (typey == Types.String || typey == Types.Number))
  358. {
  359. return Equal(TypeConverter.ToPrimitive(x), y);
  360. }
  361. return false;
  362. }
  363. public static bool StriclyEqual(object x, object y)
  364. {
  365. var typea = TypeConverter.GetType(x);
  366. var typeb = TypeConverter.GetType(y);
  367. if (typea != typeb)
  368. {
  369. return false;
  370. }
  371. if (typea == Types.Undefined || typea == Types.Null)
  372. {
  373. return true;
  374. }
  375. if (typea == Types.None)
  376. {
  377. return true;
  378. }
  379. if (typea == Types.Number)
  380. {
  381. var nx = TypeConverter.ToNumber(x);
  382. var ny = TypeConverter.ToNumber(y);
  383. if (double.IsNaN(nx) || double.IsNaN(ny))
  384. {
  385. return false;
  386. }
  387. if (nx == ny)
  388. {
  389. return true;
  390. }
  391. return false;
  392. }
  393. if (typea == Types.String)
  394. {
  395. return TypeConverter.ToString(x) == TypeConverter.ToString(y);
  396. }
  397. if (typea == Types.Boolean)
  398. {
  399. return TypeConverter.ToBoolean(x) == TypeConverter.ToBoolean(y);
  400. }
  401. return x == y;
  402. }
  403. public static bool SameValue(object x, object y)
  404. {
  405. var typea = TypeConverter.GetType(x);
  406. var typeb = TypeConverter.GetType(y);
  407. if (typea != typeb)
  408. {
  409. return false;
  410. }
  411. if (typea == Types.None)
  412. {
  413. return true;
  414. }
  415. if (typea == Types.Number)
  416. {
  417. var nx = TypeConverter.ToNumber(x);
  418. var ny = TypeConverter.ToNumber(y);
  419. if (double.IsNaN(nx) && double.IsNaN(ny))
  420. {
  421. return true;
  422. }
  423. if (nx == ny)
  424. {
  425. if (nx == 0)
  426. {
  427. // +0 !== -0
  428. return NumberInstance.IsNegativeZero(nx) == NumberInstance.IsNegativeZero(ny);
  429. }
  430. return true;
  431. }
  432. return false;
  433. }
  434. if (typea == Types.String)
  435. {
  436. return TypeConverter.ToString(x) == TypeConverter.ToString(y);
  437. }
  438. if (typea == Types.Boolean)
  439. {
  440. return TypeConverter.ToBoolean(x) == TypeConverter.ToBoolean(y);
  441. }
  442. return x == y;
  443. }
  444. public static object Compare(object x, object y, bool leftFirst = true)
  445. {
  446. object px, py;
  447. if (leftFirst)
  448. {
  449. px = TypeConverter.ToPrimitive(x, Types.Number);
  450. py = TypeConverter.ToPrimitive(y, Types.Number);
  451. }
  452. else
  453. {
  454. py = TypeConverter.ToPrimitive(y, Types.Number);
  455. px = TypeConverter.ToPrimitive(x, Types.Number);
  456. }
  457. var typea = TypeConverter.GetType(px);
  458. var typeb = TypeConverter.GetType(py);
  459. if (typea != Types.String || typeb != Types.String)
  460. {
  461. var nx = TypeConverter.ToNumber(px);
  462. var ny = TypeConverter.ToNumber(py);
  463. if (double.IsNaN(nx) || double.IsNaN(ny))
  464. {
  465. return Undefined.Instance;
  466. }
  467. if (nx == ny)
  468. {
  469. return false;
  470. }
  471. if (nx == double.PositiveInfinity)
  472. {
  473. return false;
  474. }
  475. if (ny == double.PositiveInfinity)
  476. {
  477. return true;
  478. }
  479. if (ny == double.NegativeInfinity)
  480. {
  481. return false;
  482. }
  483. if (nx == double.NegativeInfinity)
  484. {
  485. return true;
  486. }
  487. return nx < ny;
  488. }
  489. else
  490. {
  491. return String.CompareOrdinal(TypeConverter.ToString(x), TypeConverter.ToString(y)) < 0;
  492. }
  493. }
  494. public object EvaluateIdentifier(Identifier identifier)
  495. {
  496. return _engine.ExecutionContext.LexicalEnvironment.GetIdentifierReference(identifier.Name, _engine.Options.IsStrict());
  497. }
  498. public object EvaluateLiteral(Literal literal)
  499. {
  500. return literal.Value ?? Null.Instance;
  501. }
  502. public object EvaluateObjectExpression(ObjectExpression objectExpression)
  503. {
  504. // http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.5
  505. var obj = _engine.Object.Construct(Arguments.Empty);
  506. foreach (var property in objectExpression.Properties)
  507. {
  508. var propName = property.Key.GetKey();
  509. var previous = obj.GetOwnProperty(propName);
  510. PropertyDescriptor propDesc;
  511. switch (property.Kind)
  512. {
  513. case PropertyKind.Data:
  514. var exprValue = _engine.EvaluateExpression(property.Value);
  515. var propValue = _engine.GetValue(exprValue);
  516. propDesc = new DataDescriptor(propValue) {Writable=true, Enumerable=true,Configurable = true};
  517. break;
  518. case PropertyKind.Get:
  519. var getter = property.Value as FunctionExpression;
  520. if (getter == null)
  521. {
  522. throw new JavaScriptException(_engine.SyntaxError);
  523. }
  524. var get = new ScriptFunctionInstance(
  525. _engine,
  526. getter,
  527. _engine.ExecutionContext.LexicalEnvironment,
  528. getter.Strict || _engine.Options.IsStrict()
  529. );
  530. propDesc = new AccessorDescriptor(get) { Enumerable = true, Configurable = true};
  531. break;
  532. case PropertyKind.Set:
  533. var setter = property.Value as FunctionExpression;
  534. if (setter == null)
  535. {
  536. throw new JavaScriptException(_engine.SyntaxError);
  537. }
  538. var set = new ScriptFunctionInstance(
  539. _engine,
  540. setter,
  541. _engine.ExecutionContext.LexicalEnvironment,
  542. setter.Strict || _engine.Options.IsStrict()
  543. );
  544. propDesc = new AccessorDescriptor(null, set) { Enumerable = true, Configurable = true};
  545. break;
  546. default:
  547. throw new ArgumentOutOfRangeException();
  548. }
  549. if (previous != Undefined.Instance)
  550. {
  551. var previousIsData = previous.IsDataDescriptor();
  552. var previousIsAccessor = previous.IsAccessorDescriptor();
  553. var propIsData = propDesc.IsDataDescriptor();
  554. var propIsAccessor = propDesc.IsAccessorDescriptor();
  555. if (_engine.Options.IsStrict() && previousIsData && propIsData)
  556. {
  557. throw new JavaScriptException(_engine.SyntaxError);
  558. }
  559. if (previousIsData && propIsAccessor)
  560. {
  561. throw new JavaScriptException(_engine.SyntaxError);
  562. }
  563. if (previousIsAccessor && propIsData)
  564. {
  565. throw new JavaScriptException(_engine.SyntaxError);
  566. }
  567. if (previousIsAccessor && propIsAccessor)
  568. {
  569. var previousAccessor = previous.As<AccessorDescriptor>();
  570. var propAccessor = propDesc.As<AccessorDescriptor>();
  571. if (propAccessor.Set != null)
  572. {
  573. if (previousAccessor.Set != null)
  574. {
  575. throw new JavaScriptException(_engine.SyntaxError);
  576. }
  577. if (previousAccessor.Get != null)
  578. {
  579. propAccessor.Get = previousAccessor.Get;
  580. }
  581. }
  582. else if (propAccessor.Get != null)
  583. {
  584. if (previousAccessor.Get != null)
  585. {
  586. throw new JavaScriptException(_engine.SyntaxError);
  587. }
  588. if (previousAccessor.Set != null)
  589. {
  590. propAccessor.Set = previousAccessor.Set;
  591. }
  592. }
  593. }
  594. }
  595. obj.DefineOwnProperty(propName, propDesc, false);
  596. }
  597. return obj;
  598. }
  599. /// <summary>
  600. /// http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.1
  601. /// </summary>
  602. /// <param name="memberExpression"></param>
  603. /// <returns></returns>
  604. public object EvaluateMemberExpression(MemberExpression memberExpression)
  605. {
  606. var baseReference = EvaluateExpression(memberExpression.Object);
  607. var baseValue = _engine.GetValue(baseReference);
  608. string propertyNameString;
  609. if (!memberExpression.Computed) // index accessor ?
  610. {
  611. propertyNameString = memberExpression.Property.As<Identifier>().Name;
  612. }
  613. else
  614. {
  615. var propertyNameReference = EvaluateExpression(memberExpression.Property);
  616. var propertyNameValue = _engine.GetValue(propertyNameReference);
  617. TypeConverter.CheckObjectCoercible(_engine, baseValue);
  618. propertyNameString = TypeConverter.ToString(propertyNameValue);
  619. }
  620. return new Reference(baseValue, propertyNameString, _engine.Options.IsStrict());
  621. }
  622. public object EvaluateFunctionExpression(FunctionExpression functionExpression)
  623. {
  624. return new ScriptFunctionInstance(
  625. _engine,
  626. functionExpression,
  627. LexicalEnvironment.NewDeclarativeEnvironment(_engine, _engine.ExecutionContext.LexicalEnvironment),
  628. functionExpression.Strict
  629. );
  630. }
  631. public object EvaluateCallExpression(CallExpression callExpression)
  632. {
  633. var callee = EvaluateExpression(callExpression.Callee);
  634. var func = _engine.GetValue(callee);
  635. if (func == Undefined.Instance)
  636. {
  637. throw new JavaScriptException(_engine.TypeError);
  638. }
  639. object thisObject;
  640. // todo: implement as in http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.4
  641. var arguments = callExpression.Arguments.Select(EvaluateExpression).Select(_engine.GetValue).ToArray();
  642. if (TypeConverter.GetType(func) != Types.Object)
  643. {
  644. throw new JavaScriptException(_engine.TypeError);
  645. }
  646. var callable = func as ICallable;
  647. if (callable == null)
  648. {
  649. throw new JavaScriptException(_engine.TypeError);
  650. }
  651. var r = callee as Reference;
  652. if (r != null)
  653. {
  654. if (r.IsPropertyReference())
  655. {
  656. thisObject = r.GetBase();
  657. }
  658. else
  659. {
  660. var env = r.GetBase() as EnvironmentRecord;
  661. thisObject = env.ImplicitThisValue();
  662. }
  663. }
  664. else
  665. {
  666. thisObject = Undefined.Instance;
  667. }
  668. return callable.Call(thisObject, arguments);
  669. }
  670. public object EvaluateSequenceExpression(SequenceExpression sequenceExpression)
  671. {
  672. foreach (var expression in sequenceExpression.Expressions)
  673. {
  674. _engine.EvaluateExpression(expression);
  675. }
  676. return Undefined.Instance;
  677. }
  678. public object EvaluateUpdateExpression(UpdateExpression updateExpression)
  679. {
  680. var value = _engine.EvaluateExpression(updateExpression.Argument);
  681. Reference r;
  682. switch (updateExpression.Operator)
  683. {
  684. case "++":
  685. r = value as Reference;
  686. if (r != null
  687. && r.IsStrict()
  688. && (r.GetBase() is EnvironmentRecord)
  689. && (Array.IndexOf(new[] { "eval", "arguments" }, r.GetReferencedName()) != -1))
  690. {
  691. throw new JavaScriptException(_engine.SyntaxError);
  692. }
  693. var oldValue = _engine.GetValue(value);
  694. var newValue = TypeConverter.ToNumber(oldValue) + 1;
  695. _engine.PutValue(r, newValue);
  696. return updateExpression.Prefix ? newValue : oldValue;
  697. case "--":
  698. r = value as Reference;
  699. if (r != null
  700. && r.IsStrict()
  701. && (r.GetBase() is EnvironmentRecord)
  702. && (Array.IndexOf(new[] { "eval", "arguments" }, r.GetReferencedName()) != -1))
  703. {
  704. throw new JavaScriptException(_engine.SyntaxError);
  705. }
  706. oldValue = _engine.GetValue(value);
  707. newValue = TypeConverter.ToNumber(oldValue) - 1;
  708. _engine.PutValue(r, newValue);
  709. return updateExpression.Prefix ? newValue : oldValue;
  710. default:
  711. throw new ArgumentException();
  712. }
  713. }
  714. public object EvaluateThisExpression(ThisExpression thisExpression)
  715. {
  716. return _engine.ExecutionContext.ThisBinding;
  717. }
  718. public object EvaluateNewExpression(NewExpression newExpression)
  719. {
  720. var arguments = newExpression.Arguments.Select(EvaluateExpression).Select(_engine.GetValue).ToArray();
  721. // todo: optimize by defining a common abstract class or interface
  722. var callee = _engine.GetValue(EvaluateExpression(newExpression.Callee)) as IConstructor;
  723. if (callee == null)
  724. {
  725. throw new JavaScriptException(_engine.TypeError, "The object can't be used as constructor.");
  726. }
  727. // construct the new instance using the Function's constructor method
  728. var instance = callee.Construct(arguments);
  729. return instance;
  730. }
  731. public object EvaluateArrayExpression(ArrayExpression arrayExpression)
  732. {
  733. var a = _engine.Array.Construct(new object[] { arrayExpression.Elements.Count() });
  734. var n = 0;
  735. foreach (var expr in arrayExpression.Elements)
  736. {
  737. if (expr != null)
  738. {
  739. var value = _engine.GetValue(EvaluateExpression(expr));
  740. a.DefineOwnProperty(n.ToString(),
  741. new DataDescriptor(value) {Writable = true, Enumerable = true, Configurable = true}, false);
  742. }
  743. n++;
  744. }
  745. return a;
  746. }
  747. public object EvaluateUnaryExpression(UnaryExpression unaryExpression)
  748. {
  749. var value = _engine.EvaluateExpression(unaryExpression.Argument);
  750. Reference r;
  751. switch (unaryExpression.Operator)
  752. {
  753. case "+":
  754. return TypeConverter.ToNumber(_engine.GetValue(value));
  755. case "-":
  756. var n = TypeConverter.ToNumber(_engine.GetValue(value));
  757. return double.IsNaN(n) ? double.NaN : n*-1;
  758. case "~":
  759. return ~TypeConverter.ToInt32(_engine.GetValue(value));
  760. case "!":
  761. return !TypeConverter.ToBoolean(_engine.GetValue(value));
  762. case "delete":
  763. r = value as Reference;
  764. if (r == null)
  765. {
  766. return true;
  767. }
  768. if (r.IsUnresolvableReference())
  769. {
  770. if (r.IsStrict())
  771. {
  772. throw new JavaScriptException(_engine.SyntaxError);
  773. }
  774. return true;
  775. }
  776. if (r.IsPropertyReference())
  777. {
  778. var o = TypeConverter.ToObject(_engine, r.GetBase());
  779. return o.Delete(r.GetReferencedName(), r.IsStrict());
  780. }
  781. if (r.IsStrict())
  782. {
  783. throw new JavaScriptException(_engine.SyntaxError);
  784. }
  785. var bindings = r.GetBase() as EnvironmentRecord;
  786. return bindings.DeleteBinding(r.GetReferencedName());
  787. case "void":
  788. _engine.GetValue(value);
  789. return Undefined.Instance;
  790. case "typeof":
  791. r = value as Reference;
  792. if (r != null)
  793. {
  794. if (r.IsUnresolvableReference())
  795. {
  796. return "undefined";
  797. }
  798. }
  799. var v = _engine.GetValue(value);
  800. if (v == Undefined.Instance)
  801. {
  802. return "undefined";
  803. }
  804. if (v == Null.Instance)
  805. {
  806. return "object";
  807. }
  808. switch (TypeConverter.GetType(v))
  809. {
  810. case Types.Boolean: return "boolean";
  811. case Types.Number: return "number";
  812. case Types.String: return "string";
  813. }
  814. if (v is ICallable)
  815. {
  816. return "function";
  817. }
  818. return "object";
  819. default:
  820. throw new ArgumentException();
  821. }
  822. }
  823. }
  824. }