quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

commit 2aa58df6d85a468fffa07e1a45eac3b2912774dd
parent 39ab09c04be56a71e0603dfef97d79d106369348
Author: Florian Dold <florian@dold.me>
Date:   Thu, 10 Nov 2022 14:31:14 +0100

helpers needed for Taler wallet

Diffstat:
MMakefile | 2+-
Aprelude.js | 23+++++++++++++++++++++++
Mquickjs-libc.c | 29+++++++++++++++++++++++++++++
Awallet-setup.js | 3+++
4 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -28,7 +28,7 @@ endif # Windows cross compilation from Linux #CONFIG_WIN32=y # use link time optimization (smaller and faster executables but slower build) -CONFIG_LTO=y +#CONFIG_LTO=y # consider warnings as errors (for development) #CONFIG_WERROR=y # force 32 bit build for some utilities diff --git a/prelude.js b/prelude.js @@ -0,0 +1,23 @@ +import * as os from "os"; + +class TextEncoder { + encode(str) { + return new Uint8Array(_encodeUtf8(str)); + } +} + +class TextDecoder { + decode(bytes) { + return _decodeUtf8(bytes.buffer); + } +} + +globalThis.TextEncoder = TextEncoder; +globalThis.TextDecoder = TextDecoder; +globalThis.setTimeout = (f, t) => os.setTimeout(f, t); +globalThis.setImmediate = (f) => os.setTimeout(f, 0); + +// FIXME: log to the right streams! +console.info = (...args) => { console.log(...args); }; +console.warn = (...args) => { console.log(...args); }; +console.error = (...args) => { console.log(...args); }; diff --git a/quickjs-libc.c b/quickjs-libc.c @@ -3734,6 +3734,30 @@ static JSValue js_print(JSContext *ctx, JSValueConst this_val, return JS_UNDEFINED; } +static JSValue js_encode_utf8(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + const char *str; + JSValue buf; + str = JS_ToCString(ctx, argv[0]); + // FIXME: Don't copy buffer but pass destructor function + buf = JS_NewArrayBufferCopy(ctx, (const uint8_t*) str, strlen(str)); + JS_FreeCString(ctx, str); + return buf; +} + +static JSValue js_decode_utf8(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + size_t psize; + uint8_t *utf8_buf; + utf8_buf = JS_GetArrayBuffer(ctx, &psize, argv[0]); + if (NULL == utf8_buf) { + return JS_EXCEPTION; + } + return JS_NewStringLen(ctx, (char *) utf8_buf, psize); +} + void js_std_add_helpers(JSContext *ctx, int argc, char **argv) { JSValue global_obj, console, args; @@ -3747,6 +3771,11 @@ void js_std_add_helpers(JSContext *ctx, int argc, char **argv) JS_NewCFunction(ctx, js_print, "log", 1)); JS_SetPropertyStr(ctx, global_obj, "console", console); + JS_SetPropertyStr(ctx, global_obj, "_encodeUtf8", + JS_NewCFunction(ctx, js_encode_utf8, "_encodeUtf8", 1)); + JS_SetPropertyStr(ctx, global_obj, "_decodeUtf8", + JS_NewCFunction(ctx, js_decode_utf8, "_decodeUtf8", 1)); + /* same methods as the mozilla JS shell */ if (argc >= 0) { args = JS_NewArray(ctx); diff --git a/wallet-setup.js b/wallet-setup.js @@ -0,0 +1,3 @@ +globalThis.__native_sendMessage = (x) => console.log(x); +installNativeWalletListener() +__native_onMessage(JSON.stringify({operation: "init", args: {}}))