ObjectConstructor.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using System.Collections.Generic;
  2. using Jint.Collections;
  3. using Jint.Native.Array;
  4. using Jint.Native.Function;
  5. using Jint.Native.String;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Native.Object
  10. {
  11. public sealed class ObjectConstructor : FunctionInstance, IConstructor
  12. {
  13. private ObjectConstructor(Engine engine)
  14. : base(engine, "Object", null, null, false)
  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. _properties = new StringDictionarySlim<PropertyDescriptor>(15)
  29. {
  30. ["getPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getPrototypeOf", GetPrototypeOf, 1), true, false, true),
  31. ["getOwnPropertyDescriptor"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyDescriptor", GetOwnPropertyDescriptor, 2), true, false, true),
  32. ["getOwnPropertyNames"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyNames", GetOwnPropertyNames, 1), true, false, true),
  33. ["getOwnPropertySymbols"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertySymbols", GetOwnPropertySymbols, 1), true, false, true),
  34. ["create"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "create", Create, 2), true, false, true),
  35. ["defineProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "defineProperty", DefineProperty, 3), true, false, true),
  36. ["defineProperties"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "defineProperties", DefineProperties, 2), true, false, true),
  37. ["seal"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "seal", Seal, 1), true, false, true),
  38. ["freeze"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "freeze", Freeze, 1), true, false, true),
  39. ["preventExtensions"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "preventExtensions", PreventExtensions, 1), true, false, true),
  40. ["isSealed"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isSealed", IsSealed, 1), true, false, true),
  41. ["isFrozen"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isFrozen", IsFrozen, 1), true, false, true),
  42. ["isExtensible"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isExtensible", IsExtensible, 1), true, false, true),
  43. ["keys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "keys", Keys, 1), true, false, true),
  44. ["setPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setPrototypeOf", SetPrototypeOf, 2), true, false, true)
  45. };
  46. }
  47. public ObjectPrototype PrototypeObject { get; private set; }
  48. /// <summary>
  49. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.1.1
  50. /// </summary>
  51. /// <param name="thisObject"></param>
  52. /// <param name="arguments"></param>
  53. /// <returns></returns>
  54. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  55. {
  56. if (arguments.Length == 0)
  57. {
  58. return Construct(arguments);
  59. }
  60. if(arguments[0].IsNullOrUndefined())
  61. {
  62. return Construct(arguments);
  63. }
  64. return TypeConverter.ToObject(_engine, arguments[0]);
  65. }
  66. /// <summary>
  67. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
  68. /// </summary>
  69. public ObjectInstance Construct(JsValue[] arguments)
  70. {
  71. return Construct(arguments, this);
  72. }
  73. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  74. {
  75. if (arguments.Length > 0)
  76. {
  77. var value = arguments[0];
  78. var valueObj = value.TryCast<ObjectInstance>();
  79. if (!ReferenceEquals(valueObj, null))
  80. {
  81. return valueObj;
  82. }
  83. var type = value.Type;
  84. if (type == Types.String || type == Types.Number || type == Types.Boolean)
  85. {
  86. return TypeConverter.ToObject(_engine, value);
  87. }
  88. }
  89. var obj = new ObjectInstance(_engine)
  90. {
  91. _prototype = Engine.Object.PrototypeObject
  92. };
  93. return obj;
  94. }
  95. internal ObjectInstance Construct(int propertyCount)
  96. {
  97. var obj = new ObjectInstance(_engine)
  98. {
  99. _prototype = Engine.Object.PrototypeObject,
  100. _properties = propertyCount > 1
  101. ? new StringDictionarySlim<PropertyDescriptor>(propertyCount)
  102. : null
  103. };
  104. return obj;
  105. }
  106. public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  107. {
  108. var o = arguments.As<ObjectInstance>(0, _engine);
  109. return o.Prototype ?? Null;
  110. }
  111. public JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  112. {
  113. var oArg = arguments.At(0);
  114. TypeConverter.CheckObjectCoercible(_engine, oArg);
  115. var prototype = arguments.At(1);
  116. if (!prototype.IsObject() && !prototype.IsNull())
  117. {
  118. ExceptionHelper.ThrowTypeError(_engine, $"Object prototype may only be an Object or null: {prototype}");
  119. }
  120. if (!(oArg is ObjectInstance o))
  121. {
  122. return oArg;
  123. }
  124. if (!o.SetPrototypeOf(prototype))
  125. {
  126. ExceptionHelper.ThrowTypeError(_engine);
  127. }
  128. return o;
  129. }
  130. public JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  131. {
  132. var o = arguments.As<ObjectInstance>(0, _engine);
  133. var p = arguments.At(1);
  134. var name = TypeConverter.ToPropertyKey(p);
  135. var desc = o.GetOwnProperty(name);
  136. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  137. }
  138. public JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
  139. {
  140. var o = arguments.As<ObjectInstance>(0, _engine);
  141. uint n = 0;
  142. ArrayInstance array = null;
  143. var ownProperties = o.GetOwnPropertyKeys(Types.String);
  144. if (o is StringInstance s)
  145. {
  146. var length = s.PrimitiveValue.Length;
  147. array = Engine.Array.ConstructFast((uint) (ownProperties.Count + length + 1));
  148. for (var i = 0; i < length; i++)
  149. {
  150. array.SetIndexValue(n++, TypeConverter.ToString(i), updateLength: false);
  151. }
  152. array.SetIndexValue(n++, JsString.LengthString, updateLength: false);
  153. }
  154. array = array ?? Engine.Array.ConstructFast((uint) ownProperties.Count);
  155. for (var i = 0; i < ownProperties.Count; i++)
  156. {
  157. var p = ownProperties[i];
  158. array.SetIndexValue(n++, p, false);
  159. }
  160. array.SetLength(n);
  161. return array;
  162. }
  163. public JsValue GetOwnPropertySymbols(JsValue thisObject, JsValue[] arguments)
  164. {
  165. var o = arguments.As<ObjectInstance>(0, _engine);
  166. var keys = o.GetOwnPropertyKeys(Types.Symbol);
  167. return _engine.Array.Construct(keys.ToArray());
  168. }
  169. public JsValue Create(JsValue thisObject, JsValue[] arguments)
  170. {
  171. var prototype = arguments.At(0);
  172. if (!prototype.IsObject() && !prototype.IsNull())
  173. {
  174. ExceptionHelper.ThrowTypeError(_engine, "Object prototype may only be an Object or null: " + prototype);
  175. }
  176. var obj = Engine.Object.Construct(Arguments.Empty);
  177. obj._prototype = prototype.IsNull() ? null : prototype.AsObject();
  178. var properties = arguments.At(1);
  179. if (!properties.IsUndefined())
  180. {
  181. var jsValues = _engine._jsValueArrayPool.RentArray(2);
  182. jsValues[0] = obj;
  183. jsValues[1] = properties;
  184. DefineProperties(thisObject, jsValues);
  185. _engine._jsValueArrayPool.ReturnArray(jsValues);
  186. }
  187. return obj;
  188. }
  189. public JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  190. {
  191. var o = arguments.As<ObjectInstance>(0, _engine);
  192. var p = arguments.At(1);
  193. var name = TypeConverter.ToPropertyKey(p);
  194. var attributes = arguments.At(2);
  195. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  196. o.DefinePropertyOrThrow(name, desc);
  197. return o;
  198. }
  199. public JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
  200. {
  201. var o = arguments.As<ObjectInstance>(0, _engine);
  202. var properties = arguments.At(1);
  203. var props = TypeConverter.ToObject(Engine, properties);
  204. var descriptors = new List<KeyValuePair<string, PropertyDescriptor>>();
  205. foreach (var p in props.GetOwnProperties())
  206. {
  207. if (!p.Value.Enumerable)
  208. {
  209. continue;
  210. }
  211. var descObj = props.Get(p.Key, props);
  212. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, descObj);
  213. descriptors.Add(new KeyValuePair<string, PropertyDescriptor>(p.Key, desc));
  214. }
  215. foreach (var pair in descriptors)
  216. {
  217. o.DefinePropertyOrThrow(pair.Key, pair.Value);
  218. }
  219. return o;
  220. }
  221. public JsValue Seal(JsValue thisObject, JsValue[] arguments)
  222. {
  223. var o = arguments.As<ObjectInstance>(0, _engine);
  224. var properties = new List<KeyValuePair<Key, PropertyDescriptor>>(o.GetOwnProperties());
  225. foreach (var prop in properties)
  226. {
  227. var propertyDescriptor = prop.Value;
  228. if (propertyDescriptor.Configurable)
  229. {
  230. propertyDescriptor.Configurable = false;
  231. FastSetProperty(prop.Key, propertyDescriptor);
  232. }
  233. o.DefinePropertyOrThrow(prop.Key, propertyDescriptor);
  234. }
  235. o.PreventExtensions();
  236. return o;
  237. }
  238. public JsValue Freeze(JsValue thisObject, JsValue[] arguments)
  239. {
  240. var o = arguments.As<ObjectInstance>(0, _engine);
  241. var properties = new List<KeyValuePair<Key, PropertyDescriptor>>(o.GetOwnProperties());
  242. foreach (var p in properties)
  243. {
  244. var desc = o.GetOwnProperty(p.Key);
  245. if (desc.IsDataDescriptor())
  246. {
  247. if (desc.Writable)
  248. {
  249. var mutable = desc;
  250. mutable.Writable = false;
  251. desc = mutable;
  252. }
  253. }
  254. if (desc.Configurable)
  255. {
  256. var mutable = desc;
  257. mutable.Configurable = false;
  258. desc = mutable;
  259. }
  260. o.DefinePropertyOrThrow(p.Key, desc);
  261. }
  262. o.PreventExtensions();
  263. return o;
  264. }
  265. public JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  266. {
  267. var o = arguments.As<ObjectInstance>(0, _engine);
  268. o.PreventExtensions();
  269. return o;
  270. }
  271. public JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
  272. {
  273. var o = arguments.As<ObjectInstance>(0, _engine);
  274. foreach (var prop in o.GetOwnProperties())
  275. {
  276. if (prop.Value.Configurable)
  277. {
  278. return false;
  279. }
  280. }
  281. if (o.Extensible == false)
  282. {
  283. return true;
  284. }
  285. return false;
  286. }
  287. public JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
  288. {
  289. var o = arguments.As<ObjectInstance>(0, _engine);
  290. foreach (var pair in o.GetOwnProperties())
  291. {
  292. var desc = pair.Value;
  293. if (desc.IsDataDescriptor())
  294. {
  295. if (desc.Writable)
  296. {
  297. return false;
  298. }
  299. }
  300. if (desc.Configurable)
  301. {
  302. return false;
  303. }
  304. }
  305. if (o.Extensible == false)
  306. {
  307. return true;
  308. }
  309. return false;
  310. }
  311. public JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  312. {
  313. var o = arguments.As<ObjectInstance>(0, _engine);
  314. return o.Extensible;
  315. }
  316. public JsValue Keys(JsValue thisObject, JsValue[] arguments)
  317. {
  318. return EnumerableOwnPropertyNames(arguments, EnumerableOwnPropertyNamesKind.Key);
  319. }
  320. private JsValue EnumerableOwnPropertyNames(JsValue[] arguments, EnumerableOwnPropertyNamesKind kind)
  321. {
  322. var o = arguments.As<ObjectInstance>(0, _engine);
  323. var ownKeys = o.GetOwnPropertyKeys(Types.String);
  324. var array = Engine.Array.ConstructFast((uint) ownKeys.Count);
  325. uint index = 0;
  326. foreach (var key in ownKeys)
  327. {
  328. var propertyName = key.ToPropertyKey();
  329. var desc = o.GetOwnProperty(propertyName);
  330. if (desc != PropertyDescriptor.Undefined && desc.Enumerable)
  331. {
  332. if (kind == EnumerableOwnPropertyNamesKind.Key)
  333. {
  334. array.SetIndexValue(index, key, updateLength: false);
  335. }
  336. else
  337. {
  338. var value = o.Get(propertyName, o);
  339. if (kind == EnumerableOwnPropertyNamesKind.Value)
  340. {
  341. array.SetIndexValue(index, value, updateLength: false);
  342. }
  343. else
  344. {
  345. array.SetIndexValue(index, _engine.Array.Construct(new [] { key, value }), updateLength: false);
  346. }
  347. }
  348. index++;
  349. }
  350. }
  351. array.SetLength(index);
  352. return array;
  353. }
  354. private enum EnumerableOwnPropertyNamesKind
  355. {
  356. Key,
  357. Value,
  358. KeyValue
  359. }
  360. }
  361. }