summaryrefslogtreecommitdiff
path: root/source/luametatex/source/luaoptional/lmtmujs.c
blob: e1abfdc0ba94bc24288dc6d84be59347796fdcb3 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
    See license.txt in the root of this project.
*/

/*tex
    The \CLANGUAGE\ interface looks quite a bit like the \LUA\ interface. This module will only
    provide print-to-tex functions and no other interfacing. It really makes no sense to provide
    more. An interesting lightweight interface could map onto \LUA\ calls but there is no gain
    either. Consider it an experiment that might attract kids to \TEX, just because \JAVASCRIPT\
    looks familiar. We have only one instance. Of course we could use some userdata object but I
    don't think it is worth the effort and one would like to be persistent across calls. (We used
    to have multiple \LUA\ instances which made no sense either.)

    url: https://mujs.com/index.html

    Keep in mind: we don't have a \JAVASCRIPT\ interoreter embedded because this is just a small
    minimal interface to code that {\em can} be loaded at runtime, if present at all.

*/

# include "luametatex.h"
# include "lmtoptional.h"

typedef struct js_State js_State;

typedef void (*js_CFunction) (js_State *J);
typedef void (*js_Report)    (js_State *J, const char *message);
typedef void (*js_Finalize)  (js_State *J, void *p);

typedef enum js_states {
	JS_STRICT = 1,
} js_states;

typedef enum js_properties {
	JS_READONLY = 1,
	JS_DONTENUM = 2,
	JS_DONTCONF = 4,
} js_properties;

/*tex A couple of status variables: */

typedef struct mujslib_state_info {

    js_State *instance;
    int       initialized;
    int       find_file_id;
    int       open_file_id;
    int       close_file_id;
    int       read_file_id;
    int       seek_file_id;
    int       console_id;
    int       padding;

    struct js_State * (*js_newstate) (
        void  *alloc,
        void  *actx,
        int    flags
    );

    void (*js_freestate) (
        js_State *J
    );

    void (*js_setreport) (
        js_State  *J,
        js_Report  report
    );

    int (*js_dostring) (
        js_State   *J,
        const char *source
    );

    void (*js_newcfunction) (
        js_State     *J,
        js_CFunction  fun,
        const char   *name,
        int           length
    );

    void (*js_newuserdata) (
        js_State    *J,
        const char  *tag,
        void        *data,
        js_Finalize finalize
    );

    void (*js_newcconstructor) (
        js_State     *J,
        js_CFunction  fun,
        js_CFunction  con,
        const char   *name,
        int           length
    );

    int (*js_dofile) (
        js_State   *J,
        const char *filename
    );

    void (*js_currentfunction) (
        js_State *J
    );

    void         (*js_getglobal)     (js_State *J, const char *name                   );
    void         (*js_setglobal)     (js_State *J, const char *name                   );
    void         (*js_defglobal)     (js_State *J, const char *name, int atts         );

    void         (*js_getproperty)   (js_State *J, int idx, const char *name          );
    void         (*js_setproperty)   (js_State *J, int idx, const char *name          );
    void         (*js_defproperty)   (js_State *J, int idx, const char *name, int atts);

    void         (*js_pushundefined) (js_State *J                                     );
    void         (*js_pushnull)      (js_State *J                                     );
    void         (*js_pushnumber)    (js_State *J, double v                           );
    void         (*js_pushstring)    (js_State *J, const char *v                      );

    const char * (*js_tostring)      (js_State *J, int idx                            );
    int          (*js_tointeger)     (js_State *J, int idx                            );
    void       * (*js_touserdata)    (js_State *J, int idx, const char *tag           );

    int          (*js_isnumber)      (js_State *J, int idx                            );
    int          (*js_isstring)      (js_State *J, int idx                            );
    int          (*js_isundefined)   (js_State *J, int idx                            );

} mujslib_state_info;

