Engine.cs 34 KB

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