ObjectConstructor.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  101. {
  102. throw new JavaScriptException(Engine.TypeError);
  103. }
  104. var o = thisObject as ObjectInstance;
  105. var p = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
  106. var name = TypeConverter.ToString(p);
  107. var desc = o.GetOwnProperty(name);
  108. return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
  109. }
  110. public object GetOwnPropertyNames(object thisObject, object[] arguments)
  111. {
  112. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  113. {
  114. throw new JavaScriptException(Engine.TypeError);
  115. }
  116. var o = thisObject as ObjectInstance;
  117. var array = Engine.Array.Construct(Arguments.Empty);
  118. var n = 0;
  119. foreach (var p in o.Properties)
  120. {
  121. array.DefineOwnProperty(n.ToString(), new DataDescriptor(p.Key) { Writable = true, Enumerable = true, Configurable = true }, false);
  122. n++;
  123. }
  124. return array;
  125. }
  126. public object Create(object thisObject, object[] arguments)
  127. {
  128. if (TypeConverter.GetType(thisObject) != TypeCode.Object && thisObject != Null.Instance)
  129. {
  130. throw new JavaScriptException(Engine.TypeError);
  131. }
  132. var obj = Engine.Object.Construct(Arguments.Empty);
  133. obj.Prototype = thisObject as ObjectInstance;
  134. var properties = arguments.Length > 1 ? arguments[1] : Undefined.Instance;
  135. if (properties != Undefined.Instance)
  136. {
  137. DefineProperties(obj, new [] {properties});
  138. }
  139. return obj;
  140. }
  141. public object DefineProperty(object thisObject, object[] arguments)
  142. {
  143. var o = arguments[0] as ObjectInstance;
  144. var p = arguments[1];
  145. var attributes = arguments[2] as ObjectInstance;
  146. if (o == null)
  147. {
  148. throw new JavaScriptException(Engine.TypeError);
  149. }
  150. if (attributes == null)
  151. {
  152. throw new JavaScriptException(Engine.TypeError);
  153. }
  154. var name = TypeConverter.ToString(p);
  155. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  156. o.DefineOwnProperty(name, desc, true);
  157. return o;
  158. }
  159. public object DefineProperties(object thisObject, object[] arguments)
  160. {
  161. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  162. {
  163. throw new JavaScriptException(Engine.TypeError);
  164. }
  165. var o = thisObject as ObjectInstance;
  166. var properties = arguments.Length > 1 ? arguments[1] : Undefined.Instance;
  167. var props = TypeConverter.ToObject(Engine, properties);
  168. var names = props.Properties.Keys;
  169. var descriptors = new List<KeyValuePair<string, PropertyDescriptor>>();
  170. foreach (var p in names)
  171. {
  172. var descObj = props.Get(p);
  173. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, descObj);
  174. descriptors.Add(new KeyValuePair<string, PropertyDescriptor>(p, desc));
  175. }
  176. foreach (var pair in descriptors)
  177. {
  178. o.DefineOwnProperty(pair.Key, pair.Value, true);
  179. }
  180. return o;
  181. }
  182. public object Seal(object thisObject, object[] arguments)
  183. {
  184. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  185. {
  186. throw new JavaScriptException(Engine.TypeError);
  187. }
  188. var o = thisObject as ObjectInstance;
  189. foreach (var prop in o.Properties)
  190. {
  191. if (prop.Value.Configurable)
  192. {
  193. prop.Value.Configurable = false;
  194. }
  195. o.DefineOwnProperty(prop.Key, prop.Value, true);
  196. }
  197. o.Extensible = false;
  198. return o;
  199. }
  200. public object Freeze(object thisObject, object[] arguments)
  201. {
  202. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  203. {
  204. throw new JavaScriptException(Engine.TypeError);
  205. }
  206. var o = thisObject as ObjectInstance;
  207. foreach (var prop in o.Properties)
  208. {
  209. if (prop.Value.IsDataDescriptor())
  210. {
  211. var datadesc = prop.Value.As<DataDescriptor>();
  212. if (datadesc.Writable)
  213. {
  214. datadesc.Writable = false;
  215. }
  216. }
  217. if (prop.Value.Configurable)
  218. {
  219. prop.Value.Configurable = false;
  220. }
  221. o.DefineOwnProperty(prop.Key, prop.Value, true);
  222. }
  223. o.Extensible = false;
  224. return o;
  225. }
  226. public object PreventExtensions(object thisObject, object[] arguments)
  227. {
  228. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  229. {
  230. throw new JavaScriptException(Engine.TypeError);
  231. }
  232. var o = thisObject as ObjectInstance;
  233. o.Extensible = false;
  234. return o;
  235. }
  236. public object IsSealed(object thisObject, object[] arguments)
  237. {
  238. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  239. {
  240. throw new JavaScriptException(Engine.TypeError);
  241. }
  242. var o = thisObject as ObjectInstance;
  243. foreach (var prop in o.Properties)
  244. {
  245. if (prop.Value.Configurable)
  246. {
  247. return false;
  248. }
  249. }
  250. if (o.Extensible)
  251. {
  252. return true;
  253. }
  254. return false;
  255. }
  256. public object IsFrozen(object thisObject, object[] arguments)
  257. {
  258. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  259. {
  260. throw new JavaScriptException(Engine.TypeError);
  261. }
  262. var o = thisObject as ObjectInstance;
  263. foreach (var prop in o.Properties)
  264. {
  265. if (prop.Value.IsDataDescriptor())
  266. {
  267. if (prop.Value.As<DataDescriptor>().Writable)
  268. {
  269. return false;
  270. }
  271. }
  272. if (prop.Value.Configurable)
  273. {
  274. return false;
  275. }
  276. }
  277. if (o.Extensible)
  278. {
  279. return true;
  280. }
  281. return false;
  282. }
  283. public object IsExtensible(object thisObject, object[] arguments)
  284. {
  285. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  286. {
  287. throw new JavaScriptException(Engine.TypeError);
  288. }
  289. var o = thisObject as ObjectInstance;
  290. return o.Extensible;
  291. }
  292. public object Keys(object thisObject, object[] arguments)
  293. {
  294. if (TypeConverter.GetType(thisObject) != TypeCode.Object)
  295. {
  296. throw new JavaScriptException(Engine.TypeError);
  297. }
  298. var o = thisObject as ObjectInstance;
  299. var n = o.Properties.Values.Count(x => x.Enumerable);
  300. var array = Engine.Array.Construct(new object[] {n});
  301. var index = 0;
  302. foreach (var prop in o.Properties.Where(x => x.Value.Enumerable))
  303. {
  304. array.DefineOwnProperty(index.ToString(), new DataDescriptor(prop.Key) { Writable = true, Enumerable = true, Configurable = true }, false);
  305. index++;
  306. }
  307. return array;
  308. }
  309. }
  310. }