ObjectPrototype.cs 3.9 KB

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