二進制模組載入器

lua-users home
wiki

以下是一個與 Lua 提供的獨立載入器相同的載入二進制模組的模組載入器範例。你可以使用它來實作更複雜的載入器。(JeromeVuarand

module(..., package.seeall)

local function load(modulename)
  local errmsg = ""
  -- Find DLL
  local symbolname = string.gsub(modulename, "%.", "_")
  local modulepath = string.gsub(modulename, "%.", "/")
  for path in string.gmatch(package.cpath, "([^;]+)") do
    local filename = string.gsub(path, "%?", modulepath)
    local file = io.open(filename, "rb")
    if file then
      file:close()
      -- Load and return the module loader
      local loader,msg = package.loadlib(filename, "luaopen_"..symbolname)
      if not loader then
        error("error loading module '"..modulename.."' from file '"..path.."':\n\t"..msg, 3)
      end
      return loader
    end
    errmsg = errmsg.."\n\tno file '"..filename.."' (checked with custom loader)"
  end
  return errmsg
end

-- Install the loader so that it's called just before the normal binary loader
table.insert(package.loaders, 3, load)


本頁的術語需要改進。這是一個傳回「載入器」[1] 的「搜索函式」。 (無奈的是 package.loaders 沒有命名為 package.searchers。)另外,「二進制模組」一詞可能會錯誤暗示 Lua 位元組碼檔案;我建議使用參考手冊中指出的「C 函式庫」(或「共用函式庫」)。因此,我建議將頁面重新命名為「CeeLibrarySearcherFunctionInLua?」(本 wiki 目前還有四個其它「*InLua?」頁面)。 --DavidManura

RecentChanges · 偏好設定
編輯 · 歷程
最後編輯於 2009 年 9 月 6 日, 週日 上午 4:01 GMT (diff)