ObjectConstructor.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Jint.Collections;
  4. using Jint.Native.Array;
  5. using Jint.Native.Function;
  6. using Jint.Native.String;
  7. using Jint.Runtime;
  8. using Jint.Runtime.Descriptors;
  9. using Jint.Runtime.Interop;
  10. namespace Jint.Native.Object
  11. {
  12. public sealed class ObjectConstructor : FunctionInstance, IConstructor
  13. {
  14. private ObjectConstructor(Engine engine)
  15. : base(engine, "Object", null, null, false)
  16. {
  17. }
  18. public static ObjectConstructor CreateObjectConstructor(Engine engine)
  19. {
  20. var obj = new ObjectConstructor(engine);
  21. obj.Extensible = true;
  22. obj.PrototypeObject = ObjectPrototype.CreatePrototypeObject(engine, obj);
  23. obj.SetOwnProperty("length", new PropertyDescriptor(1, PropertyFlag.AllForbidden));
  24. obj.SetOwnProperty("prototype", new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden));
  25. return obj;
  26. }
  27. public void Configure()
  28. {
  29. Prototype = Engine.Function.PrototypeObject;
  30. FastAddProperty("getPrototypeOf", new ClrFunctionInstance(Engine, "getPrototypeOf", GetPrototypeOf, 1), true, false, true);
  31. FastAddProperty("getOwnPropertyDescriptor", new ClrFunctionInstance(Engine, "getOwnPropertyDescriptor", GetOwnPropertyDescriptor, 2), true, false, true);
  32. FastAddProperty("getOwnPropertyNames", new ClrFunctionInstance(Engine, "getOwnPropertyNames", GetOwnPropertyNames, 1), true, false, true);
  33. FastAddProperty("create", new ClrFunctionInstance(Engine, "create", Create, 2), true, false, true);
  34. FastAddProperty("defineProperty", new ClrFunctionInstance(Engine, "defineProperty", DefineProperty, 3), true, false, true);
  35. FastAddProperty("defineProperties", new ClrFunctionInstance(Engine, "defineProperties", DefineProperties, 2), true, false, true);
  36. FastAddProperty("seal", new ClrFunctionInstance(Engine, "seal", Seal, 1), true, false, true);
  37. FastAddProperty("freeze", new ClrFunctionInstance(Engine, "freeze", Freeze, 1), true, false, true);
  38. FastAddProperty("preventExtensions", new ClrFunctionInstance(Engine, "preventExtensions", PreventExtensions, 1), true, false, true);
  39. FastAddProperty("isSealed", new ClrFunctionInstance(Engine, "isSealed", IsSealed, 1), true, false, true);
  40. FastAddProperty("isFrozen", new ClrFunctionInstance(Engine, "isFrozen", IsFrozen, 1), true, false, true);
  41. FastAddProperty("isExtensible", new ClrFunctionInstance(Engine, "isExtensible", IsExtensible, 1), true, false, true);
  42. FastAddProperty("keys", new ClrFunctionInstance(Engine, "keys", Keys, 1), true, false, true);
  43. }
  44. public ObjectPrototype PrototypeObject { get; private set; }
  45. /// <summary>
  46. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.1.1
  47. /// </summary>
  48. /// <param name="thisObject"></param>
  49. /// <param name="arguments"></param>
  50. /// <returns></returns>
  51. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  52. {
  53. if (arguments.Length == 0)
  54. {
  55. return Construct(arguments);
  56. }
  57. if(arguments[0].IsNull() || arguments[0].IsUndefined())
  58. {
  59. return Construct(arguments);
  60. }
  61. return TypeConverter.ToObject(_engine, arguments[0]);
  62. }
  63. /// <summary>
  64. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
  65. /// </summary>
  66. /// <param name="arguments"></param>
  67. /// <returns></returns>
  68. public ObjectInstance Construct(JsValue[] arguments)
  69. {
  70. if (arguments.Length > 0)
  71. {
  72. var value = arguments[0];
  73. var valueObj = value.TryCast<ObjectInstance>();
  74. if (!ReferenceEquals(valueObj, null))
  75. {
  76. return valueObj;
  77. }
  78. var type = value.Type;
  79. if (type == Types.String || type == Types.Number || type == Types.Boolean)
  80. {
  81. return TypeConverter.ToObject(_engine, value);
  82. }
  83. }
  84. var obj = new ObjectInstance(_engine)
  85. {
  86. Extensible = true,
  87. Prototype = Engine.Object.PrototypeObject
  88. };
  89. return obj;
  90. }
  91. internal ObjectInstance Construct(int propertyCount)
  92. {
  93. var obj = new ObjectInstance(_engine)
  94. {
  95. Extensible = true,
  96. Prototype = Engine.Object.PrototypeObject,
  97. _properties = propertyCount > 0
  98. ? new StringDictionarySlim<PropertyDescriptor>(System.Math.Max(2, propertyCount))
  99. : null
  100. };
  101. return obj;
  102. }
  103. public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  104. {
  105. var oArg = arguments.At(0);
  106. var o = oArg.TryCast<ObjectInstance>();
  107. if (ReferenceEquals(o, null))
  108. {
  109. ExceptionHelper.ThrowTypeError(Engine);
  110. }
  111. return o.Prototype ?? Null;
  112. }
  113. public JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  114. {
  115. var oArg = arguments.At(0);
  116. var o = oArg.TryCast<ObjectInstance>();
  117. if (ReferenceEquals(o, null))
  118. {
  119. ExceptionHelper.ThrowTypeError(Engine);
  120. }
  121. var p = arguments.At(1);
  122. var name = TypeConverter.ToString(p);
  123. var desc = o.GetOwnProperty(name);
  124. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  125. }
  126. public JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
  127. {
  128. var oArg = arguments.At(0);
  129. var o = oArg.TryCast<ObjectInstance>();
  130. if (ReferenceEquals(o, null))
  131. {
  132. ExceptionHelper.ThrowTypeError(Engine);
  133. }
  134. uint n = 0;
  135. ArrayInstance array = null;
  136. var ownProperties = o.GetOwnProperties().ToList();
  137. if (o is StringInstance s)
  138. {
  139. var length = s.PrimitiveValue.Length;
  140. array = Engine.Array.ConstructFast((uint) (ownProperties.Count + length));
  141. for (var i = 0; i < length; i++)
  142. {
  143. array.SetIndexValue(n, TypeConverter.ToString(i), updateLength: false);
  144. n++;
  145. }
  146. }
  147. array = array ?? Engine.Array.ConstructFast((uint) ownProperties.Count);
  148. for (var i = 0; i < ownProperties.Count; i++)
  149. {
  150. var p = ownProperties[i];
  151. array.SetIndexValue(n, p.Key, false);
  152. n++;
  153. }
  154. array.SetLength(n);
  155. return array;
  156. }
  157. public JsValue Create(JsValue thisObject, JsValue[] arguments)
  158. {
  159. var oArg = arguments.At(0);
  160. var o = oArg.TryCast<ObjectInstance>();
  161. if (ReferenceEquals(o, null) && !oArg.IsNull())
  162. {
  163. ExceptionHelper.ThrowTypeError(Engine);
  164. }
  165. var obj = Engine.Object.Construct(Arguments.Empty);
  166. obj.Prototype = o;
  167. var properties = arguments.At(1);
  168. if (!properties.IsUndefined())
  169. {
  170. var jsValues = _engine._jsValueArrayPool.RentArray(2);
  171. jsValues[0] = obj;
  172. jsValues[1] = properties;
  173. DefineProperties(thisObject, jsValues);
  174. _engine._jsValueArrayPool.ReturnArray(jsValues);
  175. }
  176. return obj;
  177. }
  178. public JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  179. {
  180. var oArg = arguments.At(0);
  181. var o = oArg.TryCast<ObjectInstance>();
  182. if (ReferenceEquals(o, null))
  183. {
  184. ExceptionHelper.ThrowTypeError(Engine);
  185. }
  186. var p = arguments.At(1);
  187. var name = TypeConverter.ToString(p);
  188. var attributes = arguments.At(2);
  189. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  190. o.DefineOwnProperty(name, desc, true);
  191. return o;
  192. }
  193. public JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
  194. {
  195. var oArg = arguments.At(0);
  196. var o = oArg.TryCast<ObjectInstance>();
  197. if (ReferenceEquals(o, null))
  198. {
  199. ExceptionHelper.ThrowTypeError(Engine);
  200. }
  201. var properties = arguments.At(1);
  202. var props = TypeConverter.ToObject(Engine, properties);
  203. var descriptors = new List<KeyValuePair<string, PropertyDescriptor>>();
  204. foreach (var p in props.GetOwnProperties())
  205. {
  206. if (!p.Value.Enumerable)
  207. {
  208. continue;
  209. }
  210. var descObj = props.Get(p.Key);
  211. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, descObj);
  212. descriptors.Add(new KeyValuePair<string, PropertyDescriptor>(p.Key, desc));
  213. }
  214. foreach (var pair in descriptors)
  215. {
  216. o.DefineOwnProperty(pair.Key, pair.Value, true);
  217. }
  218. return o;
  219. }
  220. public JsValue Seal(JsValue thisObject, JsValue[] arguments)
  221. {
  222. var oArg = arguments.At(0);
  223. var o = oArg.TryCast<ObjectInstance>();
  224. if (ReferenceEquals(o, null))
  225. {
  226. ExceptionHelper.ThrowTypeError(Engine);
  227. }
  228. var properties = new List<KeyValuePair<string, PropertyDescriptor>>(o.GetOwnProperties());
  229. foreach (var prop in properties)
  230. {
  231. var propertyDescriptor = prop.Value;
  232. if (propertyDescriptor.Configurable)
  233. {
  234. propertyDescriptor.Configurable = false;
  235. FastSetProperty(prop.Key, propertyDescriptor);
  236. }
  237. o.DefineOwnProperty(prop.Key, propertyDescriptor, true);
  238. }
  239. o.Extensible = false;
  240. return o;
  241. }
  242. public JsValue Freeze(JsValue thisObject, JsValue[] arguments)
  243. {
  244. var oArg = arguments.At(0);
  245. var o = oArg.TryCast<ObjectInstance>();
  246. if (ReferenceEquals(o, null))
  247. {
  248. ExceptionHelper.ThrowTypeError(Engine);
  249. }
  250. var properties = new List<KeyValuePair<string, PropertyDescriptor>>(o.GetOwnProperties());
  251. foreach (var p in properties)
  252. {
  253. var desc = o.GetOwnProperty(p.Key);
  254. if (desc.IsDataDescriptor())
  255. {
  256. if (desc.Writable)
  257. {
  258. var mutable = desc;
  259. mutable.Writable = false;
  260. desc = mutable;
  261. }
  262. }
  263. if (desc.Configurable)
  264. {
  265. var mutable = desc;
  266. mutable.Configurable = false;
  267. desc = mutable;
  268. }
  269. o.DefineOwnProperty(p.Key, desc, true);
  270. }
  271. o.Extensible = false;
  272. return o;
  273. }
  274. public JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  275. {
  276. var oArg = arguments.At(0);
  277. var o = oArg.TryCast<ObjectInstance>();
  278. if (ReferenceEquals(o, null))
  279. {
  280. ExceptionHelper.ThrowTypeError(Engine);
  281. }
  282. o.Extensible = false;
  283. return o;
  284. }
  285. public JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
  286. {
  287. var oArg = arguments.At(0);
  288. var o = oArg.TryCast<ObjectInstance>();
  289. if (ReferenceEquals(o, null))
  290. {
  291. ExceptionHelper.ThrowTypeError(Engine);
  292. }
  293. foreach (var prop in o.GetOwnProperties())
  294. {
  295. if (prop.Value.Configurable)
  296. {
  297. return false;
  298. }
  299. }
  300. if (o.Extensible == false)
  301. {
  302. return true;
  303. }
  304. return false;
  305. }
  306. public JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
  307. {
  308. var oArg = arguments.At(0);
  309. var o = oArg.TryCast<ObjectInstance>();
  310. if (ReferenceEquals(o, null))
  311. {
  312. ExceptionHelper.ThrowTypeError(Engine);
  313. }
  314. foreach (var pair in o.GetOwnProperties())
  315. {
  316. var desc = pair.Value;
  317. if (desc.IsDataDescriptor())
  318. {
  319. if (desc.Writable)
  320. {
  321. return false;
  322. }
  323. }
  324. if (desc.Configurable)
  325. {
  326. return false;
  327. }
  328. }
  329. if (o.Extensible == false)
  330. {
  331. return true;
  332. }
  333. return false;
  334. }
  335. public JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  336. {
  337. var oArg = arguments.At(0);
  338. var o = oArg.TryCast<ObjectInstance>();
  339. if (ReferenceEquals(o, null))
  340. {
  341. ExceptionHelper.ThrowTypeError(Engine);
  342. }
  343. return o.Extensible;
  344. }
  345. public JsValue Keys(JsValue thisObject, JsValue[] arguments)
  346. {
  347. var oArg = arguments.At(0);
  348. var o = oArg.TryCast<ObjectInstance>();
  349. if (ReferenceEquals(o, null))
  350. {
  351. ExceptionHelper.ThrowTypeError(Engine);
  352. }
  353. var enumerableProperties = o.GetOwnProperties()
  354. .Where(x => x.Value.Enumerable)
  355. .ToArray();
  356. var n = enumerableProperties.Length;
  357. var array = Engine.Array.ConstructFast((uint) n);
  358. uint index = 0;
  359. foreach (var prop in enumerableProperties)
  360. {
  361. var p = prop.Key;
  362. array.SetIndexValue(index, p, updateLength: false);
  363. index++;
  364. }
  365. return array;
  366. }
  367. }
  368. }