static mujslib_state_info mujslib_state = {

    .initialized         = 0,
    .instance            = NULL,
    .find_file_id        = 0,
    .open_file_id        = 0,
    .close_file_id       = 0,
    .read_file_id        = 0,
    .seek_file_id        = 0,
    .console_id          = 0,
    .padding             = 0,

    .js_newstate         = NULL,
    .js_freestate        = NULL,
    .js_setreport        = NULL,
    .js_dostring         = NULL,
    .js_newcfunction     = NULL,
    .js_newuserdata      = NULL,
    .js_newcconstructor  = NULL,
    .js_dofile           = NULL,
    .js_currentfunction  = NULL,

    .js_getglobal        = NULL,
    .js_setglobal        = NULL,
    .js_defglobal        = NULL,

    .js_getproperty      = NULL,
    .js_setproperty      = NULL,
    .js_defproperty      = NULL,

    .js_pushundefined    = NULL,
    .js_pushnull         = NULL,
    .js_pushnumber       = NULL,
    .js_pushstring       = NULL,

    .js_tostring         = NULL,
    .js_tointeger        = NULL,
    .js_touserdata       = NULL,

    .js_isnumber         = NULL,
    .js_isstring         = NULL,
    .js_isundefined      = NULL,

};

/*tex A few callbacks: */

static int mujslib_register_function(lua_State * L, int old_id)
{
    if (! (lua_isfunction(L, -1) || lua_isnil(L, -1))) {
        return 0;
    } else {
        lua_pushvalue(L, -1);
        if (old_id) {
            luaL_unref(L, LUA_REGISTRYINDEX, old_id);
        }
        return luaL_ref(L, LUA_REGISTRYINDEX);
    }
}

static int mujslib_set_find_file(lua_State *L)
{
    mujslib_state.find_file_id = mujslib_register_function(L, mujslib_state.find_file_id);
    return 0;
}

static int mujslib_set_open_file(lua_State *L)
{
    mujslib_state.open_file_id = mujslib_register_function(L, mujslib_state.open_file_id);
    return 0;
}

static int mujslib_set_close_file(lua_State *L)
{
    mujslib_state.close_file_id = mujslib_register_function(L, mujslib_state.close_file_id);
    return 0;
}

static int mujslib_set_read_file(lua_State *L)
{
    mujslib_state.read_file_id = mujslib_register_function(L, mujslib_state.read_file_id);
    return 0;
}

static int mujslib_set_seek_file(lua_State *L)
{
    mujslib_state.seek_file_id = mujslib_register_function(L, mujslib_state.seek_file_id);
    return 0;
}

static int mujslib_set_console(lua_State *L)
{
    mujslib_state.console_id = mujslib_register_function(L, mujslib_state.console_id);
    return 0;
}

static char *mujslib_find_file(const char *fname, const char *fmode)
{
    if (mujslib_state.find_file_id) {
        lua_State *L = lmt_lua_state.lua_instance; /* todo: pass */
        lua_rawgeti(L, LUA_REGISTRYINDEX, mujslib_state.find_file_id);
        lua_pushstring(L, fname);
        lua_pushstring(L, fmode);
        if (lua_pcall(L, 2, 1, 0)) {
            tex_formatted_warning("mujs", "find file: %s\n", lua_tostring(L, -1));
        } else {
            char *s = NULL;
            const char *x = lua_tostring(L, -1);
            if (x) {
                s = strdup(x);
            }
            lua_pop(L, 1);
            return s;
        }
    } else {
        tex_normal_warning("mujs", "missing callback: find file");
    }
    return NULL;
}

/*tex A few helpers: */

static void mujslib_aux_texcprint(js_State *J, int ispartial)
{
    int c = default_catcode_table_preset;
    int i = 0;
    if (mujslib_state.js_isnumber(J, 1)) {
        if (mujslib_state.js_isnumber(J, 2) || mujslib_state.js_isstring(J, 2)) {
            c = mujslib_state.js_tointeger(J, 1);
            i = 2;
        } else {
            i = 1;
        }
    } else if (mujslib_state.js_isstring(J, 1)) {
        i = 1;
    }
    if (i) {
        const char *s = mujslib_state.js_tostring(J, i);
        if (s) {
            lmt_cstring_print(c, s, ispartial);
        }
    } else {
        tex_normal_warning("mujs", "invalid argument(s) for printing to tex");
    }
	mujslib_state.js_pushundefined(J); /* needed ? */
}

static void mujslib_aux_texprint(js_State *J)
{
    mujslib_aux_texcprint(J, 0); /* full line */
}

static void mujslib_aux_texsprint(js_State *J)
{
    mujslib_aux_texcprint(J, 1); /* partial line */
}

