且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

apolloxlua标准库 require 函数说明

更新时间:2022-08-13 23:22:18

使用方式

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 函数说明