SetPrototype.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Jint.Native.Object;
  2. using Jint.Native.Symbol;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Descriptors.Specialized;
  6. using Jint.Runtime.Interop;
  7. namespace Jint.Native.Set
  8. {
  9. /// <summary>
  10. /// https://www.ecma-international.org/ecma-262/6.0/#sec-set-objects
  11. /// </summary>
  12. public sealed class SetPrototype : ObjectInstance
  13. {
  14. private SetPrototype(Engine engine) : base(engine)
  15. {
  16. }
  17. public static SetPrototype CreatePrototypeObject(Engine engine, SetConstructor mapConstructor)
  18. {
  19. var obj = new SetPrototype(engine)
  20. {
  21. Extensible = true,
  22. Prototype = engine.Object.PrototypeObject
  23. };
  24. obj.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.Configurable));
  25. obj.SetOwnProperty("constructor", new PropertyDescriptor(mapConstructor, PropertyFlag.NonEnumerable));
  26. return obj;
  27. }
  28. public void Configure()
  29. {
  30. FastAddProperty(GlobalSymbolRegistry.Iterator._value, new ClrFunctionInstance(Engine, "iterator", Values, 1, PropertyFlag.Configurable), true, false, true);
  31. FastAddProperty(GlobalSymbolRegistry.ToStringTag._value, "Set", false, false, true);
  32. FastAddProperty("add", new ClrFunctionInstance(Engine, "add", Add, 1, PropertyFlag.Configurable), true, false, true);
  33. FastAddProperty("clear", new ClrFunctionInstance(Engine, "clear", Clear, 0, PropertyFlag.Configurable), true, false, true);
  34. FastAddProperty("delete", new ClrFunctionInstance(Engine, "delete", Delete, 1, PropertyFlag.Configurable), true, false, true);
  35. FastAddProperty("entries", new ClrFunctionInstance(Engine, "entries", Entries, 0, PropertyFlag.Configurable), true, false, true);
  36. FastAddProperty("forEach", new ClrFunctionInstance(Engine, "forEach", ForEach, 1, PropertyFlag.Configurable), true, false, true);
  37. FastAddProperty("has", new ClrFunctionInstance(Engine, "has", Has, 1, PropertyFlag.Configurable), true, false, true);
  38. FastAddProperty("keys", new ClrFunctionInstance(Engine, "keys", Values, 0, PropertyFlag.Configurable), true, false, true);
  39. FastAddProperty("values", new ClrFunctionInstance(Engine, "values", Values, 0, PropertyFlag.Configurable), true, false, true);
  40. AddProperty(
  41. "size",
  42. new GetSetPropertyDescriptor(
  43. get: new ClrFunctionInstance(Engine, "get size", Size, 0, PropertyFlag.Configurable),
  44. set: null,
  45. PropertyFlag.Configurable));
  46. }
  47. private JsValue Size(JsValue thisObj, JsValue[] arguments)
  48. {
  49. if (!thisObj.IsObject())
  50. {
  51. ExceptionHelper.ThrowTypeError(_engine);
  52. }
  53. return JsNumber.Create(0);
  54. }
  55. private JsValue Add(JsValue thisObj, JsValue[] arguments)
  56. {
  57. ((SetInstance) thisObj).Add(arguments[0]);
  58. return thisObj;
  59. }
  60. private JsValue Clear(JsValue thisObj, JsValue[] arguments)
  61. {
  62. var map = thisObj as SetInstance
  63. ?? ExceptionHelper.ThrowTypeError<SetInstance>(_engine, "object must be a Set");
  64. map.Clear();
  65. return Undefined;
  66. }
  67. private JsValue Delete(JsValue thisObj, JsValue[] arguments)
  68. {
  69. var map = thisObj as SetInstance
  70. ?? ExceptionHelper.ThrowTypeError<SetInstance>(_engine, "object must be a Set");
  71. return map.Delete(arguments[0])
  72. ? JsBoolean.True
  73. : JsBoolean.False;
  74. }
  75. private JsValue Has(JsValue thisObj, JsValue[] arguments)
  76. {
  77. return ((SetInstance) thisObj).Has(arguments[0])
  78. ? JsBoolean.True
  79. : JsBoolean.False;
  80. }
  81. private JsValue Entries(JsValue thisObj, JsValue[] arguments)
  82. {
  83. var map = thisObj as SetInstance
  84. ?? ExceptionHelper.ThrowTypeError<SetInstance>(_engine, "object must be a Set");
  85. return map.Entries();
  86. }
  87. private JsValue ForEach(JsValue thisObj, JsValue[] arguments)
  88. {
  89. var callbackfn = arguments.At(0);
  90. var thisArg = arguments.At(1);
  91. var map = (SetInstance) thisObj;
  92. var callable = GetCallable(callbackfn);
  93. map.ForEach(callable, thisArg);
  94. return Undefined;
  95. }
  96. private ObjectInstance Values(JsValue thisObj, JsValue[] arguments)
  97. {
  98. return ((SetInstance) thisObj).Values();
  99. }
  100. }
  101. }