about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-06-30 20:33:39 +0200
committerbptato <nincsnevem662@gmail.com>2024-06-30 20:33:39 +0200
commite11b25aa7e29a690dd34bb48dd14642f0b7fad62 (patch)
treeba9049f7b3c2836f6c96b88e7e56c06c20605b8b /src
parented2d399f716a3a0fb9175857781b0f70b54db032 (diff)
downloadchawan-e11b25aa7e29a690dd34bb48dd14642f0b7fad62.tar.gz
promise, container: fix nil derefs
Diffstat (limited to 'src')
-rw-r--r--src/io/promise.nim38
-rw-r--r--src/local/container.nim4
2 files changed, 25 insertions, 17 deletions
diff --git a/src/io/promise.nim b/src/io/promise.nim
index a1a5cfc9..41ccceb4 100644
--- a/src/io/promise.nim
+++ b/src/io/promise.nim
@@ -221,35 +221,41 @@ proc fromJSEmptyPromise*(ctx: JSContext; val: JSValue): JSResult[EmptyPromise] =
   return ok(p)
 
 proc toJS*(ctx: JSContext; promise: EmptyPromise): JSValue =
-  var resolving_funcs: array[2, JSValue]
-  let jsPromise = JS_NewPromiseCapability(ctx, resolving_funcs.toJSValueArray())
+  if promise == nil:
+    return JS_NULL
+  var resolvingFuncs: array[2, JSValue]
+  let jsPromise = JS_NewPromiseCapability(ctx, resolvingFuncs.toJSValueArray())
   if JS_IsException(jsPromise):
     return JS_EXCEPTION
   promise.then(proc() =
-    let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 0, nil)
+    let res = JS_Call(ctx, resolvingFuncs[0], JS_UNDEFINED, 0, nil)
     JS_FreeValue(ctx, res)
-    JS_FreeValue(ctx, resolving_funcs[0])
-    JS_FreeValue(ctx, resolving_funcs[1]))
+    JS_FreeValue(ctx, resolvingFuncs[0])
+    JS_FreeValue(ctx, resolvingFuncs[1]))
   return jsPromise
 
 proc toJS*[T](ctx: JSContext; promise: Promise[T]): JSValue =
-  var resolving_funcs: array[2, JSValue]
-  let jsPromise = JS_NewPromiseCapability(ctx, resolving_funcs.toJSValueArray())
+  if promise == nil:
+    return JS_NULL
+  var resolvingFuncs: array[2, JSValue]
+  let jsPromise = JS_NewPromiseCapability(ctx, resolvingFuncs.toJSValueArray())
   if JS_IsException(jsPromise):
     return JS_EXCEPTION
   promise.then(proc(x: T) =
     let x = toJS(ctx, x)
-    let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 1,
+    let res = JS_Call(ctx, resolvingFuncs[0], JS_UNDEFINED, 1,
       x.toJSValueArray())
     JS_FreeValue(ctx, res)
     JS_FreeValue(ctx, x)
-    JS_FreeValue(ctx, resolving_funcs[0])
-    JS_FreeValue(ctx, resolving_funcs[1]))
+    JS_FreeValue(ctx, resolvingFuncs[0])
+    JS_FreeValue(ctx, resolvingFuncs[1]))
   return jsPromise
 
 proc toJS*[T, E](ctx: JSContext; promise: Promise[Result[T, E]]): JSValue =
-  var resolving_funcs: array[2, JSValue]
-  let jsPromise = JS_NewPromiseCapability(ctx, resolving_funcs.toJSValueArray())
+  if promise == nil:
+    return JS_NULL
+  var resolvingFuncs: array[2, JSValue]
+  let jsPromise = JS_NewPromiseCapability(ctx, resolvingFuncs.toJSValueArray())
   if JS_IsException(jsPromise):
     return JS_EXCEPTION
   promise.then(proc(x: Result[T, E]) =
@@ -258,7 +264,7 @@ proc toJS*[T, E](ctx: JSContext; promise: Promise[Result[T, E]]): JSValue =
         JS_UNDEFINED
       else:
         toJS(ctx, x.get)
-      let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 1,
+      let res = JS_Call(ctx, resolvingFuncs[0], JS_UNDEFINED, 1,
         x.toJSValueArray())
       JS_FreeValue(ctx, res)
       JS_FreeValue(ctx, x)
@@ -267,10 +273,10 @@ proc toJS*[T, E](ctx: JSContext; promise: Promise[Result[T, E]]): JSValue =
         JS_UNDEFINED
       else:
         toJS(ctx, x.error)
-      let res = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED, 1,
+      let res = JS_Call(ctx, resolvingFuncs[1], JS_UNDEFINED, 1,
         x.toJSValueArray())
       JS_FreeValue(ctx, res)
       JS_FreeValue(ctx, x)
-    JS_FreeValue(ctx, resolving_funcs[0])
-    JS_FreeValue(ctx, resolving_funcs[1]))
+    JS_FreeValue(ctx, resolvingFuncs[0])
+    JS_FreeValue(ctx, resolvingFuncs[1]))
   return jsPromise
diff --git a/src/local/container.nim b/src/local/container.nim
index d78f9120..cc1e75e5 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -1602,8 +1602,10 @@ proc cursorToggleSelection(container: Container; n = 1;
 proc getSelectionText(container: Container; hl: Highlight = nil):
     Promise[string] {.jsfunc.} =
   if container.iface == nil:
-    return
+    return nil
   let hl = if hl == nil: container.currentSelection else: hl
+  if hl == nil:
+    return nil
   if hl.t != hltSelect:
     let p = newPromise[string]()
     p.resolve("")