ObjectConstructor.cs 14 KB

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