| 1 |
/* llualib.c |
| 2 |
|
| 3 |
Copyright 2006-2008 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 7 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 2 of the License, or (at your |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License along |
| 18 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include "ptexlib.h" |
| 21 |
#include "lua/luatex-api.h" |
| 22 |
|
| 23 |
#define LOAD_BUF_SIZE 64*1024 |
| 24 |
#define UINT_MAX32 0xFFFFFFFF |
| 25 |
|
| 26 |
typedef struct { |
| 27 |
unsigned char *buf; |
| 28 |
int size; |
| 29 |
/* int done; */ |
| 30 |
int alloc; |
| 31 |
} bytecode; |
| 32 |
|
| 33 |
static bytecode *lua_bytecode_registers = NULL; |
| 34 |
|
| 35 |
int luabytecode_max = -1; |
| 36 |
unsigned int luabytecode_bytes = 0; |
| 37 |
|
| 38 |
char *luanames[65536] = { NULL }; |
| 39 |
|
| 40 |
char *get_lua_name(int i) |
| 41 |
{ |
| 42 |
if (i < 0 || i > 65535) |
| 43 |
return NULL; |
| 44 |
return luanames[i]; |
| 45 |
} |
| 46 |
|
| 47 |
void dump_luac_registers(void) |
| 48 |
{ |
| 49 |
int x; |
| 50 |
int k, n; |
| 51 |
bytecode b; |
| 52 |
dump_int(luabytecode_max); |
| 53 |
if (lua_bytecode_registers != NULL) { |
| 54 |
n = 0; |
| 55 |
for (k = 0; k <= luabytecode_max; k++) { |
| 56 |
if (lua_bytecode_registers[k].size != 0) |
| 57 |
n++; |
| 58 |
} |
| 59 |
dump_int(n); |
| 60 |
for (k = 0; k <= luabytecode_max; k++) { |
| 61 |
b = lua_bytecode_registers[k]; |
| 62 |
if (b.size != 0) { |
| 63 |
dump_int(k); |
| 64 |
dump_int(b.size); |
| 65 |
do_zdump((char *) b.buf, 1, (b.size), DUMP_FILE); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
for (k = 0; k < 65536; k++) { |
| 70 |
char *a = luanames[k]; |
| 71 |
if (a != NULL) { |
| 72 |
x = (int) strlen(a) + 1; |
| 73 |
dump_int(x); |
| 74 |
dump_things(*a, x); |
| 75 |
} else { |
| 76 |
x = 0; |
| 77 |
dump_int(x); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
void undump_luac_registers(void) |
| 83 |
{ |
| 84 |
int x; |
| 85 |
int k, n; |
| 86 |
unsigned int i; |
| 87 |
bytecode b; |
| 88 |
undump_int(luabytecode_max); |
| 89 |
if (luabytecode_max >= 0) { |
| 90 |
i = (unsigned) (luabytecode_max + 1); |
| 91 |
if ((int) (UINT_MAX32 / (int) sizeof(bytecode) + 1) <= i) { |
| 92 |
fatal_error("Corrupt format file"); |
| 93 |
} |
| 94 |
lua_bytecode_registers = xmalloc((unsigned) (i * sizeof(bytecode))); |
| 95 |
luabytecode_bytes = (unsigned) (i * sizeof(bytecode)); |
| 96 |
for (i = 0; i <= (unsigned) luabytecode_max; i++) { |
| 97 |
/* lua_bytecode_registers[i].done = 0; */ |
| 98 |
lua_bytecode_registers[i].size = 0; |
| 99 |
lua_bytecode_registers[i].buf = NULL; |
| 100 |
} |
| 101 |
undump_int(n); |
| 102 |
for (i = 0; i < (unsigned) n; i++) { |
| 103 |
undump_int(k); |
| 104 |
undump_int(x); |
| 105 |
b.size = x; |
| 106 |
b.buf = xmalloc((unsigned) b.size); |
| 107 |
luabytecode_bytes += (unsigned) b.size; |
| 108 |
memset(b.buf, 0, (size_t) b.size); |
| 109 |
do_zundump((char *) b.buf, 1, b.size, DUMP_FILE); |
| 110 |
lua_bytecode_registers[k].size = b.size; |
| 111 |
lua_bytecode_registers[k].alloc = b.size; |
| 112 |
lua_bytecode_registers[k].buf = b.buf; |
| 113 |
} |
| 114 |
} |
| 115 |
for (k = 0; k < 65536; k++) { |
| 116 |
undump_int(x); |
| 117 |
if (x > 0) { |
| 118 |
char *s = xmalloc((unsigned) x); |
| 119 |
undump_things(*s, x); |
| 120 |
luanames[k] = s; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
static void bytecode_register_shadow_set(lua_State * L, int k) |
| 126 |
{ |
| 127 |
/* the stack holds the value to be set */ |
| 128 |
lua_get_metatablelua(lua_bytecodes_indirect); |
| 129 |
if (lua_istable(L, -1)) { |
| 130 |
lua_pushvalue(L, -2); |
| 131 |
lua_rawseti(L, -2, k); |
| 132 |
} |
| 133 |
lua_pop(L, 1); /* pop table or nil */ |
| 134 |
lua_pop(L, 1); /* pop value */ |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
static int bytecode_register_shadow_get(lua_State * L, int k) |
| 139 |
{ |
| 140 |
/* the stack holds the value to be set */ |
| 141 |
int ret = 0; |
| 142 |
lua_get_metatablelua(lua_bytecodes_indirect); |
| 143 |
if (lua_istable(L, -1)) { |
| 144 |
lua_rawgeti(L, -1, k); |
| 145 |
if (!lua_isnil(L, -1)) |
| 146 |
ret = 1; |
| 147 |
lua_insert(L, -3); /* store the value or nil, deeper down */ |
| 148 |
lua_pop(L, 1); /* pop the value or nil at top */ |
| 149 |
} |
| 150 |
lua_pop(L, 1); /* pop table or nil */ |
| 151 |
return ret; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
static int writer(lua_State * L, const void *b, size_t size, void *B) |
| 156 |
{ |
| 157 |
bytecode *buf = (bytecode *) B; |
| 158 |
(void) L; /* for -Wunused */ |
| 159 |
if ((int) (buf->size + (int) size) > buf->alloc) { |
| 160 |
buf->buf = xrealloc(buf->buf, (unsigned) (buf->alloc + (int) size + LOAD_BUF_SIZE)); |
| 161 |
buf->alloc = buf->alloc + (int) size + LOAD_BUF_SIZE; |
| 162 |
} |
| 163 |
memcpy(buf->buf + buf->size, b, size); |
| 164 |
buf->size += (int) size; |
| 165 |
luabytecode_bytes += (unsigned) size; |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static const char *reader(lua_State * L, void *ud, size_t * size) |
| 170 |
{ |
| 171 |
bytecode *buf = (bytecode *) ud; |
| 172 |
(void) L; /* for -Wunused */ |
| 173 |
/* |
| 174 |
if (buf->done == buf->size) { |
| 175 |
*size = 0; |
| 176 |
buf->done = 0; |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
*/ |
| 180 |
*size = (size_t) buf->size; |
| 181 |
/* |
| 182 |
buf->done = buf->size; |
| 183 |
*/ |
| 184 |
return (const char *) buf->buf; |
| 185 |
} |
| 186 |
|
| 187 |
static int get_bytecode(lua_State * L) |
| 188 |
{ |
| 189 |
int k; |
| 190 |
k = (int) luaL_checkinteger(L, -1); |
| 191 |
if (k < 0) { |
| 192 |
lua_pushnil(L); |
| 193 |
} else if (!bytecode_register_shadow_get(L, k)) { |
| 194 |
if (k <= luabytecode_max && lua_bytecode_registers[k].buf != NULL) { |
| 195 |
if (lua_load |
| 196 |
(L, reader, (void *) (lua_bytecode_registers + k), |
| 197 |
#ifdef LuajitTeX |
| 198 |
"bytecode")) { |
| 199 |
#else |
| 200 |
"bytecode", NULL)) { |
| 201 |
#endif |
| 202 |
return luaL_error(L, "bad bytecode register"); |
| 203 |
} else { |
| 204 |
lua_pushvalue(L, -1); |
| 205 |
bytecode_register_shadow_set(L, k); |
| 206 |
} |
| 207 |
} else { |
| 208 |
lua_pushnil(L); |
| 209 |
} |
| 210 |
} |
| 211 |
return 1; |
| 212 |
} |
| 213 |
|
| 214 |
void luabytecodecall(int slot) |
| 215 |
{ |
| 216 |
int i; |
| 217 |
int stacktop = lua_gettop(Luas); |
| 218 |
lua_active++; |
| 219 |
if (slot < 0 || slot > luabytecode_max) { |
| 220 |
luaL_error(Luas, "bytecode register out of range"); |
| 221 |
} else if (bytecode_register_shadow_get(Luas, slot) || lua_bytecode_registers[slot].buf == NULL) { |
| 222 |
luaL_error(Luas, "undefined bytecode register"); |
| 223 |
} else if (lua_load(Luas, reader, (void *) (lua_bytecode_registers + slot), |
| 224 |
#ifdef LuajitTeX |
| 225 |
"bytecode")) |
| 226 |
#else |
| 227 |
"bytecode", NULL)) |
| 228 |
#endif |
| 229 |
{ |
| 230 |
luaL_error(Luas, "bytecode register doesn't load well"); |
| 231 |
} else { |
| 232 |
int base = lua_gettop(Luas); /* function index */ |
| 233 |
lua_pushinteger(Luas, slot); |
| 234 |
lua_pushcfunction(Luas, lua_traceback); /* push traceback function */ |
| 235 |
lua_insert(Luas, base); /* put it under chunk */ |
| 236 |
++function_callback_count; /* this will be a dedicated counter */ |
| 237 |
i = lua_pcall(Luas, 1, 0, base); |
| 238 |
lua_remove(Luas, base); /* remove traceback function */ |
| 239 |
if (i != 0) { |
| 240 |
lua_gc(Luas, LUA_GCCOLLECT, 0); |
| 241 |
Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1)); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
lua_settop(Luas,stacktop); |
| 246 |
lua_active--; |
| 247 |
} |
| 248 |
|
| 249 |
static int set_bytecode(lua_State * L) |
| 250 |
{ |
| 251 |
int k, ltype; |
| 252 |
unsigned int i; |
| 253 |
int strip = 0; |
| 254 |
int top = lua_gettop(L); |
| 255 |
if (lua_type(L,top) == LUA_TBOOLEAN) { |
| 256 |
strip = lua_toboolean(L,top); |
| 257 |
lua_settop(L,top - 1); |
| 258 |
} |
| 259 |
k = (int) luaL_checkinteger(L, -2); |
| 260 |
i = (unsigned) k + 1; |
| 261 |
if ((int) (UINT_MAX32 / sizeof(bytecode) + 1) < i) { |
| 262 |
luaL_error(L, "value too large"); |
| 263 |
} |
| 264 |
if (k < 0) { |
| 265 |
luaL_error(L, "negative values not allowed"); |
| 266 |
} |
| 267 |
ltype = lua_type(L, -1); |
| 268 |
if (ltype != LUA_TFUNCTION && ltype != LUA_TNIL) { |
| 269 |
luaL_error(L, "unsupported type"); |
| 270 |
} |
| 271 |
if (k > luabytecode_max) { |
| 272 |
i = (unsigned) (sizeof(bytecode) * ((unsigned) k + 1)); |
| 273 |
lua_bytecode_registers = xrealloc(lua_bytecode_registers, i); |
| 274 |
if (luabytecode_max == -1) { |
| 275 |
luabytecode_bytes += |
| 276 |
(unsigned) (sizeof(bytecode) * (unsigned) (k + 1)); |
| 277 |
} else { |
| 278 |
luabytecode_bytes += |
| 279 |
(unsigned) (sizeof(bytecode) * |
| 280 |
(unsigned) (k + 1 - luabytecode_max)); |
| 281 |
} |
| 282 |
for (i = (unsigned) (luabytecode_max + 1); i <= (unsigned) k; i++) { |
| 283 |
lua_bytecode_registers[i].buf = NULL; |
| 284 |
lua_bytecode_registers[i].size = 0; |
| 285 |
/* lua_bytecode_registers[i].done = 0; */ |
| 286 |
} |
| 287 |
luabytecode_max = k; |
| 288 |
} |
| 289 |
if (lua_bytecode_registers[k].buf != NULL) { |
| 290 |
xfree(lua_bytecode_registers[k].buf); |
| 291 |
luabytecode_bytes -= (unsigned) lua_bytecode_registers[k].size; |
| 292 |
lua_bytecode_registers[k].size = 0; |
| 293 |
/* lua_bytecode_registers[k].done = 0; */ |
| 294 |
lua_pushnil(L); |
| 295 |
bytecode_register_shadow_set(L, k); |
| 296 |
} |
| 297 |
if (ltype == LUA_TFUNCTION) { |
| 298 |
lua_bytecode_registers[k].buf = xmalloc(LOAD_BUF_SIZE); |
| 299 |
lua_bytecode_registers[k].alloc = LOAD_BUF_SIZE; |
| 300 |
memset(lua_bytecode_registers[k].buf, 0, LOAD_BUF_SIZE); |
| 301 |
#ifdef LuajitTeX |
| 302 |
RESERVED_lua_dump(L, writer, (void *) (lua_bytecode_registers + k),strip); |
| 303 |
/*lua_dump(L, writer, (void *) (lua_bytecode_registers + k));*/ |
| 304 |
#else |
| 305 |
#if LUA_VERSION_NUM == 503 |
| 306 |
lua_dump(L, writer, (void *) (lua_bytecode_registers + k),strip); |
| 307 |
#endif |
| 308 |
#if LUA_VERSION_NUM == 502 |
| 309 |
lua_dump(L, writer, (void *) (lua_bytecode_registers + k)); |
| 310 |
#endif |
| 311 |
#endif |
| 312 |
} |
| 313 |
lua_pop(L, 1); |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
static int set_luaname(lua_State * L) |
| 319 |
{ |
| 320 |
int k; |
| 321 |
const char *s; |
| 322 |
if (lua_gettop(L) == 2) { |
| 323 |
k = (int) luaL_checkinteger(L, 1); |
| 324 |
if (k > 65535 || k < 0) { |
| 325 |
/* error */ |
| 326 |
} else { |
| 327 |
if (luanames[k] != NULL) { |
| 328 |
free(luanames[k]); |
| 329 |
luanames[k] = NULL; |
| 330 |
} |
| 331 |
if (lua_type(L, 2) == LUA_TSTRING) { |
| 332 |
s = lua_tostring(L, 2); |
| 333 |
if (s != NULL) |
| 334 |
luanames[k] = xstrdup(s); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
return 0; |
| 339 |
} |
| 340 |
|
| 341 |
static int get_luaname(lua_State * L) |
| 342 |
{ |
| 343 |
int k = (int) luaL_checkinteger(L, 1); |
| 344 |
if (k > 65535 || k < 0) { |
| 345 |
/* error */ |
| 346 |
lua_pushnil(L); |
| 347 |
} else { |
| 348 |
if (luanames[k] != NULL) |
| 349 |
lua_pushstring(L, luanames[k]); |
| 350 |
else |
| 351 |
lua_pushnil(L); |
| 352 |
} |
| 353 |
return 1; |
| 354 |
} |
| 355 |
|
| 356 |
static int lua_functions_get_table(lua_State * L) /* hh */ |
| 357 |
{ |
| 358 |
lua_get_metatablelua(lua_functions); |
| 359 |
return 1; |
| 360 |
} |
| 361 |
|
| 362 |
static int new_table(lua_State * L) /* hh */ |
| 363 |
{ |
| 364 |
int i = (int) luaL_checkinteger(L, 1); |
| 365 |
int h = (int) luaL_checkinteger(L, 2); |
| 366 |
lua_createtable(L,i,h); |
| 367 |
return 1; |
| 368 |
} |
| 369 |
|
| 370 |
static int get_stack_top(lua_State * L) /* hh */ |
| 371 |
{ |
| 372 |
lua_pushinteger(L, lua_gettop(L)); |
| 373 |
return 1; |
| 374 |
} |
| 375 |
|
| 376 |
static int get_call_level(lua_State * L) /* hh */ |
| 377 |
{ |
| 378 |
lua_pushinteger(L, lua_active); |
| 379 |
return 1; |
| 380 |
} |
| 381 |
|
| 382 |
static int get_code_page(lua_State *L) |
| 383 |
{ |
| 384 |
# ifdef _WIN32 |
| 385 |
lua_pushinteger(L,(int) GetOEMCP()); |
| 386 |
lua_pushinteger(L,(int) GetACP()); |
| 387 |
# else |
| 388 |
lua_pushboolean(L,0); |
| 389 |
lua_pushboolean(L,0); |
| 390 |
# endif |
| 391 |
return 2; |
| 392 |
} |
| 393 |
|
| 394 |
static const struct luaL_Reg lualib[] = { |
| 395 |
/* *INDENT-OFF* */ |
| 396 |
{"getluaname", get_luaname}, |
| 397 |
{"setluaname", set_luaname}, |
| 398 |
{"getbytecode", get_bytecode}, |
| 399 |
{"setbytecode", set_bytecode}, |
| 400 |
{"newtable", new_table}, |
| 401 |
{"get_functions_table",lua_functions_get_table}, |
| 402 |
{"getstacktop", get_stack_top}, |
| 403 |
{"getcalllevel", get_call_level}, |
| 404 |
{"getcodepage", get_code_page }, |
| 405 |
/* *INDENT-ON* */ |
| 406 |
{NULL, NULL} /* sentinel */ |
| 407 |
}; |
| 408 |
|
| 409 |
int luaopen_lua(lua_State * L, char *fname) |
| 410 |
{ |
| 411 |
luaL_openlib(L, "lua", lualib, 0); |
| 412 |
make_table(L, "bytecode", "tex.bytecode", "getbytecode", "setbytecode"); |
| 413 |
make_table(L, "name", "tex.name", "getluaname", "setluaname"); |
| 414 |
lua_newtable(L); |
| 415 |
lua_setfield(L, LUA_REGISTRYINDEX, "lua.bytecodes.indirect"); |
| 416 |
lua_pushstring(L, LUA_VERSION); |
| 417 |
lua_setfield(L, -2, "version"); |
| 418 |
if (fname == NULL) { |
| 419 |
lua_pushnil(L); |
| 420 |
} else { |
| 421 |
lua_pushstring(L, fname); |
| 422 |
} |
| 423 |
lua_setfield(L, -2, "startupfile"); |
| 424 |
return 1; |
| 425 |
} |