summaryrefslogtreecommitdiff
path: root/taler_wallet_core_lib.c
blob: cd1f0e3781c4adbc461f65fed4f7e2ce89d42633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
   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 <http://www.gnu.org/licenses/>
 */

#include "taler_wallet_core_lib.h"

#include "quickjs/quickjs.h"
#include "quickjs/quickjs-libc.h"
#include "tart_module.h"
#include <pthread.h>
#include "quickjs/list.h"
#include <unistd.h>

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);
}