From 8027e52cb221c432bed64517015ebf3182e6166d Mon Sep 17 00:00:00 2001 From: bptato Date: Fri, 2 Jun 2023 00:36:54 +0200 Subject: Add support for canvas and multipart Quite incomplete canvas implementation. Crucially, the layout engine can't do much with whatever is drawn because it doesn't support images yet. I've re-introduced multipart as well, with the FormData API. For the append function I've also introduced a hack to the JS binding generator that allows requesting the JSContext pointer in nim procs. Really I should just fix the union generator thing and add support for overloading. In conclusion, for now the only thing canvas can be used for is exporting it as PNG and uploading it somewhere. Also, we now have PNG encoding and decoding too. (Now if only we had sixels as well...) --- src/js/intl.nim | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/js/intl.nim (limited to 'src/js/intl.nim') diff --git a/src/js/intl.nim b/src/js/intl.nim new file mode 100644 index 00000000..28cc42d5 --- /dev/null +++ b/src/js/intl.nim @@ -0,0 +1,46 @@ +# Very minimal Intl module... TODO make it more complete + +import bindings/quickjs +import js/javascript + +type + NumberFormat = ref object + +#TODO ...yeah +proc newNumberFormat(name: string = "en-US", + options = none(JSObject)): NumberFormat {.jsctor.} = + return NumberFormat() + +#TODO: this should accept string/BigInt too +proc format(nf: NumberFormat, num: float64): string {.jsfunc.} = + let s = $num + var i = 0 + var L = s.len + for k in countdown(s.high, 0): + if s[k] == '.': + L = k + break + if L mod 3 != 0: + while i < L mod 3: + result &= s[i] + inc i + if i < L: + result &= ',' + let j = i + while i < L: + if j != i and i mod 3 == j: + result &= ',' + result &= s[i] + inc i + if i + 1 < s.len and s[i] == '.': + if not (s[i + 1] == '0' and s.len == i + 2): + while i < s.len: + result &= s[i] + inc i + +proc addIntlModule*(ctx: JSContext) = + let global = JS_GetGlobalObject(ctx) + let intl = JS_NewObject(ctx) + ctx.registerType(NumberFormat, namespace = intl) + ctx.defineProperty(global, "Intl", intl) + JS_FreeValue(ctx, global) -- cgit 1.4.1-2-gfad0