ObjectConstructor.cs 6.1 KB

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