diff options
author | bptato <nincsnevem662@gmail.com> | 2022-12-31 02:14:34 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-12-31 02:14:34 +0100 |
commit | 80b45c0df3f5bbbedb9abfb02fdef608113958e1 (patch) | |
tree | 475cd89df09b4147b8ee6ce9a1ec28954eb6dc30 /src/js/javascript.nim | |
parent | 310254b09ee2dd3045648848ca9f60f9f1f7c769 (diff) | |
download | chawan-80b45c0df3f5bbbedb9abfb02fdef608113958e1.tar.gz |
Add promise support to JS
Diffstat (limited to 'src/js/javascript.nim')
-rw-r--r-- | src/js/javascript.nim | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim index d7d0486c..56746a53 100644 --- a/src/js/javascript.nim +++ b/src/js/javascript.nim @@ -30,6 +30,8 @@ import strutils import tables import unicode +import io/promise + import bindings/quickjs export options @@ -719,6 +721,33 @@ func toJSObject[T](ctx: JSContext, obj: T): JSValue = setOpaque(ctx, jsObj, obj) return jsObj +func toJSPromise(ctx: JSContext, promise: EmptyPromise): JSValue = + var resolving_funcs: array[2, JSValue] + let jsPromise = JS_NewPromiseCapability(ctx, addr resolving_funcs[0]) + if JS_IsException(jsPromise): + return JS_EXCEPTION + promise.then(proc() = + var x = JS_UNDEFINED + let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 1, addr x) + JS_FreeValue(ctx, res) + JS_FreeValue(ctx, resolving_funcs[0]) + JS_FreeValue(ctx, resolving_funcs[1])) + return jsPromise + +func toJSPromise[T](ctx: JSContext, promise: Promise[T]): JSValue = + var resolving_funcs: array[2, JSValue] + let jsPromise = JS_NewPromiseCapability(ctx, addr resolving_funcs[0]) + if JS_IsException(jsPromise): + return JS_EXCEPTION + promise.then(proc(x: T) = + var x = toJS(ctx, x) + let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 1, addr x) + JS_FreeValue(ctx, res) + JS_FreeValue(ctx, x) + JS_FreeValue(ctx, resolving_funcs[0]) + JS_FreeValue(ctx, resolving_funcs[1])) + return jsPromise + proc toJS*[T](ctx: JSContext, obj: T): JSValue = when T is string: return ctx.toJSString(obj) @@ -751,6 +780,10 @@ proc toJS*[T](ctx: JSContext, obj: T): JSValue = return toJS(ctx, int(obj)) elif T is JSValue: return obj + elif T is Promise: + return toJSPromise(ctx, obj) + elif T is EmptyPromise: + return toJSPromise(ctx, obj) else: if obj == nil: return JS_NULL |