加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua 5.2 C API和要求

发布时间:2020-12-15 00:26:31 所属栏目:大数据 来源:网络整理
导读:问题 我想从一个C程序调用一个Lua脚本,该脚本需要()s lyaml模块,Lua绑定LibYAML. 我从源代码编译了Lua 5.2,并且我修改了模块以使它与Lua 5.2一起工作.它可以在github找到. Lua脚本如下,它适用于Lua 5.1和5.2: -- foo.luarequire('lyaml')function hello ()
问题

我想从一个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: 4n")
  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,1,0) != 0)
    luaL_error(L,"error calling hello: %s",-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()调用后添加以下行:

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')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读