ObjectConstructor.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using Jint.Native.Function;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Object
  7. {
  8. public sealed class ObjectConstructor : FunctionInstance, IConstructor
  9. {
  10. private readonly Engine _engine;
  11. private ObjectConstructor(Engine engine) : base(engine, null, null, false)
  12. {
  13. _engine = engine;
  14. }
  15. public static ObjectConstructor CreateObjectConstructor(Engine engine)
  16. {
  17. var obj = new ObjectConstructor(engine);
  18. obj.PrototypeObject = ObjectPrototype.CreatePrototypeObject(engine, obj);
  19. obj.FastAddProperty("length", 1, false, false, false);
  20. obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);
  21. return obj;
  22. }
  23. public void Configure()
  24. {
  25. Prototype = Engine.Function.PrototypeObject;
  26. FastAddProperty("getPrototypeOf", new ClrFunctionInstance<object, object>(Engine, GetPrototypeOf), false, false, false);
  27. FastAddProperty("getOwnPropertyDescriptor", new ClrFunctionInstance<object, object>(Engine, GetOwnPropertyDescriptor), false, false, false);
  28. FastAddProperty("getOwnPropertyNames", new ClrFunctionInstance<object, object>(Engine, GetOwnPropertyNames), false, false, false);
  29. FastAddProperty("create", new ClrFunctionInstance<object, object>(Engine, Create), false, false, false);
  30. FastAddProperty("defineProperty", new ClrFunctionInstance<object, object>(Engine, DefineProperty), false, false, false);
  31. FastAddProperty("defineProperties", new ClrFunctionInstance<object, object>(Engine, DefineProperties), false, false, false);
  32. FastAddProperty("seal", new ClrFunctionInstance<object, object>(Engine, Seal), false, false, false);
  33. FastAddProperty("freeze", new ClrFunctionInstance<object, object>(Engine, Freeze), false, false, false);
  34. FastAddProperty("preventExtensions", new ClrFunctionInstance<object, object>(Engine, PreventExtensions), false, false, false);
  35. FastAddProperty("isSealed", new ClrFunctionInstance<object, object>(Engine, IsSealed), false, false, false);
  36. FastAddProperty("isFrozen", new ClrFunctionInstance<object, object>(Engine, IsFrozen), false, false, false);
  37. FastAddProperty("isExtensible", new ClrFunctionInstance<object, object>(Engine, IsExtensible), false, false, false);
  38. FastAddProperty("keys", new ClrFunctionInstance<object, object>(Engine, Keys), false, false, false);
  39. }
  40. public ObjectPrototype PrototypeObject { get; private set; }
  41. /// <summary>
  42. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.1.1
  43. /// </summary>
  44. /// <param name="thisObject"></param>
  45. /// <param name="arguments"></param>
  46. /// <returns></returns>
  47. public override object Call(object thisObject, object[] arguments)
  48. {
  49. if (arguments.Length == 0)
  50. {
  51. return Construct(arguments);
  52. }
  53. if(arguments[0] == Null.Instance || arguments[0] == Undefined.Instance)
  54. {
  55. return Construct(arguments);
  56. }
  57. return TypeConverter.ToObject(_engine, arguments[0]);
  58. }
  59. /// <summary>
  60. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
  61. /// </summary>
  62. /// <param name="arguments"></param>
  63. /// <returns></returns>
  64. public ObjectInstance Construct(object[] arguments)
  65. {
  66. if (arguments.Length > 0)
  67. {
  68. var value = arguments[0];
  69. var valueObj = value as ObjectInstance;
  70. if (valueObj != null)
  71. {
  72. return valueObj;
  73. }
  74. var type = TypeConverter.GetType(value);
  75. if (type == TypeCode.String || type == TypeCode.Double || type == TypeCode.Boolean)
  76. {
  77. return TypeConverter.ToObject(_engine, value);
  78. }
  79. }
  80. var obj = new ObjectInstance(_engine)
  81. {
  82. Extensible = true,
  83. Prototype = Engine.Object.PrototypeObject
  84. };
  85. return obj;
  86. }
  87. public object GetPrototypeOf(object thisObject, object[] arguments)
  88. {
  89. throw new NotImplementedException();
  90. }
  91. public object GetOwnPropertyDescriptor(object thisObject, object[] arguments)
  92. {
  93. throw new NotImplementedException();
  94. }
  95. public object GetOwnPropertyNames(object thisObject, object[] arguments)
  96. {
  97. throw new NotImplementedException();
  98. }
  99. public object Create(object thisObject, object[] arguments)
  100. {
  101. throw new NotImplementedException();
  102. }
  103. public object DefineProperty(object thisObject, object[] arguments)
  104. {
  105. var o = arguments[0] as ObjectInstance;
  106. var p = arguments[1];
  107. var attributes = arguments[2] as ObjectInstance;
  108. if (o == null)
  109. {
  110. throw new JavaScriptException(Engine.TypeError);
  111. }
  112. if (attributes == null)
  113. {
  114. throw new JavaScriptException(Engine.TypeError);
  115. }
  116. var name = TypeConverter.ToString(p);
  117. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  118. o.DefineOwnProperty(name, desc, true);
  119. return o;
  120. }
  121. public object DefineProperties(object thisObject, object[] arguments)
  122. {
  123. throw new NotImplementedException();
  124. }
  125. public object Seal(object thisObject, object[] arguments)
  126. {
  127. throw new NotImplementedException();
  128. }
  129. public object Freeze(object thisObject, object[] arguments)
  130. {
  131. throw new NotImplementedException();
  132. }
  133. public object PreventExtensions(object thisObject, object[] arguments)
  134. {
  135. throw new NotImplementedException();
  136. }
  137. public object IsSealed(object thisObject, object[] arguments)
  138. {
  139. throw new NotImplementedException();
  140. }
  141. public object IsFrozen(object thisObject, object[] arguments)
  142. {
  143. throw new NotImplementedException();
  144. }
  145. public object IsExtensible(object thisObject, object[] arguments)
  146. {
  147. throw new NotImplementedException();
  148. }
  149. public object Keys(object thisObject, object[] arguments)
  150. {
  151. throw new NotImplementedException();
  152. }
  153. }
  154. }