ObjectConstructor.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. using System.Collections.Generic;
  2. using Jint.Collections;
  3. using Jint.Native.Function;
  4. using Jint.Native.Iterator;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Interop;
  8. namespace Jint.Native.Object
  9. {
  10. public sealed class ObjectConstructor : FunctionInstance, IConstructor
  11. {
  12. private static readonly JsString _name = new JsString("delegate");
  13. private ObjectConstructor(Engine engine)
  14. : base(engine, _name)
  15. {
  16. }
  17. public static ObjectConstructor CreateObjectConstructor(Engine engine)
  18. {
  19. var obj = new ObjectConstructor(engine);
  20. obj.PrototypeObject = ObjectPrototype.CreatePrototypeObject(engine, obj);
  21. obj._length = PropertyDescriptor.AllForbiddenDescriptor.NumberOne;
  22. obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  23. return obj;
  24. }
  25. protected override void Initialize()
  26. {
  27. _prototype = Engine.Function.PrototypeObject;
  28. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  29. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  30. var properties = new PropertyDictionary(15, checkExistingKeys: false)
  31. {
  32. ["assign"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "assign", Assign, 2, lengthFlags), propertyFlags),
  33. ["entries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "entries", Entries, 1, lengthFlags), propertyFlags),
  34. ["fromEntries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "fromEntries", FromEntries, 1, lengthFlags), propertyFlags),
  35. ["getPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getPrototypeOf", GetPrototypeOf, 1), propertyFlags),
  36. ["getOwnPropertyDescriptor"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyDescriptor", GetOwnPropertyDescriptor, 2), propertyFlags),
  37. ["getOwnPropertyDescriptors"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyDescriptors", GetOwnPropertyDescriptors, 1, lengthFlags), propertyFlags),
  38. ["getOwnPropertyNames"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyNames", GetOwnPropertyNames, 1), propertyFlags),
  39. ["getOwnPropertySymbols"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertySymbols", GetOwnPropertySymbols, 1, lengthFlags), propertyFlags),
  40. ["create"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "create", Create, 2), propertyFlags),
  41. ["defineProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "defineProperty", DefineProperty, 3), propertyFlags),
  42. ["defineProperties"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "defineProperties", DefineProperties, 2), propertyFlags),
  43. ["is"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "is", Is, 2, lengthFlags), propertyFlags),
  44. ["seal"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "seal", Seal, 1), propertyFlags),
  45. ["freeze"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "freeze", Freeze, 1), propertyFlags),
  46. ["preventExtensions"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "preventExtensions", PreventExtensions, 1), propertyFlags),
  47. ["isSealed"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isSealed", IsSealed, 1), propertyFlags),
  48. ["isFrozen"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isFrozen", IsFrozen, 1), propertyFlags),
  49. ["isExtensible"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isExtensible", IsExtensible, 1), propertyFlags),
  50. ["keys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "keys", Keys, 1, lengthFlags), propertyFlags),
  51. ["values"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "values", Values, 1, lengthFlags), propertyFlags),
  52. ["setPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setPrototypeOf", SetPrototypeOf, 2, lengthFlags), propertyFlags)
  53. };
  54. SetProperties(properties);
  55. }
  56. private JsValue Assign(JsValue thisObject, JsValue[] arguments)
  57. {
  58. var to = TypeConverter.ToObject(_engine, arguments.At(0));
  59. if (arguments.Length < 2)
  60. {
  61. return to;
  62. }
  63. for (var i = 1; i < arguments.Length; i++)
  64. {
  65. var nextSource = arguments[i];
  66. if (nextSource.IsNullOrUndefined())
  67. {
  68. continue;
  69. }
  70. var from = TypeConverter.ToObject(_engine, nextSource);
  71. var keys = from.GetOwnPropertyKeys();
  72. foreach (var nextKey in keys)
  73. {
  74. var desc = from.GetOwnProperty(nextKey);
  75. if (desc != PropertyDescriptor.Undefined && desc.Enumerable)
  76. {
  77. var propValue = from.Get(nextKey);
  78. to.Set(nextKey, propValue, throwOnError: true);
  79. }
  80. }
  81. }
  82. return to;
  83. }
  84. private JsValue Entries(JsValue thisObject, JsValue[] arguments)
  85. {
  86. var obj = TypeConverter.ToObject(_engine, arguments.At(0));
  87. var nameList = obj.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.KeyValue);
  88. return nameList;
  89. }
  90. private JsValue FromEntries(JsValue thisObject, JsValue[] arguments)
  91. {
  92. var iterable = arguments.At(0);
  93. TypeConverter.CheckObjectCoercible(_engine, iterable);
  94. var obj = _engine.Object.Construct(0);
  95. var adder = CreateDataPropertyOnObject.Instance;
  96. var iterator = arguments.At(0).GetIterator(_engine);
  97. IteratorProtocol.AddEntriesFromIterable(obj, iterator, adder);
  98. return obj;
  99. }
  100. private static JsValue Is(JsValue thisObject, JsValue[] arguments)
  101. {
  102. return SameValue(arguments.At(0), arguments.At(1));
  103. }
  104. public ObjectPrototype PrototypeObject { get; private set; }
  105. /// <summary>
  106. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.1.1
  107. /// </summary>
  108. /// <param name="thisObject"></param>
  109. /// <param name="arguments"></param>
  110. /// <returns></returns>
  111. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  112. {
  113. if (arguments.Length == 0)
  114. {
  115. return Construct(arguments);
  116. }
  117. if(arguments[0].IsNullOrUndefined())
  118. {
  119. return Construct(arguments);
  120. }
  121. return TypeConverter.ToObject(_engine, arguments[0]);
  122. }
  123. /// <summary>
  124. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
  125. /// </summary>
  126. public ObjectInstance Construct(JsValue[] arguments)
  127. {
  128. return Construct(arguments, this);
  129. }
  130. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  131. {
  132. if (arguments.Length > 0)
  133. {
  134. var value = arguments[0];
  135. if (value is ObjectInstance oi)
  136. {
  137. return oi;
  138. }
  139. var type = value.Type;
  140. if (type == Types.String || type == Types.Number || type == Types.Boolean)
  141. {
  142. return TypeConverter.ToObject(_engine, value);
  143. }
  144. }
  145. var obj = new ObjectInstance(_engine)
  146. {
  147. _prototype = Engine.Object.PrototypeObject
  148. };
  149. return obj;
  150. }
  151. internal ObjectInstance Construct(int propertyCount)
  152. {
  153. var obj = new ObjectInstance(_engine)
  154. {
  155. _prototype = Engine.Object.PrototypeObject,
  156. };
  157. obj.SetProperties(propertyCount > 0 ? new PropertyDictionary(propertyCount, checkExistingKeys: true) : null);
  158. return obj;
  159. }
  160. public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  161. {
  162. var obj = TypeConverter.ToObject(_engine, arguments.At(0));
  163. return obj.Prototype ?? Null;
  164. }
  165. private JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  166. {
  167. var oArg = arguments.At(0);
  168. TypeConverter.CheckObjectCoercible(_engine, oArg);
  169. var prototype = arguments.At(1);
  170. if (!prototype.IsObject() && !prototype.IsNull())
  171. {
  172. ExceptionHelper.ThrowTypeError(_engine, $"Object prototype may only be an Object or null: {prototype}");
  173. }
  174. if (!(oArg is ObjectInstance o))
  175. {
  176. return oArg;
  177. }
  178. if (!o.SetPrototypeOf(prototype))
  179. {
  180. ExceptionHelper.ThrowTypeError(_engine);
  181. }
  182. return o;
  183. }
  184. internal JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  185. {
  186. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  187. var p = arguments.At(1);
  188. var name = TypeConverter.ToPropertyKey(p);
  189. var desc = o.GetOwnProperty(name);
  190. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  191. }
  192. private JsValue GetOwnPropertyDescriptors(JsValue thisObject, JsValue[] arguments)
  193. {
  194. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  195. var ownKeys = o.GetOwnPropertyKeys();
  196. var descriptors = _engine.Object.Construct(0);
  197. foreach (var key in ownKeys)
  198. {
  199. var desc = o.GetOwnProperty(key);
  200. var descriptor = PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  201. if (descriptor != Undefined)
  202. {
  203. descriptors.CreateDataProperty(key, descriptor);
  204. }
  205. }
  206. return descriptors;
  207. }
  208. public JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
  209. {
  210. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  211. var names = o.GetOwnPropertyKeys(Types.String);
  212. return _engine.Array.Construct(names.ToArray());
  213. }
  214. private JsValue GetOwnPropertySymbols(JsValue thisObject, JsValue[] arguments)
  215. {
  216. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  217. var keys = o.GetOwnPropertyKeys(Types.Symbol);
  218. return _engine.Array.Construct(keys.ToArray());
  219. }
  220. private JsValue Create(JsValue thisObject, JsValue[] arguments)
  221. {
  222. var prototype = arguments.At(0);
  223. if (!prototype.IsObject() && !prototype.IsNull())
  224. {
  225. ExceptionHelper.ThrowTypeError(_engine, "Object prototype may only be an Object or null: " + prototype);
  226. }
  227. var obj = Engine.Object.Construct(Arguments.Empty);
  228. obj._prototype = prototype.IsNull() ? null : prototype.AsObject();
  229. var properties = arguments.At(1);
  230. if (!properties.IsUndefined())
  231. {
  232. var jsValues = _engine._jsValueArrayPool.RentArray(2);
  233. jsValues[0] = obj;
  234. jsValues[1] = properties;
  235. DefineProperties(thisObject, jsValues);
  236. _engine._jsValueArrayPool.ReturnArray(jsValues);
  237. }
  238. return obj;
  239. }
  240. private JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  241. {
  242. var o = arguments.As<ObjectInstance>(0, _engine);
  243. var p = arguments.At(1);
  244. var name = TypeConverter.ToPropertyKey(p);
  245. var attributes = arguments.At(2);
  246. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  247. o.DefinePropertyOrThrow(name, desc);
  248. return o;
  249. }
  250. private JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
  251. {
  252. var o = arguments.As<ObjectInstance>(0, _engine);
  253. var properties = arguments.At(1);
  254. var props = TypeConverter.ToObject(Engine, properties);
  255. var descriptors = new List<KeyValuePair<JsValue, PropertyDescriptor>>();
  256. foreach (var p in props.GetOwnProperties())
  257. {
  258. if (!p.Value.Enumerable)
  259. {
  260. continue;
  261. }
  262. var descObj = props.Get(p.Key, props);
  263. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, descObj);
  264. descriptors.Add(new KeyValuePair<JsValue, PropertyDescriptor>(p.Key, desc));
  265. }
  266. foreach (var pair in descriptors)
  267. {
  268. o.DefinePropertyOrThrow(pair.Key, pair.Value);
  269. }
  270. return o;
  271. }
  272. private JsValue Seal(JsValue thisObject, JsValue[] arguments)
  273. {
  274. if (!(arguments.At(0) is ObjectInstance o))
  275. {
  276. return arguments.At(0);
  277. }
  278. var properties = new List<KeyValuePair<JsValue, PropertyDescriptor>>(o.GetOwnProperties());
  279. foreach (var prop in properties)
  280. {
  281. var propertyDescriptor = prop.Value;
  282. if (propertyDescriptor.Configurable)
  283. {
  284. propertyDescriptor.Configurable = false;
  285. FastSetProperty(prop.Key, propertyDescriptor);
  286. }
  287. o.DefinePropertyOrThrow(prop.Key, propertyDescriptor);
  288. }
  289. o.PreventExtensions();
  290. return o;
  291. }
  292. private static JsValue Freeze(JsValue thisObject, JsValue[] arguments)
  293. {
  294. if (!(arguments.At(0) is ObjectInstance o))
  295. {
  296. return arguments.At(0);
  297. }
  298. foreach (var p in o.GetOwnProperties())
  299. {
  300. var desc = o.GetOwnProperty(p.Key);
  301. if (desc.IsDataDescriptor())
  302. {
  303. if (desc.Writable)
  304. {
  305. var mutable = desc;
  306. mutable.Writable = false;
  307. desc = mutable;
  308. }
  309. }
  310. if (desc.Configurable)
  311. {
  312. var mutable = desc;
  313. mutable.Configurable = false;
  314. desc = mutable;
  315. }
  316. o.DefinePropertyOrThrow(p.Key, desc);
  317. }
  318. o.PreventExtensions();
  319. return o;
  320. }
  321. private static JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  322. {
  323. if (!(arguments.At(0) is ObjectInstance o))
  324. {
  325. return arguments.At(0);
  326. }
  327. o.PreventExtensions();
  328. return o;
  329. }
  330. private static JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
  331. {
  332. if (!(arguments.At(0) is ObjectInstance o))
  333. {
  334. return arguments.At(0);
  335. }
  336. foreach (var prop in o.GetOwnProperties())
  337. {
  338. if (prop.Value.Configurable)
  339. {
  340. return false;
  341. }
  342. }
  343. if (o.Extensible == false)
  344. {
  345. return true;
  346. }
  347. return false;
  348. }
  349. private static JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
  350. {
  351. if (!(arguments.At(0) is ObjectInstance o))
  352. {
  353. return arguments.At(0);
  354. }
  355. foreach (var pair in o.GetOwnProperties())
  356. {
  357. var desc = pair.Value;
  358. if (desc.IsDataDescriptor())
  359. {
  360. if (desc.Writable)
  361. {
  362. return false;
  363. }
  364. }
  365. if (desc.Configurable)
  366. {
  367. return false;
  368. }
  369. }
  370. if (o.Extensible == false)
  371. {
  372. return true;
  373. }
  374. return false;
  375. }
  376. private static JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  377. {
  378. if (!(arguments.At(0) is ObjectInstance o))
  379. {
  380. return arguments.At(0);
  381. }
  382. return o.Extensible;
  383. }
  384. private JsValue Keys(JsValue thisObject, JsValue[] arguments)
  385. {
  386. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  387. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Key);
  388. }
  389. private JsValue Values(JsValue thisObject, JsValue[] arguments)
  390. {
  391. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  392. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Value);
  393. }
  394. private sealed class CreateDataPropertyOnObject : ICallable
  395. {
  396. internal static readonly CreateDataPropertyOnObject Instance = new CreateDataPropertyOnObject();
  397. private CreateDataPropertyOnObject()
  398. {
  399. }
  400. public JsValue Call(JsValue thisObject, JsValue[] arguments)
  401. {
  402. var o = (ObjectInstance) thisObject;
  403. var key = arguments.At(0);
  404. var value = arguments.At(1);
  405. var propertyKey = TypeConverter.ToPropertyKey(key);
  406. o.CreateDataPropertyOrThrow(propertyKey, value);
  407. return Undefined;
  408. }
  409. }
  410. }
  411. }