/*
This file is part of GNU Taler
Copyright (C) 2014-2022 Taler Systems SA
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with
GNU Taler; see the file COPYING. If not, see
*/
#include "taler_wallet_core_lib.h"
#include "quickjs/quickjs.h"
#include "quickjs/quickjs-libc.h"
#include "tart_module.h"
#include
#include "quickjs/list.h"
#include
struct HostMessage {
struct list_head link;
char *data;
};
struct TALER_WALLET_Handle {
JSRuntime *rt;
JSContext *ctx;
TALER_WALLET_MessageHandlerFn *handler_f;
void *handler_cls;
int hostmsg_send_pipe;
int hostmsg_recv_pipe;
pthread_mutex_t hostmsg_mutex;
struct list_head hostmg_queue; /* list of HostMessage.link */
};
/* also used to initialize the worker context */
static JSContext *JS_NewCustomContext(JSRuntime *rt)
{
JSContext *ctx;
ctx = JS_NewContext(rt);
if (!ctx)
return NULL;
/* system modules */
js_init_module_std(ctx, "std");
js_init_module_os(ctx, "os");
/* taler runtime */
tart_init_module_talercrypto(ctx, "tart");
return ctx;
}
void
TALER_WALLET_set_handler(struct TALER_WALLET_Handle *h,
TALER_WALLET_MessageHandlerFn handler_f,
void *handler_p)
{
}
int
TALER_WALLET_send_message (struct TALER_WALLET_Handle *h,
const char *msg)
{
return js_os_post_message_from_host(h->ctx, msg);
}
struct TALER_WALLET_Handle *
TALER_WALLET_create(void)
{
struct TALER_WALLET_Handle *wh;
JSRuntime *rt;
JSContext *ctx;
rt = JS_NewRuntime();
js_std_init_handlers(rt);
ctx = JS_NewCustomContext(rt);
if (!ctx) {
fprintf(stderr, "qjs: cannot allocate JS context\n");
return NULL;
}
JS_SetHostPromiseRejectionTracker(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;
}
return wh;
}
static void *
run(void *cls)
{
struct TALER_WALLET_Handle *wh = cls;
js_std_loop(wh->ctx);
}
void
TALER_WALLET_run (struct TALER_WALLET_Handle *wh)
{
pthread_t wallet_thread;
pthread_create(&wallet_thread, NULL, run, wh);
}