ObjectPrototype.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Jint.Collections;
  2. using Jint.Native.Symbol;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Object
  7. {
  8. public sealed class ObjectPrototype : ObjectInstance
  9. {
  10. private readonly Realm _realm;
  11. private readonly ObjectConstructor _constructor;
  12. internal ObjectPrototype(
  13. Engine engine,
  14. Realm realm,
  15. ObjectConstructor constructor) : base(engine)
  16. {
  17. _realm = realm;
  18. _constructor = constructor;
  19. }
  20. protected override void Initialize()
  21. {
  22. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  23. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  24. var properties = new PropertyDictionary(8, checkExistingKeys: false)
  25. {
  26. ["constructor"] = new PropertyDescriptor(_constructor, propertyFlags),
  27. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToObjectString, 0, lengthFlags), propertyFlags),
  28. ["toLocaleString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toLocaleString", ToLocaleString, 0, lengthFlags), propertyFlags),
  29. ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOf", ValueOf, 0, lengthFlags), propertyFlags),
  30. ["hasOwnProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "hasOwnProperty", HasOwnProperty, 1, lengthFlags), propertyFlags),
  31. ["isPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isPrototypeOf", IsPrototypeOf, 1, lengthFlags), propertyFlags),
  32. ["propertyIsEnumerable"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "propertyIsEnumerable", PropertyIsEnumerable, 1, lengthFlags), propertyFlags)
  33. };
  34. SetProperties(properties);
  35. }
  36. private JsValue PropertyIsEnumerable(JsValue thisObject, JsValue[] arguments)
  37. {
  38. var p = TypeConverter.ToPropertyKey(arguments[0]);
  39. var o = TypeConverter.ToObject(_realm, thisObject);
  40. var desc = o.GetOwnProperty(p);
  41. if (desc == PropertyDescriptor.Undefined)
  42. {
  43. return JsBoolean.False;
  44. }
  45. return desc.Enumerable;
  46. }
  47. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments)
  48. {
  49. var o = TypeConverter.ToObject(_realm, thisObject);
  50. return o;
  51. }
  52. private JsValue IsPrototypeOf(JsValue thisObject, JsValue[] arguments)
  53. {
  54. var arg = arguments[0];
  55. if (!arg.IsObject())
  56. {
  57. return JsBoolean.False;
  58. }
  59. var v = arg.AsObject();
  60. var o = TypeConverter.ToObject(_realm, thisObject);
  61. while (true)
  62. {
  63. v = v.Prototype;
  64. if (ReferenceEquals(v, null))
  65. {
  66. return JsBoolean.False;
  67. }
  68. if (ReferenceEquals(o, v))
  69. {
  70. return JsBoolean.True;
  71. }
  72. }
  73. }
  74. private JsValue ToLocaleString(JsValue thisObject, JsValue[] arguments)
  75. {
  76. var o = TypeConverter.ToObject(_realm, thisObject);
  77. var func = o.Get("toString");
  78. var callable = func as ICallable;
  79. if (callable is null)
  80. {
  81. ExceptionHelper.ThrowTypeError(_realm, "Can only invoke functions");
  82. }
  83. return TypeConverter.ToJsString(callable.Call(thisObject, arguments));
  84. }
  85. /// <summary>
  86. /// https://tc39.es/ecma262/#sec-object.prototype.tostring
  87. /// </summary>
  88. public JsValue ToObjectString(JsValue thisObject, JsValue[] arguments)
  89. {
  90. if (thisObject.IsUndefined())
  91. {
  92. return "[object Undefined]";
  93. }
  94. if (thisObject.IsNull())
  95. {
  96. return "[object Null]";
  97. }
  98. var o = TypeConverter.ToObject(_realm, thisObject);
  99. var tag = o.Get(GlobalSymbolRegistry.ToStringTag);
  100. if (!tag.IsString())
  101. {
  102. tag = o.Class.ToString();
  103. }
  104. return "[object " + tag + "]";
  105. }
  106. /// <summary>
  107. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.5
  108. /// </summary>
  109. public JsValue HasOwnProperty(JsValue thisObject, JsValue[] arguments)
  110. {
  111. var p = TypeConverter.ToPropertyKey(arguments[0]);
  112. var o = TypeConverter.ToObject(_realm, thisObject);
  113. var desc = o.GetOwnProperty(p);
  114. return desc != PropertyDescriptor.Undefined;
  115. }
  116. }
  117. }