ObjectConstructor.cs 13 KB

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