static void mujslib_aux_feedback(js_State *J, const char *category, const char *message)
{
    if (message) {
        if (mujslib_state.console_id) {
            lua_State *L = lmt_lua_state.lua_instance;
            lua_rawgeti(L, LUA_REGISTRYINDEX, mujslib_state.console_id);
            lua_pushstring(L, category);
            lua_pushstring(L, message);
            if (lua_pcall(L, 2, 0, 0)) {
                tex_formatted_warning("mujs", "console: %s\n", lua_tostring(L, -1));
            }
        } else {
            tex_print_message(message);
        }
    }
	mujslib_state.js_pushundefined(J);
}

static void mujslib_aux_console(js_State *J)
{
    mujslib_aux_feedback(J, "console", mujslib_state.js_tostring(J, 1));
}

static void mujslib_aux_report(js_State *J, const char *s)
{
    mujslib_aux_feedback(J, "report", s);
}

/*tex
    The interfaces: for loading files a finder callback is mandate so that
    we keep control over what gets read from where.
*/

static int mujslib_execute(lua_State *L)
{
    if (mujslib_state.instance) {
	    const char *s = lua_tostring(L, 1);
        if (s) {
           mujslib_state.js_dostring(mujslib_state.instance, s);
        }
    }
    return 0;
}

static int mujslib_dofile(lua_State *L)
{
    if (mujslib_state.instance) {
	    const char *name = lua_tostring(L, 1);
        if (name) {
            char *found = mujslib_find_file(name, "rb");
            if (found) {
               mujslib_state.js_dofile(mujslib_state.instance, found);
            }
            free(found);
        }
    } else {
        tex_normal_warning("mujs", "missing callback: find file");
    }
    return 0;
}

static void mujslib_start(void)
{
    if (mujslib_state.instance) {
        mujslib_state.js_freestate(mujslib_state.instance);
    }
    mujslib_state.instance = mujslib_state.js_newstate(NULL, NULL, JS_STRICT);
    if (mujslib_state.instance) {
        mujslib_state.js_newcfunction(mujslib_state.instance, mujslib_aux_texprint, "texprint", 2);
        mujslib_state.js_setglobal   (mujslib_state.instance, "texprint");
        mujslib_state.js_newcfunction(mujslib_state.instance, mujslib_aux_texsprint, "texsprint", 2);
        mujslib_state.js_setglobal   (mujslib_state.instance, "texsprint");
        mujslib_state.js_newcfunction(mujslib_state.instance, mujslib_aux_console, "console", 1);
        mujslib_state.js_setglobal   (mujslib_state.instance, "console");
        mujslib_state.js_setreport   (mujslib_state.instance, mujslib_aux_report);
    }
}

static int mujslib_reset(lua_State *L)
{
    if (mujslib_state.initialized) {
        mujslib_start();
    }
    lua_pushboolean(L, mujslib_state.initialized && mujslib_state.instance);
    return 1;
}

/*tex
    File handling: we go via the \LUA\ interface so that we have control
    over what happens. Another benefit is that we don't need memory
    management when fetching data from files.
*/

static void mujslib_file_finalize(js_State *J, void *p)
{
    int *id = p;
    (void) J;
    if (*id) {
        lua_State *L = lmt_lua_state.lua_instance;
        int top = lua_gettop(L);
        lua_rawgeti(L, LUA_REGISTRYINDEX, mujslib_state.close_file_id);
        lua_pushinteger(L, *id);
        if (lua_pcall(L, 1, 0, 0)) {
            tex_formatted_warning("mujs", "close file: %s\n", lua_tostring(L, -1));
        }
        lua_settop(L,top);
    }
}

static void mujslib_file_close(js_State *J)
{
    if (mujslib_state.instance) {
        if (mujslib_state.close_file_id) {
    	    int *id = mujslib_state.js_touserdata(J, 0, "File");
            if (*id) {
                mujslib_file_finalize(J, id);
            }
        } else {
            tex_normal_warning("mujs", "missing callback: close file");
        }
    }
	mujslib_state.js_pushundefined(J);
}

