- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个小的 C 程序,它有一个字符串,它必须代表一个 Lua 模块,它看起来像这样:
const char *lua_str = " local mymodule = {} \
function mymodule.foo() \
print(\"Hello World!\") \
end
return mymodule";
或者可能使用旧方法(如果需要):
const char *lua_str = "module(\"mymodule\", package.seeall \
function foo() \
print(\"Hello World!\") \
end";
让我们假设这是我的小型主机应用程序:
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main(int argc, char** argv)
{
lua_State *L = lua_open();
luaL_openlibs(L);
luaL_dostring(L, lua_str);
luaL_dofile(L, "test.lua");
return 0;
}
现在在 test.lua 中可以使用那个不是由文件名决定的 static 名称的模块:
local mymodule = require "mymodule"
mymodule.foo()
基本上,我需要执行该字符串并为其指定一个代表实际模块名称的自定义名称。目前名称由文件名决定,我不希望这样。
最佳答案
如果您查看 documentation for require
:
Loads the given module. The function starts by looking into the package.loaded table to determine whether modname is already loaded. If it is, then require returns the value stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.
To find a loader, require is guided by the package.loaders array. By changing this array, we can change how require looks for a module. The following explanation is based on the default configuration for package.loaders.
First require queries package.preload[modname]. If it has a value, this value (which should be a function) is the loader. Otherwise require searches for a Lua loader using the path stored in package.path. If that also fails, it searches for a C loader using the path stored in package.cpath. If that also fails, it tries an all-in-one loader (see package.loaders).
Once a loader is found, require calls the loader with a single argument, modname. If the loader returns any value, require assigns the returned value to package.loaded[modname]. If the loader returns no value and has not assigned any value to package.loaded[modname], then require assigns true to this entry. In any case, require returns the final value of package.loaded[modname].
If there is any error loading or running the module, or if it cannot find any loader for the module, then require signals an error.
您会看到它详细解释了 require
使用什么方法来查找给定模块名称的代码。该解释中隐含了关于如何将任意 block 已加载(或可加载)代码分配给您想要的任何给定名称的指示。
具体来说,如果您在 package.loaded[modname]
中设置一个值,该值将立即返回。如果做不到这一点,package.preload[modname]
将用作加载程序(这是一个采用模块名称的函数)。
关于C - LuaJit 将自定义模块名称分配给已编译的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25676920/
在 official guide 之后的一段时间里,我一直在尝试在 Windows 10 上安装 LuaJIT。 ,我实际上开始安装它。例如,如果我执行 luajit我进入提示。另外,luajit -
require "utils.lua" stdin:1: module 'utils.lua' not found: no field package.preload['utils.l
我正在尝试使用 setfenv 对一些函数进行沙盒处理,我收到以下输出: 123 nil 为什么调用sandboxTest()时testValue是nil,而在callSandboxedTest()中
只是来自“Lua 新手”的一个小问题......我一直在使用 LuaJIT,它很棒,没有问题是因为 LuaJIT 是 Lua 5.1 兼容的,这是否意味着我可以使用标准 Lua 在 LuaJIT 中使
教程部分中的示例:“为 C 类型定义元方法” 如下所示: local ffi = require("ffi") ffi.cdef[[ typedef struct { double x, y; } p
我想用 C 编写一些函数以在 Lua 中使用,我认为我能找到的最简单的方法是使用 LuaJIT 的 FFI。 我有一个 C 文件“add.c”: int add(int a, int b){ retu
我正在寻找有关如何为 LuaJIT 2 优化 Lua 代码的好指南.它应该关注 LJ2 的细节,比如如何检测哪些跟踪正在被编译,哪些不是,等等。 任何指针?收集到 Lua ML 帖子的链接可以作为答案
我正在尝试优化我的 LuaJIT 代码,我想知道是否有一个调试工具,或者我是否可以编写一个,来检查我的脚本访问全局变量/表/函数的次数? 最佳答案 您可以使用代理表来存储全局变量,并将对全局表的任何访
我有以下 qsort 示例来尝试 luajit 中的回调。但是它有内存泄漏(luajit:执行时内存不足),这对我来说并不明显。 有人可以给我一些关于如何创建正确的回调示例的提示吗? local ff
我在使用 ffi.metatype 定义的用户数据时遇到问题。当对象被垃圾收集时,我会遇到段错误。这是代码。 ffi = require("ffi") srate = 48000 ffi.cdef[[
关于 的 Luajit 手册-b 选项 说: The output file type is auto-detected from the extension of the output file n
我有一个小的 C 程序,它有一个字符串,它必须代表一个 Lua 模块,它看起来像这样: const char *lua_str = " local mymodule = {} \ functi
已关注 MSDN's GetOpenFileName example使用 LuaJIT 的 FFI。这两天我一直在努力让它工作,不仅对话框打不开,而且整个东西都崩溃了。 当使用 OllyDdb 进行调
我有这样的文件设置: main.lua (requires 'mydir.b' and then 'b') b.lua mydir/ b.so (LuaJIT C module) 从主要,
我认为 LuaJIT 很棒,现在我也看到了 eLua。 我很好奇嵌入式程序员什么时候会为嵌入式系统选择一个而不是另一个? 最佳答案 这取决于您所说的“嵌入式系统”是什么意思。您是指“移动应用程序”还是
lua -e "print(package.path)" ./?.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;/usr/lib
编辑:不幸的是,LuaJIT 被排除在下面链接的比较之外。 这个comparison编程语言的多样性表明 LuaJIT 比普通 Lua 实现有十倍以上的改进。为什么变化这么大? Lua 是否有什么特殊
我目前正在尝试使用以下命令在我的 Arch linux 发行版上使用 luajit 支持编译 suricata ( http://suricata-ids.org/ ): ./configure --
LuaJIT 知道它定义的 C 类型以及数组的长度,但它不检查边界: ffi = require("ffi") ten_ints = ffi.typeof("int [10]") p1 = ten_i
我在 LuaJit 中不断收到内存不足错误。如何增加堆栈或堆大小? 谢谢 最佳答案 除了玩具示例,我自己没有使用过 LuaJIT。但是由于还没有其他人提供任何答案... 从浏览 documentati
我是一名优秀的程序员,十分优秀!