ObjectPrototype.cs 5.1 KB

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