static void mujslib_file_read(js_State *J)
{
    if (mujslib_state.instance) {
        if (mujslib_state.read_file_id) {
            int *id = mujslib_state.js_touserdata(J, 0, "File");
            if (*id) {
                lua_State *L = lmt_lua_state.lua_instance;
                int top = lua_gettop(L);
                int n = 1;
                lua_rawgeti(L, LUA_REGISTRYINDEX, mujslib_state.read_file_id);
                lua_pushinteger(L, *id);
                if (mujslib_state.js_isstring(J, 1)) {
                    const char *how = mujslib_state.js_tostring(J, 1);
                    if (how) {
                        lua_pushstring(L, how);
                        n = 2;
                    }
                } else if (mujslib_state.js_isnumber(J, 1)) {
                    int how = mujslib_state.js_tointeger(J, 1);
                    if (how) {
                        lua_pushinteger(L, how);
                        n = 2;
                    }
                }
                if (lua_pcall(L, n, 1, 0)) {
                    tex_formatted_warning("mujs", "close file: %s\n", lua_tostring(L, -1));
                } else {
                    const char *result = strdup(lua_tostring(L, -1));
                    if (result) {
            	        mujslib_state.js_pushstring(J, result);
                        lua_settop(L, top);
                        return;
                    }
                }
                lua_settop(L, top);
            }
        } else {
            tex_normal_warning("mujs", "missing callback: read file");
        }
    }
	mujslib_state.js_pushundefined(J);
}

static void mujslib_file_seek(js_State *J)
{
    if (mujslib_state.instance) {
        if (mujslib_state.seek_file_id) {
            int *id = mujslib_state.js_touserdata(J, 0, "File");
            if (*id) {
                lua_State *L = lmt_lua_state.lua_instance;
                int top = lua_gettop(L);
                int n = 2;
                lua_rawgeti(L, LUA_REGISTRYINDEX, mujslib_state.seek_file_id);
                lua_pushinteger(L, *id);
                /* no checking here */
                lua_pushstring(L, mujslib_state.js_tostring(J, 1));
                if (mujslib_state.js_isnumber(J, 2)) {
                    lua_pushinteger(L, mujslib_state.js_tointeger(J, 2));
                    n = 3;
                }
                if (lua_pcall(L, n, 1, 0)) {
                    tex_formatted_warning("mujs", "seek file: %s\n", lua_tostring(L, -1));
                } else if (lua_type(L, -1) == LUA_TNUMBER) {
         	        mujslib_state.js_pushnumber(J, lua_tonumber(L, -1));
                    lua_settop(L, top);
                    return;
                }
                lua_settop(L, top);
            }
        } else {
            tex_normal_warning("mujs", "missing callback: seek file");
        }
    }
	mujslib_state.js_pushundefined(J);
}

static void mujslib_file_new(js_State *J)
{
    if (mujslib_state.instance) {
        if (mujslib_state.open_file_id) {
    	    const char *name = mujslib_state.js_tostring(J, 1);
            if (name) {
                lua_State *L = lmt_lua_state.lua_instance;
                int top = lua_gettop(L);
                lua_rawgeti(L, LUA_REGISTRYINDEX, mujslib_state.open_file_id);
                lua_pushstring(L, name);
                if (lua_pcall(L, 1, 1, 0)) {
                    tex_formatted_warning("mujs", "open file: %s\n", lua_tostring(L, -1));
                } else {
                    int *id = malloc(sizeof(int));
                    if (id) {
                        *((int*) id) = (int) lua_tointeger(L, -1);
                        lua_settop(L, top);
                        if (id) {
	                        mujslib_state.js_currentfunction(J);
	                        mujslib_state.js_getproperty(J, -1, "prototype");
	                        mujslib_state.js_newuserdata(J, "File", id, mujslib_file_finalize);
                            return;
                        }
                    }
                }
                lua_settop(L, top);
            }
        } else {
            tex_normal_warning("mujs", "missing callback: open file");
        }
    }
 	mujslib_state.js_pushnull(J);
}

/* Setting things up. */

