Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this file was made by tollepuxis
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include <lua.h>
- #include <lauxlib.h>
- #include <lualib.h>
- #ifdef __cplusplus
- }
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- /* pushes new closure table onto the stack, using closure table at
- * given index as its parent */
- static void lc_newclosuretable(lua_State * L, int idx) {
- lua_newtable(L);
- lua_pushvalue(L,idx);
- lua_rawseti(L,-2,0);
- }
- /* __add metamethod handler.
- * warning: assumes indices in range LUA_REGISTRYINDEX < x < 0 are relative. */
- static void lc_add(lua_State * L, int idxa, int idxb) {
- if (lua_isnumber(L,idxa) && lua_isnumber(L,idxb)) {
- lua_pushnumber(L,lua_tonumber(L,idxa) + lua_tonumber(L,idxb));
- }
- else {
- if (luaL_getmetafield(L,idxa,"__add")||luaL_getmetafield(L,idxb,"__add")) {
- lua_pushvalue(L,idxa < 0 && idxa > LUA_REGISTRYINDEX ? idxa-1 : idxa);
- lua_pushvalue(L,idxb < 0 && idxb > LUA_REGISTRYINDEX ? idxb-2 : idxb);
- lua_call(L,2,1);
- }
- else {
- luaL_error(L, "attempt to perform arithmetic");
- }
- }
- }
- /* gets upvalue with ID varid by consulting upvalue table at index
- * tidx for the upvalue table at given nesting level. */
- static void lc_getupvalue(lua_State * L, int tidx, int level, int varid) {
- if (level == 0) {
- lua_rawgeti(L,tidx,varid);
- }
- else {
- lua_pushvalue(L,tidx);
- while (--level >= 0) {
- lua_rawgeti(L,tidx,0); /* 0 links to parent table */
- lua_remove(L,-2);
- tidx = -1;
- }
- lua_rawgeti(L,-1,varid);
- lua_remove(L,-2);
- }
- }
- static void lc_setupvalue(lua_State * L, int tidx, int level, int varid) {
- if (level == 0) {
- lua_rawseti(L,tidx,varid);
- }
- else {
- lua_pushvalue(L,tidx);
- while(--level >= 0) {
- lua_rawgeti(L,tidx,0); /* 0 links to parent table */
- lua_remove(L,-2);
- tidx = -1;
- }
- lua_insert(L,-2);
- lua_rawseti(L,-2,varid);
- lua_pop(L,1);
- }
- }
- /* function(c) */
- static int lcf5 (lua_State * L) {
- enum { lc_nformalargs = 1 };
- lua_settop(L,1);
- /* if c == "\n" then */
- enum { lc2 = 1 };
- lua_pushliteral(L,"\n");
- const int lc3 = lua_equal(L,1,-1);
- lua_pop(L,1);
- lua_pushboolean(L,lc3);
- const int lc4 = lua_toboolean(L,-1);
- lua_pop(L,1);
- if (lc4) {
- /* line = line + 1 */
- lc_getupvalue(L,lua_upvalueindex(1),0,1);
- lua_pushnumber(L,1);
- lc_add(L,-2,-1);
- lua_remove(L,-2);
- lua_remove(L,-2);
- lc_setupvalue(L,lua_upvalueindex(1),0,1);
- assert(lua_gettop(L) == 1);
- }
- lua_settop(L,lc2);
- assert(lua_gettop(L) == 1);
- return 0;
- }
- /* name: GetLines
- * function(s) */
- static int lcf1_GetLines (lua_State * L) {
- enum { lc_nformalargs = 1 };
- lua_settop(L,1);
- /* local line = 1 */
- lc_newclosuretable(L,lua_upvalueindex(1));
- enum { lc1 = 2 };
- assert((lua_gettop(L) == lc1));
- lua_pushnumber(L,1);
- lua_rawseti(L,lc1,1);
- assert(lua_gettop(L) == 2);
- /* s:gsub(".",function(c)
- * if c == "\n" then line = line + 1 end
- * end) */
- lua_pushvalue(L,1);
- lua_pushliteral(L,"gsub");
- lua_gettable(L,-2);
- lua_insert(L,-2);
- lua_pushliteral(L,".");
- lua_pushvalue(L,lc1);
- lua_pushcclosure(L,lcf5,1);
- lua_call(L,3,0);
- assert(lua_gettop(L) == 2);
- /* return line */
- lc_getupvalue(L,lc1,0,1);
- return 1;
- assert(lua_gettop(L) == 2);
- }
- /* function(n) */
- static int lcf10 (lua_State * L) {
- enum { lc_nformalargs = 1 };
- lua_settop(L,1);
- /* if n ~= "\n" then */
- enum { lc7 = 1 };
- lua_pushliteral(L,"\n");
- const int lc8 = lua_equal(L,1,-1);
- lua_pop(L,1);
- lua_pushboolean(L,lc8);
- lua_pushboolean(L,!(lua_toboolean(L,-1)));
- lua_remove(L,-2);
- const int lc9 = lua_toboolean(L,-1);
- lua_pop(L,1);
- if (lc9) {
- /* k = k + 1 */
- lc_getupvalue(L,lua_upvalueindex(1),0,2);
- lua_pushnumber(L,1);
- lc_add(L,-2,-1);
- lua_remove(L,-2);
- lua_remove(L,-2);
- lc_setupvalue(L,lua_upvalueindex(1),0,2);
- assert(lua_gettop(L) == 1);
- }
- lua_settop(L,lc7);
- assert(lua_gettop(L) == 1);
- return 0;
- }
- /* name: len
- * function(c) */
- static int lcf1_len (lua_State * L) {
- enum { lc_nformalargs = 1 };
- lua_settop(L,1);
- /* local k = 1 */
- lc_newclosuretable(L,lua_upvalueindex(1));
- enum { lc6 = 2 };
- assert((lua_gettop(L) == lc6));
- lua_pushnumber(L,1);
- lua_rawseti(L,lc6,2);
- assert(lua_gettop(L) == 2);
- /* c:gsub('.',function(n)
- * if n ~= "\n" then
- * k = k + 1
- * end
- * end) */
- lua_pushvalue(L,1);
- lua_pushliteral(L,"gsub");
- lua_gettable(L,-2);
- lua_insert(L,-2);
- lua_pushliteral(L,".");
- lua_pushvalue(L,lc6);
- lua_pushcclosure(L,lcf10,1);
- lua_call(L,3,0);
- assert(lua_gettop(L) == 2);
- /* return k */
- lc_getupvalue(L,lc6,0,2);
- return 1;
- assert(lua_gettop(L) == 2);
- }
- /* name: GetChars
- * function(s) */
- static int lcf1_GetChars (lua_State * L) {
- enum { lc_nformalargs = 1 };
- lua_settop(L,1);
- /* local len = function(c)
- * local k = 1
- * c:gsub('.',function(n)
- * if n ~= "\n" then
- * k = k + 1
- * end
- * end)
- * return k
- * end */
- lua_pushcfunction(L,lcf1_len);
- assert(lua_gettop(L) == 2);
- /* return len(s) */
- const int lc11 = lua_gettop(L);
- lua_pushvalue(L,2);
- lua_pushvalue(L,1);
- lua_call(L,1,LUA_MULTRET);
- return (lua_gettop(L) - lc11);
- assert(lua_gettop(L) == 2);
- }
- /* function(n) */
- static int lcf20 (lua_State * L) {
- enum { lc_nformalargs = 1 };
- lua_settop(L,1);
- /* if n=="warn" then */
- enum { lc17 = 1 };
- lua_pushliteral(L,"warn");
- const int lc18 = lua_equal(L,1,-1);
- lua_pop(L,1);
- lua_pushboolean(L,lc18);
- const int lc19 = lua_toboolean(L,-1);
- lua_pop(L,1);
- if (lc19) {
- /* warns=warns +1 */
- lc_getupvalue(L,lua_upvalueindex(1),0,4);
- lua_pushnumber(L,1);
- lc_add(L,-2,-1);
- lua_remove(L,-2);
- lua_remove(L,-2);
- lc_setupvalue(L,lua_upvalueindex(1),0,4);
- assert(lua_gettop(L) == 1);
- }
- lua_settop(L,lc17);
- assert(lua_gettop(L) == 1);
- return 0;
- }
- /* name: callend
- * function() */
- static int lcf1_callend (lua_State * L) {
- enum { lc_nformalargs = 0 };
- lua_settop(L,0);
- /* -- find all 'Warn' statements
- * -- find all 'Error' statements
- * src:gsub("%w+",function(n)
- * if n=="warn" then
- * warns=warns +1
- * end
- * end) */
- lc_getupvalue(L,lua_upvalueindex(1),1,3);
- lua_pushliteral(L,"gsub");
- lua_gettable(L,-2);
- lua_insert(L,-2);
- lua_pushliteral(L,"%w+");
- lua_pushvalue(L,lua_upvalueindex(1));
- lua_pushcclosure(L,lcf20,1);
- lua_call(L,3,0);
- assert(lua_gettop(L) == 0);
- /* print("Executed with") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"Executed with");
- lua_call(L,1,0);
- assert(lua_gettop(L) == 0);
- /* print(errors.." error(s)") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lc_getupvalue(L,lua_upvalueindex(1),0,5);
- lua_pushliteral(L," error(s)");
- lua_concat(L,2);
- lua_call(L,1,0);
- assert(lua_gettop(L) == 0);
- /* print(warns.." warn(s)") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lc_getupvalue(L,lua_upvalueindex(1),0,4);
- lua_pushliteral(L," warn(s)");
- lua_concat(L,2);
- lua_call(L,1,0);
- assert(lua_gettop(L) == 0);
- /* print("(This is only speculation)") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"(This is only speculation)");
- lua_call(L,1,0);
- assert(lua_gettop(L) == 0);
- return 0;
- }
- /* function(...) */
- static int lcf23 (lua_State * L) {
- enum { lc_nformalargs = 0 };
- const int lc_nactualargs = lua_gettop(L);
- #ifndef NDEBUG
- const int lc_nextra = (lc_nactualargs - lc_nformalargs);
- #endif
- /* return ... */
- const int lc22 = lua_gettop(L);
- {int i; for (i=lc_nformalargs+1; i<=lc_nactualargs; i++) { lua_pushvalue(L, i); }}
- return (lua_gettop(L) - lc22);
- assert(lua_gettop(L) - lc_nextra == 0);
- }
- /* function(...) */
- static int lcf25 (lua_State * L) {
- enum { lc_nformalargs = 0 };
- const int lc_nactualargs = lua_gettop(L);
- #ifndef NDEBUG
- const int lc_nextra = (lc_nactualargs - lc_nformalargs);
- #endif
- /* return ... */
- const int lc24 = lua_gettop(L);
- {int i; for (i=lc_nformalargs+1; i<=lc_nactualargs; i++) { lua_pushvalue(L, i); }}
- return (lua_gettop(L) - lc24);
- assert(lua_gettop(L) - lc_nextra == 0);
- }
- /* function() */
- static int lcf31 (lua_State * L) {
- enum { lc_nformalargs = 0 };
- lua_settop(L,0);
- /* local func = loadstring(src) */
- lua_getfield(L,LUA_ENVIRONINDEX,"loadstring");
- lc_getupvalue(L,lua_upvalueindex(1),2,3);
- lua_call(L,1,1);
- assert(lua_gettop(L) == 1);
- /* if func == nil then */
- enum { lc26 = 1 };
- lua_pushnil(L);
- const int lc27 = lua_equal(L,1,-1);
- lua_pop(L,1);
- lua_pushboolean(L,lc27);
- const int lc28 = lua_toboolean(L,-1);
- lua_pop(L,1);
- if (lc28) {
- /* errors = errors + 1 */
- lc_getupvalue(L,lua_upvalueindex(1),1,5);
- lua_pushnumber(L,1);
- lc_add(L,-2,-1);
- lua_remove(L,-2);
- lua_remove(L,-2);
- lc_setupvalue(L,lua_upvalueindex(1),1,5);
- assert(lua_gettop(L) == 1);
- }
- lua_settop(L,lc26);
- assert(lua_gettop(L) == 1);
- /* assert(not (type(func) == nil), "Syntax Parse Failure") */
- lua_getfield(L,LUA_ENVIRONINDEX,"assert");
- lua_getfield(L,LUA_ENVIRONINDEX,"type");
- lua_pushvalue(L,1);
- lua_call(L,1,1);
- lua_pushnil(L);
- const int lc29 = lua_equal(L,-2,-1);
- lua_pop(L,2);
- lua_pushboolean(L,lc29);
- lua_pushboolean(L,!(lua_toboolean(L,-1)));
- lua_remove(L,-2);
- lua_pushliteral(L,"Syntax Parse Failure");
- lua_call(L,2,0);
- assert(lua_gettop(L) == 1);
- /* setfenv(func, setmetatable(env, {__index = getfenv()})) */
- lua_getfield(L,LUA_ENVIRONINDEX,"setfenv");
- const int lc30 = lua_gettop(L);
- lua_pushvalue(L,1);
- lua_getfield(L,LUA_ENVIRONINDEX,"setmetatable");
- lc_getupvalue(L,lua_upvalueindex(1),0,6);
- lua_createtable(L,0,1);
- lua_pushliteral(L,"__index");
- lua_getfield(L,LUA_ENVIRONINDEX,"getfenv");
- lua_call(L,0,1);
- lua_rawset(L,-3);
- lua_call(L,2,LUA_MULTRET);
- lua_call(L,(lua_gettop(L) - lc30),0);
- assert(lua_gettop(L) == 1);
- return 0;
- }
- /* name: build.BuildLua
- * function(src) */
- static int lcf1_build_BuildLua (lua_State * L) {
- enum { lc_nformalargs = 1 };
- lua_settop(L,1);
- lc_newclosuretable(L,lua_upvalueindex(1));
- enum { lc12 = 2 };
- assert((lua_gettop(L) == lc12));
- lua_pushvalue(L,1);
- lua_rawseti(L,-2,3);
- /* if GetChars(src) < 2 then */
- enum { lc13 = 2 };
- lua_getfield(L,LUA_ENVIRONINDEX,"GetChars");
- lc_getupvalue(L,lc12,0,3);
- lua_call(L,1,1);
- lua_pushnumber(L,2);
- const int lc14 = lua_lessthan(L,-2,-1);
- lua_pop(L,2);
- lua_pushboolean(L,lc14);
- const int lc15 = lua_toboolean(L,-1);
- lua_pop(L,1);
- if (lc15) {
- /* print("=== CALL ERROR ===") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"=== CALL ERROR ===");
- lua_call(L,1,0);
- assert(lua_gettop(L) == 2);
- /* print("Script Source must be above 2 characters") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"Script Source must be above 2 characters");
- lua_call(L,1,0);
- assert(lua_gettop(L) == 2);
- /* print("=== CALL ERROR ===") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"=== CALL ERROR ===");
- lua_call(L,1,0);
- assert(lua_gettop(L) == 2);
- /* return false */
- lua_pushboolean(L,0);
- return 1;
- assert(lua_gettop(L) == 2);
- }
- lua_settop(L,lc13);
- assert(lua_gettop(L) == 2);
- /* print("Building Lua...") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"Building Lua...");
- lua_call(L,1,0);
- assert(lua_gettop(L) == 2);
- /* local lines = GetLines(src) */
- lua_getfield(L,LUA_ENVIRONINDEX,"GetLines");
- lc_getupvalue(L,lc12,0,3);
- lua_call(L,1,1);
- assert(lua_gettop(L) == 3);
- /* local chars = GetChars(src) */
- lua_getfield(L,LUA_ENVIRONINDEX,"GetChars");
- lc_getupvalue(L,lc12,0,3);
- lua_call(L,1,1);
- assert(lua_gettop(L) == 4);
- /* print("==========") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"==========");
- lua_call(L,1,0);
- assert(lua_gettop(L) == 4);
- /* print("Lines >> ".. lines) */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"Lines >> ");
- lua_pushvalue(L,3);
- lua_concat(L,2);
- lua_call(L,1,0);
- assert(lua_gettop(L) == 4);
- /* print("Characters >> ".. chars) */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"Characters >> ");
- lua_pushvalue(L,4);
- lua_concat(L,2);
- lua_call(L,1,0);
- assert(lua_gettop(L) == 4);
- /* local errors,warns = 0,0 */
- lc_newclosuretable(L,lc12);
- enum { lc16 = 5 };
- assert((lua_gettop(L) == lc16));
- lua_pushnumber(L,0);
- lua_pushnumber(L,0);
- lua_rawseti(L,lc16,4);
- lua_rawseti(L,lc16,5);
- assert(lua_gettop(L) == 5);
- /* local function callend()
- * -- find all 'Warn' statements
- * -- find all 'Error' statements
- * src:gsub("%w+",function(n)
- * if n=="warn" then
- * warns=warns +1
- * end
- * end)
- * print("Executed with")
- * print(errors.." error(s)")
- * print(warns.." warn(s)")
- * print("(This is only speculation)")
- * end */
- lua_pushvalue(L,lc16);
- lua_pushcclosure(L,lcf1_callend,1);
- assert(lua_gettop(L) == 6);
- /* --local result,err = pcall(function()
- * local env = {
- * print = function(...)
- * return ...
- * end,
- * warn = function(...)
- * return ...
- * end
- * } */
- lc_newclosuretable(L,lc16);
- enum { lc21 = 7 };
- assert((lua_gettop(L) == lc21));
- lua_createtable(L,0,2);
- lua_pushliteral(L,"print");
- lua_pushcfunction(L,lcf23);
- lua_rawset(L,-3);
- lua_pushliteral(L,"warn");
- lua_pushcfunction(L,lcf25);
- lua_rawset(L,-3);
- lua_rawseti(L,lc21,6);
- assert(lua_gettop(L) == 7);
- /* local res,err = pcall(function()
- * local func = loadstring(src)
- * if func == nil then errors = errors + 1 end
- * assert(not (type(func) == nil), "Syntax Parse Failure")
- * setfenv(func, setmetatable(env, {__index = getfenv()}))
- * end) */
- lua_getfield(L,LUA_ENVIRONINDEX,"pcall");
- lua_pushvalue(L,lc21);
- lua_pushcclosure(L,lcf31,1);
- lua_call(L,1,2);
- assert(lua_gettop(L) == 9);
- /* --end)
- * if result == false then */
- enum { lc32 = 9 };
- lua_getfield(L,LUA_ENVIRONINDEX,"result");
- lua_pushboolean(L,0);
- const int lc33 = lua_equal(L,-2,-1);
- lua_pop(L,2);
- lua_pushboolean(L,lc33);
- const int lc34 = lua_toboolean(L,-1);
- lua_pop(L,1);
- if (lc34) {
- /* callend() */
- lua_pushvalue(L,6);
- lua_call(L,0,0);
- assert(lua_gettop(L) == 9);
- /* print(err) */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushvalue(L,9);
- lua_call(L,1,0);
- assert(lua_gettop(L) == 9);
- /* print("==========") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"==========");
- lua_call(L,1,0);
- assert(lua_gettop(L) == 9);
- /* return false,err */
- lua_pushboolean(L,0);
- lua_pushvalue(L,9);
- return 2;
- assert(lua_gettop(L) == 9);
- }
- else {
- /* else
- * callend() */
- lua_pushvalue(L,6);
- lua_call(L,0,0);
- assert(lua_gettop(L) == 9);
- /* print("==========") */
- lua_getfield(L,LUA_ENVIRONINDEX,"print");
- lua_pushliteral(L,"==========");
- lua_call(L,1,0);
- assert(lua_gettop(L) == 9);
- /* return true,src */
- lua_pushboolean(L,1);
- lc_getupvalue(L,lc21,2,3);
- return 2;
- assert(lua_gettop(L) == 9);
- }
- lua_settop(L,lc32);
- assert(lua_gettop(L) == 9);
- return 0;
- }
- /* name: (main)
- * function(...) */
- static int lcf_main (lua_State * L) {
- enum { lc_nformalargs = 0 };
- const int lc_nactualargs = lua_gettop(L);
- const int lc_nextra = (lc_nactualargs - lc_nformalargs);
- /* local build = {} */
- lua_newtable(L);
- assert(lua_gettop(L) - lc_nextra == 1);
- /* local binary = require("binary") */
- lua_getfield(L,LUA_ENVIRONINDEX,"require");
- lua_pushliteral(L,"binary");
- lua_call(L,1,1);
- assert(lua_gettop(L) - lc_nextra == 2);
- /* function GetLines(s)
- * local line = 1
- * s:gsub(".",function(c)
- * if c == "\n" then line = line + 1 end
- * end)
- * return line
- * end */
- lua_pushcfunction(L,lcf1_GetLines);
- lua_setfield(L,LUA_ENVIRONINDEX,"GetLines");
- assert(lua_gettop(L) - lc_nextra == 2);
- /* function GetChars(s)
- * local len = function(c)
- * local k = 1
- * c:gsub('.',function(n)
- * if n ~= "\n" then
- * k = k + 1
- * end
- * end)
- * return k
- * end
- * return len(s)
- * end */
- lua_pushcfunction(L,lcf1_GetChars);
- lua_setfield(L,LUA_ENVIRONINDEX,"GetChars");
- assert(lua_gettop(L) - lc_nextra == 2);
- /* function build.BuildLua(src)
- * if GetChars(src) < 2 then
- * print("=== CALL ERROR ===")
- * print("Script Source must be above 2 characters")
- * print("=== CALL ERROR ===")
- * return false
- * end
- * print("Building Lua...")
- * local lines = GetLines(src)
- * local chars = GetChars(src)
- * print("==========")
- * print("Lines >> ".. lines)
- * print("Characters >> ".. chars)
- * local errors,warns = 0,0
- * local function callend()
- * -- find all 'Warn' statements
- * -- find all 'Error' statements
- * src:gsub("%w+",function(n)
- * if n=="warn" then
- * warns=warns +1
- * end
- * end)
- * print("Executed with")
- * print(errors.." error(s)")
- * print(warns.." warn(s)")
- * print("(This is only speculation)")
- * end
- * --local result,err = pcall(function()
- * local env = {
- * print = function(...)
- * return ...
- * end,
- * warn = function(...)
- * return ...
- * end
- * }
- * local res,err = pcall(function()
- * local func = loadstring(src)
- * if func == nil then errors = errors + 1 end
- * assert(not (type(func) == nil), "Syntax Parse Failure")
- * setfenv(func, setmetatable(env, {__index = getfenv()}))
- * end)
- * --end)
- * if result == false then
- * callend()
- * print(err)
- * print("==========")
- * return false,err
- * else
- * callend()
- * print("==========")
- * return true,src
- * end
- * end */
- lua_pushcfunction(L,lcf1_build_BuildLua);
- lua_pushliteral(L,"BuildLua");
- lua_insert(L,-2);
- lua_settable(L,(1 + lc_nextra));
- assert(lua_gettop(L) - lc_nextra == 2);
- /* return build */
- lua_pushvalue(L,(1 + lc_nextra));
- return 1;
- assert(lua_gettop(L) - lc_nextra == 2);
- }
- /* from lua.c */
- static int traceback (lua_State *L) {
- if (!lua_isstring(L, 1)) /* 'message' not a string? */
- return 1; /* keep it intact */
- lua_getfield(L, LUA_GLOBALSINDEX, "debug");
- if (!lua_istable(L, -1)) {
- lua_pop(L, 1);
- return 1;
- }
- lua_getfield(L, -1, "traceback");
- if (!lua_isfunction(L, -1)) {
- lua_pop(L, 2);
- return 1;
- }
- lua_pushvalue(L, 1); /* pass error message */
- lua_pushinteger(L, 2); /* skip this function and traceback */
- lua_call(L, 2, 1); /* call debug.traceback */
- return 1;
- }
- static void lc_l_message (const char *pname, const char *msg) {
- if (pname) fprintf(stderr, "%s: ", pname);
- fprintf(stderr, "%s\n", msg);
- fflush(stderr);
- }
- static int lc_report (lua_State *L, int status) {
- if (status && !lua_isnil(L, -1)) {
- const char *msg = lua_tostring(L, -1);
- if (msg == NULL) msg = "(error object is not a string)";
- /*FIX-IMROVE:progname*/
- lc_l_message("lua", msg);
- lua_pop(L, 1);
- }
- return status;
- }
- static int lc_docall (lua_State *L, int narg, int clear) {
- int status;
- int base = lua_gettop(L) - narg; /* function index */
- lua_pushcfunction(L, traceback); /* push traceback function */
- lua_insert(L, base); /* put it under chunk and args */
- /*FIX? signal(SIGINT, laction); */
- status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
- /*FIX? signal(SIGINT, SIG_DFL); */
- lua_remove(L, base); /* remove traceback function */
- /* force a complete garbage collection in case of errors */
- if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
- return status;
- }
- static int lc_dofile (lua_State *L, const char *name) {
- int status = luaL_loadfile(L, name) || lc_docall(L, 0, 1);
- return lc_report(L, status);
- }
- static int lc_dostring (lua_State *L, const char *s, const char *name) {
- int status = luaL_loadbuffer(L, s, strlen(s), name) || lc_docall(L, 0, 1);
- return lc_report(L, status);
- }
- static int lc_handle_luainit (lua_State *L) {
- const char *init = getenv(LUA_INIT);
- if (init == NULL) return 0; /* status OK */
- else if (init[0] == '@')
- return lc_dofile(L, init+1);
- else
- return lc_dostring(L, init, "=" LUA_INIT);
- }
- typedef struct {
- int c;
- const char ** v;
- } lc_args_t;
- /* create global arg table */
- static void lc_createarg(lua_State * L, const lc_args_t * const args) {
- int i;
- lua_newtable(L);
- for (i=0; i < args->c; i++) {
- lua_pushstring(L, args->v[i]);
- lua_rawseti(L, -2, i);
- }
- lua_setglobal(L, "arg");
- }
- static int lc_pmain(lua_State * L) {
- luaL_openlibs(L);
- const lc_args_t * const args = (lc_args_t*)lua_touserdata(L, 1);
- lc_createarg(L, args);
- lua_pushcfunction(L, traceback);
- const int status1 = lc_handle_luainit(L);
- if (status1 != 0) return 0;
- /* note: IMPROVE: closure not always needed here */
- lua_newtable(L); /* closure table */
- lua_pushcclosure(L, lcf_main, 1);
- int i;
- for (i=1; i < args->c; i++) {
- lua_pushstring(L, args->v[i]);
- }
- int status2 = lua_pcall(L, args->c-1, 0, -2);
- if (status2 != 0) {
- const char * msg = lua_tostring(L,-1);
- if (msg == NULL) msg = "(error object is not a string)";
- fputs(msg, stderr);
- }
- return 0;
- }
- int main(int argc, const char ** argv) {
- lc_args_t args = {argc, argv};
- lua_State * L = luaL_newstate();
- if (! L) { fputs("Failed creating Lua state.", stderr); exit(1); }
- int status = lua_cpcall(L, lc_pmain, &args);
- if (status != 0) {
- fputs(lua_tostring(L,-1), stderr);
- }
- lua_close(L);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement