ArgumentsInstance.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using Jint.Native.Function;
  4. using Jint.Native.Object;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Descriptors.Specialized;
  8. using Jint.Runtime.Environments;
  9. namespace Jint.Native.Argument
  10. {
  11. /// <summary>
  12. /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.6
  13. /// </summary>
  14. public sealed class ArgumentsInstance : ObjectInstance
  15. {
  16. // cache key container for array iteration for less allocations
  17. private static readonly ThreadLocal<HashSet<string>> _mappedNamed = new ThreadLocal<HashSet<string>>(() => new HashSet<string>());
  18. private FunctionInstance _func;
  19. private string[] _names;
  20. internal JsValue[] _args;
  21. private EnvironmentRecord _env;
  22. private bool _strict;
  23. internal bool _initialized;
  24. internal ArgumentsInstance(Engine engine) : base(engine, objectClass: "Arguments")
  25. {
  26. }
  27. internal void Prepare(
  28. FunctionInstance func,
  29. string[] names,
  30. JsValue[] args,
  31. EnvironmentRecord env,
  32. bool strict)
  33. {
  34. _func = func;
  35. _names = names;
  36. _args = args;
  37. _env = env;
  38. _strict = strict;
  39. _properties?.Clear();
  40. _initialized = false;
  41. }
  42. protected override void EnsureInitialized()
  43. {
  44. if (_initialized)
  45. {
  46. return;
  47. }
  48. _initialized = true;
  49. BuildProperties();
  50. }
  51. private void BuildProperties()
  52. {
  53. var args = _args;
  54. SetOwnProperty("length", new PropertyDescriptor(args.Length, PropertyFlag.NonEnumerable));
  55. ObjectInstance map = null;
  56. if (args.Length > 0)
  57. {
  58. var mappedNamed = _mappedNamed.Value;
  59. mappedNamed.Clear();
  60. for (var i = 0; i < (uint) args.Length; i++)
  61. {
  62. var indxStr = TypeConverter.ToString(i);
  63. var val = args[i];
  64. SetOwnProperty(indxStr, new PropertyDescriptor(val, PropertyFlag.ConfigurableEnumerableWritable));
  65. if (i < _names.Length)
  66. {
  67. var name = _names[i];
  68. if (!_strict && !mappedNamed.Contains(name))
  69. {
  70. map = map ?? Engine.Object.Construct(Arguments.Empty);
  71. mappedNamed.Add(name);
  72. map.SetOwnProperty(indxStr, new ClrAccessDescriptor(_env, Engine, name));
  73. }
  74. }
  75. }
  76. }
  77. ParameterMap = map;
  78. // step 13
  79. if (!_strict)
  80. {
  81. SetOwnProperty("callee", new PropertyDescriptor(_func, PropertyFlag.NonEnumerable));
  82. }
  83. // step 14
  84. else
  85. {
  86. var thrower = Engine.Function.ThrowTypeError;
  87. const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
  88. DefineOwnProperty("caller", new GetSetPropertyDescriptor(get: thrower, set: thrower, flags), false);
  89. DefineOwnProperty("callee", new GetSetPropertyDescriptor(get: thrower, set: thrower, flags), false);
  90. }
  91. }
  92. public ObjectInstance ParameterMap { get; set; }
  93. public override PropertyDescriptor GetOwnProperty(string propertyName)
  94. {
  95. EnsureInitialized();
  96. if (!_strict && !ReferenceEquals(ParameterMap, null))
  97. {
  98. var desc = base.GetOwnProperty(propertyName);
  99. if (desc == PropertyDescriptor.Undefined)
  100. {
  101. return desc;
  102. }
  103. var isMapped = ParameterMap.GetOwnProperty(propertyName);
  104. if (isMapped != PropertyDescriptor.Undefined)
  105. {
  106. desc.Value = ParameterMap.Get(propertyName);
  107. }
  108. return desc;
  109. }
  110. return base.GetOwnProperty(propertyName);
  111. }
  112. /// Implementation from ObjectInstance official specs as the one
  113. /// in ObjectInstance is optimized for the general case and wouldn't work
  114. /// for arrays
  115. public override void Put(string propertyName, JsValue value, bool throwOnError)
  116. {
  117. EnsureInitialized();
  118. if (!CanPut(propertyName))
  119. {
  120. if (throwOnError)
  121. {
  122. ExceptionHelper.ThrowTypeError(Engine);
  123. }
  124. return;
  125. }
  126. var ownDesc = GetOwnProperty(propertyName);
  127. if (ownDesc.IsDataDescriptor())
  128. {
  129. var valueDesc = new PropertyDescriptor(value, PropertyFlag.None);
  130. DefineOwnProperty(propertyName, valueDesc, throwOnError);
  131. return;
  132. }
  133. // property is an accessor or inherited
  134. var desc = GetProperty(propertyName);
  135. if (desc.IsAccessorDescriptor())
  136. {
  137. var setter = desc.Set.TryCast<ICallable>();
  138. setter.Call(this, new[] {value});
  139. }
  140. else
  141. {
  142. var newDesc = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  143. DefineOwnProperty(propertyName, newDesc, throwOnError);
  144. }
  145. }
  146. public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  147. {
  148. if (_func is ScriptFunctionInstance scriptFunctionInstance && scriptFunctionInstance._function._hasRestParameter)
  149. {
  150. // immutable
  151. return false;
  152. }
  153. EnsureInitialized();
  154. if (!_strict && !ReferenceEquals(ParameterMap, null))
  155. {
  156. var map = ParameterMap;
  157. var isMapped = map.GetOwnProperty(propertyName);
  158. var allowed = base.DefineOwnProperty(propertyName, desc, false);
  159. if (!allowed)
  160. {
  161. if (throwOnError)
  162. {
  163. ExceptionHelper.ThrowTypeError(Engine);
  164. }
  165. }
  166. if (isMapped != PropertyDescriptor.Undefined)
  167. {
  168. if (desc.IsAccessorDescriptor())
  169. {
  170. map.Delete(propertyName, false);
  171. }
  172. else
  173. {
  174. var descValue = desc.Value;
  175. if (!ReferenceEquals(descValue, null) && !descValue.IsUndefined())
  176. {
  177. map.Put(propertyName, descValue, throwOnError);
  178. }
  179. if (desc.WritableSet && !desc.Writable)
  180. {
  181. map.Delete(propertyName, false);
  182. }
  183. }
  184. }
  185. return true;
  186. }
  187. return base.DefineOwnProperty(propertyName, desc, throwOnError);
  188. }
  189. public override bool Delete(string propertyName, bool throwOnError)
  190. {
  191. EnsureInitialized();
  192. if (!_strict && !ReferenceEquals(ParameterMap, null))
  193. {
  194. var map = ParameterMap;
  195. var isMapped = map.GetOwnProperty(propertyName);
  196. var result = base.Delete(propertyName, throwOnError);
  197. if (result && isMapped != PropertyDescriptor.Undefined)
  198. {
  199. map.Delete(propertyName, false);
  200. }
  201. return result;
  202. }
  203. return base.Delete(propertyName, throwOnError);
  204. }
  205. internal void PersistArguments()
  206. {
  207. EnsureInitialized();
  208. var args = _args;
  209. var copiedArgs = new JsValue[args.Length];
  210. System.Array.Copy(args, copiedArgs, args.Length);
  211. _args = copiedArgs;
  212. // should no longer expose arguments which is special name
  213. ParameterMap = null;
  214. }
  215. }
  216. }