SymbolPrototype.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using Jint.Collections;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Interop;
  7. namespace Jint.Native.Symbol;
  8. /// <summary>
  9. /// https://tc39.es/ecma262/#sec-properties-of-the-symbol-prototype-object
  10. /// </summary>
  11. internal sealed class SymbolPrototype : Prototype
  12. {
  13. private readonly SymbolConstructor _constructor;
  14. internal SymbolPrototype(
  15. Engine engine,
  16. Realm realm,
  17. SymbolConstructor symbolConstructor,
  18. ObjectPrototype objectPrototype)
  19. : base(engine, realm)
  20. {
  21. _prototype = objectPrototype;
  22. _constructor = symbolConstructor;
  23. }
  24. protected override void Initialize()
  25. {
  26. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  27. const PropertyFlag propertyFlags = PropertyFlag.Configurable;
  28. SetProperties(new PropertyDictionary(5, checkExistingKeys: false)
  29. {
  30. ["length"] = new PropertyDescriptor(JsNumber.PositiveZero, propertyFlags),
  31. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.Configurable | PropertyFlag.Writable),
  32. ["description"] = new GetSetPropertyDescriptor(new ClrFunction(Engine, "description", Description, 0, lengthFlags), Undefined, propertyFlags),
  33. ["toString"] = new PropertyDescriptor(new ClrFunction(Engine, "toString", ToSymbolString, 0, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable),
  34. ["valueOf"] = new PropertyDescriptor(new ClrFunction(Engine, "valueOf", ValueOf, 0, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable)
  35. });
  36. SetSymbols(new SymbolDictionary(1)
  37. {
  38. [GlobalSymbolRegistry.ToPrimitive] = new PropertyDescriptor(new ClrFunction(Engine, "[Symbol.toPrimitive]", ToPrimitive, 1, lengthFlags), propertyFlags), [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor(new JsString("Symbol"), propertyFlags)
  39. }
  40. );
  41. }
  42. /// <summary>
  43. /// https://tc39.es/ecma262/#sec-symbol.prototype.description
  44. /// </summary>
  45. private JsValue Description(JsValue thisObject, JsCallArguments arguments)
  46. {
  47. var sym = ThisSymbolValue(thisObject);
  48. return sym._value;
  49. }
  50. /// <summary>
  51. /// https://tc39.es/ecma262/#sec-symbol.prototype.tostring
  52. /// </summary>
  53. private JsValue ToSymbolString(JsValue thisObject, JsCallArguments arguments)
  54. {
  55. var sym = ThisSymbolValue(thisObject);
  56. return new JsString(sym.ToString());
  57. }
  58. /// <summary>
  59. /// https://tc39.es/ecma262/#sec-symbol.prototype.valueof
  60. /// </summary>
  61. private JsValue ValueOf(JsValue thisObject, JsCallArguments arguments)
  62. {
  63. return ThisSymbolValue(thisObject);
  64. }
  65. /// <summary>
  66. /// https://tc39.es/ecma262/#sec-symbol.prototype-@@toprimitive
  67. /// </summary>
  68. private JsValue ToPrimitive(JsValue thisObject, JsCallArguments arguments)
  69. {
  70. return ThisSymbolValue(thisObject);
  71. }
  72. private JsSymbol ThisSymbolValue(JsValue thisObject)
  73. {
  74. if (thisObject is JsSymbol s)
  75. {
  76. return s;
  77. }
  78. if (thisObject is SymbolInstance instance)
  79. {
  80. return instance.SymbolData;
  81. }
  82. ExceptionHelper.ThrowTypeError(_realm);
  83. return null;
  84. }
  85. }