about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-11-30 10:55:18 +0100
committerbptato <nincsnevem662@gmail.com>2023-11-30 10:55:18 +0100
commit12aa7f1a48fe83a690e25ada0f0f2a390b12e981 (patch)
treea6c17b96aa80ab9559e9363e7efd7da6b63084a2
parenta969b10663e923ff903ea7c77c479ef0ec71f66a (diff)
downloadchawan-12aa7f1a48fe83a690e25ada0f0f2a390b12e981.tar.gz
intl: stub out Intl.PluralRules
-rw-r--r--src/js/intl.nim17
-rw-r--r--src/js/tojs.nim9
2 files changed, 26 insertions, 0 deletions
diff --git a/src/js/intl.nim b/src/js/intl.nim
index 8892d302..285a2b4e 100644
--- a/src/js/intl.nim
+++ b/src/js/intl.nim
@@ -1,19 +1,35 @@
 # Very minimal Intl module... TODO make it more complete
 
 import bindings/quickjs
+import js/dict
 import js/javascript
 import js/tojs
 
 type
   NumberFormat = ref object
 
+  PluralRules = ref object
+
+  PRResolvedOptions = object of JSDict
+    locale: string
+
 jsDestructor(NumberFormat)
+jsDestructor(PluralRules)
 
 #TODO ...yeah
 proc newNumberFormat(name: string = "en-US",
     options = none(JSValue)): NumberFormat {.jsctor.} =
   return NumberFormat()
 
+#TODO
+proc newPluralRules(): PluralRules {.jsctor.} =
+  return PluralRules()
+
+proc resolvedOptions(this: PluralRules): PRResolvedOptions {.jsfunc.} =
+  return PRResolvedOptions(
+    locale: "en-US"
+  )
+
 #TODO: this should accept string/BigInt too
 proc format(nf: NumberFormat, num: float64): string {.jsfunc.} =
   let s = $num
@@ -45,5 +61,6 @@ proc addIntlModule*(ctx: JSContext) =
   let global = JS_GetGlobalObject(ctx)
   let intl = JS_NewObject(ctx)
   ctx.registerType(NumberFormat, namespace = intl)
+  ctx.registerType(PluralRules, namespace = intl)
   ctx.defineProperty(global, "Intl", intl)
   JS_FreeValue(ctx, global)
diff --git a/src/js/tojs.nim b/src/js/tojs.nim
index 6d953e44..5ad495af 100644
--- a/src/js/tojs.nim
+++ b/src/js/tojs.nim
@@ -39,6 +39,7 @@ proc toJS*(ctx: JSContext, f: JSCFunction): JSValue
 proc toJS*(ctx: JSContext, abuf: JSArrayBuffer): JSValue
 proc toJS*(ctx: JSContext, u8a: JSUint8Array): JSValue
 proc toJS*(ctx: JSContext, ns: NarrowString): JSValue
+proc toJS*(ctx: JSContext, dict: JSDict): JSValue
 
 # Convert Nim types to the corresponding JavaScript type, with knowledge of
 # the parent object.
@@ -282,6 +283,14 @@ proc toJS*(ctx: JSContext, u8a: JSUint8Array): JSValue =
 proc toJS*(ctx: JSContext, ns: NarrowString): JSValue =
   return JS_NewNarrowStringLen(ctx, cstring(ns), csize_t(string(ns).len))
 
+proc toJS*(ctx: JSContext, dict: JSDict): JSValue =
+  let obj = JS_NewObject(ctx)
+  if JS_IsException(obj):
+    return obj
+  for k, v in dict.fieldPairs:
+    ctx.defineProperty(obj, k, v)
+  return obj
+
 proc toJSP(ctx: JSContext, parent: ref object, child: var object): JSValue =
   let p = addr child
   # Save parent as the original ancestor for this tree.