SymbolPrototype.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Jint.Collections;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Symbol
  7. {
  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 ClrFunctionInstance(Engine, "description", Description, 0, lengthFlags), Undefined, propertyFlags),
  33. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToSymbolString, 0, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable),
  34. ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOf", ValueOf, 0, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable)
  35. });
  36. SetSymbols(new SymbolDictionary(1)
  37. {
  38. [GlobalSymbolRegistry.ToPrimitive] = new PropertyDescriptor(new ClrFunctionInstance(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, JsValue[] 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, JsValue[] 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, JsValue[] 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, JsValue[] 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. }
  86. }