/*- Author: Brian Tiffin Dedicated to the public domain Date: November 2016 Modified: 2016-11-14/15:34-0500 +*/ /* unilua.c loadfunc a Lua interpreter in Unicon tectonics: gcc -o unilua.so -shared -fpic unilua.c \ -I/usr/include/lua5.3 -llua5.3 */ #include #include #include #include #include #include "icall.h" lua_State *uniLuaState; /* unilua: execute Lua code from Unicon string */ int unilua (int argc, descriptor argv[]) { int error; char *unibuf; int luaType; if (!uniLuaState) { #ifdef LUA50 uniLuaState = lua_open(); /* opens Lua */ if (!uniLuaState) { Error(500); } luaopen_base(uniLuaState); /* opens the basic library */ luaopen_table(uniLuaState); /* opens the table library */ luaopen_io(uniLuaState); /* opens the I/O library */ luaopen_string(uniLuaState); /* opens the string lib. */ luaopen_math(uniLuaState); /* opens the math lib. */ #else uniLuaState = luaL_newstate(); if (!uniLuaState) { Error(500); } luaL_openlibs(uniLuaState); #endif } /* ensure argv[1] is a string */ ArgString(1); /* evaluate some Lua */ unibuf = StringVal(argv[1]); error = luaL_loadbuffer(uniLuaState, unibuf, strlen(unibuf), "line") || luaL_dostring(uniLuaState, unibuf); if (error) { fprintf(stderr, "%s", lua_tostring(uniLuaState, -1)); lua_pop(uniLuaState, 1); /* pop error message from the stack */ Error(107); } luaType = lua_type(uniLuaState, -1); switch (luaType) { case LUA_TSTRING: RetString((char *)lua_tostring(uniLuaState, -1)); break; case LUA_TNUMBER: if (lua_isinteger(uniLuaState, -1)) { RetInteger(lua_tointeger(uniLuaState, -1)); } else { RetReal(lua_tonumber(uniLuaState, -1)); } break; default: RetString((char *)lua_typename(uniLuaState, luaType)); break; } return 0; } /* Close Lua state */ int uniluaClose(int argc, descriptor argv[]) { lua_close(uniLuaState); RetNull(); return 0; }