I am attempting to create a GC finalizer for a function value by storing it in a weak table using the C API.
I started off by writing a prototype in pure Lua 5.2:
local function myfinalizer()
print 'Called finalizer'
end
function myfunc()
print 'Called myfunc'
end
local sentinels = setmetatable({}, { __mode='k' })
sentinels[myfunc] = setmetatable({}, { __gc=myfinalizer })
myfunc()
myfunc = nil
collectgarbage 'collect'
print 'Closing Lua'
Resulting output:
Called myfunc
Called finalizer
Closing Lua
The prototype seems to be working as intended. Below is the C version:
#include <stdlib.h>
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
static int my_finalizer(lua_State *L)
{
puts("Called finalizer");
return 0;
}
static int my_func(lua_State *L)
{
puts("Called myfunc");
return 0;
}
int main(void)
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
// create sentinels table (weak keys) in registry
lua_newtable(L); // t
lua_newtable(L); // t mt
lua_pushstring(L, "k"); // t mt v
lua_setfield(L, -2, "__mode"); // t mt
lua_setmetatable(L, -2); // t
lua_setfield(L, LUA_REGISTRYINDEX, "sentinels"); //
// push global function and register as sentinel
lua_pushcfunction(L, my_func); // f
lua_getfield(L, LUA_REGISTRYINDEX, "sentinels"); // f t
lua_pushvalue(L, 1); // f t k
lua_newuserdata(L, 0); // f t k v
lua_newtable(L); // f t k v mt
lua_pushcfunction(L, my_finalizer); // f t k v mt v
lua_setfield(L, -2, "__gc"); // f t k v mt
lua_setmetatable(L, -2); // f t k v
lua_settable(L, -3); // f t
lua_pop(L, 1); // f
lua_setglobal(L, "myfunc"); //
// execute test script and exit
if (luaL_dostring(L, "myfunc(); myfunc=nil; collectgarbage'collect'")) {
printf("Error: %s\n", lua_tostring(L, -1));
}
lua_gc(L, LUA_GCCOLLECT, 0); // suggestion: two full gc cycles
fflush(stdout); // suggestion: immediate flush
puts("Closing Lua");
lua_close(L);
fflush(stdout);
return EXIT_SUCCESS;
}
Compiled using:
$ gcc -std=c99 -Wall -Werror -pedantic -O2 -o main main.c -ldl -llua52 -lm
Resulting output:
Called myfunc
Closing Lua
Called finalizer
The C version has a few minor differences:
Instead of a local sentinels table I am storing in the registry.
Using a zero sized userdata instead of a table for sentinel value with __gc metamethod.
I am confused as to why in the C version the myfunc finalizer doesn't execute after running a full collection cycle. What am I doing wrong?
As the Lua manual states:
Only objects that have an explicit construction are removed from weak tables. Values, such as numbers and light C functions, are not subject to garbage collection, and therefore are not removed from weak tables (unless its associated value is collected).
Your my_func is pushed without any upvalues, so it is a light C function, and it isn't removed from weak tables during garbage collection, so the associated userdata does not become garbage before you close the Lua state. Your code should work if you use a Lua function instead of my_func, or if you push my_func with upvalues (and if you fix the order of the arguments in the lua_gc call!).
To summarize, the following value types are not removed from weak tables (given that their associated keys/values aren't removed either):
booleans
numbers
strings
light userdata
light C functions (Lua 5.2 only)
As a consequence your program should work fine with Lua 5.1 because there are no light C functions (you still have to fix the lua_gc call).
Related
I am trying to wrap ncurses in Lua using the C API. I am working with the stdscr pointer: This is NULL before initscr is called, and initscr is called from Lua by design of my bindings. So in the driver function I do this:
// Driver function
LUALIB_API int luaopen_liblncurses(lua_State* L){
luaL_newlib(L, lncurseslib);
// This will start off as NULL
lua_pushlightuserdata(L, stdscr);
lua_setfield(L, -2, "stdscr");
lua_pushstring(L, VERSION);
lua_setglobal(L, "_LNCURSES_VERSION");
return 1;
}
This works as intended. The trouble comes when I need to modify stdscr. initscr is bound like this:
/*
** Put the terminal in curses mode
*/
static int lncurses_initscr(lua_State* L){
initscr();
return 0;
}
I need to moify the stdscr in the library to no longer be null. Example code from Lua side:
lncurses = require("liblncurses");
lncurses.initscr();
lncurses.keypad(lncurses.stdscr, true);
lncurses.getch();
lncurses.endwin();
But, lncurses.stdscr is NULL, so the it's essentially running the c equivalent of keypad(NULL, true);
My question being, how do I modify library values in Lua after the library is created?
You can use the registry.
Lua provides a registry, a predefined table that can be used by any C code to store whatever Lua values it needs to store. The registry table is always located at pseudo-index LUA_REGISTRYINDEX. Any C library can store data into this table, but it must take care to choose keys that are different from those used by other libraries, to avoid collisions. Typically, you should use as key a string containing your library name, or a light userdata with the address of a C object in your code, or any Lua object created by your code. As with variable names, string keys starting with an underscore followed by uppercase letters are reserved for Lua.
Store a reference to the module table in the registry on creation.
LUALIB_API int luaopen_liblncurses(lua_State* L) {
luaL_newlib(L, lncurseslib);
// This will start off as NULL
lua_pushlightuserdata(L, stdscr);
lua_setfield(L, -2, "stdscr");
lua_pushstring(L, VERSION);
lua_setglobal(L, "_LNCURSES_VERSION");
// Create a reference to the module table in the registry
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, "lncurses");
return 1;
}
Then when you initscr, update the field.
static int lncurses_initscr(lua_State* L) {
initscr();
// Update "stdscr" in the module table
lua_getfield(L, LUA_REGISTRYINDEX, "lncurses");
lua_pushlightuserdata(L, stdscr);
lua_setfield(L, -2, "stdscr");
return 0;
}
What is the best way to deal with nested lua_CFunction calls? Assume I have two function like this:
static int function2(lua_State *L) {
int i = luaL_checkint(L, 1);
/* do something */
return 1;
};
static int function1(lua_State *L) {
struct udata *u = luaL_checkudata(L, 1, UDATA_METATABLE);
int i = luaL_checkint(L, 2);
/* do something */
/* this does not work, first on call stack is udata, not int */
return function2(L);
};
The function call as above does not work. One option is to modify function2() to use the last element (index -1) on stack, but this is not a sollution in general since function2() might be called from various places with different call stacks.
Another way would be to replace the return function2(L); by
lua_pushcfunction(L, function2);
lua_pushvalue(L, 2);
lua_call(L, 1, 1); /* need to know number of results */
I assume this gives function2() its own call stack so there is no need to modify it. But this sollution seems overly complicated for functions with more parameters since it requires duplicating all of them on the stack.
tl;dr: What is the recommended way/a good way to call a lua_CFunction from inside another one?
In function1 you are expecting the bottom of the stack to be user data.
When you call function2 directly, the LuaState has not changed and therefore the bottom is still user data.
You can successfully call function2 from function1 by ensuring an integer is at index 1.
You could do this by calling lua_insert(L, 1) which will move the top (assuming index 2), to index 1.
You could also do this by popping all values the pushing the integer back on:
lua_pop(L, lua_gettop(L));
lua_pushnumber(L, i);
return function2(L);
lua_CFunction is not fully Lua function. It just a way to create Lua function/closure.
static int function1(lua_State *L) {
....
int top = lua_gettop(L);
lua_pushcfunction(L, function2);
lua_pushvalue(L, 2);
lua_call(L, 1, LUA_MULTRET);
return lua_gettop(L) - 1;
}
The Lua equivalent is
function function1(arg)
return (function(arg) --[[body of f2]] end)(arg)
end
So you create each time new function and call it.
For C function it is quite ok because it no need to compile and you do not need upvalue.
Also Lua 5.2 introduce light function for that.
But if you want equivalent for
int i = 1
local function function2(arg)
i = i + 1
...
end
function function1(arg)
return function2(arg)
end
You need a way to find real Lua function e.g. (not tested)
int f2_ref;
static int function1(lua_State *L) {
...
-- push `function2` on stack
lua_rawgeti(L, LUA_REGISTRYINDEX, f2_ref);
-- as above
}
static int function2(lua_State *L) {
int my_upvalue = lua_tonumber(L, lua_upvalueindex(1));
my_upvalue++;
lua_pushnumber(L, my_upvalue);
lua_replace(L, lua_upvalueindex(1));
...
}
int luaopen_foo(lua_State *L){
-- Here we create instance of Lua function(closure)
lua_pushnumber(L, 1);
lua_pushcclosure(L, function2, 1);
f2_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pushcclosure(L, function1, 0);
return 1;
}
I'd say that calling it through lua is the recommended way to do that but if you don't want to do that for some reason then Timma's suggestions are the right ones.
I want to get several parameters in Lua from a C function.
I tried to push several arguments on the lua stack:
static int myFunc(lua_State *state)
{
lua_pushnumber(state, 1);
lua_pushnumber(state, 2);
lua_pushnumber(state, 3);
return 1;
}
and call it in Lua like this:
local a,b,c = myFunc()
Unfortunately b and c values are nil. I dont want to write a function for every value I need but to take advantage of Luas capabilities to retrieve several arguments from a function.
The return value of the C function is the number of values returned.
Change it to return 3; and you're good to go.
Here, have a reference from Programming in Lua:
static int l_sin (lua_State *L) {
double d = lua_tonumber(L, 1); /* get argument */
lua_pushnumber(L, sin(d)); /* push result */
return 1; /* number of results */
}
While trying to call a C function from Lua module, using Lua-lanes, the control doesn't transfer to the 'C' function. Is there any problem with which Lua-lanes won't work in a threaded way with an external C dll?
Below is the code snippet
Lua Snippet:
lanes.gen("*",func)
thread = func()
thread:join()
function func()
foo() -- expected to print "Hello world", by
-- calling below C function,but not happening
end
C snippet compiled to a dll with VS-2012:
static int foo(lua_state *L)
{
printf("Hello world\n")
}
If you want that C function accessible in the new thread then you have to somehow transfer that from the main lua thread over to the new thread when you create the lane. You can do this by using .required from the lua-lane docs.
For example, say you have this simple foomodule:
// foomodule.c
// compiles to foomodule.dll
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
static int foo(lua_State *L)
{
printf("Hello world\n");
return 0;
}
int luaopen_foomodule(lua_State *L)
{
lua_pushcfunction(L, foo);
lua_pushvalue(L, -1);
lua_setglobal(L, "foo");
return 1;
}
And from your lua script:
// footest.lua
lanes = require 'lanes'.configure()
function func()
print("calling foo", foo)
return foo()
end
thr = lanes.gen("*", {required = {'foomodule', }}, func)
thr():join()
One possible output:
calling foo function: 0x003dff98
Hello world
You are using lanes wrong. This is what you need to do:
function func()
foo() -- expected to print "Hello world", by
-- calling below C function,but not happening
end
local launcher = lanes.gen("*", func)
thread = launcher()
thread:join()
That should work fine.
From the documentation, I understand that a new thread created, must be
properly anchored before use.
To do that, I want to keep a reference to the new thread in the registry,
(Table[thread-addr] = thread) for that, I am doing this:
lua_State *L = NULL;
lua_State *L1 = NULL;
int tref = LUA_NOREF;
L = luaL_newstate(); // main lua thread/state
// create a table in registry: Table[thr-addr] = Thread
lua_newtable(L);
tref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pop(L, 1);
L1 = lua_newthread(L);
// Anchor it
lua_rawgeti(L, LUA_REGISTRYINDEX, tref);
lua_pushnumber(L, (ptrdiff_t) L1);
lua_pushlightuserdata(L, L1);
lua_settable(L, -3);
Once I am done with the thread, I plan to set the Table[thread-addr] = nil
Is this sufficient ? or I should also set a meta-table to it, with weak keys/values ?
Thanks.
Once I am done with the thread, I plan to set the Table[thread-addr] = nil Is this sufficient? or I should also set a meta-table to it, with weak keys/values?
A weak table is used if you don't want objects it references to count as real 'strong' references. So Lua is allowed to GC an object if there are no other references to it even if the weak table still refers to that object.
From the use case you described, making Table weak probably isn't appropriate here since Lua might collect that coroutine object before you have a chance to use it.
Also your example code here:
L1 = lua_newthread(L);
// Anchor it
lua_rawgeti(L, LUA_REGISTRYINDEX, tref);
lua_pushnumber(L, (ptrdiff_t) L1);
lua_pushlightuserdata(L, L1);
lua_settable(L, -3);
the lua_pushlightuserdata function is meant for C pointers. The coroutine object lifetime won't get managed by Lua correctly if you tell it to treat the coroutine object like a C data pointer. For this you probably meant to use lua_pushthread instead.