2
0

ObjectConstructor.cs 15 KB

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