如何从 Lua 访问运行 Lua 脚本的类的变量
How to access variables of the class that runs the Lua script from Lua
我想知道是否可以从 Lua 脚本中使用的绑定C++类访问运行 Lua 脚本的类的变量。
从下面的示例中,我想知道是否可以以某种方式从绑定Test
类访问myLua
类中的name
变量。
这是我的代码。
主要.cpp :
extern "C"
{
int luaopen_my(lua_State* L);
}
class myLua {
public:
struct myData
{
std::string name;
lua_State *L;
};
myLua(std::string name)
{
data = make_shared<myData>();
data->name = name;
data->L = luaL_newstate();
lua_State *L = data->L;
luaL_openlibs(L);
luaopen_my(L);
lua_settop(L, 0);
const char *script =
"function setup()
test = my.Test()
test:callHello()
end
function hello(name)
print('hello is called by : ' .. name)
end";
//------------Added----------------
lua_pushlightuserdata(L, data.get());
myLua::myData *b = static_cast<myLua::myData *>(lua_touserdata(L, 1));
cout << "RESULT1 : " << b->name << endl;
//---------------------------------
const int ret = luaL_loadstring(L, script);
if (ret != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0)
{
std::cout << "failed to run lua script" << std::endl;
return;
}
lua_getglobal(L, "setup");
if (lua_pcall(L, 0, 0, 0))
{
std::cout << "failed to call setup function" << std::endl;
return;
}
}
shared_ptr<myData> data;
};
void main()
{
myLua lua1("Apple");
myLua lua2("Orange");
}
绑定.h :
class Test
{
public:
void callHello(lua_State *L) {
//------------Added----------------
myLua::myData *b = static_cast<myLua::myData *>(lua_touserdata(L, -1));
cout << "RESULT2 : " << b->name << endl;
//---------------------------------
lua_getglobal(L, "hello");
lua_pushstring(L, "ClassName");
if (lua_pcall(L, 1, 0, 0))
{
std::cout << "failed to call hello function" << std::endl;
return;
}
};
};
bindings.i :(用于使用 SWIG 绑定bindings.h
(
%module my
%{
#include "bindings.h"
%}
%include <stl.i>
%include <std_string.i>
%include <std_vector.i>
%include <std_map.i>
%include <typemaps.i>
%typemap(default) (lua_State *L)
{
$1 = L;
}
typedef std::string string;
%include "bindings.h"
当前结果:
hello is called by : ClassName
hello is called by : ClassName
我想要的结果:
hello is called by : Apple
hello is called by : Orange
也许我可以以某种方式注册变量以lua_State*
?
我认为如果有类似的东西会很棒
lua_registerdata(L, &name);
然后使用类似的东西得到它
string name = lua_getregistereddata(L);
添加代码的结果:
RESULT1 : Apple
RESULT2 : 360n240300`255276255336336300ݺ220300`DD255276255336336300ݺ300217300`340_300`D376
hello is called by : ClassName
RESULT1 : Orange
RESULT2 : 360n300`255276255336336300ݺ200236300`DD255276255336336300ݺ@236300``w300`D376
hello is called by : ClassName
按值传递
我建议您将name
作为setup
和callHello
的论据传递. 这解决了对象生存期的问题。
注意:从C++调用Lua函数,然后从Lua调用C++函数似乎效率很低。 你确定你的设计吗? 你真的需要通过Lua进行这种额外的间接间接吗?
bindings.h
#pragma once
#include <iostream>
#include <string>
class Test {
public:
void callHello(std::string const &name, lua_State *L) {
lua_getglobal(L, "hello");
lua_pushstring(L, name.c_str());
if (lua_pcall(L, 1, 0, 0) != 0) {
std::cout << "failed to call hello functionn"
<< lua_tostring(L, -1) << 'n';
return;
}
}
};
test.cpp
#include <iostream>
#include <string>
#include <lua.hpp>
extern "C" int luaopen_my(lua_State *L);
class myLua {
public:
myLua(std::string const &name) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaopen_my(L);
const char *script = "function setup(name)n"
" local test = my.Test()n"
" test:callHello(name)n"
"endn"
"function hello(name)n"
" print('hello is called by : ' .. name)"
"end";
if (luaL_dostring(L, script) != 0) {
std::cout << "failed to run lua scriptn"
<< lua_tostring(L, -1) << 'n';
lua_close(L);
return;
}
lua_getglobal(L, "setup");
lua_pushstring(L, name.c_str());
if (lua_pcall(L, 1, 0, 0) != 0) {
std::cout << "failed to call setup functionn"
<< lua_tostring(L, -1) << 'n';
lua_close(L);
return;
}
lua_close(L);
}
};
int main() {
myLua lua1("Apple");
myLua lua2("Orange");
}
通过轻用户数据传递
根据您的要求,您还可以将指向字符串的指针作为 lightuserdata 推送到注册表中,并在callHello
函数中获取它。 由于各种原因,使用注册表很危险。 密钥可能会发生冲突,您必须绝对确定密钥未在其他地方使用。 指向C++数据的指针可能会悬而未决,Lua 不知道也不可能知道这一点,并且会很乐意为您提供一个无效的指针。 取消引用会导致难以调试的分段错误。
注意:我认为这是糟糕的设计,应该避免。 为了不必传递参数的方便而放弃内存安全听起来不是一个好的权衡。
bindings.h
#pragma once
#include <iostream>
#include <string>
class Test {
public:
void callHello(lua_State *L) {
// Fetch light userdata from the registry with key "name" and
// pray that it is there
lua_pushstring(L, "name");
lua_gettable(L, LUA_REGISTRYINDEX);
std::string name;
if (lua_islightuserdata(L, -1) == 1) {
name = *static_cast<std::string *>(lua_touserdata(L, -1));
lua_pop(L, 1);
} else {
lua_pushstring(L, "userdata corrupted or absent");
lua_error(L);
return;
}
// Call hello function with fetched name
lua_getglobal(L, "hello");
lua_pushstring(L, name.c_str());
if (lua_pcall(L, 1, 0, 0) != 0) {
std::cout << "failed to call hello functionn"
<< lua_tostring(L, -1) << 'n';
return;
}
}
};
test.cpp
#include <iostream>
#include <string>
#include <lua.hpp>
extern "C" int luaopen_my(lua_State *L);
class myLua {
public:
myLua(std::string name) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaopen_my(L);
const char *script = "function setup()n"
" local test = my.Test()n"
" test:callHello()n"
"endn"
"function hello(name)n"
" print('hello is called by : ' .. name)"
"end";
if (luaL_dostring(L, script) != 0) {
std::cout << "failed to run lua scriptn"
<< lua_tostring(L, -1) << 'n';
lua_close(L);
return;
}
// Push light userdata into the registry with key "name"
lua_pushstring(L, "name");
lua_pushlightuserdata(L, static_cast<void *>(&name));
lua_settable(L, LUA_REGISTRYINDEX);
lua_getglobal(L, "setup");
if (lua_pcall(L, 0, 0, 0) != 0) {
std::cout << "failed to call setup functionn"
<< lua_tostring(L, -1) << 'n';
lua_close(L);
return;
}
lua_close(L);
}
};
int main() {
myLua lua1("Apple");
myLua lua2("Orange");
}
常见位
SWIG 接口文件不需要调整,无论哪种情况都保持不变。
my.i
%module my
%{
#include "bindings.h"
%}
%include <std_string.i>
%include <typemaps.i>
%typemap(default) (lua_State *L)
{
$1 = L;
}
%include "bindings.h"
对于这两种情况,都可以进行编译和运行(例如(
$ swig -lua -c++ my.i
$ clang++ -Wall -Wextra -Wpedantic -I/usr/include/lua5.2/ my_wrap.cxx test.cpp -llua5.2
$ ./a.out
hello is called by : Apple
hello is called by : Orange
- 当使用Lua作为嵌入式语言(比如c++)时,有什么简单/方便的方法可以找到变量在Lua中的定义位置吗
- 如何在C++中正确设置Lua局部变量
- 通过require在不同的Lua状态之间共享全局变量
- 如何从 Lua 访问运行 Lua 脚本的类的变量
- 在C 函数中打印一个来自LUA的静态变量
- 使用LUA中使用后缺少的全局变量
- 如何在LUA C API中分配全局LUA函数变量
- 从C++调用 Python 或 Lua 来计算表达式,仅在需要时计算未知变量
- 加载 Lua 文件并使用变量而不执行函数
- 如何在不运行的情况下识别LUA脚本中的非初始化变量
- 使用Lua定义C++游戏中对象的变量
- 从C 访问功能环境中的LUA变量
- 如何从C++中过滤掉Lua中用户定义的全局变量
- 将 Lua 中的变量提取到C++中
- Lua、元表和全局变量
- c++lua设置全局变量时出错
- Lua中变量的内存RAM地址
- 将变量命名为Lua
- 在Lua中跟踪变量以进行读访问,以启动用户定义的c++方法/函数
- 如何将Lua变量直接映射到C++变量