如何索引转换后的用户数据值?
How to index a converted userdata value?
我尝试使用 lua_touserdata()
将 C++ class 转换为空指针,然后使用 lua_pushlightuserdata()
.
将其转换回 C++ class
但是,一旦我进行了转换,我就无法在 class 中索引变量。
这是我的测试代码:
MyBindings.h
class Vec2
{
public:
Vec2():x(0), y(0){};
Vec2(float x, float y):x(x), y(y){};
float x, y;
};
void *getPtr(void *p)
{
return p;
}
MyBindings.i
%module my
%{
#include "MyBindings.h"
%}
%typemap(typecheck) void*
{
= lua_isuserdata(L, $input);
}
%typemap(in) void*
{
= lua_touserdata(L, $input);
}
%typemap(out) void*
{
lua_pushlightuserdata(L, );
++SWIG_arg;
}
%include "MyBindings.h"
main.cpp
#include "lua.hpp"
extern "C"
{
int luaopen_my(lua_State *L);
}
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaopen_my(L);
lua_settop(L, 0);
const int ret = luaL_dostring(L, "local vec = my.Vec2(3, 4)\n"
"local p = my.getPtr(vec)\n"
"print(p.x)");
if (ret)
{
std::cout << lua_tostring(L, -1) << '\n';
}
lua_close(L);
}
我得到的结果:
[string "local vec = my.Vec2(3, 4)..."]:3: attempt to index a userdata
value (local 'p')
我期望的结果:
3
我应该怎么做才能得到我期望的结果?
如果你想这样做,你必须调整你的设计。首先,函数 getPtr
无法工作,因为它太通用了。 SWIG 无法神奇地猜测类型并做出正确的事情。您至少必须修复输入的类型。
MyBindings.h
struct Vec2 {
Vec2() : x(0), y(0){};
Vec2(float x, float y) : x(x), y(y){};
float x, y;
};
void *getPtr(Vec2 &p) { return &p; }
再说一次,你真的确定要这样做吗?因为会变丑!
你至少需要两个元方法,__index
和 __newindex
,通过指针获取和设置向量的元素。我在接口文件的文字块 (%{ ... %}
) 中实现了这些,但您也可以将它们移动到 header 并将此 header 包含在文字块中。
现在你必须让 Lua 知道你定义的元方法并将它们插入到一个命名的元表中,这样你就可以将 Vec2
类型的指针与其他指针区分开来。因此,您必须在接口文件的 %init
部分中添加一些内容,以便在解释器启动时注册元表。
因为您必须删除 getPtr
的 void*
输入参数,所以可以删除 typecheck
和 in
类型映射。 out
类型映射必须进行调整。我们必须为适合指向 Vec2
的指针的用户数据分配内存。我们将用户数据设置为该指针并将 Vec2
元表添加到它上面。现在这非常简单,不是吗?(讽刺)
MyBindings.i
%module my
%{
#define SWIG_FILE_WITH_INIT
#include <string>
#include "MyBindings.h"
static int setVec2(lua_State *L) {
Vec2 *v = *static_cast<Vec2 **>(luaL_checkudata(L, 1, "Vec2"));
luaL_argcheck(L, v != nullptr, 1, "invalid pointer");
std::string index = luaL_checkstring(L, 2);
luaL_argcheck(L, index == "x" || index == "y", 2, "index out of range");
luaL_argcheck(L, lua_isnumber(L, 3), 3, "not a number");
float record = lua_tonumber(L, 3);
if (index == "x") {
v->x = record;
} else if (index == "y") {
v->y = record;
} else {
assert(false); // Can't happen!
}
return 0;
}
static int getVec2(lua_State *L) {
Vec2 *v = *static_cast<Vec2 **>(luaL_checkudata(L, 1, "Vec2"));
luaL_argcheck(L, v != nullptr, 1, "invalid pointer");
std::string index = luaL_checkstring(L, 2);
luaL_argcheck(L, index == "x" || index == "y", 2, "index out of range");
if (index == "x") {
lua_pushnumber(L, v->x);
} else if (index == "y") {
lua_pushnumber(L, v->y);
} else {
assert(false); // Can't happen!
}
return 1;
}
static const struct luaL_Reg Vec2_meta[] = {
{"__newindex", setVec2},
{"__index", getVec2},
{nullptr, nullptr} // sentinel
};
%}
%init %{
luaL_newmetatable(L, "Vec2");
luaL_setfuncs(L, Vec2_meta, 0);
lua_pop(L, 1);
%}
%typemap(out) void*
{
void * udata = lua_newuserdata(L, sizeof(Vec2 *));
*static_cast<void **>(udata) = ;
luaL_getmetatable(L, "Vec2");
lua_setmetatable(L, -2);
++SWIG_arg;
}
%include "MyBindings.h"
让我们看看它是否有效。
test.lua
local my = require("my")
local vec = my.Vec2(3, 4)
local p = my.getPtr(vec)
print(p.x, p.y)
p.x = 1.0
p.y = 2.0
print(p.x, p.y)
print(vec.x, vec.y)
$ swig -lua -c++ MyBindings.i
$ clang++ -Wall -Wextra -Wpedantic -I/usr/include/lua5.3 -fPIC -shared MyBindings_wrap.cxx -o my.so -llua5.3
$ lua5.3 test.lua
3.0 4.0
1.0 2.0
1.0 2.0
如果你使用轻型用户数据可能会更容易一些,但缺点是所有轻型用户数据将共享相同的元表,所以你只能对一种 object.
回复评论
将指向特定类型的指针转换为 void*
称为类型擦除,因为您丢失了有关所包含数据的所有信息。因此,在恢复类型时必须小心,确保恢复的是正确的类型。转换为不相关的类型是未定义的行为,如果幸运的话,会导致程序崩溃。
您可能不希望像 Vec2
那样使用 void*
。那么,转换为 void*
的目的是什么,当您无论如何都想保留原始含义时。相反,你想要两个函数,getPtr
和 getVec2
。 getPtr
函数擦除类型并为您提供 void*
object,这在 Lua 中不可用,但可以方便地传递给接受任意数据的回调函数 [=25] =].完成后,getVec2
函数会将类型恢复为 Vec2
。
在示例中,getVec2
函数 returns 通过引用,即返回的 object 将引用您调用 getPtr
的 object .这也意味着,如果原始 object 被垃圾回收,则您的指针无效,这将使您的应用程序崩溃。
MyBindings.h
struct Vec2 {
Vec2() : x(0), y(0){};
Vec2(float x, float y) : x(x), y(y){};
float x, y;
};
void *getPtr(Vec2 &p) { return &p; }
Vec2 &getVec2(void *p) { return *static_cast<Vec2 *>(p); }
MyBindings.i
%module my
%{
#define SWIG_FILE_WITH_INIT
#include "MyBindings.h"
%}
%include "MyBindings.h"
test.lua
local my = require("my")
local vec = my.Vec2(3, 4)
-- Erase the type of vec to pass it around
local p = my.getPtr(vec)
-- Then restore the type using getVec2
local v = my.getVec2(p)
-- Take care! v is a reference to vec
v.x = 1.0
v.y = 2.0
print(v.x, v.y)
print(vec.x, vec.y)
调用示例:
$ swig -lua -c++ MyBindings.i
$ clang++ -Wall -Wextra -Wpedantic -I/usr/include/lua5.3 -fPIC -shared MyBindings_wrap.cxx -o my.so -llua5.3
$ lua5.3 test.lua
1.0 2.0
1.0 2.0
要查看引用语义失败,请在 local p = my.getPtr(vec)
之后放置 vec = nil collectgarbage()
。它不会在我的机器上崩溃,但 Valgrind 报告无效的读取和写入。
我尝试使用 lua_touserdata()
将 C++ class 转换为空指针,然后使用 lua_pushlightuserdata()
.
但是,一旦我进行了转换,我就无法在 class 中索引变量。
这是我的测试代码:
MyBindings.h
class Vec2
{
public:
Vec2():x(0), y(0){};
Vec2(float x, float y):x(x), y(y){};
float x, y;
};
void *getPtr(void *p)
{
return p;
}
MyBindings.i
%module my
%{
#include "MyBindings.h"
%}
%typemap(typecheck) void*
{
= lua_isuserdata(L, $input);
}
%typemap(in) void*
{
= lua_touserdata(L, $input);
}
%typemap(out) void*
{
lua_pushlightuserdata(L, );
++SWIG_arg;
}
%include "MyBindings.h"
main.cpp
#include "lua.hpp"
extern "C"
{
int luaopen_my(lua_State *L);
}
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaopen_my(L);
lua_settop(L, 0);
const int ret = luaL_dostring(L, "local vec = my.Vec2(3, 4)\n"
"local p = my.getPtr(vec)\n"
"print(p.x)");
if (ret)
{
std::cout << lua_tostring(L, -1) << '\n';
}
lua_close(L);
}
我得到的结果:
[string "local vec = my.Vec2(3, 4)..."]:3: attempt to index a userdata value (local 'p')
我期望的结果:
3
我应该怎么做才能得到我期望的结果?
如果你想这样做,你必须调整你的设计。首先,函数 getPtr
无法工作,因为它太通用了。 SWIG 无法神奇地猜测类型并做出正确的事情。您至少必须修复输入的类型。
MyBindings.h
struct Vec2 {
Vec2() : x(0), y(0){};
Vec2(float x, float y) : x(x), y(y){};
float x, y;
};
void *getPtr(Vec2 &p) { return &p; }
再说一次,你真的确定要这样做吗?因为会变丑!
你至少需要两个元方法,__index
和 __newindex
,通过指针获取和设置向量的元素。我在接口文件的文字块 (%{ ... %}
) 中实现了这些,但您也可以将它们移动到 header 并将此 header 包含在文字块中。
现在你必须让 Lua 知道你定义的元方法并将它们插入到一个命名的元表中,这样你就可以将 Vec2
类型的指针与其他指针区分开来。因此,您必须在接口文件的 %init
部分中添加一些内容,以便在解释器启动时注册元表。
因为您必须删除 getPtr
的 void*
输入参数,所以可以删除 typecheck
和 in
类型映射。 out
类型映射必须进行调整。我们必须为适合指向 Vec2
的指针的用户数据分配内存。我们将用户数据设置为该指针并将 Vec2
元表添加到它上面。现在这非常简单,不是吗?(讽刺)
MyBindings.i
%module my
%{
#define SWIG_FILE_WITH_INIT
#include <string>
#include "MyBindings.h"
static int setVec2(lua_State *L) {
Vec2 *v = *static_cast<Vec2 **>(luaL_checkudata(L, 1, "Vec2"));
luaL_argcheck(L, v != nullptr, 1, "invalid pointer");
std::string index = luaL_checkstring(L, 2);
luaL_argcheck(L, index == "x" || index == "y", 2, "index out of range");
luaL_argcheck(L, lua_isnumber(L, 3), 3, "not a number");
float record = lua_tonumber(L, 3);
if (index == "x") {
v->x = record;
} else if (index == "y") {
v->y = record;
} else {
assert(false); // Can't happen!
}
return 0;
}
static int getVec2(lua_State *L) {
Vec2 *v = *static_cast<Vec2 **>(luaL_checkudata(L, 1, "Vec2"));
luaL_argcheck(L, v != nullptr, 1, "invalid pointer");
std::string index = luaL_checkstring(L, 2);
luaL_argcheck(L, index == "x" || index == "y", 2, "index out of range");
if (index == "x") {
lua_pushnumber(L, v->x);
} else if (index == "y") {
lua_pushnumber(L, v->y);
} else {
assert(false); // Can't happen!
}
return 1;
}
static const struct luaL_Reg Vec2_meta[] = {
{"__newindex", setVec2},
{"__index", getVec2},
{nullptr, nullptr} // sentinel
};
%}
%init %{
luaL_newmetatable(L, "Vec2");
luaL_setfuncs(L, Vec2_meta, 0);
lua_pop(L, 1);
%}
%typemap(out) void*
{
void * udata = lua_newuserdata(L, sizeof(Vec2 *));
*static_cast<void **>(udata) = ;
luaL_getmetatable(L, "Vec2");
lua_setmetatable(L, -2);
++SWIG_arg;
}
%include "MyBindings.h"
让我们看看它是否有效。
test.lua
local my = require("my")
local vec = my.Vec2(3, 4)
local p = my.getPtr(vec)
print(p.x, p.y)
p.x = 1.0
p.y = 2.0
print(p.x, p.y)
print(vec.x, vec.y)
$ swig -lua -c++ MyBindings.i
$ clang++ -Wall -Wextra -Wpedantic -I/usr/include/lua5.3 -fPIC -shared MyBindings_wrap.cxx -o my.so -llua5.3
$ lua5.3 test.lua
3.0 4.0
1.0 2.0
1.0 2.0
如果你使用轻型用户数据可能会更容易一些,但缺点是所有轻型用户数据将共享相同的元表,所以你只能对一种 object.
回复评论
将指向特定类型的指针转换为 void*
称为类型擦除,因为您丢失了有关所包含数据的所有信息。因此,在恢复类型时必须小心,确保恢复的是正确的类型。转换为不相关的类型是未定义的行为,如果幸运的话,会导致程序崩溃。
您可能不希望像 Vec2
那样使用 void*
。那么,转换为 void*
的目的是什么,当您无论如何都想保留原始含义时。相反,你想要两个函数,getPtr
和 getVec2
。 getPtr
函数擦除类型并为您提供 void*
object,这在 Lua 中不可用,但可以方便地传递给接受任意数据的回调函数 [=25] =].完成后,getVec2
函数会将类型恢复为 Vec2
。
在示例中,getVec2
函数 returns 通过引用,即返回的 object 将引用您调用 getPtr
的 object .这也意味着,如果原始 object 被垃圾回收,则您的指针无效,这将使您的应用程序崩溃。
MyBindings.h
struct Vec2 {
Vec2() : x(0), y(0){};
Vec2(float x, float y) : x(x), y(y){};
float x, y;
};
void *getPtr(Vec2 &p) { return &p; }
Vec2 &getVec2(void *p) { return *static_cast<Vec2 *>(p); }
MyBindings.i
%module my
%{
#define SWIG_FILE_WITH_INIT
#include "MyBindings.h"
%}
%include "MyBindings.h"
test.lua
local my = require("my")
local vec = my.Vec2(3, 4)
-- Erase the type of vec to pass it around
local p = my.getPtr(vec)
-- Then restore the type using getVec2
local v = my.getVec2(p)
-- Take care! v is a reference to vec
v.x = 1.0
v.y = 2.0
print(v.x, v.y)
print(vec.x, vec.y)
调用示例:
$ swig -lua -c++ MyBindings.i
$ clang++ -Wall -Wextra -Wpedantic -I/usr/include/lua5.3 -fPIC -shared MyBindings_wrap.cxx -o my.so -llua5.3
$ lua5.3 test.lua
1.0 2.0
1.0 2.0
要查看引用语义失败,请在 local p = my.getPtr(vec)
之后放置 vec = nil collectgarbage()
。它不会在我的机器上崩溃,但 Valgrind 报告无效的读取和写入。