ObjectConstructor.cs 14 KB

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