about summary refs log tree commit diff stats
path: root/src/js
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-07-30 19:46:22 +0200
committerbptato <nincsnevem662@gmail.com>2024-07-30 19:58:42 +0200
commitafec3aabd21d418603a8566393750e789ee6007a (patch)
tree41308537a21906c5bd40b70feded49e3e5227e26 /src/js
parent98fdd778d2c222b9b6b61d181680a74a09ecf44f (diff)
downloadchawan-afec3aabd21d418603a8566393750e789ee6007a.tar.gz
console, client: optimize console.log (etc.)
use JSValue varargs instead
Diffstat (limited to 'src/js')
-rw-r--r--src/js/console.nim53
1 files changed, 37 insertions, 16 deletions
diff --git a/src/js/console.nim b/src/js/console.nim
index 2ea0fa91..b61dedf7 100644
--- a/src/js/console.nim
+++ b/src/js/console.nim
@@ -1,6 +1,8 @@
 import io/dynstream
-import monoucha/jserror
+import monoucha/fromjs
 import monoucha/javascript
+import monoucha/jserror
+import types/opt
 
 type Console* = ref object
   err*: DynStream
@@ -19,36 +21,55 @@ proc newConsole*(err: DynStream; clearFun: proc() = nil; showFun: proc() = nil;
     hideFun: hideFun
   )
 
-proc log*(console: Console; ss: varargs[string]) {.jsfunc.} =
+proc log*(console: Console; ss: varargs[string]) =
+  var buf = ""
   for i, s in ss:
-    console.err.write(s)
+    buf &= s
+    if i != ss.high:
+      buf &= ' '
+  buf &= '\n'
+  console.err.write(buf)
+
+proc error*(console: Console; ss: varargs[string]) =
+  console.log(ss)
+
+proc log*(ctx: JSContext; console: Console; ss: varargs[JSValue]):
+    JSResult[void] {.jsfunc.} =
+  var buf = ""
+  for i, val in ss:
+    buf &= ?fromJS[string](ctx, val)
     if i != ss.high:
-      console.err.write(' ')
-  console.err.write('\n')
-  console.err.sflush()
+      buf &= ' '
+  buf &= '\n'
+  console.err.write(buf)
+  ok()
 
 proc clear(console: Console) {.jsfunc.} =
   if console.clearFun != nil:
     console.clearFun()
 
 # For now, these are the same as log().
-proc debug(console: Console; ss: varargs[string]) {.jsfunc.} =
-  console.log(ss)
+proc debug(ctx: JSContext; console: Console; ss: varargs[JSValue]):
+    JSResult[void] {.jsfunc.} =
+  return log(ctx, console, ss)
 
-proc error*(console: Console; ss: varargs[string]) {.jsfunc.} =
-  console.log(ss)
+proc error(ctx: JSContext; console: Console; ss: varargs[JSValue]):
+    JSResult[void] {.jsfunc.} =
+  return log(ctx, console, ss)
 
-proc info(console: Console; ss: varargs[string]) {.jsfunc.} =
-  console.log(ss)
+proc info(ctx: JSContext; console: Console; ss: varargs[JSValue]):
+    JSResult[void] {.jsfunc.} =
+  return log(ctx, console, ss)
 
-proc warn(console: Console; ss: varargs[string]) {.jsfunc.} =
-  console.log(ss)
+proc warn(ctx: JSContext; console: Console; ss: varargs[JSValue]):
+    JSResult[void] {.jsfunc.} =
+  return log(ctx, console, ss)
 
-proc show(console: Console; ss: varargs[string]) {.jsfunc.} =
+proc show(console: Console) {.jsfunc.} =
   if console.showFun != nil:
     console.showFun()
 
-proc hide(console: Console; ss: varargs[string]) {.jsfunc.} =
+proc hide(console: Console) {.jsfunc.} =
   if console.hideFun != nil:
     console.hideFun()