ObjectPrototype.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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), true, false, true);
  23. FastAddProperty("isPrototypeOf", new ClrFunctionInstance(Engine, IsPrototypeOf), true, false, true);
  24. FastAddProperty("propertyIsEnumerable", new ClrFunctionInstance(Engine, PropertyIsEnumerable), true, false, true);
  25. }
  26. private JsValue PropertyIsEnumerable(JsValue thisObject, JsValue[] arguments)
  27. {
  28. var p = TypeConverter.ToString(arguments[0]).AsString();
  29. var o = TypeConverter.ToObject(Engine, thisObject).AsObject();
  30. var desc = o.GetOwnProperty(p);
  31. if (desc == PropertyDescriptor.Undefined)
  32. {
  33. return false;
  34. }
  35. return desc.Enumerable.HasValue && desc.Enumerable.Value.AsBoolean();
  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 v = arguments[0];
  45. if (!v.IsObject())
  46. {
  47. return false;
  48. }
  49. var o = TypeConverter.ToObject(Engine, thisObject);
  50. while (true)
  51. {
  52. if (v.AsObject().Prototype == null)
  53. {
  54. return false;
  55. }
  56. if (o == v)
  57. {
  58. return true;
  59. }
  60. }
  61. }
  62. private JsValue ToLocaleString(JsValue thisObject, JsValue[] arguments)
  63. {
  64. var o = TypeConverter.ToObject(Engine, thisObject).AsObject();
  65. var toString = o.Get("toString").TryCast<ICallable>(x =>
  66. {
  67. throw new JavaScriptException(Engine.TypeError);
  68. });
  69. return toString.Call(o, Arguments.Empty);
  70. }
  71. /// <summary>
  72. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.2
  73. /// </summary>
  74. /// <param name="thisObject"></param>
  75. /// <param name="arguments"></param>
  76. /// <returns></returns>
  77. public JsValue ToObjectString(JsValue thisObject, JsValue[] arguments)
  78. {
  79. if (thisObject == Undefined.Instance)
  80. {
  81. return "[object Undefined]";
  82. }
  83. if (thisObject == Null.Instance)
  84. {
  85. return "[object Null]";
  86. }
  87. var o = TypeConverter.ToObject(Engine, thisObject).AsObject();
  88. return "[object " + o.Class + "]";
  89. }
  90. /// <summary>
  91. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.5
  92. /// </summary>
  93. /// <param name="thisObject"></param>
  94. /// <param name="arguments"></param>
  95. /// <returns></returns>
  96. public JsValue HasOwnProperty(JsValue thisObject, JsValue[] arguments)
  97. {
  98. var p = TypeConverter.ToString(arguments[0]).AsString();
  99. var o = TypeConverter.ToObject(Engine, thisObject).AsObject();
  100. var desc = o.GetOwnProperty(p);
  101. return desc != PropertyDescriptor.Undefined;
  102. }
  103. }
  104. }