jit.gl.lua で複数ファイル分割したい!という分けで調べてみました。Dog.lua は、こちらのサイトのソースを流用させて頂きました。
Main.lua
[javascript]
–この関数によってMain.luaのpathに置いてある .luaファイルもrequireで読み込めるようになる
function addmodulepath(path)
— add to package paths (if not already present)
if not string.find(package.path, path, 0, true) then
package.path = string.format("%s/?.lua;%s", path, package.path)
package.path = string.format("%s/?/init.lua;%s", path, package.path)
package.cpath = string.format("%s/?.so;%s", path, package.cpath)
end
end
— Main.lua のファイルパスを登録
addmodulepath(this.path)
— Dog Class のロード
require("Dog")
–インスタンス化
jiro = Dog.new("jiro", 3)
sabu = Dog.new("sabu", 2)
jiro:showProfile()
sabu:showProfile()
[/javascript]
Dog.lua
[javascript]
— Dog クラスを定義
Dog = {}
Dog.new = function(name, age)
local obj = {}
obj.name = name
obj.age = age
obj.showProfile = function(self) s = string.format("[name=%s,age=%d]", self.name, self.age) print(s) end
return obj end
[/javascript]