static void mujslib_file_initialize(js_State *J)
{
	mujslib_state.js_getglobal(J, "Object");
	mujslib_state.js_getproperty(J, -1, "prototype");
	mujslib_state.js_newuserdata(J, "File", stdin, NULL);
	{
		mujslib_state.js_newcfunction(J, mujslib_file_read, "File.prototype.read", 0);
		mujslib_state.js_defproperty(J, -2, "read", JS_DONTENUM);
		mujslib_state.js_newcfunction(J, mujslib_file_seek, "File.prototype.seek", 0);
		mujslib_state.js_defproperty(J, -2, "seek", JS_DONTENUM);
		mujslib_state.js_newcfunction(J, mujslib_file_close, "File.prototype.close", 0);
		mujslib_state.js_defproperty(J, -2, "close", JS_DONTENUM);
	}
	mujslib_state.js_newcconstructor(J, mujslib_file_new, mujslib_file_new, "File", 1);
	mujslib_state.js_defglobal(J, "File", JS_DONTENUM);
}

static int mujslib_initialize(lua_State *L)
{
    if (! mujslib_state.initialized) {
        const char *filename = lua_tostring(L, 1);
        if (filename) {

            lmt_library lib = lmt_library_load(filename);

            mujslib_state.js_newstate        = lmt_library_find(lib, "js_newstate");
            mujslib_state.js_freestate       = lmt_library_find(lib, "js_freestate");
            mujslib_state.js_setreport       = lmt_library_find(lib, "js_setreport");

            mujslib_state.js_newcfunction    = lmt_library_find(lib, "js_newcfunction");
            mujslib_state.js_newuserdata     = lmt_library_find(lib, "js_newuserdata");
            mujslib_state.js_newcconstructor = lmt_library_find(lib, "js_newcconstructor");

            mujslib_state.js_pushundefined   = lmt_library_find(lib, "js_pushundefined");
            mujslib_state.js_pushnull        = lmt_library_find(lib, "js_pushnull");
            mujslib_state.js_pushnumber      = lmt_library_find(lib, "js_pushnumber");
            mujslib_state.js_pushstring      = lmt_library_find(lib, "js_pushstring");

            mujslib_state.js_dostring        = lmt_library_find(lib, "js_dostring");
            mujslib_state.js_dofile          = lmt_library_find(lib, "js_dofile");

            mujslib_state.js_tostring        = lmt_library_find(lib, "js_tostring");
            mujslib_state.js_tointeger       = lmt_library_find(lib, "js_tointeger");
            mujslib_state.js_touserdata      = lmt_library_find(lib, "js_touserdata");

            mujslib_state.js_getglobal       = lmt_library_find(lib, "js_getglobal");
            mujslib_state.js_setglobal       = lmt_library_find(lib, "js_setglobal");
            mujslib_state.js_defglobal       = lmt_library_find(lib, "js_defglobal");

            mujslib_state.js_getproperty     = lmt_library_find(lib, "js_getproperty");
            mujslib_state.js_setproperty     = lmt_library_find(lib, "js_setproperty");
            mujslib_state.js_defproperty     = lmt_library_find(lib, "js_defproperty");

            mujslib_state.js_isstring        = lmt_library_find(lib, "js_isstring");
            mujslib_state.js_isnumber        = lmt_library_find(lib, "js_isnumber");
            mujslib_state.js_isundefined     = lmt_library_find(lib, "js_isundefined");

            mujslib_state.js_currentfunction = lmt_library_find(lib, "js_currentfunction");

            mujslib_state.initialized = lmt_library_okay(lib);

            mujslib_start();

            mujslib_file_initialize(mujslib_state.instance);
        }
    }
    lua_pushboolean(L, mujslib_state.initialized && mujslib_state.instance);
    return 1;
}

static struct luaL_Reg mujslib_function_list[] = {
    { "initialize",   mujslib_initialize     }, /* mandate */
    { "reset",        mujslib_reset          },
    { "execute",      mujslib_execute        },
    { "dofile",       mujslib_dofile         },
    { "setfindfile",  mujslib_set_find_file  }, /* mandate */
    { "setopenfile",  mujslib_set_open_file  }, /* mandate */
    { "setclosefile", mujslib_set_close_file }, /* mandate */
    { "setreadfile",  mujslib_set_read_file  }, /* mandate */
    { "setseekfile",  mujslib_set_seek_file  },
    { "setconsole",   mujslib_set_console    },
    { NULL,           NULL                   },
};

int luaopen_mujs(lua_State *L)
{
    lmt_library_register(L, "mujs", mujslib_function_list);
    return 0;
}