ObjectConstructor.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  90. {
  91. var oArg = arguments.At(0);
  92. var o = oArg.TryCast<ObjectInstance>();
  93. if (ReferenceEquals(o, null))
  94. {
  95. ExceptionHelper.ThrowTypeError(Engine);
  96. }
  97. return o.Prototype ?? Null;
  98. }
  99. public JsValue GetOwnPropertyDescriptor(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. var p = arguments.At(1);
  108. var name = TypeConverter.ToString(p);
  109. var desc = o.GetOwnProperty(name);
  110. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  111. }
  112. public JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
  113. {
  114. var oArg = arguments.At(0);
  115. var o = oArg.TryCast<ObjectInstance>();
  116. if (ReferenceEquals(o, null))
  117. {
  118. ExceptionHelper.ThrowTypeError(Engine);
  119. }
  120. uint n = 0;
  121. ArrayInstance array = null;
  122. var ownProperties = o.GetOwnProperties().ToList();
  123. if (o is StringInstance s)
  124. {
  125. var length = s.PrimitiveValue.AsStringWithoutTypeCheck().Length;
  126. array = Engine.Array.Construct(ownProperties.Count + length);
  127. for (var i = 0; i < length; i++)
  128. {
  129. array.SetIndexValue(n, TypeConverter.ToString(i), updateLength: false);
  130. n++;
  131. }
  132. }
  133. array = array ?? Engine.Array.ConstructFast((uint) ownProperties.Count);
  134. for (var i = 0; i < ownProperties.Count; i++)
  135. {
  136. var p = ownProperties[i];
  137. array.SetIndexValue(n, p.Key, false);
  138. n++;
  139. }
  140. array.SetLength(n);
  141. return array;
  142. }
  143. public JsValue Create(JsValue thisObject, JsValue[] arguments)
  144. {
  145. var oArg = arguments.At(0);
  146. var o = oArg.TryCast<ObjectInstance>();
  147. if (ReferenceEquals(o, null) && !oArg.IsNull())
  148. {
  149. ExceptionHelper.ThrowTypeError(Engine);
  150. }
  151. var obj = Engine.Object.Construct(Arguments.Empty);
  152. obj.Prototype = o;
  153. var properties = arguments.At(1);
  154. if (!properties.IsUndefined())
  155. {
  156. var jsValues = _engine.JsValueArrayPool.RentArray(2);
  157. jsValues[0] = obj;
  158. jsValues[1] = properties;
  159. DefineProperties(thisObject, jsValues);
  160. _engine.JsValueArrayPool.ReturnArray(jsValues);
  161. }
  162. return obj;
  163. }
  164. public JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  165. {
  166. var oArg = arguments.At(0);
  167. var o = oArg.TryCast<ObjectInstance>();
  168. if (ReferenceEquals(o, null))
  169. {
  170. ExceptionHelper.ThrowTypeError(Engine);
  171. }
  172. var p = arguments.At(1);
  173. var name = TypeConverter.ToString(p);
  174. var attributes = arguments.At(2);
  175. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  176. o.DefineOwnProperty(name, desc, true);
  177. return o;
  178. }
  179. public JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
  180. {
  181. var oArg = arguments.At(0);
  182. var o = oArg.TryCast<ObjectInstance>();
  183. if (ReferenceEquals(o, null))
  184. {
  185. ExceptionHelper.ThrowTypeError(Engine);
  186. }
  187. var properties = arguments.At(1);
  188. var props = TypeConverter.ToObject(Engine, properties);
  189. var descriptors = new List<KeyValuePair<string, PropertyDescriptor>>();
  190. foreach (var p in props.GetOwnProperties())
  191. {
  192. if (!p.Value.Enumerable)
  193. {
  194. continue;
  195. }
  196. var descObj = props.Get(p.Key);
  197. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, descObj);
  198. descriptors.Add(new KeyValuePair<string, PropertyDescriptor>(p.Key, desc));
  199. }
  200. foreach (var pair in descriptors)
  201. {
  202. o.DefineOwnProperty(pair.Key, pair.Value, true);
  203. }
  204. return o;
  205. }
  206. public JsValue Seal(JsValue thisObject, JsValue[] arguments)
  207. {
  208. var oArg = arguments.At(0);
  209. var o = oArg.TryCast<ObjectInstance>();
  210. if (ReferenceEquals(o, null))
  211. {
  212. ExceptionHelper.ThrowTypeError(Engine);
  213. }
  214. var properties = new List<KeyValuePair<string, PropertyDescriptor>>(o.GetOwnProperties());
  215. foreach (var prop in properties)
  216. {
  217. var propertyDescriptor = prop.Value;
  218. if (propertyDescriptor.Configurable)
  219. {
  220. propertyDescriptor.Configurable = false;
  221. FastSetProperty(prop.Key, propertyDescriptor);
  222. }
  223. o.DefineOwnProperty(prop.Key, propertyDescriptor, true);
  224. }
  225. o.Extensible = false;
  226. return o;
  227. }
  228. public JsValue Freeze(JsValue thisObject, JsValue[] arguments)
  229. {
  230. var oArg = arguments.At(0);
  231. var o = oArg.TryCast<ObjectInstance>();
  232. if (ReferenceEquals(o, null))
  233. {
  234. ExceptionHelper.ThrowTypeError(Engine);
  235. }
  236. var properties = new List<KeyValuePair<string, PropertyDescriptor>>(o.GetOwnProperties());
  237. foreach (var p in properties)
  238. {
  239. var desc = o.GetOwnProperty(p.Key);
  240. if (desc.IsDataDescriptor())
  241. {
  242. if (desc.Writable)
  243. {
  244. var mutable = desc as PropertyDescriptor ?? new PropertyDescriptor(desc);
  245. mutable.Writable = false;
  246. desc = mutable;
  247. }
  248. }
  249. if (desc.Configurable)
  250. {
  251. var mutable = desc as PropertyDescriptor ?? new PropertyDescriptor(desc);
  252. mutable.Configurable = false;
  253. desc = mutable;
  254. }
  255. o.DefineOwnProperty(p.Key, desc, true);
  256. }
  257. o.Extensible = false;
  258. return o;
  259. }
  260. public JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  261. {
  262. var oArg = arguments.At(0);
  263. var o = oArg.TryCast<ObjectInstance>();
  264. if (ReferenceEquals(o, null))
  265. {
  266. ExceptionHelper.ThrowTypeError(Engine);
  267. }
  268. o.Extensible = false;
  269. return o;
  270. }
  271. public JsValue IsSealed(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. foreach (var prop in o.GetOwnProperties())
  280. {
  281. if (prop.Value.Configurable)
  282. {
  283. return false;
  284. }
  285. }
  286. if (o.Extensible == false)
  287. {
  288. return true;
  289. }
  290. return false;
  291. }
  292. public JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
  293. {
  294. var oArg = arguments.At(0);
  295. var o = oArg.TryCast<ObjectInstance>();
  296. if (ReferenceEquals(o, null))
  297. {
  298. ExceptionHelper.ThrowTypeError(Engine);
  299. }
  300. foreach (var pair in o.GetOwnProperties())
  301. {
  302. var desc = pair.Value;
  303. if (desc.IsDataDescriptor())
  304. {
  305. if (desc.Writable)
  306. {
  307. return false;
  308. }
  309. }
  310. if (desc.Configurable)
  311. {
  312. return false;
  313. }
  314. }
  315. if (o.Extensible == false)
  316. {
  317. return true;
  318. }
  319. return false;
  320. }
  321. public JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  322. {
  323. var oArg = arguments.At(0);
  324. var o = oArg.TryCast<ObjectInstance>();
  325. if (ReferenceEquals(o, null))
  326. {
  327. ExceptionHelper.ThrowTypeError(Engine);
  328. }
  329. return o.Extensible;
  330. }
  331. public JsValue Keys(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. var enumerableProperties = o.GetOwnProperties()
  340. .Where(x => x.Value.Enumerable)
  341. .ToArray();
  342. var n = enumerableProperties.Length;
  343. var args = _engine.JsValueArrayPool.RentArray(1);
  344. args[0] = n;
  345. var array = Engine.Array.Construct(args, (uint) n);
  346. _engine.JsValueArrayPool.ReturnArray(args);
  347. uint index = 0;
  348. foreach (var prop in enumerableProperties)
  349. {
  350. var p = prop.Key;
  351. array.SetIndexValue(index, p, updateLength: false);
  352. index++;
  353. }
  354. array.SetLength(index);
  355. return array;
  356. }
  357. }
  358. }