SymbolPrototype.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4
  10. /// </summary>
  11. public 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. private JsValue Description(JsValue thisObject, JsValue[] arguments)
  43. {
  44. var sym = ThisSymbolValue(thisObject);
  45. return sym._value;
  46. }
  47. private JsValue ToSymbolString(JsValue thisObject, JsValue[] arguments)
  48. {
  49. var sym = ThisSymbolValue(thisObject);
  50. return new JsString(SymbolDescriptiveString(sym));
  51. }
  52. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments)
  53. {
  54. return ThisSymbolValue(thisObject);
  55. }
  56. private JsValue ToPrimitive(JsValue thisObject, JsValue[] arguments)
  57. {
  58. return ThisSymbolValue(thisObject);
  59. }
  60. internal static string SymbolDescriptiveString(JsSymbol symbol) => symbol.ToString();
  61. private JsSymbol ThisSymbolValue(JsValue thisObject)
  62. {
  63. if (thisObject is JsSymbol s)
  64. {
  65. return s;
  66. }
  67. if (thisObject is SymbolInstance instance)
  68. {
  69. return instance.SymbolData;
  70. }
  71. ExceptionHelper.ThrowTypeError(_realm);
  72. return null;
  73. }
  74. }
  75. }