天天看點

apolloxlua标準庫 require 函數說明

使用方式

require("檔案位址")

在apollox實體設計上,支援lua的require子產品方式,require方法可以在web模式和tool方式使用。 本文簡單介紹一下,在web模式下的配合vfs的使用。

require的具體細節和lua的實作方式類似, 子產品作為程式的最小單元存在,子產品與子產品之間的關系,應該是隔離的。 在web模式下使用vfs組織子產品查找的路徑。

使用require在某種情況下會有限制,他們分别是如果子產品的文法存在錯誤,将無法交織到子產品的代碼抛出錯誤。 如果vfs裡并沒有該子產品的平坦模式的代碼, 會抛出錯誤。如果vfs配置了baseURL,一般vfs在記憶體無法查找到該檔案将會根據baseURL的路徑進行遠端加載該子產品。

一個簡單示例的vfs的視圖

apolloxlua标準庫 require 函數說明

lua_module.lua 的代碼如下

--請注意這個代碼在web console示例程式中是無法執行的。
--這是一個lua的new子產品,module case 裡使用

local m = {}

local hellow  = function () 
    print("hellow, i am a module method");
end

m.hellow = hellow;

return m;


           

lua_duplicatedef.lua 的代碼如下

--請注意這個代碼在web console示例程式中是無法執行的。
--這是一個lua的new子產品,module case 裡使用

local other = require("build/lua_module.lua")
local m = {}

local hellow  = function () 
    print("hellow, i am duplicate def");
end

m.hellow = hellow;
m.other  = other.hellow;
return m;

           

module include case 的代碼如下

//////////
/// 子產品測試
/////////
var module = require("build/lua_module.lua");

if(module) {
    module.hellow();
}

var module2 = require("build/lua_duplicatedef.lua");

if(module) {
    module2.hellow();
    module2.other();
}
           

執行結果:

apolloxlua标準庫 require 函數說明