1 /**
2 * \file   luashim.c
3 * \brief  Lua shim for premake binary modules.
4 * \author Copyright (c) 2017 Tom van Dijck and the Premake project
5 */
6 #include "luashim.h"
7 #include <assert.h>
8 #include "lstate.h"
9 
10 static const LuaFunctionTable_t* g_shimTable;
11 
luaL_register(lua_State * L,const char * libname,const luaL_Reg * l)12 void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l)
13 {
14 	assert(g_shimTable != NULL);
15 	g_shimTable->shimL_register(L, libname, l);
16 }
17 
lua_newstate(lua_Alloc f,void * ud)18 lua_State* lua_newstate(lua_Alloc f, void* ud)
19 {
20 	assert(g_shimTable != NULL);
21 	return g_shimTable->shim_newstate(f, ud);
22 }
23 
lua_close(lua_State * L)24 void lua_close(lua_State* L)
25 {
26 	assert(g_shimTable != NULL);
27 	g_shimTable->shim_close(L);
28 }
29 
lua_newthread(lua_State * L)30 lua_State* lua_newthread(lua_State* L)
31 {
32 	assert(g_shimTable != NULL);
33 	return g_shimTable->shim_newthread(L);
34 }
35 
lua_atpanic(lua_State * L,lua_CFunction panicf)36 lua_CFunction lua_atpanic(lua_State* L, lua_CFunction panicf)
37 {
38 	assert(g_shimTable != NULL);
39 	return g_shimTable->shim_atpanic(L, panicf);
40 }
41 
lua_version(lua_State * L)42 const lua_Number* lua_version(lua_State* L)
43 {
44 	assert(g_shimTable != NULL);
45 	return g_shimTable->shim_version(L);
46 }
47 
lua_absindex(lua_State * L,int idx)48 int lua_absindex(lua_State* L, int idx)
49 {
50 	assert(g_shimTable != NULL);
51 	return g_shimTable->shim_absindex(L, idx);
52 }
53 
lua_gettop(lua_State * L)54 int lua_gettop(lua_State* L)
55 {
56 	assert(g_shimTable != NULL);
57 	return g_shimTable->shim_gettop(L);
58 }
59 
lua_settop(lua_State * L,int idx)60 void lua_settop(lua_State* L, int idx)
61 {
62 	assert(g_shimTable != NULL);
63 	g_shimTable->shim_settop(L, idx);
64 }
65 
lua_pushvalue(lua_State * L,int idx)66 void lua_pushvalue(lua_State* L, int idx)
67 {
68 	assert(g_shimTable != NULL);
69 	g_shimTable->shim_pushvalue(L, idx);
70 }
71 
lua_rotate(lua_State * L,int idx,int n)72 void lua_rotate(lua_State* L, int idx, int n)
73 {
74 	assert(g_shimTable != NULL);
75 	g_shimTable->shim_rotate(L, idx, n);
76 }
77 
lua_copy(lua_State * L,int fromidx,int toidx)78 void lua_copy(lua_State* L, int fromidx, int toidx)
79 {
80 	assert(g_shimTable != NULL);
81 	g_shimTable->shim_copy(L, fromidx, toidx);
82 }
83 
lua_checkstack(lua_State * L,int n)84 int lua_checkstack(lua_State* L, int n)
85 {
86 	assert(g_shimTable != NULL);
87 	return g_shimTable->shim_checkstack(L, n);
88 }
89 
lua_xmove(lua_State * from,lua_State * to,int n)90 void lua_xmove(lua_State* from, lua_State* to, int n)
91 {
92 	assert(g_shimTable != NULL);
93 	g_shimTable->shim_xmove(from, to, n);
94 }
95 
lua_isnumber(lua_State * L,int idx)96 int lua_isnumber(lua_State* L, int idx)
97 {
98 	assert(g_shimTable != NULL);
99 	return g_shimTable->shim_isnumber(L, idx);
100 }
101 
lua_isstring(lua_State * L,int idx)102 int lua_isstring(lua_State* L, int idx)
103 {
104 	assert(g_shimTable != NULL);
105 	return g_shimTable->shim_isstring(L, idx);
106 }
107 
lua_iscfunction(lua_State * L,int idx)108 int lua_iscfunction(lua_State* L, int idx)
109 {
110 	assert(g_shimTable != NULL);
111 	return g_shimTable->shim_iscfunction(L, idx);
112 }
113 
lua_isinteger(lua_State * L,int idx)114 int lua_isinteger(lua_State* L, int idx)
115 {
116 	assert(g_shimTable != NULL);
117 	return g_shimTable->shim_isinteger(L, idx);
118 }
119 
lua_isuserdata(lua_State * L,int idx)120 int lua_isuserdata(lua_State* L, int idx)
121 {
122 	assert(g_shimTable != NULL);
123 	return g_shimTable->shim_isuserdata(L, idx);
124 }
125 
lua_type(lua_State * L,int idx)126 int lua_type(lua_State* L, int idx)
127 {
128 	assert(g_shimTable != NULL);
129 	return g_shimTable->shim_type(L, idx);
130 }
131 
lua_typename(lua_State * L,int tp)132 const char* lua_typename(lua_State* L, int tp)
133 {
134 	assert(g_shimTable != NULL);
135 	return g_shimTable->shim_typename(L, tp);
136 }
137 
lua_tonumberx(lua_State * L,int idx,int * isnum)138 lua_Number lua_tonumberx(lua_State* L, int idx, int* isnum)
139 {
140 	assert(g_shimTable != NULL);
141 	return g_shimTable->shim_tonumberx(L, idx, isnum);
142 }
143 
lua_tointegerx(lua_State * L,int idx,int * isnum)144 lua_Integer lua_tointegerx(lua_State* L, int idx, int* isnum)
145 {
146 	assert(g_shimTable != NULL);
147 	return g_shimTable->shim_tointegerx(L, idx, isnum);
148 }
149 
lua_toboolean(lua_State * L,int idx)150 int lua_toboolean(lua_State* L, int idx)
151 {
152 	assert(g_shimTable != NULL);
153 	return g_shimTable->shim_toboolean(L, idx);
154 }
155 
lua_tolstring(lua_State * L,int idx,size_t * len)156 const char* lua_tolstring(lua_State* L, int idx, size_t* len)
157 {
158 	assert(g_shimTable != NULL);
159 	return g_shimTable->shim_tolstring(L, idx, len);
160 }
161 
lua_rawlen(lua_State * L,int idx)162 size_t lua_rawlen(lua_State* L, int idx)
163 {
164 	assert(g_shimTable != NULL);
165 	return g_shimTable->shim_rawlen(L, idx);
166 }
167 
lua_tocfunction(lua_State * L,int idx)168 lua_CFunction lua_tocfunction(lua_State* L, int idx)
169 {
170 	assert(g_shimTable != NULL);
171 	return g_shimTable->shim_tocfunction(L, idx);
172 }
173 
lua_touserdata(lua_State * L,int idx)174 void* lua_touserdata(lua_State* L, int idx)
175 {
176 	assert(g_shimTable != NULL);
177 	return g_shimTable->shim_touserdata(L, idx);
178 }
179 
lua_tothread(lua_State * L,int idx)180 lua_State* lua_tothread(lua_State* L, int idx)
181 {
182 	assert(g_shimTable != NULL);
183 	return g_shimTable->shim_tothread(L, idx);
184 }
185 
lua_topointer(lua_State * L,int idx)186 const void* lua_topointer(lua_State* L, int idx)
187 {
188 	assert(g_shimTable != NULL);
189 	return g_shimTable->shim_topointer(L, idx);
190 }
191 
lua_arith(lua_State * L,int op)192 void lua_arith(lua_State* L, int op)
193 {
194 	assert(g_shimTable != NULL);
195 	g_shimTable->shim_arith(L, op);
196 }
197 
lua_rawequal(lua_State * L,int idx1,int idx2)198 int lua_rawequal(lua_State* L, int idx1, int idx2)
199 {
200 	assert(g_shimTable != NULL);
201 	return g_shimTable->shim_rawequal(L, idx1, idx2);
202 }
203 
lua_compare(lua_State * L,int idx1,int idx2,int op)204 int lua_compare(lua_State* L, int idx1, int idx2, int op)
205 {
206 	assert(g_shimTable != NULL);
207 	return g_shimTable->shim_compare(L, idx1, idx2, op);
208 }
209 
lua_pushnil(lua_State * L)210 void lua_pushnil(lua_State* L)
211 {
212 	assert(g_shimTable != NULL);
213 	g_shimTable->shim_pushnil(L);
214 }
215 
lua_pushnumber(lua_State * L,lua_Number n)216 void lua_pushnumber(lua_State* L, lua_Number n)
217 {
218 	assert(g_shimTable != NULL);
219 	g_shimTable->shim_pushnumber(L, n);
220 }
221 
lua_pushinteger(lua_State * L,lua_Integer n)222 void lua_pushinteger(lua_State* L, lua_Integer n)
223 {
224 	assert(g_shimTable != NULL);
225 	g_shimTable->shim_pushinteger(L, n);
226 }
227 
lua_pushlstring(lua_State * L,const char * s,size_t len)228 const char* lua_pushlstring(lua_State* L, const char* s, size_t len)
229 {
230 	assert(g_shimTable != NULL);
231 	return g_shimTable->shim_pushlstring(L, s, len);
232 }
233 
lua_pushstring(lua_State * L,const char * s)234 const char* lua_pushstring(lua_State* L, const char* s)
235 {
236 	assert(g_shimTable != NULL);
237 	return g_shimTable->shim_pushstring(L, s);
238 }
239 
lua_pushvfstring(lua_State * L,const char * fmt,va_list argp)240 const char* lua_pushvfstring(lua_State* L, const char* fmt, va_list argp)
241 {
242 	assert(g_shimTable != NULL);
243 	return g_shimTable->shim_pushvfstring(L, fmt, argp);
244 }
245 
lua_pushfstring(lua_State * L,const char * fmt,...)246 const char* lua_pushfstring(lua_State* L, const char* fmt, ...)
247 {
248 	const char* ret;
249 	va_list argp;
250 	va_start(argp, fmt);
251 	ret = lua_pushvfstring(L, fmt, argp);
252 	va_end(argp);
253 	return ret;
254 }
255 
lua_pushcclosure(lua_State * L,lua_CFunction fn,int n)256 void lua_pushcclosure(lua_State* L, lua_CFunction fn, int n)
257 {
258 	assert(g_shimTable != NULL);
259 	g_shimTable->shim_pushcclosure(L, fn, n);
260 }
261 
lua_pushboolean(lua_State * L,int b)262 void lua_pushboolean(lua_State* L, int b)
263 {
264 	assert(g_shimTable != NULL);
265 	g_shimTable->shim_pushboolean(L, b);
266 }
267 
lua_pushlightuserdata(lua_State * L,void * p)268 void lua_pushlightuserdata(lua_State* L, void* p)
269 {
270 	assert(g_shimTable != NULL);
271 	g_shimTable->shim_pushlightuserdata(L, p);
272 }
273 
lua_pushthread(lua_State * L)274 int lua_pushthread(lua_State* L)
275 {
276 	assert(g_shimTable != NULL);
277 	return g_shimTable->shim_pushthread(L);
278 }
279 
lua_getglobal(lua_State * L,const char * name)280 int lua_getglobal(lua_State* L, const char* name)
281 {
282 	assert(g_shimTable != NULL);
283 	return g_shimTable->shim_getglobal(L, name);
284 }
285 
lua_gettable(lua_State * L,int idx)286 int lua_gettable(lua_State* L, int idx)
287 {
288 	assert(g_shimTable != NULL);
289 	return g_shimTable->shim_gettable(L, idx);
290 }
291 
lua_getfield(lua_State * L,int idx,const char * k)292 int lua_getfield(lua_State* L, int idx, const char* k)
293 {
294 	assert(g_shimTable != NULL);
295 	return g_shimTable->shim_getfield(L, idx, k);
296 }
297 
lua_geti(lua_State * L,int idx,lua_Integer n)298 int lua_geti(lua_State* L, int idx, lua_Integer n)
299 {
300 	assert(g_shimTable != NULL);
301 	return g_shimTable->shim_geti(L, idx, n);
302 }
303 
lua_rawget(lua_State * L,int idx)304 int lua_rawget(lua_State* L, int idx)
305 {
306 	assert(g_shimTable != NULL);
307 	return g_shimTable->shim_rawget(L, idx);
308 }
309 
lua_rawgeti(lua_State * L,int idx,lua_Integer n)310 int lua_rawgeti(lua_State* L, int idx, lua_Integer n)
311 {
312 	assert(g_shimTable != NULL);
313 	return g_shimTable->shim_rawgeti(L, idx, n);
314 }
315 
lua_rawgetp(lua_State * L,int idx,const void * p)316 int lua_rawgetp(lua_State* L, int idx, const void* p)
317 {
318 	assert(g_shimTable != NULL);
319 	return g_shimTable->shim_rawgetp(L, idx, p);
320 }
321 
lua_createtable(lua_State * L,int narr,int nrec)322 void lua_createtable(lua_State* L, int narr, int nrec)
323 {
324 	assert(g_shimTable != NULL);
325 	g_shimTable->shim_createtable(L, narr, nrec);
326 }
327 
lua_newuserdata(lua_State * L,size_t sz)328 void* lua_newuserdata(lua_State* L, size_t sz)
329 {
330 	assert(g_shimTable != NULL);
331 	return g_shimTable->shim_newuserdata(L, sz);
332 }
333 
lua_getmetatable(lua_State * L,int objindex)334 int lua_getmetatable(lua_State* L, int objindex)
335 {
336 	assert(g_shimTable != NULL);
337 	return g_shimTable->shim_getmetatable(L, objindex);
338 }
339 
lua_getuservalue(lua_State * L,int idx)340 int lua_getuservalue(lua_State* L, int idx)
341 {
342 	assert(g_shimTable != NULL);
343 	return g_shimTable->shim_getuservalue(L, idx);
344 }
345 
lua_setglobal(lua_State * L,const char * name)346 void lua_setglobal(lua_State* L, const char* name)
347 {
348 	assert(g_shimTable != NULL);
349 	g_shimTable->shim_setglobal(L, name);
350 }
351 
lua_settable(lua_State * L,int idx)352 void lua_settable(lua_State* L, int idx)
353 {
354 	assert(g_shimTable != NULL);
355 	g_shimTable->shim_settable(L, idx);
356 }
357 
lua_setfield(lua_State * L,int idx,const char * k)358 void lua_setfield(lua_State* L, int idx, const char* k)
359 {
360 	assert(g_shimTable != NULL);
361 	g_shimTable->shim_setfield(L, idx, k);
362 }
363 
lua_seti(lua_State * L,int idx,lua_Integer n)364 void lua_seti(lua_State* L, int idx, lua_Integer n)
365 {
366 	assert(g_shimTable != NULL);
367 	g_shimTable->shim_seti(L, idx, n);
368 }
369 
lua_rawset(lua_State * L,int idx)370 void lua_rawset(lua_State* L, int idx)
371 {
372 	assert(g_shimTable != NULL);
373 	g_shimTable->shim_rawset(L, idx);
374 }
375 
lua_rawseti(lua_State * L,int idx,lua_Integer n)376 void lua_rawseti(lua_State* L, int idx, lua_Integer n)
377 {
378 	assert(g_shimTable != NULL);
379 	g_shimTable->shim_rawseti(L, idx, n);
380 }
381 
lua_rawsetp(lua_State * L,int idx,const void * p)382 void lua_rawsetp(lua_State* L, int idx, const void* p)
383 {
384 	assert(g_shimTable != NULL);
385 	g_shimTable->shim_rawsetp(L, idx, p);
386 }
387 
lua_setmetatable(lua_State * L,int objindex)388 int lua_setmetatable(lua_State* L, int objindex)
389 {
390 	assert(g_shimTable != NULL);
391 	return g_shimTable->shim_setmetatable(L, objindex);
392 }
393 
lua_setuservalue(lua_State * L,int idx)394 void lua_setuservalue(lua_State* L, int idx)
395 {
396 	assert(g_shimTable != NULL);
397 	g_shimTable->shim_setuservalue(L, idx);
398 }
399 
lua_callk(lua_State * L,int nargs,int nresults,lua_KContext ctx,lua_KFunction k)400 void lua_callk(lua_State* L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k)
401 {
402 	assert(g_shimTable != NULL);
403 	g_shimTable->shim_callk(L, nargs, nresults, ctx, k);
404 }
405 
lua_pcallk(lua_State * L,int nargs,int nresults,int errfunc,lua_KContext ctx,lua_KFunction k)406 int lua_pcallk(lua_State* L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k)
407 {
408 	assert(g_shimTable != NULL);
409 	return g_shimTable->shim_pcallk(L, nargs, nresults, errfunc, ctx, k);
410 }
411 
lua_load(lua_State * L,lua_Reader reader,void * dt,const char * chunkname,const char * mode)412 int lua_load(lua_State* L, lua_Reader reader, void* dt, const char* chunkname, const char* mode)
413 {
414 	assert(g_shimTable != NULL);
415 	return g_shimTable->shim_load(L, reader, dt, chunkname, mode);
416 }
417 
lua_dump(lua_State * L,lua_Writer writer,void * data,int strip)418 int lua_dump(lua_State* L, lua_Writer writer, void* data, int strip)
419 {
420 	assert(g_shimTable != NULL);
421 	return g_shimTable->shim_dump(L, writer, data, strip);
422 }
423 
lua_yieldk(lua_State * L,int nresults,lua_KContext ctx,lua_KFunction k)424 int lua_yieldk(lua_State* L, int nresults, lua_KContext ctx, lua_KFunction k)
425 {
426 	assert(g_shimTable != NULL);
427 	return g_shimTable->shim_yieldk(L, nresults, ctx, k);
428 }
429 
lua_resume(lua_State * L,lua_State * from,int narg)430 int lua_resume(lua_State* L, lua_State* from, int narg)
431 {
432 	assert(g_shimTable != NULL);
433 	return g_shimTable->shim_resume(L, from, narg);
434 }
435 
lua_status(lua_State * L)436 int lua_status(lua_State* L)
437 {
438 	assert(g_shimTable != NULL);
439 	return g_shimTable->shim_status(L);
440 }
441 
lua_isyieldable(lua_State * L)442 int lua_isyieldable(lua_State* L)
443 {
444 	assert(g_shimTable != NULL);
445 	return g_shimTable->shim_isyieldable(L);
446 }
447 
lua_gc(lua_State * L,int what,int data)448 int lua_gc(lua_State* L, int what, int data)
449 {
450 	assert(g_shimTable != NULL);
451 	return g_shimTable->shim_gc(L, what, data);
452 }
453 
lua_error(lua_State * L)454 int lua_error(lua_State* L)
455 {
456 	assert(g_shimTable != NULL);
457 	return g_shimTable->shim_error(L);
458 }
459 
lua_next(lua_State * L,int idx)460 int lua_next(lua_State* L, int idx)
461 {
462 	assert(g_shimTable != NULL);
463 	return g_shimTable->shim_next(L, idx);
464 }
465 
lua_concat(lua_State * L,int n)466 void lua_concat(lua_State* L, int n)
467 {
468 	assert(g_shimTable != NULL);
469 	g_shimTable->shim_concat(L, n);
470 }
471 
lua_len(lua_State * L,int idx)472 void lua_len(lua_State* L, int idx)
473 {
474 	assert(g_shimTable != NULL);
475 	g_shimTable->shim_len(L, idx);
476 }
477 
lua_stringtonumber(lua_State * L,const char * s)478 size_t lua_stringtonumber(lua_State* L, const char* s)
479 {
480 	assert(g_shimTable != NULL);
481 	return g_shimTable->shim_stringtonumber(L, s);
482 }
483 
lua_getallocf(lua_State * L,void ** ud)484 lua_Alloc lua_getallocf(lua_State* L, void** ud)
485 {
486 	assert(g_shimTable != NULL);
487 	return g_shimTable->shim_getallocf(L, ud);
488 }
489 
lua_setallocf(lua_State * L,lua_Alloc f,void * ud)490 void lua_setallocf(lua_State* L, lua_Alloc f, void* ud)
491 {
492 	assert(g_shimTable != NULL);
493 	g_shimTable->shim_setallocf(L, f, ud);
494 }
495 
lua_getstack(lua_State * L,int level,lua_Debug * ar)496 int lua_getstack(lua_State* L, int level, lua_Debug* ar)
497 {
498 	assert(g_shimTable != NULL);
499 	return g_shimTable->shim_getstack(L, level, ar);
500 }
501 
lua_getinfo(lua_State * L,const char * what,lua_Debug * ar)502 int lua_getinfo(lua_State* L, const char* what, lua_Debug* ar)
503 {
504 	assert(g_shimTable != NULL);
505 	return g_shimTable->shim_getinfo(L, what, ar);
506 }
507 
lua_getlocal(lua_State * L,const lua_Debug * ar,int n)508 const char* lua_getlocal(lua_State* L, const lua_Debug* ar, int n)
509 {
510 	assert(g_shimTable != NULL);
511 	return g_shimTable->shim_getlocal(L, ar, n);
512 }
513 
lua_setlocal(lua_State * L,const lua_Debug * ar,int n)514 const char* lua_setlocal(lua_State* L, const lua_Debug* ar, int n)
515 {
516 	assert(g_shimTable != NULL);
517 	return g_shimTable->shim_setlocal(L, ar, n);
518 }
519 
lua_getupvalue(lua_State * L,int funcindex,int n)520 const char* lua_getupvalue(lua_State* L, int funcindex, int n)
521 {
522 	assert(g_shimTable != NULL);
523 	return g_shimTable->shim_getupvalue(L, funcindex, n);
524 }
525 
lua_setupvalue(lua_State * L,int funcindex,int n)526 const char* lua_setupvalue(lua_State* L, int funcindex, int n)
527 {
528 	assert(g_shimTable != NULL);
529 	return g_shimTable->shim_setupvalue(L, funcindex, n);
530 }
531 
lua_upvalueid(lua_State * L,int fidx,int n)532 void* lua_upvalueid(lua_State* L, int fidx, int n)
533 {
534 	assert(g_shimTable != NULL);
535 	return g_shimTable->shim_upvalueid(L, fidx, n);
536 }
537 
lua_upvaluejoin(lua_State * L,int fidx1,int n1,int fidx2,int n2)538 void lua_upvaluejoin(lua_State* L, int fidx1, int n1, int fidx2, int n2)
539 {
540 	assert(g_shimTable != NULL);
541 	g_shimTable->shim_upvaluejoin(L, fidx1, n1, fidx2, n2);
542 }
543 
lua_sethook(lua_State * L,lua_Hook func,int mask,int count)544 void lua_sethook(lua_State* L, lua_Hook func, int mask, int count)
545 {
546 	assert(g_shimTable != NULL);
547 	g_shimTable->shim_sethook(L, func, mask, count);
548 }
549 
lua_gethook(lua_State * L)550 lua_Hook lua_gethook(lua_State* L)
551 {
552 	assert(g_shimTable != NULL);
553 	return g_shimTable->shim_gethook(L);
554 }
555 
lua_gethookmask(lua_State * L)556 int lua_gethookmask(lua_State* L)
557 {
558 	assert(g_shimTable != NULL);
559 	return g_shimTable->shim_gethookmask(L);
560 }
561 
lua_gethookcount(lua_State * L)562 int lua_gethookcount(lua_State* L)
563 {
564 	assert(g_shimTable != NULL);
565 	return g_shimTable->shim_gethookcount(L);
566 }
567 
luaL_checkversion_(lua_State * L,lua_Number ver,size_t sz)568 void luaL_checkversion_(lua_State* L, lua_Number ver, size_t sz)
569 {
570 	assert(g_shimTable != NULL);
571 	g_shimTable->shimL_checkversion_(L, ver, sz);
572 }
573 
luaL_getmetafield(lua_State * L,int obj,const char * e)574 int luaL_getmetafield(lua_State* L, int obj, const char* e)
575 {
576 	assert(g_shimTable != NULL);
577 	return g_shimTable->shimL_getmetafield(L, obj, e);
578 }
579 
luaL_callmeta(lua_State * L,int obj,const char * e)580 int luaL_callmeta(lua_State* L, int obj, const char* e)
581 {
582 	assert(g_shimTable != NULL);
583 	return g_shimTable->shimL_callmeta(L, obj, e);
584 }
585 
luaL_tolstring(lua_State * L,int idx,size_t * len)586 const char* luaL_tolstring(lua_State* L, int idx, size_t* len)
587 {
588 	assert(g_shimTable != NULL);
589 	return g_shimTable->shimL_tolstring(L, idx, len);
590 }
591 
luaL_argerror(lua_State * L,int arg,const char * extramsg)592 int luaL_argerror(lua_State* L, int arg, const char* extramsg)
593 {
594 	assert(g_shimTable != NULL);
595 	return g_shimTable->shimL_argerror(L, arg, extramsg);
596 }
597 
luaL_checklstring(lua_State * L,int arg,size_t * l)598 const char* luaL_checklstring(lua_State* L, int arg, size_t* l)
599 {
600 	assert(g_shimTable != NULL);
601 	return g_shimTable->shimL_checklstring(L, arg, l);
602 }
603 
luaL_optlstring(lua_State * L,int arg,const char * def,size_t * l)604 const char* luaL_optlstring(lua_State* L, int arg, const char* def, size_t* l)
605 {
606 	assert(g_shimTable != NULL);
607 	return g_shimTable->shimL_optlstring(L, arg, def, l);
608 }
609 
luaL_checknumber(lua_State * L,int arg)610 lua_Number luaL_checknumber(lua_State* L, int arg)
611 {
612 	assert(g_shimTable != NULL);
613 	return g_shimTable->shimL_checknumber(L, arg);
614 }
615 
luaL_optnumber(lua_State * L,int arg,lua_Number def)616 lua_Number luaL_optnumber(lua_State* L, int arg, lua_Number def)
617 {
618 	assert(g_shimTable != NULL);
619 	return g_shimTable->shimL_optnumber(L, arg, def);
620 }
621 
luaL_checkinteger(lua_State * L,int arg)622 lua_Integer luaL_checkinteger(lua_State* L, int arg)
623 {
624 	assert(g_shimTable != NULL);
625 	return g_shimTable->shimL_checkinteger(L, arg);
626 }
627 
luaL_optinteger(lua_State * L,int arg,lua_Integer def)628 lua_Integer luaL_optinteger(lua_State* L, int arg, lua_Integer def)
629 {
630 	assert(g_shimTable != NULL);
631 	return g_shimTable->shimL_optinteger(L, arg, def);
632 }
633 
luaL_checkstack(lua_State * L,int sz,const char * msg)634 void luaL_checkstack(lua_State* L, int sz, const char* msg)
635 {
636 	assert(g_shimTable != NULL);
637 	g_shimTable->shimL_checkstack(L, sz, msg);
638 }
639 
luaL_checktype(lua_State * L,int arg,int t)640 void luaL_checktype(lua_State* L, int arg, int t)
641 {
642 	assert(g_shimTable != NULL);
643 	g_shimTable->shimL_checktype(L, arg, t);
644 }
645 
luaL_checkany(lua_State * L,int arg)646 void luaL_checkany(lua_State* L, int arg)
647 {
648 	assert(g_shimTable != NULL);
649 	g_shimTable->shimL_checkany(L, arg);
650 }
651 
luaL_newmetatable(lua_State * L,const char * tname)652 int luaL_newmetatable(lua_State* L, const char* tname)
653 {
654 	assert(g_shimTable != NULL);
655 	return g_shimTable->shimL_newmetatable(L, tname);
656 }
657 
luaL_setmetatable(lua_State * L,const char * tname)658 void luaL_setmetatable(lua_State* L, const char* tname)
659 {
660 	assert(g_shimTable != NULL);
661 	g_shimTable->shimL_setmetatable(L, tname);
662 }
663 
luaL_testudata(lua_State * L,int ud,const char * tname)664 void* luaL_testudata(lua_State* L, int ud, const char* tname)
665 {
666 	assert(g_shimTable != NULL);
667 	return g_shimTable->shimL_testudata(L, ud, tname);
668 }
669 
luaL_checkudata(lua_State * L,int ud,const char * tname)670 void* luaL_checkudata(lua_State* L, int ud, const char* tname)
671 {
672 	assert(g_shimTable != NULL);
673 	return g_shimTable->shimL_checkudata(L, ud, tname);
674 }
675 
luaL_where(lua_State * L,int lvl)676 void luaL_where(lua_State* L, int lvl)
677 {
678 	assert(g_shimTable != NULL);
679 	g_shimTable->shimL_where(L, lvl);
680 }
681 
luaL_error(lua_State * L,const char * fmt,...)682 int luaL_error(lua_State* L, const char* fmt, ...)
683 {
684 	va_list argp;
685 	va_start(argp, fmt);
686 	luaL_where(L, 1);
687 	lua_pushvfstring(L, fmt, argp);
688 	va_end(argp);
689 	lua_concat(L, 2);
690 	return lua_error(L);
691 }
692 
luaL_checkoption(lua_State * L,int arg,const char * def,const char * const lst[])693 int luaL_checkoption(lua_State* L, int arg, const char* def, const char* const lst[])
694 {
695 	assert(g_shimTable != NULL);
696 	return g_shimTable->shimL_checkoption(L, arg, def, lst);
697 }
698 
luaL_fileresult(lua_State * L,int stat,const char * fname)699 int luaL_fileresult(lua_State* L, int stat, const char* fname)
700 {
701 	assert(g_shimTable != NULL);
702 	return g_shimTable->shimL_fileresult(L, stat, fname);
703 }
704 
luaL_execresult(lua_State * L,int stat)705 int luaL_execresult(lua_State* L, int stat)
706 {
707 	assert(g_shimTable != NULL);
708 	return g_shimTable->shimL_execresult(L, stat);
709 }
710 
luaL_ref(lua_State * L,int t)711 int luaL_ref(lua_State* L, int t)
712 {
713 	assert(g_shimTable != NULL);
714 	return g_shimTable->shimL_ref(L, t);
715 }
716 
luaL_unref(lua_State * L,int t,int ref)717 void luaL_unref(lua_State* L, int t, int ref)
718 {
719 	assert(g_shimTable != NULL);
720 	g_shimTable->shimL_unref(L, t, ref);
721 }
722 
luaL_loadfilex(lua_State * L,const char * filename,const char * mode)723 int luaL_loadfilex(lua_State* L, const char* filename, const char* mode)
724 {
725 	assert(g_shimTable != NULL);
726 	return g_shimTable->shimL_loadfilex(L, filename, mode);
727 }
728 
luaL_loadbufferx(lua_State * L,const char * buff,size_t sz,const char * name,const char * mode)729 int luaL_loadbufferx(lua_State* L, const char* buff, size_t sz, const char* name, const char* mode)
730 {
731 	assert(g_shimTable != NULL);
732 	return g_shimTable->shimL_loadbufferx(L, buff, sz, name, mode);
733 }
734 
luaL_loadstring(lua_State * L,const char * s)735 int luaL_loadstring(lua_State* L, const char* s)
736 {
737 	assert(g_shimTable != NULL);
738 	return g_shimTable->shimL_loadstring(L, s);
739 }
740 
luaL_newstate()741 lua_State* luaL_newstate()
742 {
743 	assert(g_shimTable != NULL);
744 	return g_shimTable->shimL_newstate();
745 }
746 
luaL_len(lua_State * L,int idx)747 lua_Integer luaL_len(lua_State* L, int idx)
748 {
749 	assert(g_shimTable != NULL);
750 	return g_shimTable->shimL_len(L, idx);
751 }
752 
luaL_gsub(lua_State * L,const char * s,const char * p,const char * r)753 const char* luaL_gsub(lua_State* L, const char* s, const char* p, const char* r)
754 {
755 	assert(g_shimTable != NULL);
756 	return g_shimTable->shimL_gsub(L, s, p, r);
757 }
758 
luaL_setfuncs(lua_State * L,const luaL_Reg * l,int nup)759 void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup)
760 {
761 	assert(g_shimTable != NULL);
762 	g_shimTable->shimL_setfuncs(L, l, nup);
763 }
764 
luaL_getsubtable(lua_State * L,int idx,const char * fname)765 int luaL_getsubtable(lua_State* L, int idx, const char* fname)
766 {
767 	assert(g_shimTable != NULL);
768 	return g_shimTable->shimL_getsubtable(L, idx, fname);
769 }
770 
luaL_traceback(lua_State * L,lua_State * L1,const char * msg,int level)771 void luaL_traceback(lua_State* L, lua_State* L1, const char* msg, int level)
772 {
773 	assert(g_shimTable != NULL);
774 	g_shimTable->shimL_traceback(L, L1, msg, level);
775 }
776 
luaL_requiref(lua_State * L,const char * modname,lua_CFunction openf,int glb)777 void luaL_requiref(lua_State* L, const char* modname, lua_CFunction openf, int glb)
778 {
779 	assert(g_shimTable != NULL);
780 	g_shimTable->shimL_requiref(L, modname, openf, glb);
781 }
782 
luaL_buffinit(lua_State * L,luaL_Buffer * B)783 void luaL_buffinit(lua_State* L, luaL_Buffer* B)
784 {
785 	assert(g_shimTable != NULL);
786 	g_shimTable->shimL_buffinit(L, B);
787 }
788 
luaL_prepbuffsize(luaL_Buffer * B,size_t sz)789 char* luaL_prepbuffsize(luaL_Buffer* B, size_t sz)
790 {
791 	assert(g_shimTable != NULL);
792 	return g_shimTable->shimL_prepbuffsize(B, sz);
793 }
794 
luaL_addlstring(luaL_Buffer * B,const char * s,size_t l)795 void luaL_addlstring(luaL_Buffer* B, const char* s, size_t l)
796 {
797 	assert(g_shimTable != NULL);
798 	g_shimTable->shimL_addlstring(B, s, l);
799 }
800 
luaL_addstring(luaL_Buffer * B,const char * s)801 void luaL_addstring(luaL_Buffer* B, const char* s)
802 {
803 	assert(g_shimTable != NULL);
804 	g_shimTable->shimL_addstring(B, s);
805 }
806 
luaL_addvalue(luaL_Buffer * B)807 void luaL_addvalue(luaL_Buffer* B)
808 {
809 	assert(g_shimTable != NULL);
810 	g_shimTable->shimL_addvalue(B);
811 }
812 
luaL_pushresult(luaL_Buffer * B)813 void luaL_pushresult(luaL_Buffer* B)
814 {
815 	assert(g_shimTable != NULL);
816 	g_shimTable->shimL_pushresult(B);
817 }
818 
luaL_pushresultsize(luaL_Buffer * B,size_t sz)819 void luaL_pushresultsize(luaL_Buffer* B, size_t sz)
820 {
821 	assert(g_shimTable != NULL);
822 	g_shimTable->shimL_pushresultsize(B, sz);
823 }
824 
luaL_buffinitsize(lua_State * L,luaL_Buffer * B,size_t sz)825 char* luaL_buffinitsize(lua_State* L, luaL_Buffer* B, size_t sz)
826 {
827 	assert(g_shimTable != NULL);
828 	return g_shimTable->shimL_buffinitsize(L, B, sz);
829 }
830 
hashpow2(const Table * t,int n)831 static const Node* hashpow2(const Table* t, int n) {
832 	int i = lmod(n, sizenode(t));
833 	return &t->node[i];
834 }
835 
findNode(const Table * t,int key)836 static const Node* findNode(const Table* t, int key) {
837 	const Node* n = hashpow2(t, key);
838 	while (n->i_key.tvk.value_.i != key)
839 	{
840 		int nx = n->i_key.nk.next;
841 		if (nx == 0)
842 			return NULL;
843 		n += nx;
844 	}
845 	return n;
846 }
847 
shimInitialize(lua_State * L)848 void shimInitialize(lua_State* L)
849 {
850 	lua_lock(L);
851 
852 	// Find the 'SHIM' entry in the registry.
853 	const Table* reg = hvalue(&G(L)->l_registry);
854 	const Node* n = findNode(reg, 0x5348494D); // equal to 'SHIM'
855 	assert(n != NULL);
856 
857 	g_shimTable = (const LuaFunctionTable_t*)n->i_val.value_.p;
858 	assert(g_shimTable != NULL);
859 
860 	lua_unlock(L);
861 }
862