Lua 5.2 C API的要求如何改写才能形成长尾词?

2026-04-01 20:031阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计725个文字,预计阅读时间需要3分钟。

Lua 5.2 C API的要求如何改写才能形成长尾词?

问题:我想从C程序调用一个Lua脚本,该脚本需要使用s+lyaml模块,Lua绑定LibYAML。我已从源代码编译了Lua 5.2,并修改了模块以使其与Lua 5.2兼容。它可以在GitHub上找到。Lua脚本如下:

lua-- Lua脚本内容

问题

我想从一个C程序调用一个Lua脚本,该脚本需要()s lyaml模块,Lua绑定LibYAML.

我从源代码编译了Lua 5.2,并且我修改了模块以使它与Lua 5.2一起工作.它可以在github找到.

Lua脚本如下,它适用于Lua 5.1和5.2:

-- foo.lua require('lyaml') function hello () res = lyaml.load("a: 4\n") return res.a end -- then calling hello() it works like a charm print( hello() ) -> 4

问题

我编写了一个C程序,它应该在Programming in Lua, Chapter 25和Lua 5.2 Reference Manual之后从脚本中调用hello().

C程序如下:

/* foo.c */ #include <lua.h> #include <lualib.h> #include <lauxlib.h> int main(void) { double z; lua_State *L = luaL_newstate(); luaL_openlibs(L); if (luaL_dofile(L, "foo.lua")) luaL_error(L, "error running script: %s", lua_tostring(L, -1)); lua_getglobal(L, "hello"); if (lua_pcall(L, 0, 1, 0) != 0) luaL_error(L, "error calling hello: %s", lua_tostring(L, -1)); if (!lua_isnumber(L, -1)) luaL_error(L, "result must be number"); z = lua_tonumber(L, -1); lua_pop(L, 1); lua_close(L); return 0; }

我编译发行:

gcc -Wall -o foo foo.c -ldl -lm -llua

然后在运行foo时,我在运行时收到以下错误:

PANIC: unprotected error in call tu Lua API ( error running script: error loading module 'lyaml' from file '/path/to/lyaml.so': /path/to/lyaml.so: undefined symbol: lua_gettop) Aborted

所以我尝试从C程序加载lyaml,在luaL_openlibs()调用后添加以下行:

Lua 5.2 C API的要求如何改写才能形成长尾词?

luaL_requiref(L, "lyaml", luaopen_package, 1);

重新编译后,错误变为:

PANIC: unprotected error in call tu Lua API ( error running script: hello.lua:4: attempt to index global 'lyaml' (a nil value)) Aborted

所以我想,没有lyaml符号,并且require()调用以某种方式失败.

通过阅读luaL_requiref()文档,我认为modname将通过其调用设置glb标志设置为true:

void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb);

Calls function openf with string modname as an argument and sets the call result in package.loaded[modname], as if that function has been called through require.

If glb is true, also stores the result into global modname.
Leaves a copy of that result on the stack.

我试图在Lua脚本中注释require()调用,结果是一样的.

我做错了什么?我忘了做某事吗?

编辑

我用其替代品破解了模块更新已弃用(已删除)的函数/类型,如下所示:

lua_strlen() -> luaL_len() luaL_reg -> luaL_Reg luaL_getn() -> luaL_len()

然而Lua脚本使用lyaml工作所以我认为问题不是我的黑客.

我用Lua 5.1尝试了原始的lyaml模块.结果是一样的,所以我确定问题不是我的黑客.

UPDATE

根据Doug Currie的回答,添加以下行,C程序与Lua 5.1完美配合.我仍然在5.2中得到相同的错误.

lyaml = require('lyaml') 写的foo.lua只能在Lua 5.1.x中运行 – 你被黑的lyaml.c没有设置全局lyaml,在Lua 5.2中也没有要求.我怀疑你的PATH没有Lua 5.2当你运行第一个测试“就像魅力一样”,或者你在加载foo.lua之前手动设置了lyaml.

foo.lua应该从

lyaml = require('lyaml')

本文共计725个文字,预计阅读时间需要3分钟。

Lua 5.2 C API的要求如何改写才能形成长尾词?

问题:我想从C程序调用一个Lua脚本,该脚本需要使用s+lyaml模块,Lua绑定LibYAML。我已从源代码编译了Lua 5.2,并修改了模块以使其与Lua 5.2兼容。它可以在GitHub上找到。Lua脚本如下:

lua-- Lua脚本内容

问题

我想从一个C程序调用一个Lua脚本,该脚本需要()s lyaml模块,Lua绑定LibYAML.

我从源代码编译了Lua 5.2,并且我修改了模块以使它与Lua 5.2一起工作.它可以在github找到.

Lua脚本如下,它适用于Lua 5.1和5.2:

-- foo.lua require('lyaml') function hello () res = lyaml.load("a: 4\n") return res.a end -- then calling hello() it works like a charm print( hello() ) -> 4

问题

我编写了一个C程序,它应该在Programming in Lua, Chapter 25和Lua 5.2 Reference Manual之后从脚本中调用hello().

C程序如下:

/* foo.c */ #include <lua.h> #include <lualib.h> #include <lauxlib.h> int main(void) { double z; lua_State *L = luaL_newstate(); luaL_openlibs(L); if (luaL_dofile(L, "foo.lua")) luaL_error(L, "error running script: %s", lua_tostring(L, -1)); lua_getglobal(L, "hello"); if (lua_pcall(L, 0, 1, 0) != 0) luaL_error(L, "error calling hello: %s", lua_tostring(L, -1)); if (!lua_isnumber(L, -1)) luaL_error(L, "result must be number"); z = lua_tonumber(L, -1); lua_pop(L, 1); lua_close(L); return 0; }

我编译发行:

gcc -Wall -o foo foo.c -ldl -lm -llua

然后在运行foo时,我在运行时收到以下错误:

PANIC: unprotected error in call tu Lua API ( error running script: error loading module 'lyaml' from file '/path/to/lyaml.so': /path/to/lyaml.so: undefined symbol: lua_gettop) Aborted

所以我尝试从C程序加载lyaml,在luaL_openlibs()调用后添加以下行:

Lua 5.2 C API的要求如何改写才能形成长尾词?

luaL_requiref(L, "lyaml", luaopen_package, 1);

重新编译后,错误变为:

PANIC: unprotected error in call tu Lua API ( error running script: hello.lua:4: attempt to index global 'lyaml' (a nil value)) Aborted

所以我想,没有lyaml符号,并且require()调用以某种方式失败.

通过阅读luaL_requiref()文档,我认为modname将通过其调用设置glb标志设置为true:

void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb);

Calls function openf with string modname as an argument and sets the call result in package.loaded[modname], as if that function has been called through require.

If glb is true, also stores the result into global modname.
Leaves a copy of that result on the stack.

我试图在Lua脚本中注释require()调用,结果是一样的.

我做错了什么?我忘了做某事吗?

编辑

我用其替代品破解了模块更新已弃用(已删除)的函数/类型,如下所示:

lua_strlen() -> luaL_len() luaL_reg -> luaL_Reg luaL_getn() -> luaL_len()

然而Lua脚本使用lyaml工作所以我认为问题不是我的黑客.

我用Lua 5.1尝试了原始的lyaml模块.结果是一样的,所以我确定问题不是我的黑客.

UPDATE

根据Doug Currie的回答,添加以下行,C程序与Lua 5.1完美配合.我仍然在5.2中得到相同的错误.

lyaml = require('lyaml') 写的foo.lua只能在Lua 5.1.x中运行 – 你被黑的lyaml.c没有设置全局lyaml,在Lua 5.2中也没有要求.我怀疑你的PATH没有Lua 5.2当你运行第一个测试“就像魅力一样”,或者你在加载foo.lua之前手动设置了lyaml.

foo.lua应该从

lyaml = require('lyaml')