ObjectConstructor.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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, lengthFlags), 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 (!ReferenceEquals(this, newTarget) && !newTarget.IsUndefined())
  133. {
  134. return OrdinaryCreateFromConstructor(newTarget, _engine.Object.PrototypeObject, (engine, state) => new ObjectInstance(engine));
  135. }
  136. if (arguments.Length > 0)
  137. {
  138. var value = arguments[0];
  139. if (value is ObjectInstance oi)
  140. {
  141. return oi;
  142. }
  143. var type = value.Type;
  144. if (type == Types.String || type == Types.Number || type == Types.Boolean)
  145. {
  146. return TypeConverter.ToObject(_engine, value);
  147. }
  148. }
  149. var obj = new ObjectInstance(_engine)
  150. {
  151. _prototype = Engine.Object.PrototypeObject
  152. };
  153. return obj;
  154. }
  155. internal ObjectInstance Construct(int propertyCount)
  156. {
  157. var obj = new ObjectInstance(_engine)
  158. {
  159. _prototype = Engine.Object.PrototypeObject,
  160. };
  161. obj.SetProperties(propertyCount > 0 ? new PropertyDictionary(propertyCount, checkExistingKeys: true) : null);
  162. return obj;
  163. }
  164. public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  165. {
  166. var obj = TypeConverter.ToObject(_engine, arguments.At(0));
  167. return obj.Prototype ?? Null;
  168. }
  169. private JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  170. {
  171. var oArg = arguments.At(0);
  172. TypeConverter.CheckObjectCoercible(_engine, oArg);
  173. var prototype = arguments.At(1);
  174. if (!prototype.IsObject() && !prototype.IsNull())
  175. {
  176. ExceptionHelper.ThrowTypeError(_engine, $"Object prototype may only be an Object or null: {prototype}");
  177. }
  178. if (!(oArg is ObjectInstance o))
  179. {
  180. return oArg;
  181. }
  182. if (!o.SetPrototypeOf(prototype))
  183. {
  184. ExceptionHelper.ThrowTypeError(_engine);
  185. }
  186. return o;
  187. }
  188. internal JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  189. {
  190. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  191. var p = arguments.At(1);
  192. var name = TypeConverter.ToPropertyKey(p);
  193. var desc = o.GetOwnProperty(name);
  194. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  195. }
  196. private JsValue GetOwnPropertyDescriptors(JsValue thisObject, JsValue[] arguments)
  197. {
  198. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  199. var ownKeys = o.GetOwnPropertyKeys();
  200. var descriptors = _engine.Object.Construct(0);
  201. foreach (var key in ownKeys)
  202. {
  203. var desc = o.GetOwnProperty(key);
  204. var descriptor = PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  205. if (!ReferenceEquals(descriptor, Undefined))
  206. {
  207. descriptors.CreateDataProperty(key, descriptor);
  208. }
  209. }
  210. return descriptors;
  211. }
  212. public JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
  213. {
  214. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  215. var names = o.GetOwnPropertyKeys(Types.String);
  216. return _engine.Array.ConstructFast(names);
  217. }
  218. private JsValue GetOwnPropertySymbols(JsValue thisObject, JsValue[] arguments)
  219. {
  220. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  221. var keys = o.GetOwnPropertyKeys(Types.Symbol);
  222. return _engine.Array.ConstructFast(keys);
  223. }
  224. private JsValue Create(JsValue thisObject, JsValue[] arguments)
  225. {
  226. var prototype = arguments.At(0);
  227. if (!prototype.IsObject() && !prototype.IsNull())
  228. {
  229. ExceptionHelper.ThrowTypeError(_engine, "Object prototype may only be an Object or null: " + prototype);
  230. }
  231. var obj = Engine.Object.Construct(Arguments.Empty);
  232. obj._prototype = prototype.IsNull() ? null : prototype.AsObject();
  233. var properties = arguments.At(1);
  234. if (!properties.IsUndefined())
  235. {
  236. var jsValues = _engine._jsValueArrayPool.RentArray(2);
  237. jsValues[0] = obj;
  238. jsValues[1] = properties;
  239. DefineProperties(thisObject, jsValues);
  240. _engine._jsValueArrayPool.ReturnArray(jsValues);
  241. }
  242. return obj;
  243. }
  244. private JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  245. {
  246. if (!(arguments.At(0) is ObjectInstance o))
  247. {
  248. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Object.defineProperty called on non-object");
  249. }
  250. var p = arguments.At(1);
  251. var name = TypeConverter.ToPropertyKey(p);
  252. var attributes = arguments.At(2);
  253. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  254. o.DefinePropertyOrThrow(name, desc);
  255. return arguments.At(0);
  256. }
  257. private JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
  258. {
  259. if (!(arguments.At(0) is ObjectInstance o))
  260. {
  261. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Object.defineProperty called on non-object");
  262. }
  263. var properties = arguments.At(1);
  264. var props = TypeConverter.ToObject(Engine, 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(Engine, 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(_engine, arguments.At(0));
  397. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Key);
  398. }
  399. private JsValue Values(JsValue thisObject, JsValue[] arguments)
  400. {
  401. var o = TypeConverter.ToObject(_engine, 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. }