ObjectConstructor.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. if (!(arguments.At(0) is ObjectInstance o))
  243. {
  244. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Object.defineProperty called on non-object");
  245. }
  246. var p = arguments.At(1);
  247. var name = TypeConverter.ToPropertyKey(p);
  248. var attributes = arguments.At(2);
  249. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  250. o.DefinePropertyOrThrow(name, desc);
  251. return arguments.At(0);
  252. }
  253. private JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
  254. {
  255. if (!(arguments.At(0) is ObjectInstance o))
  256. {
  257. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Object.defineProperty called on non-object");
  258. }
  259. var properties = arguments.At(1);
  260. var props = TypeConverter.ToObject(Engine, properties);
  261. var descriptors = new List<KeyValuePair<JsValue, PropertyDescriptor>>();
  262. foreach (var p in props.GetOwnProperties())
  263. {
  264. if (!p.Value.Enumerable)
  265. {
  266. continue;
  267. }
  268. var descObj = props.Get(p.Key, props);
  269. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, descObj);
  270. descriptors.Add(new KeyValuePair<JsValue, PropertyDescriptor>(p.Key, desc));
  271. }
  272. foreach (var pair in descriptors)
  273. {
  274. o.DefinePropertyOrThrow(pair.Key, pair.Value);
  275. }
  276. return o;
  277. }
  278. private JsValue Seal(JsValue thisObject, JsValue[] arguments)
  279. {
  280. if (!(arguments.At(0) is ObjectInstance o))
  281. {
  282. return arguments.At(0);
  283. }
  284. var properties = new List<KeyValuePair<JsValue, PropertyDescriptor>>(o.GetOwnProperties());
  285. foreach (var prop in properties)
  286. {
  287. var propertyDescriptor = prop.Value;
  288. if (propertyDescriptor.Configurable)
  289. {
  290. propertyDescriptor.Configurable = false;
  291. FastSetProperty(prop.Key, propertyDescriptor);
  292. }
  293. o.DefinePropertyOrThrow(prop.Key, propertyDescriptor);
  294. }
  295. o.PreventExtensions();
  296. return o;
  297. }
  298. private static JsValue Freeze(JsValue thisObject, JsValue[] arguments)
  299. {
  300. if (!(arguments.At(0) is ObjectInstance o))
  301. {
  302. return arguments.At(0);
  303. }
  304. foreach (var p in o.GetOwnProperties())
  305. {
  306. var desc = o.GetOwnProperty(p.Key);
  307. if (desc.IsDataDescriptor())
  308. {
  309. if (desc.Writable)
  310. {
  311. var mutable = desc;
  312. mutable.Writable = false;
  313. desc = mutable;
  314. }
  315. }
  316. if (desc.Configurable)
  317. {
  318. var mutable = desc;
  319. mutable.Configurable = false;
  320. desc = mutable;
  321. }
  322. o.DefinePropertyOrThrow(p.Key, desc);
  323. }
  324. o.PreventExtensions();
  325. return o;
  326. }
  327. private static JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  328. {
  329. if (!(arguments.At(0) is ObjectInstance o))
  330. {
  331. return arguments.At(0);
  332. }
  333. o.PreventExtensions();
  334. return o;
  335. }
  336. private static JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
  337. {
  338. if (!(arguments.At(0) is ObjectInstance o))
  339. {
  340. return arguments.At(0);
  341. }
  342. foreach (var prop in o.GetOwnProperties())
  343. {
  344. if (prop.Value.Configurable)
  345. {
  346. return false;
  347. }
  348. }
  349. if (o.Extensible == false)
  350. {
  351. return true;
  352. }
  353. return false;
  354. }
  355. private static JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
  356. {
  357. if (!(arguments.At(0) is ObjectInstance o))
  358. {
  359. return arguments.At(0);
  360. }
  361. foreach (var pair in o.GetOwnProperties())
  362. {
  363. var desc = pair.Value;
  364. if (desc.IsDataDescriptor())
  365. {
  366. if (desc.Writable)
  367. {
  368. return false;
  369. }
  370. }
  371. if (desc.Configurable)
  372. {
  373. return false;
  374. }
  375. }
  376. if (o.Extensible == false)
  377. {
  378. return true;
  379. }
  380. return false;
  381. }
  382. private static JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  383. {
  384. if (!(arguments.At(0) is ObjectInstance o))
  385. {
  386. return arguments.At(0);
  387. }
  388. return o.Extensible;
  389. }
  390. private JsValue Keys(JsValue thisObject, JsValue[] arguments)
  391. {
  392. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  393. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Key);
  394. }
  395. private JsValue Values(JsValue thisObject, JsValue[] arguments)
  396. {
  397. var o = TypeConverter.ToObject(_engine, arguments.At(0));
  398. return o.EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind.Value);
  399. }
  400. private sealed class CreateDataPropertyOnObject : ICallable
  401. {
  402. internal static readonly CreateDataPropertyOnObject Instance = new CreateDataPropertyOnObject();
  403. private CreateDataPropertyOnObject()
  404. {
  405. }
  406. public JsValue Call(JsValue thisObject, JsValue[] arguments)
  407. {
  408. var o = (ObjectInstance) thisObject;
  409. var key = arguments.At(0);
  410. var value = arguments.At(1);
  411. var propertyKey = TypeConverter.ToPropertyKey(key);
  412. o.CreateDataPropertyOrThrow(propertyKey, value);
  413. return Undefined;
  414. }
  415. }
  416. }
  417. }