ArgumentsInstance.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  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 class ArgumentsInstance : ObjectInstance
  15. {
  16. private ArgumentsInstance(Engine engine, Action<ArgumentsInstance> initializer) : base(engine)
  17. {
  18. _initializer = initializer;
  19. _initialized = false;
  20. }
  21. public bool Strict { get; set; }
  22. private Action<ArgumentsInstance> _initializer;
  23. private bool _initialized;
  24. protected override void EnsureInitialized()
  25. {
  26. if(_initialized)
  27. {
  28. return;
  29. }
  30. _initialized = true;
  31. _initializer(this);
  32. }
  33. public static ArgumentsInstance CreateArgumentsObject(Engine engine, FunctionInstance func, string[] names, JsValue[] args, EnvironmentRecord env, bool strict)
  34. {
  35. var obj = new ArgumentsInstance(engine, self =>
  36. {
  37. var len = args.Length;
  38. self.FastAddProperty("length", len, true, false, true);
  39. var map = engine.Object.Construct(Arguments.Empty);
  40. var mappedNamed = new List<string>();
  41. var indx = 0;
  42. while (indx <= len - 1)
  43. {
  44. var indxStr = TypeConverter.ToString(indx);
  45. var val = args[indx];
  46. self.FastAddProperty(indxStr, val, true, true, true);
  47. if (indx < names.Length)
  48. {
  49. var name = names[indx];
  50. if (!strict && !mappedNamed.Contains(name))
  51. {
  52. mappedNamed.Add(name);
  53. Func<JsValue, JsValue> g = n => env.GetBindingValue(name, false);
  54. var p = new Action<JsValue, JsValue>((n, o) => env.SetMutableBinding(name, o, true));
  55. map.DefineOwnProperty(indxStr, new ClrAccessDescriptor(engine, g, p) { Configurable = true }, false);
  56. }
  57. }
  58. indx++;
  59. }
  60. // step 12
  61. if (mappedNamed.Count > 0)
  62. {
  63. self.ParameterMap = map;
  64. }
  65. // step 13
  66. if (!strict)
  67. {
  68. self.FastAddProperty("callee", func, true, false, true);
  69. }
  70. // step 14
  71. else
  72. {
  73. var thrower = engine.Function.ThrowTypeError;
  74. self.DefineOwnProperty("caller", new PropertyDescriptor(get: thrower, set: thrower, enumerable: false, configurable: false), false);
  75. self.DefineOwnProperty("callee", new PropertyDescriptor(get: thrower, set: thrower, enumerable: false, configurable: false), false);
  76. }
  77. });
  78. // These properties are pre-initialized as their don't trigger
  79. // the EnsureInitialized() event and are cheap
  80. obj.Prototype = engine.Object.PrototypeObject;
  81. obj.Extensible = true;
  82. obj.Strict = strict;
  83. return obj;
  84. }
  85. public ObjectInstance ParameterMap { get; set; }
  86. public override string Class
  87. {
  88. get
  89. {
  90. return "Arguments";
  91. }
  92. }
  93. public override PropertyDescriptor GetOwnProperty(string propertyName)
  94. {
  95. EnsureInitialized();
  96. if (!Strict && 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. throw new JavaScriptException(Engine.TypeError);
  123. }
  124. return;
  125. }
  126. var ownDesc = GetOwnProperty(propertyName);
  127. if (ownDesc.IsDataDescriptor())
  128. {
  129. var valueDesc = new PropertyDescriptor(value: value, writable: null, enumerable: null, configurable: null);
  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(new JsValue(this), new[] { value });
  139. }
  140. else
  141. {
  142. var newDesc = new PropertyDescriptor(value, true, true, true);
  143. DefineOwnProperty(propertyName, newDesc, throwOnError);
  144. }
  145. }
  146. public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  147. {
  148. EnsureInitialized();
  149. if (!Strict && 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. throw new JavaScriptException(Engine.TypeError);
  159. }
  160. }
  161. if (isMapped != PropertyDescriptor.Undefined)
  162. {
  163. if (desc.IsAccessorDescriptor())
  164. {
  165. map.Delete(propertyName, false);
  166. }
  167. else
  168. {
  169. if (desc.Value != null && desc.Value != Undefined.Instance)
  170. {
  171. map.Put(propertyName, desc.Value, throwOnError);
  172. }
  173. if (desc.Writable.HasValue && desc.Writable.Value == false)
  174. {
  175. map.Delete(propertyName, false);
  176. }
  177. }
  178. }
  179. return true;
  180. }
  181. return base.DefineOwnProperty(propertyName, desc, throwOnError);
  182. }
  183. public override bool Delete(string propertyName, bool throwOnError)
  184. {
  185. EnsureInitialized();
  186. if (!Strict && ParameterMap != null)
  187. {
  188. var map = ParameterMap;
  189. var isMapped = map.GetOwnProperty(propertyName);
  190. var result = base.Delete(propertyName, throwOnError);
  191. if (result && isMapped != PropertyDescriptor.Undefined)
  192. {
  193. map.Delete(propertyName, false);
  194. }
  195. return result;
  196. }
  197. return base.Delete(propertyName, throwOnError);
  198. }
  199. }
  200. }