about summary refs log tree commit diff stats
path: root/src/ips
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-19 18:13:10 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-19 18:15:09 +0200
commit17097052794aef56bbc55327d3e6c84ae1c67378 (patch)
tree13b81e1105c07c69d7a8d1e7367a698f41663a01 /src/ips
parente372bdaa0344b23c91aefa4da44c578fbf8f49e2 (diff)
downloadchawan-17097052794aef56bbc55327d3e6c84ae1c67378.tar.gz
Rework JS exception system
Now we use Result for passing exceptions to JS. As a result, we can
finally get rid of the .jserr pragma.
Diffstat (limited to 'src/ips')
-rw-r--r--src/ips/serialize.nim41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/ips/serialize.nim b/src/ips/serialize.nim
index be5bbe28..f7fb934e 100644
--- a/src/ips/serialize.nim
+++ b/src/ips/serialize.nim
@@ -11,6 +11,7 @@ import types/blob
 import types/buffersource
 import types/formdata
 import types/url
+import utils/opt
 
 proc swrite*(stream: Stream, n: SomeNumber)
 proc sread*(stream: Stream, n: var SomeNumber)
@@ -68,6 +69,10 @@ proc swrite*[T](stream: Stream, o: Option[T])
 proc sread*[T](stream: Stream, o: var Option[T])
 func slen*[T](o: Option[T]): int
 
+proc swrite*[T, E](stream: Stream, o: Result[T, E])
+proc sread*[T, E](stream: Stream, o: var Result[T, E])
+func slen*[T, E](o: Result[T, E]): int
+
 proc swrite*(stream: Stream, regex: Regex)
 proc sread*(stream: Stream, regex: var Regex)
 func slen*(regex: Regex): int
@@ -339,6 +344,42 @@ func slen*[T](o: Option[T]): int =
   if o.isSome:
     result += slen(o.get)
 
+proc swrite*[T, E](stream: Stream, o: Result[T, E]) =
+  stream.swrite(o.isOk)
+  if o.isOk:
+    when not (T is void):
+      stream.swrite(o.get)
+  else:
+    when not (E is void):
+      stream.swrite(o.error)
+
+proc sread*[T, E](stream: Stream, o: var Result[T, E]) =
+  var x: bool
+  stream.sread(x)
+  if x:
+    when not (T is void):
+      var m: T
+      stream.sread(m)
+      o.ok(m)
+    else:
+      o.ok()
+  else:
+    when not (E is void):
+      var e: E
+      stream.sread(e)
+      o.err(e)
+    else:
+      o.err()
+
+func slen*[T, E](o: Result[T, E]): int =
+  result = slen(o.isSome)
+  if o.isSome:
+    when not (T is void):
+      result += slen(o.get)
+  else:
+    when not (E is void):
+      result += slen(o.error)
+
 proc swrite*(stream: Stream, regex: Regex) =
   stream.swrite(regex.plen)
   stream.writeData(regex.bytecode, regex.plen)