JsArguments.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System.Threading;
  2. using Jint.Native.Object;
  3. using Jint.Native.Symbol;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Descriptors.Specialized;
  7. using Jint.Runtime.Environments;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Native;
  10. /// <summary>
  11. /// https://tc39.es/ecma262/#sec-arguments-exotic-objects
  12. /// </summary>
  13. public sealed class JsArguments : ObjectInstance
  14. {
  15. // cache property container for array iteration for less allocations
  16. private static readonly ThreadLocal<HashSet<Key>> _mappedNamed = new(() => []);
  17. private Function.Function _func = null!;
  18. private Key[] _names = null!;
  19. private JsValue[] _args = null!;
  20. private DeclarativeEnvironment _env = null!;
  21. private bool _canReturnToPool;
  22. private bool _hasRestParameter;
  23. private bool _materialized;
  24. internal JsArguments(Engine engine)
  25. : base(engine, ObjectClass.Arguments)
  26. {
  27. }
  28. internal void Prepare(
  29. Function.Function func,
  30. Key[] names,
  31. JsValue[] args,
  32. DeclarativeEnvironment env,
  33. bool hasRestParameter)
  34. {
  35. _func = func;
  36. _names = names;
  37. _args = args;
  38. _env = env;
  39. _hasRestParameter = hasRestParameter;
  40. _canReturnToPool = true;
  41. ClearProperties();
  42. }
  43. protected override void Initialize()
  44. {
  45. _canReturnToPool = false;
  46. var args = _args;
  47. DefinePropertyOrThrow(CommonProperties.Length, new PropertyDescriptor(_args.Length, PropertyFlag.NonEnumerable));
  48. if (_func is null)
  49. {
  50. // unmapped
  51. ParameterMap = null;
  52. for (uint i = 0; i < (uint) args.Length; i++)
  53. {
  54. var val = args[i];
  55. CreateDataProperty(JsString.Create(i), val);
  56. }
  57. DefinePropertyOrThrow(CommonProperties.Callee, new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(_engine, PropertyFlag.None));
  58. }
  59. else
  60. {
  61. ObjectInstance? map = null;
  62. if (args.Length > 0)
  63. {
  64. var mappedNamed = _mappedNamed.Value!;
  65. mappedNamed.Clear();
  66. map = Engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
  67. for (uint i = 0; i < (uint) args.Length; i++)
  68. {
  69. SetOwnProperty(JsString.Create(i), new PropertyDescriptor(args[i], PropertyFlag.ConfigurableEnumerableWritable));
  70. if (i < _names.Length)
  71. {
  72. var name = _names[i];
  73. if (mappedNamed.Add(name))
  74. {
  75. map.SetOwnProperty(JsString.Create(i), new ClrAccessDescriptor(_env, Engine, name));
  76. }
  77. }
  78. }
  79. }
  80. ParameterMap = map;
  81. // step 13
  82. DefinePropertyOrThrow(CommonProperties.Callee, new PropertyDescriptor(_func, PropertyFlag.NonEnumerable));
  83. }
  84. var iteratorFunction = new ClrFunction(Engine, "iterator", _engine.Realm.Intrinsics.Array.PrototypeObject.Values, 0, PropertyFlag.Configurable);
  85. DefinePropertyOrThrow(GlobalSymbolRegistry.Iterator, new PropertyDescriptor(iteratorFunction, PropertyFlag.Writable | PropertyFlag.Configurable));
  86. }
  87. internal ObjectInstance? ParameterMap { get; set; }
  88. internal override bool IsArrayLike => true;
  89. internal override bool IsIntegerIndexedArray => true;
  90. public uint Length => (uint) _args.Length;
  91. public override PropertyDescriptor GetOwnProperty(JsValue property)
  92. {
  93. EnsureInitialized();
  94. if (ParameterMap is not null)
  95. {
  96. var desc = base.GetOwnProperty(property);
  97. if (desc == PropertyDescriptor.Undefined)
  98. {
  99. return desc;
  100. }
  101. if (ParameterMap.TryGetValue(property, out var jsValue) && !jsValue.IsUndefined())
  102. {
  103. desc.Value = jsValue;
  104. }
  105. return desc;
  106. }
  107. return base.GetOwnProperty(property);
  108. }
  109. /// Implementation from ObjectInstance official specs as the one
  110. /// in ObjectInstance is optimized for the general case and wouldn't work
  111. /// for arrays
  112. public override bool Set(JsValue property, JsValue value, JsValue receiver)
  113. {
  114. EnsureInitialized();
  115. if (!CanPut(property))
  116. {
  117. return false;
  118. }
  119. var ownDesc = GetOwnProperty(property);
  120. if (ownDesc.IsDataDescriptor())
  121. {
  122. var valueDesc = new PropertyDescriptor(value, PropertyFlag.None);
  123. return DefineOwnProperty(property, valueDesc);
  124. }
  125. // property is an accessor or inherited
  126. var desc = GetOwnProperty(property);
  127. if (desc.IsAccessorDescriptor())
  128. {
  129. if (desc.Set is not ICallable setter)
  130. {
  131. return false;
  132. }
  133. setter.Call(receiver, value);
  134. }
  135. else
  136. {
  137. var newDesc = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  138. return DefineOwnProperty(property, newDesc);
  139. }
  140. return true;
  141. }
  142. public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  143. {
  144. if (_hasRestParameter)
  145. {
  146. // immutable
  147. return true;
  148. }
  149. EnsureInitialized();
  150. if (ParameterMap is not null)
  151. {
  152. var map = ParameterMap;
  153. var isMapped = map.GetOwnProperty(property);
  154. var allowed = base.DefineOwnProperty(property, desc);
  155. if (!allowed)
  156. {
  157. return false;
  158. }
  159. if (isMapped != PropertyDescriptor.Undefined)
  160. {
  161. if (desc.IsAccessorDescriptor())
  162. {
  163. map.Delete(property);
  164. }
  165. else
  166. {
  167. var descValue = desc.Value;
  168. if (descValue is not null && !descValue.IsUndefined())
  169. {
  170. map.Set(property, descValue, false);
  171. }
  172. if (desc.WritableSet && !desc.Writable)
  173. {
  174. map.Delete(property);
  175. }
  176. }
  177. }
  178. return true;
  179. }
  180. return base.DefineOwnProperty(property, desc);
  181. }
  182. public override bool Delete(JsValue property)
  183. {
  184. EnsureInitialized();
  185. if (ParameterMap is not null)
  186. {
  187. var map = ParameterMap;
  188. var isMapped = map.GetOwnProperty(property);
  189. var result = base.Delete(property);
  190. if (result && isMapped != PropertyDescriptor.Undefined)
  191. {
  192. map.Delete(property);
  193. }
  194. return result;
  195. }
  196. return base.Delete(property);
  197. }
  198. internal void Materialize()
  199. {
  200. if (_materialized)
  201. {
  202. // already done
  203. return;
  204. }
  205. _materialized = true;
  206. EnsureInitialized();
  207. var args = _args;
  208. var copiedArgs = new JsValue[args.Length];
  209. System.Array.Copy(args, copiedArgs, args.Length);
  210. _args = copiedArgs;
  211. _canReturnToPool = false;
  212. }
  213. internal void FunctionWasCalled()
  214. {
  215. // should no longer expose arguments which is special name
  216. ParameterMap = null;
  217. if (_canReturnToPool)
  218. {
  219. _engine._argumentsInstancePool.Return(this);
  220. // prevent double-return
  221. _canReturnToPool = false;
  222. }
  223. }
  224. }