-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluatoc.lua
64 lines (56 loc) · 1.59 KB
/
luatoc.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
local out = {[[// Generated file, see luatoc.lua
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
static char code[] = "\n\n\n\n\n"
]]}
local dep = {}
local function mod_from_fname(fname)
return string.gsub(string.sub(fname, 1, -5), "/", ".")
end
local function add_module(fname, modname)
local run = false
if string.sub(fname, 1, 1) == '+' then
run = true
fname = string.sub(fname, 2)
end
dep[#dep+1] = fname
if run then
out[#out+1] = ' "do\\n"\n'
else
modname = modname or mod_from_fname(fname)
out[#out+1] = ' "package.preload[\\"'..modname..'\\"] = function (...)\\n"\n'
end
for line in io.lines(fname) do
out[#out+1] = ' " '
out[#out+1] = string.gsub(line, "[\\\"]", { ['\\'] = '\\\\', ['"'] = '\\"' })
out[#out+1] = '\\n"\n'
end
out[#out+1] = ' "end\\n"\n'
end
local func_name = table.remove(arg, 1)
dep[#dep+1] = string.format("%s.c:", func_name)
for i,name in ipairs(arg) do
-- strip directory names
local modname = mod_from_fname(string.match(name, "([^/]*)$"))
add_module(name, modname)
end
out[#out+1] = [[ ;
int ]]..func_name..[[ (lua_State *L)
{
int n = lua_gettop(L);
if (luaL_loadbuffer(L, code, sizeof(code) - 1, "@]]..func_name..[[.c")) return lua_error(L);
lua_insert(L, 1);
lua_call(L, n, 0);
return 0;
}
]]
local f = assert(io.open(func_name .. '.c', 'w'))
assert(f:write(table.concat(out)))
assert(f:close())
local f = assert(io.open('.' .. func_name .. '.d', 'w'))
assert(f:write(table.concat(dep, ' ')..'\n'))
table.remove(dep, 1)
dep[#dep+1] = ''
assert(f:write(table.concat(dep, ':\n')))
assert(f:close())