简体   繁体   English

嵌入Lua 5.2并定义库

[英]Embedding Lua 5.2 and defining libraries

Lua comes with a free on-line reference manual for version 5.2 (which I am using) and Programming in Lua for version 5.0 is also available. Lua附带了5.2版本的免费在线参考手册 (我正在使用),也可以使用Lua中的5.0版编程

There have been, however, a couple of changes between these versions that I cannot seem to be able to surpass. 然而,这些版本之间有一些变化,我似乎无法超越。 The changes are noted in the successive versions of the reference manual for 5.2 and 5.1 . 这些更改在5.25.1参考手册的后续版本中有所说明。 Notice the successive deprecation of luaL_openlib() in favour of luaL_register() , then luaL_register() in favor of luaL_setfuncs() . 注意luaL_openlib()的连续弃用有利于luaL_register() ,然后luaL_register()有利于luaL_setfuncs()

The searches on the web give mixed results, with most of them pointing to luaL_register() . Web上的搜索结果不一,其中大多数都指向luaL_register()

What I try to achieve may be summarized by the mini-program below that may be compiled and linked with say, gcc ./main.c -llua -ldl -lm -o lua_test 我尝试实现的目标可以通过以下迷你程序进行总结,该程序可以编译并链接,例如, gcc ./main.c -llua -ldl -lm -o lua_test

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include <stdio.h>
#include <string.h>


static int test_fun_1( lua_State * L )
{
    printf( "t1 function fired\n" );
    return 0;
}

int main ( void )
{
    char buff[256];
    lua_State * L;
    int error;

    printf( "Test starts.\n\n" );

    L = luaL_newstate();
    luaL_openlibs( L ); 

    lua_register( L, "t1", test_fun_1 );

    while ( fgets( buff, sizeof(buff), stdin ) != NULL)
    {
      if ( strcmp( buff, "q\n" ) == 0 )
      {
          break;
      }
      error = luaL_loadbuffer( L, buff, strlen(buff), "line" ) ||
              lua_pcall( L, 0, 0, 0 );
      if (error)
      {
        printf( "Test error: %s\n", lua_tostring( L, -1 ) );
        lua_pop( L, 1 );
      }
    }
    lua_close( L );

    printf( "\n\nTest ended.\n" );
    return 0;
 }

This works as expected and typing t1() produces the expected result. 这按预期工作,输入t1()会产生预期的结果。

I would now like to create a library/package visible to Lua. 我现在想创建一个Lua可见的库/包。 The Programming in Lua advices us to use an array and a load function: Lua中Programming建议我们使用数组和加载函数:

static int test_fun_2( lua_State * L )
{
    printf( "t2 function fired\n" );
    return 0;
}

static const struct luaL_Reg tlib_funcs [] =
{
  { "t2", test_fun_2 },
  { NULL, NULL }  /* sentinel */
};

int luaopen_tlib ( lua_State * L )
{
  luaL_openlib(L, "tlib", tlib_funcs, 0);

  return 1;
}

then use luaopen_tlib() after luaL_openlibs() . 然后在luaopen_tlib()之后使用luaL_openlibs() Doing so allows us to use tlib:t2() if we define LUA_COMPAT_MODULE (work in compatible mode). 如果我们定义LUA_COMPAT_MODULE (在兼容模式下工作tlib:t2()这样做允许我们使用tlib:t2() )。

What is the proper way of doing this in Lua 5.2? 在Lua 5.2中这样做的正确方法是什么?

The luaopen_tlib function should be written that way: luaopen_tlib函数应该这样编写:

int luaopen_tlib ( lua_State * L )
{
  luaL_newlib(L, tlib_funcs);
  return 1;
}

And in the main function, you should load the module like this: main函数中,您应该像这样加载模块:

int main ( void )
{
    // ...
    luaL_requiref(L, "tlib", luaopen_tlib, 1);
    // ...
}

Or alternatively, you can add an entry {"tlib", luaopen_tlib} to the loadedlibs table in linit.c . 或者,您可以在linit.cloadedlibs表中添加条目{"tlib", luaopen_tlib}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM