about summary refs log tree commit diff stats
path: root/src/types/refstring.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/types/refstring.nim')
-rw-r--r--src/types/refstring.nim26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/types/refstring.nim b/src/types/refstring.nim
new file mode 100644
index 00000000..55c98530
--- /dev/null
+++ b/src/types/refstring.nim
@@ -0,0 +1,26 @@
+import monoucha/fromjs
+import monoucha/javascript
+import monoucha/tojs
+import types/opt
+
+type RefString* = ref object
+  s*: string
+
+proc newRefString*(s: sink string): RefString =
+  return RefString(s: s)
+
+converter `$`*(rs: RefString): lent string =
+  rs.s
+
+template `&=`*(rs: var RefString; ss: string) =
+  rs.s &= ss
+
+proc toJS*(ctx: JSContext; rs: RefString): JSValue =
+  return ctx.toJS($rs)
+
+proc fromJS*(ctx: JSContext; val: JSValue; rs: out RefString): Opt[void] =
+  rs = RefString()
+  if ctx.fromJS(val, rs.s).isNone:
+    rs = nil
+    return err()
+  return ok()