Engine.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using Esprima;
  5. using Esprima.Ast;
  6. using Jint.Native;
  7. using Jint.Native.Array;
  8. using Jint.Native.Boolean;
  9. using Jint.Native.Date;
  10. using Jint.Native.Error;
  11. using Jint.Native.Function;
  12. using Jint.Native.Global;
  13. using Jint.Native.Iterator;
  14. using Jint.Native.Json;
  15. using Jint.Native.Map;
  16. using Jint.Native.Math;
  17. using Jint.Native.Number;
  18. using Jint.Native.Object;
  19. using Jint.Native.RegExp;
  20. using Jint.Native.Set;
  21. using Jint.Native.String;
  22. using Jint.Native.Symbol;
  23. using Jint.Pooling;
  24. using Jint.Runtime;
  25. using Jint.Runtime.CallStack;
  26. using Jint.Runtime.Debugger;
  27. using Jint.Runtime.Descriptors;
  28. using Jint.Runtime.Environments;
  29. using Jint.Runtime.Interop;
  30. using Jint.Runtime.Interpreter;
  31. using Jint.Runtime.References;
  32. namespace Jint
  33. {
  34. public sealed class Engine
  35. {
  36. private static readonly ParserOptions DefaultParserOptions = new ParserOptions
  37. {
  38. AdaptRegexp = true,
  39. Tolerant = false,
  40. Loc = true
  41. };
  42. private readonly ExecutionContextStack _executionContexts;
  43. private JsValue _completionValue = JsValue.Undefined;
  44. private int _statementsCount;
  45. private long _initialMemoryUsage;
  46. private long _timeoutTicks;
  47. internal INode _lastSyntaxNode;
  48. // cached access
  49. private readonly bool _isDebugMode;
  50. internal readonly bool _isStrict;
  51. private readonly int _maxStatements;
  52. private readonly long _memoryLimit;
  53. internal readonly bool _runBeforeStatementChecks;
  54. internal readonly IReferenceResolver _referenceResolver;
  55. internal readonly ReferencePool _referencePool;
  56. internal readonly ArgumentsInstancePool _argumentsInstancePool;
  57. internal readonly JsValueArrayPool _jsValueArrayPool;
  58. public ITypeConverter ClrTypeConverter;
  59. // cache of types used when resolving CLR type names
  60. internal Dictionary<string, Type> TypeCache = new Dictionary<string, Type>();
  61. internal static Dictionary<Type, Func<Engine, object, JsValue>> TypeMappers = new Dictionary<Type, Func<Engine, object, JsValue>>
  62. {
  63. { typeof(bool), (Engine engine, object v) => (bool) v ? JsBoolean.True : JsBoolean.False },
  64. { typeof(byte), (Engine engine, object v) => JsNumber.Create((byte)v) },
  65. { typeof(char), (Engine engine, object v) => JsString.Create((char)v) },
  66. { typeof(DateTime), (Engine engine, object v) => engine.Date.Construct((DateTime)v) },
  67. { typeof(DateTimeOffset), (Engine engine, object v) => engine.Date.Construct((DateTimeOffset)v) },
  68. { typeof(decimal), (Engine engine, object v) => (JsValue) (double)(decimal)v },
  69. { typeof(double), (Engine engine, object v) => (JsValue)(double)v },
  70. { typeof(Int16), (Engine engine, object v) => JsNumber.Create((Int16)v) },
  71. { typeof(Int32), (Engine engine, object v) => JsNumber.Create((Int32)v) },
  72. { typeof(Int64), (Engine engine, object v) => (JsValue)(Int64)v },
  73. { typeof(SByte), (Engine engine, object v) => JsNumber.Create((SByte)v) },
  74. { typeof(Single), (Engine engine, object v) => (JsValue)(Single)v },
  75. { typeof(string), (Engine engine, object v) => (JsValue) (string)v },
  76. { typeof(UInt16), (Engine engine, object v) => JsNumber.Create((UInt16)v) },
  77. { typeof(UInt32), (Engine engine, object v) => JsNumber.Create((UInt32)v) },
  78. { typeof(UInt64), (Engine engine, object v) => JsNumber.Create((UInt64)v) },
  79. { typeof(System.Text.RegularExpressions.Regex), (Engine engine, object v) => engine.RegExp.Construct((System.Text.RegularExpressions.Regex)v, "") }
  80. };
  81. internal struct ClrPropertyDescriptorFactoriesKey : IEquatable<ClrPropertyDescriptorFactoriesKey>
  82. {
  83. public ClrPropertyDescriptorFactoriesKey(Type type, string propertyName)
  84. {
  85. Type = type;
  86. PropertyName = propertyName;
  87. }
  88. internal readonly Type Type;
  89. internal readonly string PropertyName;
  90. public bool Equals(ClrPropertyDescriptorFactoriesKey other)
  91. {
  92. return Type == other.Type && PropertyName == other.PropertyName;
  93. }
  94. public override bool Equals(object obj)
  95. {
  96. if (ReferenceEquals(null, obj))
  97. {
  98. return false;
  99. }
  100. return obj is ClrPropertyDescriptorFactoriesKey other && Equals(other);
  101. }
  102. public override int GetHashCode()
  103. {
  104. unchecked
  105. {
  106. return (Type.GetHashCode() * 397) ^ PropertyName.GetHashCode();
  107. }
  108. }
  109. }
  110. internal readonly Dictionary<ClrPropertyDescriptorFactoriesKey, Func<Engine, object, PropertyDescriptor>> ClrPropertyDescriptorFactories =
  111. new Dictionary<ClrPropertyDescriptorFactoriesKey, Func<Engine, object, PropertyDescriptor>>();
  112. internal JintCallStack CallStack = new JintCallStack();
  113. static Engine()
  114. {
  115. var methodInfo = typeof(GC).GetMethod("GetAllocatedBytesForCurrentThread");
  116. if (methodInfo != null)
  117. {
  118. GetAllocatedBytesForCurrentThread = (Func<long>)Delegate.CreateDelegate(typeof(Func<long>), null, methodInfo);
  119. }
  120. }
  121. public Engine() : this(null)
  122. {
  123. }
  124. public Engine(Action<Options> options)
  125. {
  126. _executionContexts = new ExecutionContextStack();
  127. Global = GlobalObject.CreateGlobalObject(this);
  128. Object = ObjectConstructor.CreateObjectConstructor(this);
  129. Function = FunctionConstructor.CreateFunctionConstructor(this);
  130. Symbol = SymbolConstructor.CreateSymbolConstructor(this);
  131. Array = ArrayConstructor.CreateArrayConstructor(this);
  132. Map = MapConstructor.CreateMapConstructor(this);
  133. Set = SetConstructor.CreateSetConstructor(this);
  134. Iterator= IteratorConstructor.CreateIteratorConstructor(this);
  135. String = StringConstructor.CreateStringConstructor(this);
  136. RegExp = RegExpConstructor.CreateRegExpConstructor(this);
  137. Number = NumberConstructor.CreateNumberConstructor(this);
  138. Boolean = BooleanConstructor.CreateBooleanConstructor(this);
  139. Date = DateConstructor.CreateDateConstructor(this);
  140. Math = MathInstance.CreateMathObject(this);
  141. Json = JsonInstance.CreateJsonObject(this);
  142. Error = ErrorConstructor.CreateErrorConstructor(this, "Error");
  143. EvalError = ErrorConstructor.CreateErrorConstructor(this, "EvalError");
  144. RangeError = ErrorConstructor.CreateErrorConstructor(this, "RangeError");
  145. ReferenceError = ErrorConstructor.CreateErrorConstructor(this, "ReferenceError");
  146. SyntaxError = ErrorConstructor.CreateErrorConstructor(this, "SyntaxError");
  147. TypeError = ErrorConstructor.CreateErrorConstructor(this, "TypeError");
  148. UriError = ErrorConstructor.CreateErrorConstructor(this, "URIError");
  149. GlobalSymbolRegistry = new GlobalSymbolRegistry();
  150. // Because the properties might need some of the built-in object
  151. // their configuration is delayed to a later step
  152. Global.Configure();
  153. Object.Configure();
  154. Object.PrototypeObject.Configure();
  155. Symbol.Configure();
  156. Symbol.PrototypeObject.Configure();
  157. Function.Configure();
  158. Function.PrototypeObject.Configure();
  159. Array.Configure();
  160. Array.PrototypeObject.Configure();
  161. Map.Configure();
  162. Map.PrototypeObject.Configure();
  163. Set.Configure();
  164. Set.PrototypeObject.Configure();
  165. Iterator.Configure();
  166. Iterator.PrototypeObject.Configure();
  167. String.Configure();
  168. String.PrototypeObject.Configure();
  169. RegExp.Configure();
  170. RegExp.PrototypeObject.Configure();
  171. Number.Configure();
  172. Number.PrototypeObject.Configure();
  173. Boolean.Configure();
  174. Boolean.PrototypeObject.Configure();
  175. Date.Configure();
  176. Date.PrototypeObject.Configure();
  177. Math.Configure();
  178. Json.Configure();
  179. Error.Configure();
  180. Error.PrototypeObject.Configure();
  181. // create the global environment http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.3
  182. GlobalEnvironment = LexicalEnvironment.NewObjectEnvironment(this, Global, null, false);
  183. // create the global execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.1.1
  184. EnterExecutionContext(GlobalEnvironment, GlobalEnvironment, Global);
  185. Options = new Options();
  186. options?.Invoke(Options);
  187. // gather some options as fields for faster checks
  188. _isDebugMode = Options.IsDebugMode;
  189. _isStrict = Options.IsStrict;
  190. _maxStatements = Options._MaxStatements;
  191. _referenceResolver = Options.ReferenceResolver;
  192. _memoryLimit = Options._MemoryLimit;
  193. _runBeforeStatementChecks = (_maxStatements > 0 &&_maxStatements < int.MaxValue)
  194. || Options._TimeoutInterval.Ticks > 0
  195. || _memoryLimit > 0
  196. || _isDebugMode;
  197. _referencePool = new ReferencePool();
  198. _argumentsInstancePool = new ArgumentsInstancePool(this);
  199. _jsValueArrayPool = new JsValueArrayPool();
  200. Eval = new EvalFunctionInstance(this, System.ArrayExt.Empty<string>(), LexicalEnvironment.NewDeclarativeEnvironment(this, ExecutionContext.LexicalEnvironment), StrictModeScope.IsStrictModeCode);
  201. Global.FastAddProperty("eval", Eval, true, false, true);
  202. if (Options._IsClrAllowed)
  203. {
  204. Global.FastAddProperty("System", new NamespaceReference(this, "System"), false, false, false);
  205. Global.FastAddProperty("importNamespace", new ClrFunctionInstance(
  206. this,
  207. "importNamespace",
  208. (thisObj, arguments) => new NamespaceReference(this, TypeConverter.ToString(arguments.At(0)))), false, false, false);
  209. }
  210. ClrTypeConverter = new DefaultTypeConverter(this);
  211. BreakPoints = new List<BreakPoint>();
  212. DebugHandler = new DebugHandler(this);
  213. }
  214. public LexicalEnvironment GlobalEnvironment { get; }
  215. public GlobalObject Global { get; }
  216. public ObjectConstructor Object { get; }
  217. public FunctionConstructor Function { get; }
  218. public ArrayConstructor Array { get; }
  219. public MapConstructor Map { get; }
  220. public SetConstructor Set { get; }
  221. public IteratorConstructor Iterator { get; }
  222. public StringConstructor String { get; }
  223. public RegExpConstructor RegExp { get; }
  224. public BooleanConstructor Boolean { get; }
  225. public NumberConstructor Number { get; }
  226. public DateConstructor Date { get; }
  227. public MathInstance Math { get; }
  228. public JsonInstance Json { get; }
  229. public SymbolConstructor Symbol { get; }
  230. public EvalFunctionInstance Eval { get; }
  231. public ErrorConstructor Error { get; }
  232. public ErrorConstructor EvalError { get; }
  233. public ErrorConstructor SyntaxError { get; }
  234. public ErrorConstructor TypeError { get; }
  235. public ErrorConstructor RangeError { get; }
  236. public ErrorConstructor ReferenceError { get; }
  237. public ErrorConstructor UriError { get; }
  238. public ref readonly ExecutionContext ExecutionContext
  239. {
  240. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  241. get { return ref _executionContexts.Peek(); }
  242. }
  243. public GlobalSymbolRegistry GlobalSymbolRegistry { get; }
  244. internal Options Options { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; }
  245. #region Debugger
  246. public delegate StepMode DebugStepDelegate(object sender, DebugInformation e);
  247. public delegate StepMode BreakDelegate(object sender, DebugInformation e);
  248. public event DebugStepDelegate Step;
  249. public event BreakDelegate Break;
  250. internal DebugHandler DebugHandler { get; private set; }
  251. public List<BreakPoint> BreakPoints { get; private set; }
  252. internal StepMode? InvokeStepEvent(DebugInformation info)
  253. {
  254. return Step?.Invoke(this, info);
  255. }
  256. internal StepMode? InvokeBreakEvent(DebugInformation info)
  257. {
  258. return Break?.Invoke(this, info);
  259. }
  260. #endregion
  261. private static readonly Func<long> GetAllocatedBytesForCurrentThread;
  262. public void EnterExecutionContext(
  263. LexicalEnvironment lexicalEnvironment,
  264. LexicalEnvironment variableEnvironment,
  265. JsValue thisBinding)
  266. {
  267. var context = new ExecutionContext(
  268. lexicalEnvironment,
  269. variableEnvironment,
  270. thisBinding);
  271. _executionContexts.Push(context);
  272. }
  273. public Engine SetValue(string name, Delegate value)
  274. {
  275. Global.FastAddProperty(name, new DelegateWrapper(this, value), true, false, true);
  276. return this;
  277. }
  278. public Engine SetValue(string name, string value)
  279. {
  280. return SetValue(name, (JsValue) value);
  281. }
  282. public Engine SetValue(string name, double value)
  283. {
  284. return SetValue(name, JsNumber.Create(value));
  285. }
  286. public Engine SetValue(string name, int value)
  287. {
  288. return SetValue(name, JsNumber.Create(value));
  289. }
  290. public Engine SetValue(string name, bool value)
  291. {
  292. return SetValue(name, value ? JsBoolean.True : JsBoolean.False);
  293. }
  294. public Engine SetValue(string name, JsValue value)
  295. {
  296. Global.Put(name, value, false);
  297. return this;
  298. }
  299. public Engine SetValue(string name, object obj)
  300. {
  301. return SetValue(name, JsValue.FromObject(this, obj));
  302. }
  303. public void LeaveExecutionContext()
  304. {
  305. _executionContexts.Pop();
  306. }
  307. /// <summary>
  308. /// Initializes the statements count
  309. /// </summary>
  310. public void ResetStatementsCount()
  311. {
  312. _statementsCount = 0;
  313. }
  314. public void ResetMemoryUsage()
  315. {
  316. if (GetAllocatedBytesForCurrentThread != null)
  317. {
  318. _initialMemoryUsage = GetAllocatedBytesForCurrentThread();
  319. }
  320. }
  321. public void ResetTimeoutTicks()
  322. {
  323. var timeoutIntervalTicks = Options._TimeoutInterval.Ticks;
  324. _timeoutTicks = timeoutIntervalTicks > 0 ? DateTime.UtcNow.Ticks + timeoutIntervalTicks : 0;
  325. }
  326. /// <summary>
  327. /// Initializes list of references of called functions
  328. /// </summary>
  329. public void ResetCallStack()
  330. {
  331. CallStack.Clear();
  332. }
  333. public Engine Execute(string source)
  334. {
  335. return Execute(source, DefaultParserOptions);
  336. }
  337. public Engine Execute(string source, ParserOptions parserOptions)
  338. {
  339. var parser = new JavaScriptParser(source, parserOptions);
  340. return Execute(parser.ParseProgram());
  341. }
  342. public Engine Execute(Program program)
  343. {
  344. ResetStatementsCount();
  345. if (_memoryLimit > 0)
  346. {
  347. ResetMemoryUsage();
  348. }
  349. ResetTimeoutTicks();
  350. ResetLastStatement();
  351. ResetCallStack();
  352. using (new StrictModeScope(_isStrict || program.Strict))
  353. {
  354. DeclarationBindingInstantiation(
  355. DeclarationBindingType.GlobalCode,
  356. program.HoistingScope.FunctionDeclarations,
  357. program.HoistingScope.VariableDeclarations,
  358. functionInstance: null,
  359. arguments: null);
  360. var list = new JintStatementList(this, null, program.Body);
  361. var result = list.Execute();
  362. if (result.Type == CompletionType.Throw)
  363. {
  364. var ex = new JavaScriptException(result.GetValueOrDefault()).SetCallstack(this, result.Location);
  365. throw ex;
  366. }
  367. _completionValue = result.GetValueOrDefault();
  368. }
  369. return this;
  370. }
  371. private void ResetLastStatement()
  372. {
  373. _lastSyntaxNode = null;
  374. }
  375. /// <summary>
  376. /// Gets the last evaluated statement completion value
  377. /// </summary>
  378. public JsValue GetCompletionValue()
  379. {
  380. return _completionValue;
  381. }
  382. internal void RunBeforeExecuteStatementChecks(Statement statement)
  383. {
  384. if (_maxStatements > 0 && _statementsCount++ > _maxStatements)
  385. {
  386. ExceptionHelper.ThrowStatementsCountOverflowException();
  387. }
  388. if (_timeoutTicks > 0 && _timeoutTicks < DateTime.UtcNow.Ticks)
  389. {
  390. ExceptionHelper.ThrowTimeoutException();
  391. }
  392. if (_memoryLimit > 0)
  393. {
  394. if (GetAllocatedBytesForCurrentThread != null)
  395. {
  396. var memoryUsage = GetAllocatedBytesForCurrentThread() - _initialMemoryUsage;
  397. if (memoryUsage > _memoryLimit)
  398. {
  399. ExceptionHelper.ThrowMemoryLimitExceededException($"Script has allocated {memoryUsage} but is limited to {_memoryLimit}");
  400. }
  401. }
  402. else
  403. {
  404. ExceptionHelper.ThrowPlatformNotSupportedException("The current platform doesn't support MemoryLimit.");
  405. }
  406. }
  407. if (_isDebugMode)
  408. {
  409. DebugHandler.OnStep(statement);
  410. }
  411. }
  412. /// <summary>
  413. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.7.1
  414. /// </summary>
  415. public JsValue GetValue(object value)
  416. {
  417. return GetValue(value, false);
  418. }
  419. internal JsValue GetValue(object value, bool returnReferenceToPool)
  420. {
  421. if (value is JsValue jsValue)
  422. {
  423. return jsValue;
  424. }
  425. if (!(value is Reference reference))
  426. {
  427. return ((Completion) value).Value;
  428. }
  429. return GetValue(reference, returnReferenceToPool);
  430. }
  431. internal JsValue GetValue(Reference reference, bool returnReferenceToPool)
  432. {
  433. if (reference._baseValue._type == Types.Undefined)
  434. {
  435. if (_referenceResolver != null &&
  436. _referenceResolver.TryUnresolvableReference(this, reference, out JsValue val))
  437. {
  438. return val;
  439. }
  440. ExceptionHelper.ThrowReferenceError(this, reference);
  441. }
  442. var baseValue = reference._baseValue;
  443. if (reference.IsPropertyReference())
  444. {
  445. if (_referenceResolver != null &&
  446. _referenceResolver.TryPropertyReference(this, reference, ref baseValue))
  447. {
  448. return baseValue;
  449. }
  450. var referencedName = reference._name;
  451. if (returnReferenceToPool)
  452. {
  453. _referencePool.Return(reference);
  454. }
  455. if (reference._baseValue._type == Types.Object)
  456. {
  457. var o = TypeConverter.ToObject(this, baseValue);
  458. var v = o.Get(referencedName);
  459. return v;
  460. }
  461. else
  462. {
  463. var o = TypeConverter.ToObject(this, baseValue);
  464. var desc = o.GetProperty(referencedName);
  465. if (desc == PropertyDescriptor.Undefined)
  466. {
  467. return JsValue.Undefined;
  468. }
  469. if (desc.IsDataDescriptor())
  470. {
  471. return desc.Value;
  472. }
  473. var getter = desc.Get;
  474. if (getter.IsUndefined())
  475. {
  476. return Undefined.Instance;
  477. }
  478. var callable = (ICallable) getter.AsObject();
  479. return callable.Call(baseValue, Arguments.Empty);
  480. }
  481. }
  482. if (!(baseValue is EnvironmentRecord record))
  483. {
  484. return ExceptionHelper.ThrowArgumentException<JsValue>();
  485. }
  486. var bindingValue = record.GetBindingValue(reference._name, reference._strict);
  487. if (returnReferenceToPool)
  488. {
  489. _referencePool.Return(reference);
  490. }
  491. return bindingValue;
  492. }
  493. /// <summary>
  494. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.7.2
  495. /// </summary>
  496. public void PutValue(Reference reference, JsValue value)
  497. {
  498. if (reference._baseValue._type == Types.Undefined)
  499. {
  500. if (reference._strict)
  501. {
  502. ExceptionHelper.ThrowReferenceError(this, reference);
  503. }
  504. Global.Put(reference._name, value, false);
  505. }
  506. else if (reference.IsPropertyReference())
  507. {
  508. var baseValue = reference._baseValue;
  509. if (reference._baseValue._type == Types.Object || reference._baseValue._type == Types.None)
  510. {
  511. ((ObjectInstance) baseValue).Put(reference._name, value, reference._strict);
  512. }
  513. else
  514. {
  515. PutPrimitiveBase(baseValue, reference._name, value, reference._strict);
  516. }
  517. }
  518. else
  519. {
  520. var baseValue = reference._baseValue;
  521. ((EnvironmentRecord) baseValue).SetMutableBinding(reference._name, value, reference._strict);
  522. }
  523. }
  524. /// <summary>
  525. /// Used by PutValue when the reference has a primitive base value
  526. /// </summary>
  527. public void PutPrimitiveBase(JsValue b, string name, JsValue value, bool throwOnError)
  528. {
  529. var o = TypeConverter.ToObject(this, b);
  530. if (!o.CanPut(name))
  531. {
  532. if (throwOnError)
  533. {
  534. ExceptionHelper.ThrowTypeError(this);
  535. }
  536. return;
  537. }
  538. var ownDesc = o.GetOwnProperty(name);
  539. if (ownDesc.IsDataDescriptor())
  540. {
  541. if (throwOnError)
  542. {
  543. ExceptionHelper.ThrowTypeError(this);
  544. }
  545. return;
  546. }
  547. var desc = o.GetProperty(name);
  548. if (desc.IsAccessorDescriptor())
  549. {
  550. var setter = (ICallable)desc.Set.AsObject();
  551. setter.Call(b, new[] { value });
  552. }
  553. else
  554. {
  555. if (throwOnError)
  556. {
  557. ExceptionHelper.ThrowTypeError(this);
  558. }
  559. }
  560. }
  561. /// <summary>
  562. /// Invoke the current value as function.
  563. /// </summary>
  564. /// <param name="propertyName">The name of the function to call.</param>
  565. /// <param name="arguments">The arguments of the function call.</param>
  566. /// <returns>The value returned by the function call.</returns>
  567. public JsValue Invoke(string propertyName, params object[] arguments)
  568. {
  569. return Invoke(propertyName, null, arguments);
  570. }
  571. /// <summary>
  572. /// Invoke the current value as function.
  573. /// </summary>
  574. /// <param name="propertyName">The name of the function to call.</param>
  575. /// <param name="thisObj">The this value inside the function call.</param>
  576. /// <param name="arguments">The arguments of the function call.</param>
  577. /// <returns>The value returned by the function call.</returns>
  578. public JsValue Invoke(string propertyName, object thisObj, object[] arguments)
  579. {
  580. var value = GetValue(propertyName);
  581. return Invoke(value, thisObj, arguments);
  582. }
  583. /// <summary>
  584. /// Invoke the current value as function.
  585. /// </summary>
  586. /// <param name="value">The function to call.</param>
  587. /// <param name="arguments">The arguments of the function call.</param>
  588. /// <returns>The value returned by the function call.</returns>
  589. public JsValue Invoke(JsValue value, params object[] arguments)
  590. {
  591. return Invoke(value, null, arguments);
  592. }
  593. /// <summary>
  594. /// Invoke the current value as function.
  595. /// </summary>
  596. /// <param name="value">The function to call.</param>
  597. /// <param name="thisObj">The this value inside the function call.</param>
  598. /// <param name="arguments">The arguments of the function call.</param>
  599. /// <returns>The value returned by the function call.</returns>
  600. public JsValue Invoke(JsValue value, object thisObj, object[] arguments)
  601. {
  602. var callable = value as ICallable ?? ExceptionHelper.ThrowArgumentException<ICallable>("Can only invoke functions");
  603. var items = _jsValueArrayPool.RentArray(arguments.Length);
  604. for (int i = 0; i < arguments.Length; ++i)
  605. {
  606. items[i] = JsValue.FromObject(this, arguments[i]);
  607. }
  608. var result = callable.Call(JsValue.FromObject(this, thisObj), items);
  609. _jsValueArrayPool.ReturnArray(items);
  610. return result;
  611. }
  612. /// <summary>
  613. /// Gets a named value from the Global scope.
  614. /// </summary>
  615. /// <param name="propertyName">The name of the property to return.</param>
  616. public JsValue GetValue(string propertyName)
  617. {
  618. return GetValue(Global, propertyName);
  619. }
  620. /// <summary>
  621. /// Gets the last evaluated <see cref="INode"/>.
  622. /// </summary>
  623. public INode GetLastSyntaxNode()
  624. {
  625. return _lastSyntaxNode;
  626. }
  627. /// <summary>
  628. /// Gets a named value from the specified scope.
  629. /// </summary>
  630. /// <param name="scope">The scope to get the property from.</param>
  631. /// <param name="propertyName">The name of the property to return.</param>
  632. public JsValue GetValue(JsValue scope, string propertyName)
  633. {
  634. AssertNotNullOrEmpty(nameof(propertyName), propertyName);
  635. var reference = _referencePool.Rent(scope, propertyName, _isStrict);
  636. var jsValue = GetValue(reference, false);
  637. _referencePool.Return(reference);
  638. return jsValue;
  639. }
  640. // http://www.ecma-international.org/ecma-262/5.1/#sec-10.5
  641. internal bool DeclarationBindingInstantiation(
  642. DeclarationBindingType declarationBindingType,
  643. List<FunctionDeclaration> functionDeclarations,
  644. List<VariableDeclaration> variableDeclarations,
  645. FunctionInstance functionInstance,
  646. JsValue[] arguments)
  647. {
  648. var env = ExecutionContext.VariableEnvironment._record;
  649. bool configurableBindings = declarationBindingType == DeclarationBindingType.EvalCode;
  650. var strict = StrictModeScope.IsStrictModeCode;
  651. var der = env as DeclarativeEnvironmentRecord;
  652. bool canReleaseArgumentsInstance = false;
  653. if (declarationBindingType == DeclarationBindingType.FunctionCode)
  654. {
  655. var argsObj = _argumentsInstancePool.Rent(functionInstance, functionInstance._formalParameters, arguments, env, strict);
  656. canReleaseArgumentsInstance = true;
  657. var functionDeclaration = (functionInstance as ScriptFunctionInstance)?.FunctionDeclaration;
  658. if (!ReferenceEquals(der, null))
  659. {
  660. der.AddFunctionParameters(functionInstance, arguments, argsObj, functionDeclaration);
  661. }
  662. else
  663. {
  664. // slow path
  665. var parameters = functionInstance._formalParameters;
  666. for (var i = 0; i < parameters.Length; i++)
  667. {
  668. var argName = parameters[i];
  669. var v = i + 1 > arguments.Length ? Undefined.Instance : arguments[i];
  670. v = DeclarativeEnvironmentRecord.HandleAssignmentPatternIfNeeded(functionDeclaration, v, i);
  671. var argAlreadyDeclared = env.HasBinding(argName);
  672. if (!argAlreadyDeclared)
  673. {
  674. env.CreateMutableBinding(argName, v);
  675. }
  676. env.SetMutableBinding(argName, v, strict);
  677. }
  678. env.CreateMutableBinding("arguments", argsObj);
  679. }
  680. }
  681. if (functionDeclarations.Count > 0)
  682. {
  683. AddFunctionDeclarations(functionDeclarations, env, configurableBindings, strict);
  684. }
  685. if (variableDeclarations.Count == 0)
  686. {
  687. return canReleaseArgumentsInstance;
  688. }
  689. // process all variable declarations in the current parser scope
  690. if (!ReferenceEquals(der, null))
  691. {
  692. der.AddVariableDeclarations(variableDeclarations);
  693. }
  694. else
  695. {
  696. // slow path
  697. var variableDeclarationsCount = variableDeclarations.Count;
  698. for (var i = 0; i < variableDeclarationsCount; i++)
  699. {
  700. var variableDeclaration = variableDeclarations[i];
  701. var declarationsCount = variableDeclaration.Declarations.Count;
  702. for (var j = 0; j < declarationsCount; j++)
  703. {
  704. var d = variableDeclaration.Declarations[j];
  705. var dn = ((Identifier) d.Id).Name;
  706. var varAlreadyDeclared = env.HasBinding(dn);
  707. if (!varAlreadyDeclared)
  708. {
  709. env.CreateMutableBinding(dn, Undefined.Instance);
  710. }
  711. }
  712. }
  713. }
  714. return canReleaseArgumentsInstance;
  715. }
  716. private void AddFunctionDeclarations(List<FunctionDeclaration> functionDeclarations, EnvironmentRecord env, bool configurableBindings, bool strict)
  717. {
  718. var functionDeclarationsCount = functionDeclarations.Count;
  719. for (var i = 0; i < functionDeclarationsCount; i++)
  720. {
  721. var f = functionDeclarations[i];
  722. var fn = f.Id.Name;
  723. var fo = Function.CreateFunctionObject(f);
  724. var funcAlreadyDeclared = env.HasBinding(fn);
  725. if (!funcAlreadyDeclared)
  726. {
  727. env.CreateMutableBinding(fn, configurableBindings);
  728. }
  729. else
  730. {
  731. if (ReferenceEquals(env, GlobalEnvironment._record))
  732. {
  733. var go = Global;
  734. var existingProp = go.GetProperty(fn);
  735. if (existingProp.Configurable)
  736. {
  737. var flags = PropertyFlag.Writable | PropertyFlag.Enumerable;
  738. if (configurableBindings)
  739. {
  740. flags |= PropertyFlag.Configurable;
  741. }
  742. var descriptor = new PropertyDescriptor(Undefined.Instance, flags);
  743. go.DefineOwnProperty(fn, descriptor, true);
  744. }
  745. else
  746. {
  747. if (existingProp.IsAccessorDescriptor() || !existingProp.Enumerable)
  748. {
  749. ExceptionHelper.ThrowTypeError(this);
  750. }
  751. }
  752. }
  753. }
  754. env.SetMutableBinding(fn, fo, strict);
  755. }
  756. }
  757. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  758. internal void UpdateLexicalEnvironment(LexicalEnvironment newEnv)
  759. {
  760. _executionContexts.ReplaceTopLexicalEnvironment(newEnv);
  761. }
  762. private static void AssertNotNullOrEmpty(string propertyname, string propertyValue)
  763. {
  764. if (string.IsNullOrEmpty(propertyValue))
  765. {
  766. ExceptionHelper.ThrowArgumentException(propertyname);
  767. }
  768. }
  769. }
  770. }