|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- -*- coding: raw-text-unix -*-
-- For the luaopen_xxx convention, see: PiL 2nd ed, p.244.
-- «.demo» (to "demo")
ctemplate = [[
// Generated by: (find-angg "LUA/ctemplate.lua")
#include <lua.h>
#include <lauxlib.h>
#include <stdio.h>
{extras}
static int lua_{name}(lua_State* L) {
{body}
}
LUALIB_API int luaopen_{name}(lua_State *L) {
lua_register(L, "{name}", lua_{name});
return 0;
}
]]
ccompile = function (T)
T.extras = T.extras or ""
local replacements = {name=T.name, body=T.body, extras=T.extras or ""}
T.ccode = ee_template(replacements, ctemplate)
T.init = "luaopen_"..T.name
T.dir = T.dir or "/tmp/"
T.cfname = T.dir..T.name..".c"
T.sofname = T.dir..T.name..".so"
writefile(T.cfname, T.ccode)
T.lua51src = T.lua51src or ee_expand "$LUA51SRC/src"
T.compile = "rm -v " .. T.sofname .. "; " ..
"gcc -g -Wall -shared -I".. T.lua51src ..
" -o " .. T.sofname .. " " .. T.cfname .. "; " ..
"ls -lF " .. T.sofname .. " " .. T.cfname
T.output = getoutput(T.compile)
T.load = function (T)
if _G[T.name] then error("_G."..T.name.." exists - cannot reload") end
ee_loadlib(T.sofname, T.init)
return T
end
return T
end
cloadfoo = function () -- hackish: uses a fixed name ("foo") and lots of globals
T = ccompile {name="foo", body=cbody, extra=cextra}
T:load()
print(T.output)
return T
end
--[=[
-- «demo» (to ".demo")
-- (find-es "lua5" "ctemplate")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
ee_dofile "~/LUA/ctemplate.lua" -- (find-angg "LUA/ctemplate.lua")
T = ccompile {name="foo", body=[[printf("Hello!\n"); return 0;]]}
= T.ccode
= T.output
= T.init
T:load()
foo()
T = ccompile {name="foo", body=[[printf("Hello 2!\n"); return 0;]]}
T:load()
--]=]