diff --git a/tcc.lua b/tcc.lua index a6e43d7..0188803 100644 --- a/tcc.lua +++ b/tcc.lua @@ -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 @@ -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) @@ -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