|
| 1 | +using Jint.Native.Object; |
| 2 | +using Jint.Native.Symbol; |
| 3 | +using Jint.Runtime; |
| 4 | +using Jint.Runtime.Descriptors; |
| 5 | +using Jint.Runtime.Interop; |
| 6 | + |
| 7 | +namespace Jint.Native.Disposable; |
| 8 | + |
| 9 | +internal sealed class AsyncDisposableStackPrototype : Prototype |
| 10 | +{ |
| 11 | + private readonly AsyncDisposableStackConstructor _constructor; |
| 12 | + |
| 13 | + internal AsyncDisposableStackPrototype( |
| 14 | + Engine engine, |
| 15 | + Realm realm, |
| 16 | + AsyncDisposableStackConstructor constructor, |
| 17 | + ObjectPrototype objectPrototype) : base(engine, realm) |
| 18 | + { |
| 19 | + _prototype = objectPrototype; |
| 20 | + _constructor = constructor; |
| 21 | + } |
| 22 | + |
| 23 | + protected override void Initialize() |
| 24 | + { |
| 25 | + var disposeFunction = new ClrFunction(Engine, "disposeAsync", Dispose, 0, PropertyFlag.Configurable); |
| 26 | + |
| 27 | + const PropertyFlag PropertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; |
| 28 | + var properties = new PropertyDictionary(8, checkExistingKeys: false) |
| 29 | + { |
| 30 | + ["length"] = new PropertyDescriptor(0, PropertyFlag.Configurable), |
| 31 | + ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable), |
| 32 | + ["adopt"] = new PropertyDescriptor(new ClrFunction(Engine, "adopt", Adopt, 2, PropertyFlag.Configurable), PropertyFlags), |
| 33 | + ["defer"] = new PropertyDescriptor(new ClrFunction(Engine, "defer", Defer, 1, PropertyFlag.Configurable), PropertyFlags), |
| 34 | + ["disposeAsync"] = new PropertyDescriptor(disposeFunction, PropertyFlags), |
| 35 | + ["disposed"] = new GetSetPropertyDescriptor(get: new ClrFunction(Engine, "get disposed", Disposed, 0, PropertyFlag.Configurable), set: Undefined, PropertyFlags), |
| 36 | + ["move"] = new PropertyDescriptor(new ClrFunction(Engine, "move", Move, 0, PropertyFlag.Configurable), PropertyFlags), |
| 37 | + ["use"] = new PropertyDescriptor(new ClrFunction(Engine, "use", Use, 1, PropertyFlag.Configurable), PropertyFlags), |
| 38 | + }; |
| 39 | + SetProperties(properties); |
| 40 | + |
| 41 | + var symbols = new SymbolDictionary(2) |
| 42 | + { |
| 43 | + [GlobalSymbolRegistry.AsyncDispose] = new PropertyDescriptor(disposeFunction, PropertyFlags), |
| 44 | + [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("AsyncDisposableStack", PropertyFlag.Configurable), |
| 45 | + }; |
| 46 | + SetSymbols(symbols); |
| 47 | + } |
| 48 | + |
| 49 | + private JsValue Adopt(JsValue thisObject, JsCallArguments arguments) |
| 50 | + { |
| 51 | + var stack = AssertDisposableStack(thisObject); |
| 52 | + return stack.Adopt(arguments.At(0), arguments.At(1)); |
| 53 | + } |
| 54 | + |
| 55 | + private JsValue Defer(JsValue thisObject, JsCallArguments arguments) |
| 56 | + { |
| 57 | + var stack = AssertDisposableStack(thisObject); |
| 58 | + stack.Defer(arguments.At(0)); |
| 59 | + return Undefined; |
| 60 | + } |
| 61 | + |
| 62 | + private JsValue Dispose(JsValue thisObject, JsCallArguments arguments) |
| 63 | + { |
| 64 | + var stack = AssertDisposableStack(thisObject); |
| 65 | + return stack.Dispose(); |
| 66 | + } |
| 67 | + |
| 68 | + private JsValue Disposed(JsValue thisObject, JsCallArguments arguments) |
| 69 | + { |
| 70 | + var stack = AssertDisposableStack(thisObject); |
| 71 | + return stack.State == DisposableState.Disposed; |
| 72 | + } |
| 73 | + |
| 74 | + private JsValue Move(JsValue thisObject, JsCallArguments arguments) |
| 75 | + { |
| 76 | + var stack = AssertDisposableStack(thisObject); |
| 77 | + var newDisposableStack = _engine.Intrinsics.Function.OrdinaryCreateFromConstructor( |
| 78 | + _engine.Intrinsics.AsyncDisposableStack, |
| 79 | + static intrinsics => intrinsics.AsyncDisposableStack.PrototypeObject, |
| 80 | + static (Engine engine, Realm _, object? _) => new DisposableStack(engine, DisposeHint.Async)); |
| 81 | + |
| 82 | + return stack.Move(newDisposableStack); |
| 83 | + } |
| 84 | + |
| 85 | + private JsValue Use(JsValue thisObject, JsCallArguments arguments) |
| 86 | + { |
| 87 | + var stack = AssertDisposableStack(thisObject); |
| 88 | + return stack.Use(arguments.At(0)); |
| 89 | + } |
| 90 | + |
| 91 | + private DisposableStack AssertDisposableStack(JsValue thisObject) |
| 92 | + { |
| 93 | + if (thisObject is not DisposableStack { _hint: DisposeHint.Async } stack) |
| 94 | + { |
| 95 | + ExceptionHelper.ThrowTypeError(_engine.Realm, "This is not a AsyncDisposableStack instance."); |
| 96 | + return null!; |
| 97 | + } |
| 98 | + |
| 99 | + return stack; |
| 100 | + } |
| 101 | +} |
0 commit comments