ObjectConstructor.cs 6.1 KB

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