summaryrefslogtreecommitdiff
path: root/taler_wallet_core_lib.c
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2023-01-02 22:51:14 +0100
committerFlorian Dold <florian@dold.me>2023-01-02 22:51:14 +0100
commita9d2a10f9dfc2cacf17e574928e6b851e084beef (patch)
treea933ed371b4647fcb1ba81d3be5d12d06b0a1d83 /taler_wallet_core_lib.c
parent6dba524755c9b5a3dccc4372cf74cf1ed6d46586 (diff)
downloadquickjs-tart-a9d2a10f9dfc2cacf17e574928e6b851e084beef.tar.gz
quickjs-tart-a9d2a10f9dfc2cacf17e574928e6b851e084beef.tar.bz2
quickjs-tart-a9d2a10f9dfc2cacf17e574928e6b851e084beef.zip
wip
Diffstat (limited to 'taler_wallet_core_lib.c')
-rw-r--r--taler_wallet_core_lib.c120
1 files changed, 88 insertions, 32 deletions
diff --git a/taler_wallet_core_lib.c b/taler_wallet_core_lib.c
index cd1f0e3..d66a146 100644
--- a/taler_wallet_core_lib.c
+++ b/taler_wallet_core_lib.c
@@ -22,6 +22,13 @@
#include <pthread.h>
#include "quickjs/list.h"
#include <unistd.h>
+#include <string.h>
+
+extern const uint8_t qjsc_prelude[];
+extern const uint32_t qjsc_prelude_size;
+
+extern const uint8_t qjsc_wallet_core[];
+extern const uint32_t qjsc_wallet_core_size;
struct HostMessage {
struct list_head link;
@@ -29,18 +36,32 @@ struct HostMessage {
};
struct TALER_WALLET_Handle {
+ char *test;
+
JSRuntime *rt;
JSContext *ctx;
- TALER_WALLET_MessageHandlerFn *handler_f;
- void *handler_cls;
+ pthread_t wallet_thread;
- int hostmsg_send_pipe;
- int hostmsg_recv_pipe;
- pthread_mutex_t hostmsg_mutex;
- struct list_head hostmg_queue; /* list of HostMessage.link */
+ TALER_WALLET_MessageHandlerFn handler_f;
+ void *handler_cls;
};
+static int eval_buf(JSContext *ctx, const char *codestr, const char *filename)
+{
+ JSValue val;
+ int ret;
+ val = JS_Eval(ctx, codestr, strlen(codestr), filename, 0);
+ if (JS_IsException(val)) {
+ js_std_dump_error(ctx);
+ ret = -1;
+ } else {
+ ret = 0;
+ }
+ JS_FreeValue(ctx, val);
+ return ret;
+}
+
/* also used to initialize the worker context */
static JSContext *JS_NewCustomContext(JSRuntime *rt)
{
@@ -59,9 +80,10 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
void
TALER_WALLET_set_handler(struct TALER_WALLET_Handle *h,
TALER_WALLET_MessageHandlerFn handler_f,
- void *handler_p)
+ void *handler_cls)
{
-
+ h->handler_cls = handler_cls;
+ h->handler_f = handler_f;
}
int
@@ -71,50 +93,84 @@ TALER_WALLET_send_message (struct TALER_WALLET_Handle *h,
return js_os_post_message_from_host(h->ctx, msg);
}
+static void
+wallet_host_message_handler(void *cls, const char *msg)
+{
+ struct TALER_WALLET_Handle *wh = cls;
+
+ if (wh->handler_f) {
+ wh->handler_f(wh->handler_cls, msg);
+ }
+}
+
struct TALER_WALLET_Handle *
TALER_WALLET_create(void)
{
struct TALER_WALLET_Handle *wh;
- JSRuntime *rt;
- JSContext *ctx;
+ wh = malloc(sizeof (*wh));
+ memset(wh, 0, sizeof *wh);
+ wh->test = "foo";
+
+ wh->rt = JS_NewRuntime();
+
+ return wh;
+}
+
+static void *
+run(void *cls)
+{
+ struct TALER_WALLET_Handle *wh = cls;
- rt = JS_NewRuntime();
- js_std_init_handlers(rt);
- ctx = JS_NewCustomContext(rt);
+ printf("TEST: %s\n", wh->test);
- if (!ctx) {
+ js_std_init_handlers(wh->rt);
+ wh->ctx = JS_NewCustomContext(wh->rt);
+
+
+ if (!wh->ctx) {
fprintf(stderr, "qjs: cannot allocate JS context\n");
return NULL;
}
- JS_SetHostPromiseRejectionTracker(rt, js_std_promise_rejection_tracker,
+ eval_buf(wh->ctx, "console.log('hi');", "<talerwallet>");
+
+ JS_SetHostPromiseRejectionTracker(wh->rt, js_std_promise_rejection_tracker,
NULL);
- wh = malloc(sizeof (*wh));
- wh->ctx = ctx;
- wh->rt = rt;
- wh->handler_cls = NULL;
- wh->handler_f = NULL;
- if (0 != pthread_mutex_init(&wh->hostmsg_mutex, NULL)) {
- return NULL;
- }
+ js_std_add_helpers(wh->ctx, 0, NULL);
+ js_std_eval_binary(wh->ctx, qjsc_prelude, qjsc_prelude_size, 0);
+ js_std_eval_binary(wh->ctx, qjsc_wallet_core, qjsc_wallet_core_size, 0);
- return wh;
-}
+ js_os_set_host_message_handler(wh->ctx, wallet_host_message_handler, wh);
-static void *
-run(void *cls)
-{
- struct TALER_WALLET_Handle *wh = cls;
+ printf("starting main loop\n");
- js_std_loop(wh->ctx);
+ eval_buf(wh->ctx, "console.log('hi');", "<talerwallet>");
+ eval_buf(wh->ctx, "console.log(typeof testWithLocal);", "<talerwallet>");
+ eval_buf(wh->ctx, "console.log('hello, world, bla!');", "<talerwallet>");
+ eval_buf(wh->ctx, "testWithLocal();", "<talerwallet>");
+
+ js_std_loop(wh->ctx);
+
+ printf("done with main loop\n");
}
void
TALER_WALLET_run (struct TALER_WALLET_Handle *wh)
{
- pthread_t wallet_thread;
+ pthread_t wallet_thread;
+ char *line;
+ size_t line_sz;
- pthread_create(&wallet_thread, NULL, run, wh);
+ //run(wh);
+
+ pthread_create(&wallet_thread, NULL, run, wh);
+
+ wh->wallet_thread = wallet_thread;
+}
+
+void TALER_WALLET_join(struct TALER_WALLET_Handle *wh)
+{
+ pthread_join(wh->wallet_thread, NULL);
}