AtomicsInstance.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Threading;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Atomics;
  7. /// <summary>
  8. /// https://tc39.es/ecma262/#sec-atomics-object
  9. /// </summary>
  10. internal sealed class AtomicsInstance : ObjectInstance
  11. {
  12. private readonly Realm _realm;
  13. public AtomicsInstance(
  14. Engine engine,
  15. Realm realm,
  16. ObjectPrototype objectPrototype) : base(engine)
  17. {
  18. _realm = realm;
  19. _prototype = objectPrototype;
  20. }
  21. protected override void Initialize()
  22. {
  23. var properties = new PropertyDictionary(1, checkExistingKeys: false)
  24. {
  25. ["pause"] = new(new ClrFunction(Engine, "pause", Pause, 0, PropertyFlag.Configurable), true, false, true),
  26. };
  27. SetProperties(properties);
  28. }
  29. private JsValue Pause(JsValue thisObject, JsCallArguments arguments)
  30. {
  31. var iterationNumber = arguments.At(0);
  32. if (!iterationNumber.IsUndefined())
  33. {
  34. if (!iterationNumber.IsNumber())
  35. {
  36. ExceptionHelper.ThrowTypeError(_realm, "Invalid iteration count");
  37. }
  38. var n = TypeConverter.ToNumber(iterationNumber);
  39. if (!TypeConverter.IsIntegralNumber(n))
  40. {
  41. ExceptionHelper.ThrowTypeError(_realm, "Invalid iteration count");
  42. }
  43. if (n < 0)
  44. {
  45. ExceptionHelper.ThrowRangeError(_realm, "Invalid iteration count");
  46. }
  47. n = System.Math.Min(n, _engine.Options.Constraints.MaxAtomicsPauseIterations);
  48. Thread.SpinWait((int) n);
  49. }
  50. else
  51. {
  52. Thread.SpinWait(1);
  53. }
  54. return Undefined;
  55. }
  56. }