Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions tcc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tcc.clib = nil
--local tcc = require("tcc").load("tcc", "/lib/tcc")
function tcc.load(lib, home_path)
if home_path then
tcc.home_path = tccdir
tcc.home_path = home_path
else
tcc.home_path = os.getenv("CONFIG_TCCDIR") or tcc.home_path
end
Expand Down Expand Up @@ -78,11 +78,16 @@ function tcc.load(lib, home_path)
int tcc_relocate(TCCState *s1, void *ptr);
void *tcc_get_symbol(TCCState *s, const char *name);
]])
ffi.metatype("TCCState", tcc.State)
-- ffi.metatype fails on the second call: once a metatype is set it becomes
-- protected and cannot be changed ("cannot change a protected metatable").
-- Guard it so tcc.load() is idempotent and can be called more than once
-- (e.g. from several modules or after a failed first load).
pcall(ffi.metatype, "TCCState", tcc.State)

local clib
if tcc.home_path then
clib = pcall(ffi.load(("%s/%s"):format(tcc.home_path, lib)))
local ok_load, lib_handle = pcall(ffi.load, ("%s/%s"):format(tcc.home_path, lib))
clib = ok_load and lib_handle or nil
end
if not clib then
clib = ffi.load(lib)
Expand All @@ -102,10 +107,10 @@ end
function tcc.new(add_paths)
local state = tcc.clib.tcc_new()
ffi.gc(state, tcc.State.__gc)
if addpaths ~= false and tcc.tccdir then
state:set_home_path(tcc.tccdir)
state:add_sysinclude_path(tcc.tccdir .. "/include")
state:add_library_path(tcc.tccdir .. "/lib")
if add_paths ~= false and tcc.home_path then
state:set_home_path(tcc.home_path)
state:add_sysinclude_path(tcc.home_path .. "/include")
state:add_library_path(tcc.home_path .. "/lib")
end
return state
end
Expand Down