Debian Codegraph @

texk/web2c/luatexdir/lua/luastuff.c

raw
1 /*
2
3 luastuff.w
4
5 Copyright 2006-2013 Taco Hoekwater <taco@@luatex.org>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 #include "ptexlib.h"
25 #include "lua/luatex-api.h"
26 #ifdef LuajitTeX
27 #include "lua/lauxlib_bridge.h"
28 #endif
29
30 lua_State *Luas = NULL;
31
32 int luastate_bytes = 0;
33 int lua_active = 0;
34
35 #ifdef LuajitTeX
36 #define Luas_load(Luas,getS,ls,lua_id) \
37 lua_load(Luas,getS,ls,lua_id);
38 #define Luas_open(name,luaopen_lib) \
39 lua_pushcfunction(L, luaopen_lib); \
40 lua_pushstring(L, name); \
41 lua_call(L, 1, 0);
42 #else
43 #define Luas_load(Luas,getS,ls,lua_id) \
44 lua_load(Luas,getS,ls,lua_id,NULL);
45 #define Luas_open(name,luaopen_lib) \
46 luaL_requiref(L, name, luaopen_lib, 1); \
47 lua_pop(L, 1);
48 #endif
49
50 void make_table(lua_State * L, const char *tab, const char *mttab, const char *getfunc, const char *setfunc)
51 {
52 /*tex make the table *//* |[{<tex>}]| */
53 /*tex |[{<tex>},"dimen"]| */
54 lua_pushstring(L, tab);
55 /*tex |[{<tex>},"dimen",{}]| */
56 lua_newtable(L);
57 /*tex |[{<tex>}]| */
58 lua_settable(L, -3);
59 /*tex fetch it back */
60 /*tex |[{<tex>},"dimen"]| */
61 lua_pushstring(L, tab);
62 /*tex |[{<tex>},{<dimen>}]| */
63 lua_gettable(L, -2);
64 /*tex make the meta entries */
65 /*tex |[{<tex>},{<dimen>},{<dimen_m>}]| */
66 luaL_newmetatable(L, mttab);
67 /*tex |[{<tex>},{<dimen>},{<dimen_m>},"__index"]| */
68 lua_pushstring(L, "__index");
69 /*tex |[{<tex>},{<dimen>},{<dimen_m>},"__index","getdimen"]| */
70 lua_pushstring(L, getfunc);
71 /*tex |[{<tex>},{<dimen>},{<dimen_m>},"__index",<tex.getdimen>]| */
72 lua_gettable(L, -5);
73 /*tex |[{<tex>},{<dimen>},{<dimen_m>}]| */
74 lua_settable(L, -3);
75 lua_pushstring(L, "__newindex"); /*tex |[{<tex>},{<dimen>},{<dimen_m>},"__newindex"]| */
76 /*tex |[{<tex>},{<dimen>},{<dimen_m>},"__newindex","setdimen"]| */
77 lua_pushstring(L, setfunc);
78 /*tex |[{<tex>},{<dimen>},{<dimen_m>},"__newindex",<tex.setdimen>]| */
79 lua_gettable(L, -5);
80 /*tex |[{<tex>},{<dimen>},{<dimen_m>}]| */
81 lua_settable(L, -3);
82 /*tex |[{<tex>},{<dimen>}]| : assign the metatable */
83 lua_setmetatable(L, -2);
84 /*tex |[{<tex>}]| : clean the stack */
85 lua_pop(L, 1);
86 }
87
88 static const char *getS(lua_State * L, void *ud, size_t * size)
89 {
90 LoadS *ls = (LoadS *) ud;
91 (void) L;
92 if (ls->size == 0)
93 return NULL;
94 *size = ls->size;
95 ls->size = 0;
96 return ls->s;
97 }
98
99 #ifdef LuajitTeX
100 /*
101 \LUATEX\ has its own memory allocator, \LUAJIITEX\ uses the standard one
102 from the stock. We left this space as reference, but be careful: memory
103 allocator is a key component in \LUAJIT, it's easy to get sub-optimal
104 performances.
105 */
106 #else
107 static void *my_luaalloc(void *ud, void *ptr, size_t osize, size_t nsize)
108 {
109 void *ret = NULL;
110 /*tex define |ud| for -Wunused */
111 (void) ud;
112 if (nsize == 0)
113 free(ptr);
114 else
115 ret = realloc(ptr, nsize);
116 luastate_bytes += (int) (nsize - osize);
117 return ret;
118 }
119 #endif
120
121 static int my_luapanic(lua_State * L)
122 {
123 /*tex define |L| to avoid warnings */
124 (void) L;
125 fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", lua_tostring(L, -1));
126 return 0;
127 }
128
129 void luafunctioncall(int slot)
130 {
131 int i ;
132 int stacktop = lua_gettop(Luas);
133 lua_active++;
134 lua_rawgeti(Luas, LUA_REGISTRYINDEX, lua_key_index(lua_functions));
135 lua_gettable(Luas, LUA_REGISTRYINDEX);
136 lua_rawgeti(Luas, -1,slot);
137 if (lua_isfunction(Luas,-1)) {
138 /*tex function index */
139 int base = lua_gettop(Luas);
140 lua_pushinteger(Luas, slot);
141 /* push traceback function */
142 lua_pushcfunction(Luas, lua_traceback);
143 /*tex put it under chunk */
144 lua_insert(Luas, base);
145 ++function_callback_count;
146 i = lua_pcall(Luas, 1, 0, base);
147 /*tex remove traceback function */
148 lua_remove(Luas, base);
149 if (i != 0) {
150 lua_gc(Luas, LUA_GCCOLLECT, 0);
151 Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
152 }
153 }
154 lua_settop(Luas,stacktop);
155 lua_active--;
156 }
157
158 static const luaL_Reg lualibs[] = {
159 /*tex standard \LUA\ libraries */
160 { "_G", luaopen_base },
161 { "package", luaopen_package },
162 { "table", luaopen_table },
163 { "io", luaopen_io },
164 { "os", luaopen_os },
165 { "string", luaopen_string },
166 { "math", luaopen_math },
167 { "debug", luaopen_debug },
168 { "lpeg", luaopen_lpeg },
169 { "bit32", luaopen_bit32 },
170 #ifdef LuajitTeX
171 /*tex |bit| is only in \LUAJIT */
172 /*tex |coroutine| is loaded in a special way */
173 { "bit", luaopen_bit },
174 #else
175 #if LUA_VERSION_NUM == 503
176 { "utf8", luaopen_utf8 },
177 #endif
178 { "coroutine", luaopen_coroutine },
179 #endif
180 /*tex additional (public) libraries */
181 { "unicode", luaopen_unicode },
182 { "zip", luaopen_zip },
183 { "md5", luaopen_md5 },
184 { "sha2", luaopen_sha2 },
185 { "lfs", luaopen_lfs },
186 /*tex extra standard lua libraries */
187 #ifdef LuajitTeX
188 { "jit", luaopen_jit },
189 #endif
190 { "ffi", luaopen_ffi },
191 /*tex more libraries will be loaded later */
192 #ifdef LUATEX_HARFBUZZ_ENABLED
193 { "luaharfbuzz", luaopen_luaharfbuzz },
194 { "luaharfbuzzsubset", luaopen_luaharfbuzzsubset },
195 #endif
196 { NULL, NULL }
197 };
198
199 static void do_openlibs(lua_State * L)
200 {
201 const luaL_Reg *lib = lualibs;
202 for (; lib->func; lib++) {
203 Luas_open(lib->name,lib->func);
204 }
205 }
206
207 #ifdef LuajitTeX
208 /*tex in \LUAJIT\ |load_aux| is not used.*/
209 #else
210 static int load_aux (lua_State *L, int status) {
211 if (status == 0)
212 /*tex okay */
213 return 1;
214 else {
215 /*tex return nil plus error message */
216 lua_pushnil(L);
217 /*tex put before error message */
218 lua_insert(L, -2);
219 return 2;
220 }
221 }
222 #endif
223
224 static int luatex_loadfile (lua_State *L) {
225 int status = 0;
226 const char *fname = luaL_optstring(L, 1, NULL);
227 const char *mode = luaL_optstring(L, 2, NULL);
228 #ifdef LuajitTeX
229 /* 5.1 */
230 #else
231 /*tex the |env| parameter */
232 int env = !lua_isnone(L, 3);
233 #endif
234 if (!lua_only && !fname && interaction == batch_mode) {
235 /*tex return |nil| plus error message */
236 lua_pushnil(L);
237 lua_pushstring(L, "reading from stdin is disabled in batch mode");
238 return 2;
239 }
240 status = luaL_loadfilex(L, fname, mode);
241 if (status == LUA_OK) {
242 recorder_record_input(fname);
243 #ifdef LuajitTeX
244 /* 5.1 */
245 #else
246 if (env) {
247 /*tex the |env| parameter */
248 lua_pushvalue(L, 3);
249 /*tex set it as first upvalue of loaded chunk */
250 lua_setupvalue(L, -2, 1);
251 }
252 #endif
253 }
254 #ifdef LuajitTeX
255 return RESERVED_load_aux_JIT(L, status,3);
256 #else
257 return load_aux(L, status);
258 #endif
259 }
260
261 static int luatex_dofile (lua_State *L) {
262 const char *fname = luaL_optstring(L, 1, NULL);
263 int n = lua_gettop(L);
264 if (!lua_only && !fname) {
265 if (interaction == batch_mode) {
266 /*tex return |nil| plus error message */
267 lua_pushnil(L);
268 lua_pushstring(L, "reading from stdin is disabled in batch mode");
269 return 2;
270 } else {
271 tprint_nl("lua> ");
272 }
273 }
274 if (luaL_loadfile(L, fname) != 0)
275 lua_error(L);
276 recorder_record_input(fname);
277 lua_call(L, 0, LUA_MULTRET);
278 return lua_gettop(L) - n;
279 }
280
281 void luainterpreter(void)
282 {
283 lua_State *L;
284 #ifdef LuajitTeX
285 if (jithash_hashname == NULL) {
286 /*tex default lua51 */
287 luajittex_choose_hash_function = 0;
288 jithash_hashname = (char *) xmalloc(strlen("lua51") + 1);
289 jithash_hashname = strcpy ( jithash_hashname, "lua51");
290 } else if (strcmp((const char*)jithash_hashname,"lua51") == 0) {
291 luajittex_choose_hash_function = 0;
292 } else if (strcmp((const char*)jithash_hashname,"luajit20") == 0) {
293 luajittex_choose_hash_function = 1;
294 } else {
295 /*tex default lua51 */
296 luajittex_choose_hash_function = 0;
297 jithash_hashname = strcpy ( jithash_hashname, "lua51");
298 }
299 L = luaL_newstate() ;
300 #else
301 L = lua_newstate(my_luaalloc, NULL);
302 #endif
303 if (L == NULL) {
304 fprintf(stderr, "Can't create the Lua state.\n");
305 return;
306 }
307 lua_atpanic(L, &my_luapanic);
308 /*tex This initializes all the `simple' libraries: */
309 do_openlibs(L);
310 #ifdef LuajitTeX
311 if (luajiton){
312 luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_ON);
313 }
314 else {
315 luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_OFF);
316 }
317 #endif
318 lua_pushcfunction(L,luatex_dofile);
319 lua_setglobal(L, "dofile");
320 lua_pushcfunction(L,luatex_loadfile);
321 lua_setglobal(L, "loadfile");
322 open_oslibext(L);
323 open_strlibext(L);
324 /*tex
325 The socket and mime libraries are a bit tricky to open because they use a
326 load-time dependency that has to be worked around for luatex, where the C
327 module is loaded way before the lua module.
328 The mime library is always available, even if the socket library is not enabled.
329 */
330 if (!nosocket_option) {
331 /* todo: move this to common */
332 lua_getglobal(L, "package");
333 lua_getfield(L, -1, "loaded");
334 if (!lua_istable(L, -1)) {
335 lua_newtable(L);
336 lua_setfield(L, -2, "loaded");
337 lua_getfield(L, -1, "loaded");
338 }
339 /*tex |package.loaded.socket = nil| */
340 luaopen_socket_core(L);
341 lua_setfield(L, -2, "socket.core");
342 lua_pushnil(L);
343 lua_setfield(L, -2, "socket");
344 /*tex |package.loaded.mime = nil| */
345 luaopen_mime_core(L);
346 lua_setfield(L, -2, "mime.core");
347 lua_pushnil(L);
348 lua_setfield(L, -2, "mime");
349 /*tex pop the tables */
350 lua_pop(L, 2);
351 /*tex preload the pure \LUA\ modules */
352 luatex_socketlua_open(L);
353 } else {
354 lua_getglobal(L, "package");
355 lua_getfield(L, -1, "loaded");
356 if (!lua_istable(L, -1)) {
357 lua_newtable(L);
358 lua_setfield(L, -2, "loaded");
359 lua_getfield(L, -1, "loaded");
360 }
361 /*tex |package.loaded.mime = nil| */
362 luaopen_mime_core(L);
363 lua_setfield(L, -2, "mime.core");
364 lua_pushnil(L);
365 lua_setfield(L, -2, "mime");
366 /*tex pop the table */
367 lua_pop(L, 1);
368 /*tex preload the pure \LUA\ mime module */
369 luatex_socketlua_safe_open(L);
370 }
371 luaopen_zlib(L);
372 luaopen_gzip(L);
373 /*tex our own libraries register themselves */
374 luaopen_fio(L);
375 luaopen_ff(L);
376 luaopen_tex(L);
377 luaopen_token(L);
378 luaopen_node(L);
379 luaopen_texio(L);
380 luaopen_kpse(L);
381 luaopen_callback(L);
382 /*tex now we plug in extra \LUA\ startup code */
383 luaopen_lua(L, startup_filename);
384 /*tex and open some \TEX\ ones */
385 luaopen_stats(L);
386 luaopen_font(L);
387 luaopen_lang(L);
388 luaopen_mplib(L);
389 luaopen_vf(L);
390 luaopen_pdf(L);
391 luaopen_pdfe(L);
392 luaopen_pdfscanner(L);
393 luaopen_img(L);
394 lua_createtable(L, 0, 0);
395 lua_setglobal(L, "texconfig");
396 Luas = L;
397 }
398
399 int hide_lua_table(lua_State * L, const char *name)
400 {
401 int r = 0;
402 lua_getglobal(L, name);
403 if (lua_istable(L, -1)) {
404 r = luaL_ref(L, LUA_REGISTRYINDEX);
405 lua_pushnil(L);
406 lua_setglobal(L, name);
407 }
408 return r;
409 }
410
411 void unhide_lua_table(lua_State * L, const char *name, int r)
412 {
413 lua_rawgeti(L, LUA_REGISTRYINDEX, r);
414 lua_setglobal(L, name);
415 luaL_unref(L, LUA_REGISTRYINDEX, r);
416 }
417
418 int hide_lua_value(lua_State * L, const char *name, const char *item)
419 {
420 int r = 0;
421 lua_getglobal(L, name);
422 if (lua_istable(L, -1)) {
423 lua_getfield(L, -1, item);
424 r = luaL_ref(L, LUA_REGISTRYINDEX);
425 lua_pushnil(L);
426 lua_setfield(L, -2, item);
427 }
428 return r;
429 }
430
431 void unhide_lua_value(lua_State * L, const char *name, const char *item, int r)
432 {
433 lua_getglobal(L, name);
434 if (lua_istable(L, -1)) {
435 lua_rawgeti(L, LUA_REGISTRYINDEX, r);
436 lua_setfield(L, -2, item);
437 luaL_unref(L, LUA_REGISTRYINDEX, r);
438 }
439 }
440
441 int lua_traceback(lua_State * L)
442 {
443 lua_getglobal(L, "debug");
444 if (!lua_istable(L, -1)) {
445 lua_pop(L, 1);
446 return 1;
447 }
448 lua_getfield(L, -1, "traceback");
449 if (!lua_isfunction(L, -1)) {
450 lua_pop(L, 2);
451 return 1;
452 }
453 /*tex pass error message */
454 lua_pushvalue(L, 1);
455 /*tex skip this function and traceback */
456 lua_pushinteger(L, 2);
457 /*tex call |debug.traceback| */
458 lua_call(L, 2, 1);
459 return 1;
460 }
461
462 static void luacall(int p, int nameptr, boolean is_string, halfword w)
463 {
464 LoadS ls;
465 int i;
466 size_t ll = 0;
467 char *lua_id;
468 char *s = NULL;
469 int stacktop = lua_gettop(Luas);
470 if (Luas == NULL) {
471 luainterpreter();
472 }
473 lua_active++;
474 if (is_string) {
475 const char *ss = NULL;
476 lua_rawgeti(Luas, LUA_REGISTRYINDEX, p);
477 if (lua_isfunction(Luas,-1)) {
478 /*tex function index */
479 int base = lua_gettop(Luas);
480 lua_checkstack(Luas, 1);
481 /*tex push traceback function */
482 lua_pushcfunction(Luas, lua_traceback);
483 /*tex put it under chunk */
484 lua_insert(Luas, base);
485 ++late_callback_count;
486 lua_nodelib_push_fast(Luas, w);
487 i = lua_pcall(Luas, 1, 0, base);
488 /*tex remove traceback function */
489 lua_remove(Luas, base);
490 if (i != 0) {
491 lua_gc(Luas, LUA_GCCOLLECT, 0);
492 Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
493 }
494 lua_settop(Luas,stacktop);
495 lua_active--;
496 return ;
497 }
498 ss = lua_tolstring(Luas, -1, &ll);
499 s = xmalloc(ll+1);
500 memcpy(s,ss,ll+1);
501 lua_pop(Luas,1);
502 } else {
503 int l = 0;
504 s = tokenlist_to_cstring(p, 1, &l);
505 ll = (size_t)l;
506 }
507 ls.s = s;
508 ls.size = ll;
509 if (ls.size > 0) {
510 if (nameptr > 0) {
511 /*tex |l| is not used */
512 int l = 0;
513 lua_id = tokenlist_to_cstring(nameptr, 1, &l);
514 i = Luas_load(Luas, getS, &ls, lua_id);
515 xfree(lua_id);
516 } else if (nameptr < 0) {
517 lua_id = get_lua_name((nameptr + 65536));
518 if (lua_id != NULL) {
519 i = Luas_load(Luas, getS, &ls, lua_id);
520 } else {
521 i = Luas_load(Luas, getS, &ls, "=[\\latelua]");
522 }
523 } else {
524 i = Luas_load(Luas, getS, &ls, "=[\\latelua]");
525 }
526 if (i != 0) {
527 Luas = luatex_error(Luas, (i == LUA_ERRSYNTAX ? 0 : 1));
528 } else {
529 /*tex function index */
530 int base = lua_gettop(Luas);
531 lua_checkstack(Luas, 1);
532 /*tex push traceback function */
533 lua_pushcfunction(Luas, lua_traceback);
534 /*tex put it under chunk */
535 lua_insert(Luas, base);
536 ++late_callback_count;
537 i = lua_pcall(Luas, 0, 0, base);
538 /*tex remove traceback function */
539 lua_remove(Luas, base);
540 if (i != 0) {
541 lua_gc(Luas, LUA_GCCOLLECT, 0);
542 Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
543 }
544 }
545 xfree(ls.s);
546 }
547 lua_settop(Luas,stacktop);
548 lua_active--;
549 }
550
551 void luacall_vf(int p, int f, int c)
552 {
553 int i;
554 int stacktop = lua_gettop(Luas);
555 if (Luas == NULL) {
556 luainterpreter();
557 }
558 lua_active++;
559 lua_rawgeti(Luas, LUA_REGISTRYINDEX, p);
560 if (lua_isfunction(Luas,-1)) {
561 /*tex function index */
562 int base = lua_gettop(Luas);
563 lua_checkstack(Luas, 1);
564 /*tex push traceback function */
565 lua_pushcfunction(Luas, lua_traceback);
566 /*tex put it under chunk */
567 lua_insert(Luas, base);
568 lua_pushinteger(Luas, f);
569 lua_pushinteger(Luas, c);
570 ++late_callback_count;
571 i = lua_pcall(Luas, 2, 0, base);
572 /*tex remove traceback function */
573 lua_remove(Luas, base);
574 if (i != 0) {
575 lua_gc(Luas, LUA_GCCOLLECT, 0);
576 Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
577 }
578 } else {
579 LoadS ls;
580 size_t ll = 0;
581 char *s = NULL;
582 const char *ss = NULL;
583 ss = lua_tolstring(Luas, -1, &ll);
584 s = xmalloc(ll+1);
585 memcpy(s,ss,ll+1);
586 lua_pop(Luas,1);
587 ls.s = s;
588 ls.size = ll;
589 if (ls.size > 0) {
590 i = Luas_load(Luas, getS, &ls, "=[vf command]");
591 if (i != 0) {
592 Luas = luatex_error(Luas, (i == LUA_ERRSYNTAX ? 0 : 1));
593 } else {
594 int base = lua_gettop(Luas); /* function index */
595 lua_checkstack(Luas, 1);
596 lua_pushcfunction(Luas, lua_traceback); /* push traceback function */
597 lua_insert(Luas, base); /* put it under chunk */
598 ++late_callback_count;
599 i = lua_pcall(Luas, 0, 0, base);
600 lua_remove(Luas, base); /* remove traceback function */
601 if (i != 0) {
602 lua_gc(Luas, LUA_GCCOLLECT, 0);
603 Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
604 }
605 }
606 xfree(ls.s);
607 }
608 }
609 lua_settop(Luas,stacktop);
610 lua_active--;
611 }
612
613 void late_lua(PDF pdf, halfword p)
614 {
615 halfword t;
616 (void) pdf;
617 t = late_lua_type(p);
618 if (t == normal) {
619 /*tex sets |def_ref| */
620 expand_macros_in_tokenlist(late_lua_data(p));
621 luacall(def_ref, late_lua_name(p), false, p);
622 flush_list(def_ref);
623 } else if (t == lua_refid_call) {
624 luafunctioncall(late_lua_data(p));
625 } else if (t == lua_refid_literal) {
626 luacall(late_lua_data(p), late_lua_name(p), true, p);
627 } else {
628 /*tex Let's just ignore it, could be some user specific thing. */
629 }
630 }
631
632 void luatokencall(int p, int nameptr)
633 {
634 LoadS ls;
635 int i;
636 int l = 0;
637 char *s = NULL;
638 char *lua_id;
639 int stacktop = lua_gettop(Luas);
640 lua_active++;
641 s = tokenlist_to_cstring(p, 1, &l);
642 ls.s = s;
643 ls.size = (size_t) l;
644 if (ls.size > 0) {
645 if (nameptr > 0) {
646 lua_id = tokenlist_to_cstring(nameptr, 1, &l);
647 i = Luas_load(Luas, getS, &ls, lua_id);
648 xfree(lua_id);
649 } else if (nameptr < 0) {
650 lua_id = get_lua_name((nameptr + 65536));
651 if (lua_id != NULL) {
652 i = Luas_load(Luas, getS, &ls, lua_id);
653 } else {
654 i = Luas_load(Luas, getS, &ls, "=[\\directlua]");
655 }
656 } else {
657 i = Luas_load(Luas, getS, &ls, "=[\\directlua]");
658 }
659 xfree(s);
660 if (i != 0) {
661 Luas = luatex_error(Luas, (i == LUA_ERRSYNTAX ? 0 : 1));
662 } else {
663 /*tex function index */
664 int base = lua_gettop(Luas);
665 lua_checkstack(Luas, 1);
666 /*tex push traceback function */
667 lua_pushcfunction(Luas, lua_traceback);
668 /*tex put it under chunk */
669 lua_insert(Luas, base);
670 ++direct_callback_count;
671 i = lua_pcall(Luas, 0, 0, base);
672 /*tex remove traceback function */
673 lua_remove(Luas, base);
674 if (i != 0) {
675 lua_gc(Luas, LUA_GCCOLLECT, 0);
676 Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
677 }
678 }
679 }
680 lua_settop(Luas,stacktop);
681 lua_active--;
682 }
683
684 lua_State *luatex_error(lua_State * L, int is_fatal)
685 {
686 const_lstring luaerr;
687 char *err = NULL;
688 if (lua_type(L, -1) == LUA_TSTRING) {
689 luaerr.s = lua_tolstring(L, -1, &luaerr.l);
690 /*tex
691 Free the last one.
692 */
693 err = (char *) xmalloc((unsigned) (luaerr.l + 1));
694 snprintf(err, (luaerr.l + 1), "%s", luaerr.s);
695 /*tex
696 What if we have several .. not freed?
697 */
698 last_lua_error = err;
699 }
700 if (is_fatal > 0) {
701 /*
702 Normally a memory error from lua. The pool may overflow during the
703 |maketexlstring()|, but we are crashing anyway so we may as well
704 abort on the pool size
705 */
706 normal_error("lua",err);
707 /*tex
708 This is never reached.
709 */
710 lua_close(L);
711 return (lua_State *) NULL;
712 } else {
713 normal_warning("lua",err);
714 return L;
715 }
716 }
717
718 void preset_environment(lua_State * L, const parm_struct * p, const char *s)
719 {
720 int i;
721 assert(L != NULL);
722 /*tex double call with same s gives assert(0) */
723 lua_pushstring(L, s);
724 /*tex state: s */
725 lua_gettable(L, LUA_REGISTRYINDEX);
726 /*tex state: t */
727 assert(lua_isnil(L, -1));
728 lua_pop(L, 1);
729 /*tex state: - */
730 lua_pushstring(L, s);
731 /*tex state: s */
732 lua_newtable(L);
733 /*tex state: t s */
734 for (i = 1, ++p; p->name != NULL; i++, p++) {
735 assert(i == p->idx);
736 lua_pushstring(L, p->name);
737 /*tex state: k t s */
738 lua_pushinteger(L, p->idx);
739 /*tex state: v k t s */
740 lua_settable(L, -3);
741 /*tex state: t s */
742 }
743 lua_settable(L, LUA_REGISTRYINDEX);
744 /* tex state: - */
745 }
746
747 /*tex
748 Here comes a \LUAJIT\ compatibility layer for \LUATEX\ \LUA5.2:
749 */
750
751 #ifdef LuajitTeX
752
753 LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
754 lua_State *L = B->L;
755 if (sz > LUAL_BUFFERSIZE )
756 luaL_error(L, "buffer too large");
757 return luaL_prepbuffer(B) ;
758 }
759
760 LUA_API int lua_compare (lua_State *L, int o1, int o2, int op) {
761 /*StkId o1, o2;*/
762 int i = 0;
763 lua_lock(L); /* may call tag method */
764 /* o1 = index2addr(L, index1); */
765 /* o2 = index2addr(L, index2); */
766 /* if (isvalid(o1) && isvalid(o2)) {*/
767 switch (op) {
768 case LUA_OPEQ: i = lua_equal(L, o1, o2); break;
769 case LUA_OPLT: i = lua_lessthan(L, o1, o2); break;
770 case LUA_OPLE: i = (lua_lessthan(L, o1, o2) || lua_equal(L, o1, o2)) ; break;
771 default: luaL_error(L, "invalid option");
772 }
773 /* } */
774 lua_unlock(L);
775 return i;
776 }
777
778 #endif