ObjectPrototype.cs 5.1 KB

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