ObjectPrototype.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Jint.Runtime;
  2. using Jint.Runtime.Descriptors;
  3. using Jint.Runtime.Interop;
  4. namespace Jint.Native.Object
  5. {
  6. public sealed class ObjectPrototype : ObjectInstance
  7. {
  8. private ObjectPrototype(Engine engine) : base(engine)
  9. {
  10. }
  11. public static ObjectPrototype CreatePrototypeObject(Engine engine, ObjectConstructor objectConstructor)
  12. {
  13. var obj = new ObjectPrototype(engine) { Extensible = true };
  14. obj.FastAddProperty("constructor", objectConstructor, true, false, true);
  15. return obj;
  16. }
  17. public void Configure()
  18. {
  19. FastAddProperty("toString", new ClrFunctionInstance(Engine, ToObjectString), true, false, true);
  20. FastAddProperty("toLocaleString", new ClrFunctionInstance(Engine, ToLocaleString), true, false, true);
  21. FastAddProperty("valueOf", new ClrFunctionInstance(Engine, ValueOf), true, false, true);
  22. FastAddProperty("hasOwnProperty", new ClrFunctionInstance(Engine, HasOwnProperty, 1), true, false, true);
  23. FastAddProperty("isPrototypeOf", new ClrFunctionInstance(Engine, IsPrototypeOf, 1), true, false, true);
  24. FastAddProperty("propertyIsEnumerable", new ClrFunctionInstance(Engine, PropertyIsEnumerable, 1), true, false, true);
  25. }
  26. private JsValue PropertyIsEnumerable(JsValue thisObject, JsValue[] arguments)
  27. {
  28. var p = TypeConverter.ToString(arguments[0]);
  29. var o = TypeConverter.ToObject(Engine, thisObject);
  30. var desc = o.GetOwnProperty(p);
  31. if (desc == PropertyDescriptor.Undefined)
  32. {
  33. return false;
  34. }
  35. return desc.Enumerable;
  36. }
  37. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments)
  38. {
  39. var o = TypeConverter.ToObject(Engine, thisObject);
  40. return o;
  41. }
  42. private JsValue IsPrototypeOf(JsValue thisObject, JsValue[] arguments)
  43. {
  44. var arg = arguments[0];
  45. if (!arg.IsObject())
  46. {
  47. return false;
  48. }
  49. var v = arg.AsObject();
  50. var o = TypeConverter.ToObject(Engine, thisObject);
  51. while (true)
  52. {
  53. v = v.Prototype;
  54. if (ReferenceEquals(v, null))
  55. {
  56. return false;
  57. }
  58. if (o == v)
  59. {
  60. return true;
  61. }
  62. }
  63. }
  64. private JsValue ToLocaleString(JsValue thisObject, JsValue[] arguments)
  65. {
  66. var o = TypeConverter.ToObject(Engine, thisObject);
  67. var toString = o.Get("toString").TryCast<ICallable>(x =>
  68. {
  69. throw new JavaScriptException(Engine.TypeError);
  70. });
  71. return toString.Call(o, Arguments.Empty);
  72. }
  73. /// <summary>
  74. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.2
  75. /// </summary>
  76. /// <param name="thisObject"></param>
  77. /// <param name="arguments"></param>
  78. /// <returns></returns>
  79. public JsValue ToObjectString(JsValue thisObject, JsValue[] arguments)
  80. {
  81. if (thisObject.IsUndefined())
  82. {
  83. return "[object Undefined]";
  84. }
  85. if (thisObject.IsNull())
  86. {
  87. return "[object Null]";
  88. }
  89. var o = TypeConverter.ToObject(Engine, thisObject);
  90. return "[object " + o.Class + "]";
  91. }
  92. /// <summary>
  93. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.5
  94. /// </summary>
  95. /// <param name="thisObject"></param>
  96. /// <param name="arguments"></param>
  97. /// <returns></returns>
  98. public JsValue HasOwnProperty(JsValue thisObject, JsValue[] arguments)
  99. {
  100. var p = TypeConverter.ToString(arguments[0]);
  101. var o = TypeConverter.ToObject(Engine, thisObject);
  102. var desc = o.GetOwnProperty(p);
  103. return desc != PropertyDescriptor.Undefined;
  104. }
  105. }
  106. }