ObjectConstructor.cs 14 KB

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