about summary refs log tree commit diff stats
path: root/src/js
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-08-09 00:29:31 +0200
committerbptato <nincsnevem662@gmail.com>2024-08-09 00:29:31 +0200
commitb6b998bf608f2f82d5b639455b2fd6224b0919e2 (patch)
tree8dd1e2869b64ac584700b6de034db5c667200954 /src/js
parent4c64687290c908cd791a058dede9bd4f2a1c7757 (diff)
downloadchawan-b6b998bf608f2f82d5b639455b2fd6224b0919e2.tar.gz
Update monoucha
Diffstat (limited to 'src/js')
-rw-r--r--src/js/console.nim29
-rw-r--r--src/js/domexception.nim2
-rw-r--r--src/js/encoding.nim15
-rw-r--r--src/js/jscolor.nim34
-rw-r--r--src/js/timeout.nim6
5 files changed, 45 insertions, 41 deletions
diff --git a/src/js/console.nim b/src/js/console.nim
index b61dedf7..755910a5 100644
--- a/src/js/console.nim
+++ b/src/js/console.nim
@@ -1,7 +1,6 @@
 import io/dynstream
 import monoucha/fromjs
 import monoucha/javascript
-import monoucha/jserror
 import types/opt
 
 type Console* = ref object
@@ -33,11 +32,13 @@ proc log*(console: Console; ss: varargs[string]) =
 proc error*(console: Console; ss: varargs[string]) =
   console.log(ss)
 
-proc log*(ctx: JSContext; console: Console; ss: varargs[JSValue]):
-    JSResult[void] {.jsfunc.} =
+proc log*(ctx: JSContext; console: Console; ss: varargs[JSValue]): Opt[void]
+    {.jsfunc.} =
   var buf = ""
   for i, val in ss:
-    buf &= ?fromJS[string](ctx, val)
+    var res: string
+    ?ctx.fromJS(val, res)
+    buf &= res
     if i != ss.high:
       buf &= ' '
   buf &= '\n'
@@ -49,20 +50,20 @@ proc clear(console: Console) {.jsfunc.} =
     console.clearFun()
 
 # For now, these are the same as log().
-proc debug(ctx: JSContext; console: Console; ss: varargs[JSValue]):
-    JSResult[void] {.jsfunc.} =
+proc debug(ctx: JSContext; console: Console; ss: varargs[JSValue]): Opt[void]
+    {.jsfunc.} =
   return log(ctx, console, ss)
 
-proc error(ctx: JSContext; console: Console; ss: varargs[JSValue]):
-    JSResult[void] {.jsfunc.} =
+proc error(ctx: JSContext; console: Console; ss: varargs[JSValue]): Opt[void]
+    {.jsfunc.} =
   return log(ctx, console, ss)
 
-proc info(ctx: JSContext; console: Console; ss: varargs[JSValue]):
-    JSResult[void] {.jsfunc.} =
+proc info(ctx: JSContext; console: Console; ss: varargs[JSValue]): Opt[void]
+    {.jsfunc.} =
   return log(ctx, console, ss)
 
-proc warn(ctx: JSContext; console: Console; ss: varargs[JSValue]):
-    JSResult[void] {.jsfunc.} =
+proc warn(ctx: JSContext; console: Console; ss: varargs[JSValue]): Opt[void]
+    {.jsfunc.} =
   return log(ctx, console, ss)
 
 proc show(console: Console) {.jsfunc.} =
@@ -81,7 +82,3 @@ proc addConsoleModule*(ctx: JSContext) =
 proc writeException*(ctx: JSContext; s: DynStream) =
   s.write(ctx.getExceptionMsg())
   s.sflush()
-
-proc writeException*(ctx: JSContext; s: DynStream; err: JSError) =
-  s.write(ctx.getExceptionMsg(err))
-  s.sflush()
diff --git a/src/js/domexception.nim b/src/js/domexception.nim
index 1fab7ed9..d07e88f4 100644
--- a/src/js/domexception.nim
+++ b/src/js/domexception.nim
@@ -1,5 +1,3 @@
-import std/tables
-
 import monoucha/javascript
 import monoucha/jserror
 import monoucha/quickjs
diff --git a/src/js/encoding.nim b/src/js/encoding.nim
index 2394cc6c..7d1bd126 100644
--- a/src/js/encoding.nim
+++ b/src/js/encoding.nim
@@ -13,7 +13,7 @@ type
     encoding: Charset
     ignoreBOM {.jsget.}: bool
     errorMode: DecoderErrorMode
-    doNotFlush: bool
+    stream: bool
     bomSeen: bool
     tdctx: TextDecoderContext
 
@@ -21,10 +21,10 @@ jsDestructor(JSTextDecoder)
 jsDestructor(JSTextEncoder)
 
 type TextDecoderOptions = object of JSDict
-  fatal: bool
-  ignoreBOM: bool
+  fatal {.jsdefault.}: bool
+  ignoreBOM {.jsdefault.}: bool
 
