summaryrefslogtreecommitdiff
path: root/taler_wallet_core_lib.c
blob: d66a14687c97deab0aed9348dae9dddfa43235a7 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
   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>
#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;
    char *data;
};

struct TALER_WALLET_Handle {
    char *test;

  JSRuntime *rt;
  JSContext *ctx;

  pthread_t wallet_thread;

  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)
{
    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_cls)
{
  h->handler_cls = handler_cls;
  h->handler_f = handler_f;
}

int
TALER_WALLET_send_message (struct TALER_WALLET_Handle *h,
                           const char *msg)
{
  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;
    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;

    printf("TEST: %s\n", wh->test);

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

    eval_buf(wh->ctx, "console.log('hi');", "<talerwallet>");

    JS_SetHostPromiseRejectionTracker(wh->rt, js_std_promise_rejection_tracker,
                                          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);

    js_os_set_host_message_handler(wh->ctx, wallet_host_message_handler, wh);

    printf("starting main loop\n");

    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;
    char *line;
    size_t line_sz;

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