ObjectConstructor.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Jint.Native.Function;
  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 readonly Engine _engine;
  13. private ObjectConstructor(Engine engine) : base(engine, null, null, false)
  14. {
  15. _engine = engine;
  16. }
  17. public static ObjectConstructor CreateObjectConstructor(Engine engine)
  18. {
  19. var obj = new ObjectConstructor(engine);
  20. obj.Extensible = true;
  21. obj.PrototypeObject = ObjectPrototype.CreatePrototypeObject(engine, obj);
  22. obj.FastAddProperty("length", 1, false, false, false);
  23. obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);
  24. return obj;
  25. }
  26. public void Configure()
  27. {
  28. Prototype = Engine.Function.PrototypeObject;
  29. FastAddProperty("getPrototypeOf", new ClrFunctionInstance<object, object>(Engine, GetPrototypeOf), false, false, false);
  30. FastAddProperty("getOwnPropertyDescriptor", new ClrFunctionInstance<object, object>(Engine, GetOwnPropertyDescriptor), false, false, false);
  31. FastAddProperty("getOwnPropertyNames", new ClrFunctionInstance<object, object>(Engine, GetOwnPropertyNames), false, false, false);
  32. FastAddProperty("create", new ClrFunctionInstance<object, object>(Engine, Create), false, false, false);
  33. FastAddProperty("defineProperty", new ClrFunctionInstance<object, object>(Engine, DefineProperty), false, false, false);
  34. FastAddProperty("defineProperties", new ClrFunctionInstance<object, object>(Engine, DefineProperties), false, false, false);
  35. FastAddProperty("seal", new ClrFunctionInstance<object, object>(Engine, Seal, 1), false, false, false);
  36. FastAddProperty("freeze", new ClrFunctionInstance<object, object>(Engine, Freeze, 1), false, false, false);
  37. FastAddProperty("preventExtensions", new ClrFunctionInstance<object, object>(Engine, PreventExtensions, 1), false, false, false);
  38. FastAddProperty("isSealed", new ClrFunctionInstance<object, object>(Engine, IsSealed, 1), false, false, false);
  39. FastAddProperty("isFrozen", new ClrFunctionInstance<object, object>(Engine, IsFrozen, 1), false, false, false);
  40. FastAddProperty("isExtensible", new ClrFunctionInstance<object, object>(Engine, IsExtensible, 1), false, false, false);
  41. FastAddProperty("keys", new ClrFunctionInstance<object, object>(Engine, Keys), false, false, false);
  42. }
  43. public ObjectPrototype PrototypeObject { get; private set; }
  44. /// <summary>
  45. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.1.1
  46. /// </summary>
  47. /// <param name="thisObject"></param>
  48. /// <param name="arguments"></param>
  49. /// <returns></returns>
  50. public override object Call(object thisObject, object[] arguments)
  51. {
  52. if (arguments.Length == 0)
  53. {
  54. return Construct(arguments);
  55. }
  56. if(arguments[0] == Null.Instance || arguments[0] == Undefined.Instance)
  57. {
  58. return Construct(arguments);
  59. }
  60. return TypeConverter.ToObject(_engine, arguments[0]);
  61. }
  62. /// <summary>
  63. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
  64. /// </summary>
  65. /// <param name="arguments"></param>
  66. /// <returns></returns>
  67. public ObjectInstance Construct(object[] arguments)
  68. {
  69. if (arguments.Length > 0)
  70. {
  71. var value = arguments[0];
  72. var valueObj = value as ObjectInstance;
  73. if (valueObj != null)
  74. {
  75. return valueObj;
  76. }
  77. var type = TypeConverter.GetType(value);
  78. if (type == TypeCode.String || type == TypeCode.Double || type == TypeCode.Boolean)
  79. {
  80. return TypeConverter.ToObject(_engine, value);
  81. }
  82. }
  83. var obj = new ObjectInstance(_engine)
  84. {
  85. Extensible = true,
  86. Prototype = Engine.Object.PrototypeObject
  87. };
  88. return obj;
  89. }
  90. public object GetPrototypeOf(object thisObject, object[] arguments)
  91. {
  92. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  93. {
  94. throw new JavaScriptException(Engine.TypeError);
  95. }
  96. var o = thisObject as ObjectInstance;
  97. return o.Prototype ?? Null.Instance;
  98. }
  99. public object GetOwnPropertyDescriptor(object thisObject, object[] arguments)
  100. {
  101. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  102. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  103. {
  104. throw new JavaScriptException(Engine.TypeError);
  105. }
  106. var o = oArg as ObjectInstance;
  107. var p = arguments.Length > 1 ? arguments[1] : Undefined.Instance;
  108. var name = TypeConverter.ToString(p);
  109. var desc = o.GetOwnProperty(name);
  110. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  111. }
  112. public object GetOwnPropertyNames(object thisObject, object[] arguments)
  113. {
  114. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  115. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  116. {
  117. throw new JavaScriptException(Engine.TypeError);
  118. }
  119. var o = oArg as ObjectInstance;
  120. var array = Engine.Array.Construct(Arguments.Empty);
  121. var n = 0;
  122. foreach (var p in o.Properties)
  123. {
  124. array.DefineOwnProperty(n.ToString(), new DataDescriptor(p.Key) { Writable = true, Enumerable = true, Configurable = true }, false);
  125. n++;
  126. }
  127. return array;
  128. }
  129. public object Create(object thisObject, object[] arguments)
  130. {
  131. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  132. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  133. {
  134. throw new JavaScriptException(Engine.TypeError);
  135. }
  136. var obj = Engine.Object.Construct(Arguments.Empty);
  137. obj.Prototype = thisObject as ObjectInstance;
  138. var properties = arguments.Length > 1 ? arguments[1] : Undefined.Instance;
  139. if (properties != Undefined.Instance)
  140. {
  141. DefineProperties(thisObject, new [] {obj, properties});
  142. }
  143. return obj;
  144. }
  145. public object DefineProperty(object thisObject, object[] arguments)
  146. {
  147. var o = arguments[0] as ObjectInstance;
  148. var p = arguments[1];
  149. var attributes = arguments[2] as ObjectInstance;
  150. if (o == null)
  151. {
  152. throw new JavaScriptException(Engine.TypeError);
  153. }
  154. if (attributes == null)
  155. {
  156. throw new JavaScriptException(Engine.TypeError);
  157. }
  158. var name = TypeConverter.ToString(p);
  159. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  160. o.DefineOwnProperty(name, desc, true);
  161. return o;
  162. }
  163. public object DefineProperties(object thisObject, object[] arguments)
  164. {
  165. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  166. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  167. {
  168. throw new JavaScriptException(Engine.TypeError);
  169. }
  170. var o = oArg as ObjectInstance;
  171. var properties = arguments.Length > 1 ? arguments[1] : Undefined.Instance;
  172. var props = TypeConverter.ToObject(Engine, properties);
  173. var names = props.Properties.Keys;
  174. var descriptors = new List<KeyValuePair<string, PropertyDescriptor>>();
  175. foreach (var p in names)
  176. {
  177. var descObj = props.Get(p);
  178. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, descObj);
  179. descriptors.Add(new KeyValuePair<string, PropertyDescriptor>(p, desc));
  180. }
  181. foreach (var pair in descriptors)
  182. {
  183. o.DefineOwnProperty(pair.Key, pair.Value, true);
  184. }
  185. return o;
  186. }
  187. public object Seal(object thisObject, object[] arguments)
  188. {
  189. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  190. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  191. {
  192. throw new JavaScriptException(Engine.TypeError);
  193. }
  194. var o = oArg as ObjectInstance;
  195. foreach (var prop in o.Properties)
  196. {
  197. if (prop.Value.ConfigurableIsSet)
  198. {
  199. prop.Value.Configurable = false;
  200. }
  201. o.DefineOwnProperty(prop.Key, prop.Value, true);
  202. }
  203. o.Extensible = false;
  204. return o;
  205. }
  206. public object Freeze(object thisObject, object[] arguments)
  207. {
  208. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  209. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  210. {
  211. throw new JavaScriptException(Engine.TypeError);
  212. }
  213. var o = oArg as ObjectInstance;
  214. foreach (var prop in o.Properties)
  215. {
  216. if (prop.Value.IsDataDescriptor())
  217. {
  218. var datadesc = prop.Value.As<DataDescriptor>();
  219. if (datadesc.WritableIsSet)
  220. {
  221. datadesc.Writable = false;
  222. }
  223. }
  224. if (prop.Value.ConfigurableIsSet)
  225. {
  226. prop.Value.Configurable = false;
  227. }
  228. o.DefineOwnProperty(prop.Key, prop.Value, true);
  229. }
  230. o.Extensible = false;
  231. return o;
  232. }
  233. public object PreventExtensions(object thisObject, object[] arguments)
  234. {
  235. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  236. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  237. {
  238. throw new JavaScriptException(Engine.TypeError);
  239. }
  240. var o = oArg as ObjectInstance;
  241. o.Extensible = false;
  242. return o;
  243. }
  244. public object IsSealed(object thisObject, object[] arguments)
  245. {
  246. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  247. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  248. {
  249. throw new JavaScriptException(Engine.TypeError);
  250. }
  251. var o = oArg as ObjectInstance;
  252. foreach (var prop in o.Properties)
  253. {
  254. if (prop.Value.ConfigurableIsSet)
  255. {
  256. return false;
  257. }
  258. }
  259. if (o.Extensible == false)
  260. {
  261. return true;
  262. }
  263. return false;
  264. }
  265. public object IsFrozen(object thisObject, object[] arguments)
  266. {
  267. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  268. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  269. {
  270. throw new JavaScriptException(Engine.TypeError);
  271. }
  272. var o = oArg as ObjectInstance;
  273. foreach (var prop in o.Properties)
  274. {
  275. if (prop.Value.IsDataDescriptor())
  276. {
  277. if (prop.Value.As<DataDescriptor>().WritableIsSet)
  278. {
  279. return false;
  280. }
  281. }
  282. if (prop.Value.ConfigurableIsSet)
  283. {
  284. return false;
  285. }
  286. }
  287. if (o.Extensible)
  288. {
  289. return true;
  290. }
  291. return false;
  292. }
  293. public object IsExtensible(object thisObject, object[] arguments)
  294. {
  295. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  296. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  297. {
  298. throw new JavaScriptException(Engine.TypeError);
  299. }
  300. var o = oArg as ObjectInstance;
  301. return o.Extensible;
  302. }
  303. public object Keys(object thisObject, object[] arguments)
  304. {
  305. var oArg = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  306. if (TypeConverter.GetType(oArg) != TypeCode.Object)
  307. {
  308. throw new JavaScriptException(Engine.TypeError);
  309. }
  310. var o = oArg as ObjectInstance;
  311. var n = o.Properties.Values.Count(x => x.EnumerableIsSet);
  312. var array = Engine.Array.Construct(new object[] {n});
  313. var index = 0;
  314. foreach (var prop in o.Properties.Where(x => x.Value.EnumerableIsSet))
  315. {
  316. array.DefineOwnProperty(index.ToString(), new DataDescriptor(prop.Key) { Writable = true, Enumerable = true, Configurable = true }, false);
  317. index++;
  318. }
  319. return array;
  320. }
  321. }
  322. }