ArgumentsInstance.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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(FunctionInstance func, string[] names, JsValue[] args, EnvironmentRecord env, bool strict)
  28. {
  29. Clear();
  30. _func = func;
  31. _names = names;
  32. _args = args;
  33. _env = env;
  34. _strict = strict;
  35. _initialized = false;
  36. }
  37. public bool Strict { get; set; }
  38. protected override void EnsureInitialized()
  39. {
  40. if (_initialized)
  41. {
  42. return;
  43. }
  44. _initialized = true;
  45. var self = this;
  46. var len = _args.Length;
  47. self.SetOwnProperty("length", new PropertyDescriptor(len, PropertyFlag.NonEnumerable));
  48. if (_args.Length > 0)
  49. {
  50. var map = Engine.Object.Construct(Arguments.Empty);
  51. var mappedNamed = _mappedNamed.Value;
  52. mappedNamed.Clear();
  53. for (var indx = 0; indx < len; indx++)
  54. {
  55. var indxStr = TypeConverter.ToString(indx);
  56. var val = _args[indx];
  57. self.SetOwnProperty(indxStr, new PropertyDescriptor(val, PropertyFlag.ConfigurableEnumerableWritable));
  58. if (indx < _names.Length)
  59. {
  60. var name = _names[indx];
  61. if (!_strict && !mappedNamed.Contains(name))
  62. {
  63. mappedNamed.Add(name);
  64. map.SetOwnProperty(indxStr, new ClrAccessDescriptor(_env, Engine, name));
  65. }
  66. }
  67. }
  68. // step 12
  69. if (mappedNamed.Count > 0)
  70. {
  71. self.ParameterMap = map;
  72. }
  73. }
  74. // step 13
  75. if (!_strict)
  76. {
  77. self.SetOwnProperty("callee", new PropertyDescriptor(_func, PropertyFlag.NonEnumerable));
  78. }
  79. // step 14
  80. else
  81. {
  82. var thrower = Engine.Function.ThrowTypeError;
  83. const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
  84. self.DefineOwnProperty("caller", new GetSetPropertyDescriptor(get: thrower, set: thrower, flags), false);
  85. self.DefineOwnProperty("callee", new GetSetPropertyDescriptor(get: thrower, set: thrower, flags), false);
  86. }
  87. }
  88. public ObjectInstance ParameterMap { get; set; }
  89. public override PropertyDescriptor GetOwnProperty(string propertyName)
  90. {
  91. EnsureInitialized();
  92. if (!Strict && !ReferenceEquals(ParameterMap, null))
  93. {
  94. var desc = base.GetOwnProperty(propertyName);
  95. if (desc == PropertyDescriptor.Undefined)
  96. {
  97. return desc;
  98. }
  99. var isMapped = ParameterMap.GetOwnProperty(propertyName);
  100. if (isMapped != PropertyDescriptor.Undefined)
  101. {
  102. desc.Value = ParameterMap.Get(propertyName);
  103. }
  104. return desc;
  105. }
  106. return base.GetOwnProperty(propertyName);
  107. }
  108. /// Implementation from ObjectInstance official specs as the one
  109. /// in ObjectInstance is optimized for the general case and wouldn't work
  110. /// for arrays
  111. public override void Put(string propertyName, JsValue value, bool throwOnError)
  112. {
  113. EnsureInitialized();
  114. if (!CanPut(propertyName))
  115. {
  116. if (throwOnError)
  117. {
  118. ExceptionHelper.ThrowTypeError(Engine);
  119. }
  120. return;
  121. }
  122. var ownDesc = GetOwnProperty(propertyName);
  123. if (ownDesc.IsDataDescriptor())
  124. {
  125. var valueDesc = new PropertyDescriptor(value, PropertyFlag.None);
  126. DefineOwnProperty(propertyName, valueDesc, throwOnError);
  127. return;
  128. }
  129. // property is an accessor or inherited
  130. var desc = GetProperty(propertyName);
  131. if (desc.IsAccessorDescriptor())
  132. {
  133. var setter = desc.Set.TryCast<ICallable>();
  134. setter.Call(this, new[] {value});
  135. }
  136. else
  137. {
  138. var newDesc = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  139. DefineOwnProperty(propertyName, newDesc, throwOnError);
  140. }
  141. }
  142. public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  143. {
  144. EnsureInitialized();
  145. if (!Strict && !ReferenceEquals(ParameterMap, null))
  146. {
  147. var map = ParameterMap;
  148. var isMapped = map.GetOwnProperty(propertyName);
  149. var allowed = base.DefineOwnProperty(propertyName, desc, false);
  150. if (!allowed)
  151. {
  152. if (throwOnError)
  153. {
  154. ExceptionHelper.ThrowTypeError(Engine);
  155. }
  156. }
  157. if (isMapped != PropertyDescriptor.Undefined)
  158. {
  159. if (desc.IsAccessorDescriptor())
  160. {
  161. map.Delete(propertyName, false);
  162. }
  163. else
  164. {
  165. var descValue = desc.Value;
  166. if (!ReferenceEquals(descValue, null) && !descValue.IsUndefined())
  167. {
  168. map.Put(propertyName, descValue, throwOnError);
  169. }
  170. if (desc.WritableSet && !desc.Writable)
  171. {
  172. map.Delete(propertyName, false);
  173. }
  174. }
  175. }
  176. return true;
  177. }
  178. return base.DefineOwnProperty(propertyName, desc, throwOnError);
  179. }
  180. public override bool Delete(string propertyName, bool throwOnError)
  181. {
  182. EnsureInitialized();
  183. if (!Strict && !ReferenceEquals(ParameterMap, null))
  184. {
  185. var map = ParameterMap;
  186. var isMapped = map.GetOwnProperty(propertyName);
  187. var result = base.Delete(propertyName, throwOnError);
  188. if (result && isMapped != PropertyDescriptor.Undefined)
  189. {
  190. map.Delete(propertyName, false);
  191. }
  192. return result;
  193. }
  194. return base.Delete(propertyName, throwOnError);
  195. }
  196. }
  197. }