ArgumentsInstance.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. private JsValue[] _args;
  21. private EnvironmentRecord _env;
  22. private bool _strict;
  23. private 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. var self = this;
  50. var len = _args.Length;
  51. self.SetOwnProperty("length", new PropertyDescriptor(len, PropertyFlag.NonEnumerable));
  52. if (_args.Length > 0)
  53. {
  54. var map = Engine.Object.Construct(Arguments.Empty);
  55. var mappedNamed = _mappedNamed.Value;
  56. mappedNamed.Clear();
  57. for (var indx = 0; indx < len; indx++)
  58. {
  59. var indxStr = TypeConverter.ToString(indx);
  60. var val = _args[indx];
  61. self.SetOwnProperty(indxStr, new PropertyDescriptor(val, PropertyFlag.ConfigurableEnumerableWritable));
  62. if (indx < _names.Length)
  63. {
  64. var name = _names[indx];
  65. if (!_strict && !mappedNamed.Contains(name))
  66. {
  67. mappedNamed.Add(name);
  68. map.SetOwnProperty(indxStr, new ClrAccessDescriptor(_env, Engine, name));
  69. }
  70. }
  71. }
  72. // step 12
  73. if (mappedNamed.Count > 0)
  74. {
  75. self.ParameterMap = map;
  76. }
  77. }
  78. // step 13
  79. if (!_strict)
  80. {
  81. self.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. self.DefineOwnProperty("caller", new GetSetPropertyDescriptor(get: thrower, set: thrower, flags), false);
  89. self.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. EnsureInitialized();
  149. if (!_strict && !ReferenceEquals(ParameterMap, null))
  150. {
  151. var map = ParameterMap;
  152. var isMapped = map.GetOwnProperty(propertyName);
  153. var allowed = base.DefineOwnProperty(propertyName, desc, false);
  154. if (!allowed)
  155. {
  156. if (throwOnError)
  157. {
  158. ExceptionHelper.ThrowTypeError(Engine);
  159. }
  160. }
  161. if (isMapped != PropertyDescriptor.Undefined)
  162. {
  163. if (desc.IsAccessorDescriptor())
  164. {
  165. map.Delete(propertyName, false);
  166. }
  167. else
  168. {
  169. var descValue = desc.Value;
  170. if (!ReferenceEquals(descValue, null) && !descValue.IsUndefined())
  171. {
  172. map.Put(propertyName, descValue, throwOnError);
  173. }
  174. if (desc.WritableSet && !desc.Writable)
  175. {
  176. map.Delete(propertyName, false);
  177. }
  178. }
  179. }
  180. return true;
  181. }
  182. return base.DefineOwnProperty(propertyName, desc, throwOnError);
  183. }
  184. public override bool Delete(string propertyName, bool throwOnError)
  185. {
  186. EnsureInitialized();
  187. if (!_strict && !ReferenceEquals(ParameterMap, null))
  188. {
  189. var map = ParameterMap;
  190. var isMapped = map.GetOwnProperty(propertyName);
  191. var result = base.Delete(propertyName, throwOnError);
  192. if (result && isMapped != PropertyDescriptor.Undefined)
  193. {
  194. map.Delete(propertyName, false);
  195. }
  196. return result;
  197. }
  198. return base.Delete(propertyName, throwOnError);
  199. }
  200. }
  201. }