注册C ++类以供在Lua 5.2中使用

我在阅读有关如何通过这个小博客教程创建并注册用于Lua的C++类。

但是,尽管简单、信息丰富和易读,但它似乎是为较旧版本的Lua而设计的。

因此,一些函数/宏可能丢失或只有不同的签名。

在Lua C版本5.2中,以下代码将是什么样子?

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

using namespace std;

// Just a useless test class
class MyClass
{
private:
    int _X;

public:
    MyClass(int x) : _X(x) {};
    ~MyClass() { Release() };

    // Displays your number in a welcoming message box
    void Hello()
    {
        stringstream ss;
        ss << "Hello!" << endl << "Your number is: " << _X;
        string s(ss.str());
        MessageBoxA(NULL, s.c_str(), "MyClass", MB_ICONINFORMATION);
    }

    void Release() {
        //release code goes here
    }
};

// Functions that will create/destroy MyClass instances
static int newMyClass(lua_State* L)
{
    int n = lua_gettop(L); // Number of arguments
    if (n != 2)
        return luaL_error(L, "Got %d arguments, expected 2 (class, x)", n);
    // First argument is now a table that represent the class to instantiate
    luaL_checktype(L, 1, LUA_TTABLE);

    lua_newtable(L); // Create table to represent instance

    // Set first argument of new to metatable of instance
    lua_pushvalue(L, 1);
    lua_setmetatable(L, -2);

    // Do function lookups in metatable
    lua_pushvalue(L, 1);
    lua_setfield(L, 1, "__index");

    // Allocate memory for a pointer to to object
    MyClass** s = (MyClass**)lua_newuserdata(L, sizeof(MyClass*));

    int x = luaL_checknumber(L, 2);

    *s = new MyClass(x);

    luaL_getmetatable(L, "Lua.MyClass");
    lua_setmetatable(L, -2);
    lua_setfield(L, -2, "__self");

    return 1;
}

static int doSomethingMyClass(lua_State* L)
{
    MyClass* c = nullptr;
    checkUserData(L, "Lua.MyClass", c);
    c->Hello();
    return 0;
}

static int destroyMyClass(lua_State* L)
{
    MyClass* c = nullptr;
    checkUserData(L, "Lua.MyClass", c);
    c->Release();
    return 0;
}

// Functions that will show up in our Lua environment
static const luaL_Reg gMyClassFuncs[] = {
    // Creation
    { "new", newMyClass) },
    { "hello", helloMyClass },
    { NULL, NULL }
};

static const luaL_Reg gDestroyMyClassFuncs[] = {
    {"__gc", destroyMyClass},
    {NULL, NULL}
};

// Registers the class for use in Lua
void registerMyClass(lua_State *L)
{
    // Register metatable for user data in registry
    luaL_newmetatable(L, "Lua.MyClass");
    luaL_register(L, 0, gDestroyMyClassFuncs);
    luaL_register(L, 0, gMyClassFuncs);
    lua_pushvalue(L, -1);
    lua_setfield(L, -2, "__index");

    // Register the base class for instances of Sprite
    luaL_register(L, "MyClass", gSpriteFuncs);
}

基本上,目标是能够在Lua中编写以下内容:

-- Create a new MyClass instance
local c = MyClass:new(5)
-- Show message
c:Hello() -- Should say something like "Hello! Your number is: 5"

我需要做出什么改变,才能使其适用于5.2?

点赞
用户107090
用户107090

在该代码中,仅有的一个不属于 Lua 5.2 的函数是 luaL_register。你应该使用 luaL_setfuncs 替代它。

你还应该手动设置全局变量 MyClass,或者在你的 Lua 代码中使用 local MyClass=require"MyClass",因为 require 不再设置全局变量。

如果你要嵌入 Lua,你可以使用编译选项 -DLUA_COMPAT_MODULE 来编译你的代码和 Lua,并且获取 5.1 的函数。但是,如果你打算使用该版本,考虑现在是将你的代码迁移到 Lua 5.2 的好时机。

2013-06-02 10:23:12