From 6cc89eff2caae5d70000a3d5837ea1c2c673ed79 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Sat, 19 Nov 2022 04:57:59 -0800 Subject: [PATCH] [internal] Introduce `JSPromise.Strong` --- src/bun.js/bindings/bindings.zig | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index a43f687725..7167cf8282 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -1472,6 +1472,30 @@ pub const JSPromise = extern struct { Rejected = 2, }; + pub const Strong = struct { + strong: JSC.Strong = .{}, + + pub fn init(globalThis: *JSC.JSGlobalObject) Strong { + return Strong{ + .strong = JSC.Strong.create(globalThis, JSC.JSPromise.create(globalThis).asValue(globalThis)), + }; + } + + pub fn get(this: *Strong) *JSC.JSPromise { + return this.strong.get().?.asPromise().?; + } + + pub fn value(this: *Strong) JSValue { + return this.strong.get().?; + } + + pub fn swap(this: *Strong) *JSC.JSPromise { + var prom = this.strong.swap().asPromise().?; + this.strong.deinit(); + return prom; + } + }; + pub fn wrap( globalObject: *JSGlobalObject, value: JSValue,