-func newJSTextDecoder(label = "utf-8", options = TextDecoderOptions()):
+func newJSTextDecoder(label = "utf-8"; options = TextDecoderOptions()):
     JSResult[JSTextDecoder] {.jsctor.} =
   let encoding = getCharset(label)
   if encoding in {CHARSET_UNKNOWN, CHARSET_REPLACEMENT}:
@@ -52,17 +52,16 @@ proc decode0(this: JSTextDecoder; ctx: JSContext; input: JSArrayBufferView;
   return ok(JS_NewStringLen(ctx, cstring(oq), csize_t(oq.len)))
 
 type TextDecodeOptions = object of JSDict
-  stream: bool
+  stream {.jsdefault.}: bool
 
 #TODO AllowSharedBufferSource
 proc decode(ctx: JSContext; this: JSTextDecoder;
     input = none(JSArrayBufferView); options = TextDecodeOptions()):
     JSResult[JSValue] {.jsfunc.} =
-  if not this.doNotFlush:
+  if not this.stream:
     this.tdctx = initTextDecoderContext(this.encoding, this.errorMode)
     this.bomSeen = false
-  if this.doNotFlush != options.stream:
-    this.doNotFlush = options.stream
+  this.stream = options.stream
   if input.isSome:
     return this.decode0(ctx, input.get, options.stream)
   return ok(JS_NewString(ctx, ""))
diff --git a/src/js/jscolor.nim b/src/js/jscolor.nim
index 8491e778..fef9c8aa 100644
--- a/src/js/jscolor.nim
+++ b/src/js/jscolor.nim
@@ -2,7 +2,6 @@ import std/strutils
 
 import monoucha/fromjs
 import monoucha/javascript
-import monoucha/jserror
 import monoucha/quickjs
 import monoucha/tojs
 import types/color
@@ -10,12 +9,12 @@ import types/opt
 import utils/charcategory
 import utils/twtstr
 
-func parseLegacyColor*(s: string): JSResult[RGBColor] =
+func parseLegacyColor*(s: string): Result[RGBColor, cstring] =
   if s == "":
-    return errTypeError("Color value must not be the empty string")
+    return err(cstring"color value must not be the empty string")
   let s = s.strip(chars = AsciiWhitespace).toLowerAscii()
   if s == "transparent":
-    return errTypeError("Color must not be transparent")
+    return err(cstring"color must not be transparent")
   return ok(parseLegacyColor0(s))
 
 proc toJS*(ctx: JSContext; rgb: RGBColor): JSValue =
@@ -25,8 +24,15 @@ proc toJS*(ctx: JSContext; rgb: RGBColor): JSValue =
   res.pushHex(rgb.b)
   return toJS(ctx, res)
 
-proc fromJSRGBColor*(ctx: JSContext; val: JSValue): JSResult[RGBColor] =
-  return parseLegacyColor(?fromJS[string](ctx, val))
+proc fromJS*(ctx: JSContext; val: JSValue; res: var RGBColor): Err[void] =
+  var s: string
+  ?ctx.fromJS(val, s)
+  let x = parseLegacyColor(s)
+  if x.isNone:
+    JS_ThrowTypeError(ctx, x.error)
+    return err()
+  res = x.get
+  return ok()
 
 proc toJS*(ctx: JSContext; rgba: ARGBColor): JSValue =
   var res = "#"
@@ -36,12 +42,16 @@ proc toJS*(ctx: JSContext; rgba: ARGBColor): JSValue =
   res.pushHex(rgba.a)
   return toJS(ctx, res)
 
-proc fromJSARGBColor*(ctx: JSContext; val: JSValue): JSResult[ARGBColor] =
+proc fromJS*(ctx: JSContext; val: JSValue; res: var ARGBColor): Err[void] =
   if JS_IsNumber(val):
     # as hex
-    return ok(ARGBColor(?fromJS[uint32](ctx, val)))
+    ?ctx.fromJS(val, uint32(res))
+    return ok()
   # parse
-  let x = parseARGBColor(?fromJS[string](ctx, val))
-  if x.isSome:
-    return ok(x.get)
-  return errTypeError("Unrecognized color")
+  var s: string
+  ?ctx.fromJS(val, s)
+  if (let x = parseARGBColor(s); x.isSome):
+    res = x.get
+    return ok()
+  JS_ThrowTypeError(ctx, "unrecognized color")
+  return err()
diff --git a/src/js/timeout.nim b/src/js/timeout.nim
index f8b8ed8a..0213156a 100644
--- a/src/js/timeout.nim
+++ b/src/js/timeout.nim
@@ -75,9 +75,9 @@ proc runEntry(state: var TimeoutState; entry: TimeoutEntry; name: string) =
       state.jsctx.writeException(state.err)
     JS_FreeValue(state.jsctx, ret)
   else:
-    let s = fromJS[string](state.jsctx, entry.val)
-    if s.isSome:
-      state.evalJSFree(s.get, name)
+    var s: string
+    if state.jsctx.fromJS(entry.val, s).isSome:
+      state.evalJSFree(s, name)
 
 proc runTimeoutFd*(state: var TimeoutState; fd: int): bool =
   if fd notin state.timeoutFds: