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 readonly Engine _engine;
  14. private ObjectConstructor(Engine engine) : base(engine, null, null, false)
  15. {
  16. _engine = engine;
  17. }
  18. public static ObjectConstructor CreateObjectConstructor(Engine engine)
  19. {
  20. var obj = new ObjectConstructor(engine);
  21. obj.Extensible = true;
  22. obj.PrototypeObject = ObjectPrototype.CreatePrototypeObject(engine, obj);
  23. obj.SetOwnProperty("length", new PropertyDescriptor(1, PropertyFlag.AllForbidden));
  24. obj.SetOwnProperty("prototype", new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden));
  25. return obj;
  26. }
  27. public void Configure()
  28. {
  29. Prototype = Engine.Function.PrototypeObject;
  30. FastAddProperty("getPrototypeOf", new ClrFunctionInstance(Engine, GetPrototypeOf, 1), true, false, true);
  31. FastAddProperty("getOwnPropertyDescriptor", new ClrFunctionInstance(Engine, GetOwnPropertyDescriptor, 2), true, false, true);
  32. FastAddProperty("getOwnPropertyNames", new ClrFunctionInstance(Engine, GetOwnPropertyNames, 1), true, false, true);
  33. FastAddProperty("create", new ClrFunctionInstance(Engine, Create, 2), true, false, true);
  34. FastAddProperty("defineProperty", new ClrFunctionInstance(Engine, DefineProperty, 3), true, false, true);
  35. FastAddProperty("defineProperties", new ClrFunctionInstance(Engine, DefineProperties, 2), true, false, true);
  36. FastAddProperty("seal", new ClrFunctionInstance(Engine, Seal, 1), true, false, true);
  37. FastAddProperty("freeze", new ClrFunctionInstance(Engine, Freeze, 1), true, false, true);
  38. FastAddProperty("preventExtensions", new ClrFunctionInstance(Engine, PreventExtensions, 1), true, false, true);
  39. FastAddProperty("isSealed", new ClrFunctionInstance(Engine, IsSealed, 1), true, false, true);
  40. FastAddProperty("isFrozen", new ClrFunctionInstance(Engine, IsFrozen, 1), true, false, true);
  41. FastAddProperty("isExtensible", new ClrFunctionInstance(Engine, IsExtensible, 1), true, false, true);
  42. FastAddProperty("keys", new ClrFunctionInstance(Engine, Keys, 1), true, false, true);
  43. }
  44. public ObjectPrototype PrototypeObject { get; private set; }
  45. /// <summary>
  46. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.1.1
  47. /// </summary>
  48. /// <param name="thisObject"></param>
  49. /// <param name="arguments"></param>
  50. /// <returns></returns>
  51. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  52. {
  53. if (arguments.Length == 0)
  54. {
  55. return Construct(arguments);
  56. }
  57. if(ReferenceEquals(arguments[0], Null) || ReferenceEquals(arguments[0], Undefined))
  58. {
  59. return Construct(arguments);
  60. }
  61. return TypeConverter.ToObject(_engine, arguments[0]);
  62. }
  63. /// <summary>
  64. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
  65. /// </summary>
  66. /// <param name="arguments"></param>
  67. /// <returns></returns>
  68. public ObjectInstance Construct(JsValue[] arguments)
  69. {
  70. if (arguments.Length > 0)
  71. {
  72. var value = arguments[0];
  73. var valueObj = value.TryCast<ObjectInstance>();
  74. if (!ReferenceEquals(valueObj, null))
  75. {
  76. return valueObj;
  77. }
  78. var type = value.Type;
  79. if (type == Types.String || type == Types.Number || type == Types.Boolean)
  80. {
  81. return TypeConverter.ToObject(_engine, value);
  82. }
  83. }
  84. var obj = new ObjectInstance(_engine)
  85. {
  86. Extensible = true,
  87. Prototype = Engine.Object.PrototypeObject
  88. };
  89. return obj;
  90. }
  91. public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  92. {
  93. var oArg = arguments.At(0);
  94. var o = oArg.TryCast<ObjectInstance>();
  95. if (ReferenceEquals(o, null))
  96. {
  97. throw new JavaScriptException(Engine.TypeError);
  98. }
  99. return o.Prototype ?? Null;
  100. }
  101. public JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  102. {
  103. var oArg = arguments.At(0);
  104. var o = oArg.TryCast<ObjectInstance>();
  105. if (ReferenceEquals(o, null))
  106. {
  107. throw new JavaScriptException(Engine.TypeError);
  108. }
  109. var p = arguments.At(1);
  110. var name = TypeConverter.ToString(p);
  111. var desc = o.GetOwnProperty(name);
  112. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  113. }
  114. public JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
  115. {
  116. var oArg = arguments.At(0);
  117. var o = oArg.TryCast<ObjectInstance>();
  118. if (ReferenceEquals(o, null))
  119. {
  120. throw new JavaScriptException(Engine.TypeError);
  121. }
  122. uint n = 0;
  123. ArrayInstance array = null;
  124. var ownProperties = o.GetOwnProperties().ToList();
  125. if (o is StringInstance s)
  126. {
  127. var length = s.PrimitiveValue.AsString().Length;
  128. array = Engine.Array.Construct(ownProperties.Count + length);
  129. for (var i = 0; i < length; i++)
  130. {
  131. array.SetIndexValue(n, TypeConverter.ToString(i), throwOnError: false);
  132. n++;
  133. }
  134. }
  135. array = array ?? Engine.Array.Construct(ownProperties.Count);
  136. foreach (var p in ownProperties)
  137. {
  138. array.SetIndexValue(n, p.Key, false);
  139. n++;
  140. }
  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) && !ReferenceEquals(oArg, Null))
  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 (!ReferenceEquals(properties, Undefined))
  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, throwOnError: false);
  348. index++;
  349. }
  350. return array;
  351. }
  352. }
  353. }