ObjectConstructor.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  126. {
  127. if (!ReferenceEquals(this, newTarget) && !newTarget.IsUndefined())
  128. {
  129. return OrdinaryCreateFromConstructor(
  130. newTarget,
  131. static intrinsics => intrinsics.Object.PrototypeObject,
  132. (engine, realm, state) => new ObjectInstance(engine));
  133. }
  134. if (arguments.Length > 0)
  135. {
  136. var value = arguments[0];
  137. if (value is ObjectInstance oi)
  138. {
  139. return oi;
  140. }
  141. var type = value.Type;
  142. if (type == Types.String || type == Types.Number || type == Types.Boolean)
  143. {
  144. return TypeConverter.ToObject(_realm, value);
  145. }
  146. }
  147. return new ObjectInstance(_engine);
  148. }
  149. internal ObjectInstance Construct(int propertyCount)
  150. {
  151. var obj = new ObjectInstance(_engine);
  152. obj.SetProperties(propertyCount > 0 ? new PropertyDictionary(propertyCount, checkExistingKeys: true) : null);
  153. return obj;
  154. }
  155. public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  156. {
  157. var obj = TypeConverter.ToObject(_realm, arguments.At(0));
  158. return obj.Prototype ?? Null;
  159. }
  160. private JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  161. {
  162. var oArg = arguments.At(0);
  163. TypeConverter.CheckObjectCoercible(_engine, oArg);
  164. var prototype = arguments.At(1);
  165. if (!prototype.IsObject() && !prototype.IsNull())
  166. {
  167. ExceptionHelper.ThrowTypeError(_realm, $"Object prototype may only be an Object or null: {prototype}");
  168. }
  169. if (!(oArg is ObjectInstance o))
  170. {
  171. return oArg;
  172. }
  173. if (!o.SetPrototypeOf(prototype))
  174. {
  175. ExceptionHelper.ThrowTypeError(_realm);
  176. }
  177. return o;
  178. }
  179. private JsValue HasOwn(JsValue thisObject, JsValue[] arguments)
  180. {
  181. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  182. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  183. return o.HasOwnProperty(property) ? JsBoolean.True : JsBoolean.False;
  184. }
  185. internal JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  186. {
  187. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  188. var p = arguments.At(1);
  189. var name = TypeConverter.ToPropertyKey(p);
  190. var desc = o.GetOwnProperty(name);
  191. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  192. }
  193. private JsValue GetOwnPropertyDescriptors(JsValue thisObject, JsValue[] arguments)
  194. {
  195. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  196. var ownKeys = o.GetOwnPropertyKeys();
  197. var descriptors = _realm.Intrinsics.Object.Construct(0);
  198. foreach (var key in ownKeys)
  199. {
  200. var desc = o.GetOwnProperty(key);
  201. var descriptor = PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  202. if (!ReferenceEquals(descriptor, Undefined))
  203. {
  204. descriptors.CreateDataProperty(key, descriptor);
  205. }
  206. }
  207. return descriptors;
  208. }
  209. public JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
  210. {
  211. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  212. var names = o.GetOwnPropertyKeys(Types.String);
  213. return _realm.Intrinsics.Array.ConstructFast(names);
  214. }
  215. private JsValue GetOwnPropertySymbols(JsValue thisObject, JsValue[] arguments)
  216. {
  217. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  218. var keys = o.GetOwnPropertyKeys(Types.Symbol);
  219. return _realm.Intrinsics.Array.ConstructFast(keys);
  220. }
  221. private JsValue Create(JsValue thisObject, JsValue[] arguments)
  222. {
  223. var prototype = arguments.At(0);
  224. if (!prototype.IsObject() && !prototype.IsNull())
  225. {
  226. ExceptionHelper.ThrowTypeError(_realm, "Object prototype may only be an Object or null: " + prototype);
  227. }
  228. var obj = Engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
  229. obj._prototype = prototype.IsNull() ? null : prototype.AsObject();
  230. var properties = arguments.At(1);
  231. if (!properties.IsUndefined())
  232. {
  233. var jsValues = _engine._jsValueArrayPool.RentArray(2);
  234. jsValues[0] = obj;
  235. jsValues[1] = properties;
  236. DefineProperties(thisObject, jsValues);
  237. _engine._jsValueArrayPool.ReturnArray(jsValues);
  238. }
  239. return obj;
  240. }
  241. private JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  242. {
  243. var o = arguments.At(0) as ObjectInstance;
  244. if (o is null)
  245. {
  246. ExceptionHelper.ThrowTypeError(_realm, "Object.defineProperty called on non-object");
  247. }
  248. var p = arguments.At(1);
  249. var name = TypeConverter.ToPropertyKey(p);
  250. var attributes = arguments.At(2);
  251. var desc = PropertyDescriptor.ToPropertyDescriptor(_realm, attributes);
  252. o.DefinePropertyOrThrow(name, desc);
  253. return arguments.At(0);
  254. }
  255. private JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
  256. {
  257. var o = arguments.At(0) as ObjectInstance;
  258. if (o is null)
  259. {
  260. ExceptionHelper.ThrowTypeError(_realm, "Object.defineProperty called on non-object");
  261. }
  262. var properties = arguments.At(1);
  263. var props = TypeConverter.ToObject(_realm, properties);
  264. var descriptors = new List<KeyValuePair<JsValue, PropertyDescriptor>>();
  265. foreach (var p in props.GetOwnProperties())
  266. {
  267. if (!p.Value.Enumerable)
  268. {
  269. continue;
  270. }
  271. var descObj = props.Get(p.Key, props);
  272. var desc = PropertyDescriptor.ToPropertyDescriptor(_realm, descObj);
  273. descriptors.Add(new KeyValuePair<JsValue, PropertyDescriptor>(p.Key, desc));
  274. }
  275. foreach (var pair in descriptors)
  276. {
  277. o.DefinePropertyOrThrow(pair.Key, pair.Value);
  278. }
  279. return o;
  280. }
  281. private JsValue Seal(JsValue thisObject, JsValue[] arguments)
  282. {
  283. if (!(arguments.At(0) is ObjectInstance o))
  284. {
  285. return arguments.At(0);
  286. }
  287. var properties = new List<KeyValuePair<JsValue, PropertyDescriptor>>(o.GetOwnProperties());
  288. foreach (var prop in properties)
  289. {
  290. var propertyDescriptor = prop.Value;
  291. if (propertyDescriptor.Configurable)
  292. {
  293. propertyDescriptor.Configurable = false;
  294. FastSetProperty(prop.Key, propertyDescriptor);
  295. }
  296. o.DefinePropertyOrThrow(prop.Key, propertyDescriptor);
  297. }
  298. o.PreventExtensions();
  299. return o;
  300. }
  301. private static JsValue Freeze(JsValue thisObject, JsValue[] arguments)
  302. {
  303. if (!(arguments.At(0) is ObjectInstance o))
  304. {
  305. return arguments.At(0);
  306. }
  307. foreach (var p in o.GetOwnProperties())
  308. {
  309. var desc = o.GetOwnProperty(p.Key);
  310. if (desc.IsDataDescriptor())
  311. {
  312. if (desc.Writable)
  313. {
  314. var mutable = desc;
  315. mutable.Writable = false;
  316. desc = mutable;
  317. }
  318. }
  319. if (desc.Configurable)
  320. {
  321. var mutable = desc;
  322. mutable.Configurable = false;
  323. desc = mutable;
  324. }
  325. o.DefinePropertyOrThrow(p.Key, desc);
  326. }
  327. o.PreventExtensions();
  328. return o;
  329. }
  330. private static JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  331. {
  332. if (!(arguments.At(0) is ObjectInstance o))
  333. {
  334. return arguments.At(0);
  335. }
  336. o.PreventExtensions();
  337. return o;
  338. }
  339. private static JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
  340. {
  341. if (!(arguments.At(0) is ObjectInstance o))
  342. {
  343. return arguments.At(0);
  344. }
  345. foreach (var prop in o.GetOwnProperties())
  346. {
  347. if (prop.Value.Configurable)
  348. {
  349. return false;
  350. }
  351. }
  352. if (o.Extensible == false)
  353. {
  354. return true;
  355. }
  356. return false;
  357. }
  358. private static JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
  359. {
  360. if (!(arguments.At(0) is ObjectInstance o))
  361. {
  362. return arguments.At(0);
  363. }
  364. foreach (var pair in o.GetOwnProperties())
  365. {
  366. var desc = pair.Value;
  367. if (desc.IsDataDescriptor())
  368. {
  369. if (desc.Writable)
  370. {
  371. return false;
  372. }
  373. }
  374. if (desc.Configurable)
  375. {
  376. return false;
  377. }
  378. }
  379. if (o.Extensible == false)
  380. {
  381. return true;
  382. }
  383. return false;
  384. }
  385. private static JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  386. {
  387. if (!(arguments.At(0) is ObjectInstance o))
  388. {
  389. return arguments.At(0);
  390. }
  391. return o.Extensible;
  392. }
  393. private JsValue Keys(JsValue thisObject, JsValue[] arguments)
  394. {
  395. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  396. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Key);
  397. }
  398. private JsValue Values(JsValue thisObject, JsValue[] arguments)
  399. {
  400. var o = TypeConverter.ToObject(_realm, arguments.At(0));
  401. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Value);
  402. }
  403. private sealed class CreateDataPropertyOnObject : ICallable
  404. {
  405. internal static readonly CreateDataPropertyOnObject Instance = new CreateDataPropertyOnObject();
  406. private CreateDataPropertyOnObject()
  407. {
  408. }
  409. public JsValue Call(JsValue thisObject, JsValue[] arguments)
  410. {
  411. var o = (ObjectInstance) thisObject;
  412. var key = arguments.At(0);
  413. var value = arguments.At(1);
  414. var propertyKey = TypeConverter.ToPropertyKey(key);
  415. o.CreateDataPropertyOrThrow(propertyKey, value);
  416. return Undefined;
  417. }
  418. }
  419. }
  420. }