ObjectConstructor.cs 19 KB

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