ObjectConstructor.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. throw new JavaScriptException(Engine.TypeError);
  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. throw new JavaScriptException(Engine.TypeError);
  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. throw new JavaScriptException(Engine.TypeError);
  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. throw new JavaScriptException(Engine.TypeError);
  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. DefineProperties(thisObject, new [] {obj, properties});
  157. }
  158. return obj;
  159. }
  160. public JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  161. {
  162. var oArg = arguments.At(0);
  163. var o = oArg.TryCast<ObjectInstance>();
  164. if (ReferenceEquals(o, null))
  165. {
  166. throw new JavaScriptException(Engine.TypeError);
  167. }
  168. var p = arguments.At(1);
  169. var name = TypeConverter.ToString(p);
  170. var attributes = arguments.At(2);
  171. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  172. o.DefineOwnProperty(name, desc, true);
  173. return o;
  174. }
  175. public JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
  176. {
  177. var oArg = arguments.At(0);
  178. var o = oArg.TryCast<ObjectInstance>();
  179. if (ReferenceEquals(o, null))
  180. {
  181. throw new JavaScriptException(Engine.TypeError);
  182. }
  183. var properties = arguments.At(1);
  184. var props = TypeConverter.ToObject(Engine, properties);
  185. var descriptors = new List<KeyValuePair<string, PropertyDescriptor>>();
  186. foreach (var p in props.GetOwnProperties())
  187. {
  188. if (!p.Value.Enumerable)
  189. {
  190. continue;
  191. }
  192. var descObj = props.Get(p.Key);
  193. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, descObj);
  194. descriptors.Add(new KeyValuePair<string, PropertyDescriptor>(p.Key, desc));
  195. }
  196. foreach (var pair in descriptors)
  197. {
  198. o.DefineOwnProperty(pair.Key, pair.Value, true);
  199. }
  200. return o;
  201. }
  202. public JsValue Seal(JsValue thisObject, JsValue[] arguments)
  203. {
  204. var oArg = arguments.At(0);
  205. var o = oArg.TryCast<ObjectInstance>();
  206. if (ReferenceEquals(o, null))
  207. {
  208. throw new JavaScriptException(Engine.TypeError);
  209. }
  210. var properties = new List<KeyValuePair<string, PropertyDescriptor>>(o.GetOwnProperties());
  211. foreach (var prop in properties)
  212. {
  213. var propertyDescriptor = prop.Value;
  214. if (propertyDescriptor.Configurable)
  215. {
  216. propertyDescriptor.Configurable = false;
  217. FastSetProperty(prop.Key, propertyDescriptor);
  218. }
  219. o.DefineOwnProperty(prop.Key, propertyDescriptor, true);
  220. }
  221. o.Extensible = false;
  222. return o;
  223. }
  224. public JsValue Freeze(JsValue thisObject, JsValue[] arguments)
  225. {
  226. var oArg = arguments.At(0);
  227. var o = oArg.TryCast<ObjectInstance>();
  228. if (ReferenceEquals(o, null))
  229. {
  230. throw new JavaScriptException(Engine.TypeError);
  231. }
  232. var properties = new List<KeyValuePair<string, PropertyDescriptor>>(o.GetOwnProperties());
  233. foreach (var p in properties)
  234. {
  235. var desc = o.GetOwnProperty(p.Key);
  236. if (desc.IsDataDescriptor())
  237. {
  238. if (desc.Writable)
  239. {
  240. var mutable = desc as PropertyDescriptor ?? new PropertyDescriptor(desc);
  241. mutable.Writable = false;
  242. desc = mutable;
  243. }
  244. }
  245. if (desc.Configurable)
  246. {
  247. var mutable = desc as PropertyDescriptor ?? new PropertyDescriptor(desc);
  248. mutable.Configurable = false;
  249. desc = mutable;
  250. }
  251. o.DefineOwnProperty(p.Key, desc, true);
  252. }
  253. o.Extensible = false;
  254. return o;
  255. }
  256. public JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  257. {
  258. var oArg = arguments.At(0);
  259. var o = oArg.TryCast<ObjectInstance>();
  260. if (ReferenceEquals(o, null))
  261. {
  262. throw new JavaScriptException(Engine.TypeError);
  263. }
  264. o.Extensible = false;
  265. return o;
  266. }
  267. public JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
  268. {
  269. var oArg = arguments.At(0);
  270. var o = oArg.TryCast<ObjectInstance>();
  271. if (ReferenceEquals(o, null))
  272. {
  273. throw new JavaScriptException(Engine.TypeError);
  274. }
  275. foreach (var prop in o.GetOwnProperties())
  276. {
  277. if (prop.Value.Configurable)
  278. {
  279. return false;
  280. }
  281. }
  282. if (o.Extensible == false)
  283. {
  284. return true;
  285. }
  286. return false;
  287. }
  288. public JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
  289. {
  290. var oArg = arguments.At(0);
  291. var o = oArg.TryCast<ObjectInstance>();
  292. if (ReferenceEquals(o, null))
  293. {
  294. throw new JavaScriptException(Engine.TypeError);
  295. }
  296. foreach (var pair in o.GetOwnProperties())
  297. {
  298. var desc = pair.Value;
  299. if (desc.IsDataDescriptor())
  300. {
  301. if (desc.Writable)
  302. {
  303. return false;
  304. }
  305. }
  306. if (desc.Configurable)
  307. {
  308. return false;
  309. }
  310. }
  311. if (o.Extensible == false)
  312. {
  313. return true;
  314. }
  315. return false;
  316. }
  317. public JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  318. {
  319. var oArg = arguments.At(0);
  320. var o = oArg.TryCast<ObjectInstance>();
  321. if (ReferenceEquals(o, null))
  322. {
  323. throw new JavaScriptException(Engine.TypeError);
  324. }
  325. return o.Extensible;
  326. }
  327. public JsValue Keys(JsValue thisObject, JsValue[] arguments)
  328. {
  329. var oArg = arguments.At(0);
  330. var o = oArg.TryCast<ObjectInstance>();
  331. if (ReferenceEquals(o, null))
  332. {
  333. throw new JavaScriptException(Engine.TypeError);
  334. }
  335. var enumerableProperties = o.GetOwnProperties()
  336. .Where(x => x.Value.Enumerable)
  337. .ToArray();
  338. var n = enumerableProperties.Length;
  339. var args = _engine.JsValueArrayPool.RentArray(1);
  340. args[0] = n;
  341. var array = Engine.Array.Construct(args, (uint) n);
  342. _engine.JsValueArrayPool.ReturnArray(args);
  343. uint index = 0;
  344. foreach (var prop in enumerableProperties)
  345. {
  346. var p = prop.Key;
  347. array.SetIndexValue(index, p, updateLength: false);
  348. index++;
  349. }
  350. array.SetLength(index);
  351. return array;
  352. }
  353. }
  354. }