I'm trying to call A:update(x) and get a returned value x + 3 in C++.
Here's my code:
#include <lua.hpp>
void main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_settop(L, 0);
luaL_dostring(L, "package.preload['A'] = function () local A = {}\n"
"function A:update(x) return x + 3 end \n"
"return A end");
//call function
lua_getglobal(L, "require");
lua_pushstring(L, "A");
if (lua_pcall(L, 1, LUA_MULTRET, 0) != 0) {
std::cerr << "lua:" << lua_tostring(L, 1) << '\n';
lua_pop(L, 1);
}
int top = lua_gettop(L);
lua_getfield(L, -1, "update");
if (!lua_isfunction(L, -1))
{
std::cerr << "lua:" << lua_tostring(L, 1) << '\n';
lua_pop(L, 1);
}
lua_pushnumber(L, 5); //pass the argument 5
if (lua_pcall(L, 1, LUA_MULTRET, 0))
{
std::cerr << "lua:" << lua_tostring(L, 1) << '\n';
lua_pop(L, 1);
}
if (lua_gettop(L) - top)
{
if (lua_isnumber(L, -1))
{
std::cout << "RETURNED : " << lua_tonumber(L, -1) << std::endl;
}
}
lua_pop(L, 1); // pop 'update'
lua_pop(L, 1); // pop 'A'
lua_close(L);
}
I expect it to print RETURNED : 8 but I get the following error:
Thread 1:EXC_BAD_ACCESS (code=1, address=0x0)
How should I correct my code to work?
EDITED: It worked as soon as I change A:update(x) to A.update(x). I thought they work identically except I can use self in a function that uses :. Could someone please explain to me why this happens?
The notation A:update(x) is syntactic sugar for A.update(A,x). That means you have to call the function update with two parameters. You are lacking the first of the two parameters.
The first parameter A is already on the stack but is located “below” the update function. Using lua_pushvalue we can push a copy of the table onto the stack.
Thus you have to call the function like this (omitting the error handling bits)
lua_getfield(L, -1, "update");
lua_pushvalue(L, -2); // push a copy of "A" onto the stack
lua_pushnumber(L, 5); //pass the argument 5
lua_pcall(L, 2, LUA_MULTRET, 0);
Related
I'm trying to get userdata from a Lua script(chunk A) in C++(through a returned variable from function in my example) and then, later pass this userdata back to Lua script(chunk B) from C++(through a function argument in my example) so the userdata can be used in chunk B as it was in the chunk A.
MyBindings.h
class Vec2
{
public:
Vec2():x(0), y(0){};
Vec2(float x, float y):x(x), y(y){};
float x, y;
};
MyBindings.i
%module my
%{
#include "MyBindings.h"
%}
%include "MyBindings.h"
main.cpp
#include <iostream>
#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);
/* chunk A */
luaL_dostring(L, "local vec2 = my.Vec2(3, 4)\n"
"function setup()\n"
"return vec2\n"
"end\n");
/* chunk B */
luaL_dostring(L, "function test(p)\n"
"print(p.x)\n"
"end\n");
void *userDataPtr = nullptr;
/* call setup function */
int top = lua_gettop(L);
lua_getglobal(L, "setup");
if (lua_pcall(L, 0, LUA_MULTRET, 0))
{
std::cout << lua_tostring(L, -1) << '\n';
lua_pop(L, 1);
}
/* check the return value */
if (lua_gettop(L) - top)
{
/* store userdata to a pointer */
if (lua_isuserdata(L, -1))
userDataPtr = lua_touserdata(L, -1);
}
/* check if userDataPtr is valid */
if (userDataPtr != nullptr)
{
/* call test function */
lua_getglobal(L, "test");
lua_pushlightuserdata(L, userDataPtr); /* pass userdata as an argument */
if (lua_pcall(L, 1, 0, 0))
{
std::cout << lua_tostring(L, -1) << '\n';
lua_pop(L, 1);
}
}
lua_close(L);
}
The Result I get :
[string "local vec2 = my.Vec2(3, 4)..."]:6: attempt to index a
userdata value (local 'p')
The Result I expect :
3
Is it possible to get userdata from chunk A and then pass this to chunk B so it can be used like it was in chunk A?
You're losing all information about the object's type when you get raw pointer to userdata's data and pushing it to arguments as lightuserdata. The lightuserdata even has no individual metatables.
The correct way would be to pass the Lua value as it is. Leave the original returned value on Lua stack, or copy it into other Lua container (your Lua table for temporaries, or Lua registry), then copy that value on Lua stack to pass it as an argument. That way you don't have to know anything about binding implementation. You don't even have to care if that's a userdata or any other Lua type.
Based on your code, this might look like this:
#include <iostream>
#include <lua.hpp>
extern "C"
{
int luaopen_my(lua_State *L);
}
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
/* chunk A */
luaL_dostring(L, "local vec2 = {x=3, y=4}\n"
"function setup()\n"
"return vec2\n"
"end\n");
/* chunk B */
luaL_dostring(L, "function test(p)\n"
"print(p.x)\n"
"end\n");
/* call setup function */
int top = lua_gettop(L);
lua_getglobal(L, "setup");
if (lua_pcall(L, 0, LUA_MULTRET, 0))
{
std::cout << lua_tostring(L, -1) << '\n';
lua_pop(L, 1);
exit(EXIT_FAILURE); // simpy fail for demo
}
/* check the return value */
if (lua_gettop(L) - top)
{
// the top now contains the value returned from setup()
/* call test function */
lua_getglobal(L, "test");
// copy the original value as argument
lua_pushvalue(L, -2);
if (lua_pcall(L, 1, 0, 0))
{
std::cout << lua_tostring(L, -1) << '\n';
lua_pop(L, 1);
exit(EXIT_FAILURE);
}
// drop the original value
lua_pop(L, 1);
}else
{
// nothing is returned, nothing to do
}
lua_close(L);
}
In addition to the other answer I would like to show a variant where you store the a reference to the value in the Lua registry. The advantage of this approach is that you don't have to keep the value on the stack and think about what the offset will be. See also 27.3.2 – References in “Programming in Lua”.
This approach uses three functions:
int luaL_ref (lua_State *L, int t);
Pops from the stack the uppermost value, stores it into the table at index t and returns the index the value has in that table. Hence to save a value in the registry we use
userDataRef = luaL_ref(L, LUA_REGISTRYINDEX);
int lua_rawgeti (lua_State *L, int index, lua_Integer n);
Pushes onto the stack the value of the element n of the table at index (t[n] in Lua). Hence to retrieve a value at index userDataRef from the registry we use
lua_rawgeti(L, LUA_REGISTRYINDEX, userDataRef);
void luaL_unref (lua_State *L, int t, int ref);
Removes the reference stored at index ref in the table at t such that the reference can be garbage collected and the index ref can be reused. Hence to remove the reference userDataRef from the registry we use
luaL_unref(L, LUA_REGISTRYINDEX, userDataRef);
#include <iostream>
#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);
/* chunk A */
luaL_dostring(L, "local vec2 = my.Vec2(3, 4)\n"
"function setup()\n"
"return vec2\n"
"end\n");
/* chunk B */
luaL_dostring(L, "function test(p)\n"
"print(p.x)\n"
"end\n");
int userDataRef = LUA_NOREF;
/* call setup function */
int top = lua_gettop(L);
lua_getglobal(L, "setup");
if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
std::cout << lua_tostring(L, -1) << '\n';
lua_pop(L, 1);
}
/* check the return value */
if (lua_gettop(L) - top) {
/* store userdata to a pointer */
userDataRef = luaL_ref(L, LUA_REGISTRYINDEX);
}
/* check if userDataRef is valid */
if (userDataRef != LUA_NOREF && userDataRef != LUA_REFNIL) {
/* call test function */
lua_getglobal(L, "test");
lua_rawgeti(L, LUA_REGISTRYINDEX, userDataRef);
/* free the registry slot (if you are done) */
luaL_unref(L, LUA_REGISTRYINDEX, userDataRef);
if (lua_pcall(L, 1, 0, 0)) {
std::cout << lua_tostring(L, -1) << '\n';
lua_pop(L, 1);
}
}
lua_close(L);
}
Maybe you want to check out the Sol2 wrapper for the Lua-C-API. It can do exactly what you want with minimal boilerplate. However, it requires C++14.
#include <iostream>
#define SOL_CHECK_ARGUMENTS 1
#include <sol.hpp>
extern "C" int luaopen_my(lua_State *L);
int main() {
sol::state L;
L.open_libraries();
luaopen_my(L);
/* chunk A */
L.script("local vec2 = my.Vec2(3, 4)\n"
"function setup()\n"
"return vec2\n"
"end\n");
/* chunk B */
L.script("function test(p)\n"
"print(p.x)\n"
"end\n");
auto userDataRef = L["setup"]();
L["test"](userDataRef);
}
I use the following code to call Lua function from C++ but it crashes sometimes.
void callLuaFunction(lua_State *L, const char *name)
{
if (L == nullptr) return;
lua_getglobal(L, name);
if (lua_type(L, -1) != LUA_TFUNCTION)
return;
if (lua_pcall(L, 0, 0, 0))
error("%s", lua_tostring(L, -1));
}
Is there anything wrong with my code?
I'm trying to register a vector type with Lua, but I'm getting a strange "attempt to index a new value" error when I'm calling the addition metafunction from Lua.
Here is the code section involved. I did not include any other metafunctions (they have the same problem, the only difference is the math operator used in one of the last lines). The error seems to come from the static int LuaVector_lua___add(lua_State *L) function.
static void LuaVector_pushVector(lua_State *L, double x, double y)
{
lua_newtable(L);
lua_pushstring(L, "x");
lua_pushnumber(L, x);
lua_settable(L, -3);
lua_pushstring(L, "y");
lua_pushnumber(L, y);
lua_settable(L, -3);
lua_newtable(L);
lua_pushstring(L, "__add");
lua_pushcfunction(L, LuaVector_lua___add);
lua_settable(L, -3);
lua_setmetatable(L, -2);
}
static int LuaVector_lua___add(lua_State *L)
{
if (!lua_istable(L, 1))
luaL_error(L, "Table excepted for argument #1 LuaVector_lua___add");
if (!lua_istable(L, 2))
luaL_error(L, "Table excepted for argument #2 LuaVector_lua___add");
double x1=0, y1=0, x2=0, y2=0;
/* The error occurs somewhere between here */
lua_pushstring(L, "x");
lua_gettable(L, 1);
x1 = lua_tonumber(L, -1);
lua_pop(L, -1);
lua_pushstring(L, "y");
lua_gettable(L, 1);
y1 = lua_tonumber(L, -1);
lua_pop(L, -1);
lua_pushstring(L, "x");
lua_gettable(L, 2);
x2 = lua_tonumber(L, -1);
lua_pop(L, -1);
lua_pushstring(L, "y");
lua_gettable(L, 2);
y2 = lua_tonumber(L, -1);
lua_pop(L, -1);
/* And here */
LuaVector_pushVector(L, x1 + x2, y1 + y2);
return 1;
}
int LuaVector_lua_new(lua_State *L)
{
double x = 0;
if (!lua_isnil(L, 1))
x = lua_tonumber(L, 1);
double y = 0;
if (!lua_isnil(L, 2))
y = lua_tonumber(L, 2);
LuaVector_pushVector(L, x, y);
return 1;
}
void LuaVector_luaregister(lua_State *L)
{
lua_newtable(L);
lua_pushstring(L, "new");
lua_pushcfunction(L, LuaVector_lua_new);
lua_settable(L, -3);
lua_setglobal(L, "Vector");
}
It crashes with the code:
local vec1 = Vector.new(2, 2)
local vec2 = Vector.new(4, 4)
local vec3 = vec1 + vec2
I tried to isolate what caused it, but I couldn't determine the actual line that is faulty (though, I believe it's lua_gettable that triggers the error itself). So it could be anything, but I can't seem to figure it out.
lua_pop Pops n elements from the stack
But you have written lua_pop(L, -1) (which clears the entire stack). Change these to lua_pop(L, 1) to pop just the top of the stack.
I'm trying to get an understanding of how I can use co-routines to "pause" a script and wait until some processing is done before resuming.
Perhaps I'm looking at co-routines in the wrong way. But my attempt is structured similar to the example given in this answer.
The loop in loop.lua never reaches a second iteration, and hence never reaches the i == 4 condition required to exit the running loop in the C code. If I do not yield in loop.lua, then this code performs as expected.
main.cpp
#include <lua/lua.hpp>
bool running = true;
int lua_finish(lua_State *) {
running = false;
printf("lua_finish called\n");
return 0;
}
int lua_sleep(lua_State *L) {
printf("lua_sleep called\n");
return lua_yield(L,0);
}
int main() {
lua_State* L = lua_open();
luaL_openlibs(L);
lua_register(L, "sleep", lua_sleep);
lua_register(L, "finish", lua_finish);
luaL_dofile(L, "scripts/init.lua");
lua_State* cL = lua_newthread(L);
luaL_dofile(cL, "scripts/loop.lua");
while (running) {
int status;
status = lua_resume(cL,0);
if (status == LUA_YIELD) {
printf("loop yielding\n");
} else {
running=false; // you can't try to resume if it didn't yield
// catch any errors below
if (status == LUA_ERRRUN && lua_isstring(cL, -1)) {
printf("isstring: %s\n", lua_tostring(cL, -1));
lua_pop(cL, -1);
}
}
}
luaL_dofile(L, "scripts/end.lua");
lua_close(L);
return 0;
}
loop.lua
print("loop.lua")
local i = 0
while true do
print("lua_loop iteration")
sleep()
i = i + 1
if i == 4 then
break
end
end
finish()
EDIT: Added a bounty, to hopefully get some help on how to accomplish this.
Return code 2 from lua_resume is a LUA_ERRRUN. Check the string on the top of the Lua stack to find the error message.
A similar pattern has worked for me, though I did use coroutine.yield instead of lua_yield and I'm in C rather than C++. I don't see why your solution wouldn't work
On your resume call, it's not clear if you're over simplifying for an example, but I'd make the following changes in your while loop:
int status;
status=lua_resume(cL,0);
if (status == LUA_YIELD) {
printf("loop yielding\n");
}
else {
running=false; // you can't try to resume if it didn't yield
// catch any errors below
if (status == LUA_ERRRUN && lua_isstring(cL, -1)) {
printf("isstring: %s\n", lua_tostring(cL, -1));
lua_pop(cL, -1);
}
}
Edit 2:
For debugging, add the following before you run your resume. You've got a string getting pushed on the stack somewhere:
int status;
// add this debugging code
if (lua_isstring(cL, -1)) {
printf("string on stack: %s\n", lua_tostring(cL, -1));
exit(1);
}
status = lua_resume(cL,0);
Edit 3:
Oh good grief, I can't believe I didn't see this already, you don't want to run luaL_dofile when you're going to yield because you can't yield a pcall directly as best I know, which is what happens in the dofile (5.2 will pass it through, but I think you still need the lua_resume). Switch to this:
luaL_loadfile(cL, "scripts/loop.lua");
Last time i was messing with Lua coroutines I ended with such code
const char *program =
"function hello()\n"
" io.write(\"Hello world 1!\")\n"
" io.write(\"Hello world 2!\")\n"
" io.write(\"Hello world 3!\")\n"
"end\n"
"function hate()\n"
" io.write(\"Hate world 1!\")\n"
" io.write(\"Hate world 2!\")\n"
" io.write(\"Hate world 3!\")\n"
"end\n";
const char raw_program[] =
"function hello()\n"
" io.write(\"Hello World!\")\n"
"end\n"
"\n"
"cos = {}\n"
"\n"
"for i = 0, 1000, 1 do\n"
" cos[i] = coroutine.create(hello)\n"
"end\n"
"\n"
"for i = 0, 1000, 1 do\n"
" coroutine.resume(cos[i])\n"
"end";
int _tmain(int argc, _TCHAR* argv[])
{
lua_State *L = lua_open();
lua_State *Lt[1000];
global_State *g = G(L);
printf("Lua memory usage after open: %d\n", g->totalbytes);
luaL_openlibs(L);
printf("Lua memory usage after openlibs: %d\n", g->totalbytes);
lua_checkstack(L, 2048);
printf("Lua memory usage after checkstack: %d\n", g->totalbytes);
//lua_load(L, my_lua_Reader, (void *)program, "code");
luaL_loadbuffer(L, program, strlen(program), "line");
printf("Lua memory usage after loadbuffer: %d\n", g->totalbytes);
int error = lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1);
}
printf("Lua memory usage after pcall: %d\n", g->totalbytes);
for (int i = 0; i < 1000; i++) {
Lt[i] = lua_newthread(L);
lua_getglobal(Lt[i], i % 2 ? "hello" : "hate");
}
printf("Lua memory usage after creating 1000 threads: %d\n", g->totalbytes);
for (int i = 0; i < 1000; i++) {
lua_resume(Lt[i], 0);
}
printf("Lua memory usage after running 1000 threads: %d\n", g->totalbytes);
lua_close(L);
return 0;
}
It seems you could not load file as coroutine? but use function instead And it should be selected to the top of the stack.
lua_getglobal(Lt[i], i % 2 ? "hello" : "hate");
edit:
As I commented before, my problem is I can't create a table in C which uses strings as keys. I made a quick test program which demonstrates the problem I'm having:
Here is the C++ part of it:
#include <lua.hpp>
#include <String.h>
BString
LuaTypeToString(lua_State *L, int index, int type)
{
BString out;
switch (type)
{
case LUA_TSTRING:
{
out << "'" << lua_tostring(L, index) << "'";
break;
}
case LUA_TBOOLEAN:
{
out << (lua_toboolean(L, index) ? "true" : "false");
break;
}
case LUA_TNUMBER:
{
out << (float)lua_tonumber(L, index);
break;
}
default:
{
out << lua_typename(L, type);
break;
}
}
return out;
}
void
DumpLuaTable(lua_State *L, int tableIndex)
{
lua_pushnil(L);
printf("\t{ ");
while (lua_next(L, tableIndex) != 0)
{
BString keyString = lua_tostring(L, -2);
BString valueString;
int type = lua_type(L, -1);
if (type == LUA_TTABLE)
DumpLuaTable(L, lua_gettop(L));
else
valueString = LuaTypeToString(L, -1, type);
printf("%s=%s,",
keyString.String(),
valueString.String());
lua_pop(L, 1);
if (lua_isnumber(L, -1))
{
lua_pop(L, 1);
break;
}
}
printf(" }");
}
void
DumpLuaStack(lua_State *L)
{
printf("DumpLuaStack:\n");
int top = lua_gettop(L);
for (int i = 1; i <= top; i++)
{
int type = lua_type(L, i);
if (type == LUA_TTABLE)
DumpLuaTable(L, i);
else
printf("\t%s ", LuaTypeToString(L, i, type).String());
}
printf("\n");
}
static
int
ReturnTable(lua_State *L)
{
lua_newtable(L);
lua_pushnumber(L, 3.14);
lua_pushstring(L, "1");
lua_settable(L, -3);
DumpLuaStack(L);
return 1;
}
int
main(void)
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_pushcfunction(L, ReturnTable);
lua_setglobal(L, "ReturnTable");
int status = luaL_dofile(L, "luatest.lua");
if (status)
printf("Status = %d: %s\n", status, lua_tostring(L, -1));
lua_close(L);
return status;
}
And the Lua part of it:
function DumpTable(table)
print("LuaDumpTable")
local out = "\t"
if #table > 0 then
for i, v in ipairs(table) do
out = out .. i .. "=" .. v .. " "
end
print(out)
else
print("\tEmpty table")
end
end
value = ReturnTable()
if (type(value) == "table") then
DumpTable(value)
else
print(type(value))
end
The code as it is works without problem -- the Lua script prints out what you would expect. Change the line which pushes 3.14 to the stack to, say, lua_pushstring(L, "foo"); and all I get is an empty table on the Lua side. Any ideas what I'm doing wrong here?
You're checking the table incorrectly in Lua:
if #table > 0 then
The #table is just telling you that the first unused integer in the table is 0. When you have a string inserted into the table, it doesn't increment the count since it's not part of a continuous array, and you also can't process it with ipairs. Just switch to using pairs and you should see foo:
function DumpTable(table)
print("LuaDumpTable")
local out = "\t"
for i, v in pairs(table) do
out = out .. i .. "=" .. v .. " "
end
print(out)
end
Older note, left as an fyi:
I doubt this would cause the issue, but for API's being called from Lua, you typically want to pop the input off of the stack. So after the lua_tonumber call:
int32 value = lua_tonumber(L, 1);
lua_pop(L, 1);
rgb_color c = ui_color((color_which)value);