Lua C API register fallback __index - c

I would like to know how I can write something like this Lua Snippet from http://lua-users.org/wiki/MetamethodsTutorial
local func_example = setmetatable({}, {__index = function (t, k)
return "key doesn't exist"
end})
local fallback_tbl = setmetatable({
foo = "bar",
[123] = 456,
}, {__index=func_example})
local fallback_example = setmetatable({}, {__index=fallback_tbl})
print(func_example[1]) --> key doesn't exist
print(fallback_example.foo) --> bar
print(fallback_example[123]) --> 456
print(fallback_example[456]) --> key doesn't exist
in the lua C api. I.e. I want lua to first check whether a key is in a members metatable and otherwise call the __index implementation. I have come up with something like this:
static const struct luaL_reg
lobj_fallback[] = {
{"__index", lobject_index},
{"__newindex", lobject_newindex},
{"__tostring", lobject_tostring},
{NULL, NULL},
};
static const struct luaL_reg
lobj_members[] = {
{"delete", lobject_delete},
{NULL, NULL}
};
{
// ....
luaL_newmetatable(L, "MyMetaTable");
luaL_register(L, NULL, lobj_members);
luaL_newmetatable(L, "MyMetaTableFallback");
luaL_register(L, NULL, lobj_fallback);
lua_setmetatable(L, -2);
// ...
}
Yet this does not work as expected, processing the fallback __index works but not the members metatable ("attempt to call method 'delete' (a nil value)").

If your intention is to fallback to the indexing function when accessing "MyMetaTable", then you have swapped the names of your table and metatable. Thus the members table (lobj_members) becomes called "MyMetaTableFallback" and the metatable (lobj_fallback) becomes called "MyMetaTable".

Related

Lua userdata array access and methods

I am writing in C a userdata type for use in Lua. It has some array-type properties and various methods aswell. Right now if u is of this type, I use u:set(k,v) resp. u:get(k) to access data and e.g. u:sort() as method. For this I set __index to a table containing these methods. Now if I want to access the data using u[k] = v or u[k], I need to set __newindex and __index to set resp get. But then the other methods are no longer accessible...
What's the best way to deal with this in C? I am guessing I need to write a function in C to register as __index and somehow deal with it there. Maybe check if key belongs to a Lua table of methods and if so call it.
Any help/hints would be appreciated. I did not find examples like this, although it seems a very natural thing to do (to me.)
edit: Added my C version of the solution in Lua posted in the answer below. This is more or less a direct translation, so all credit goes to #gilles-gregoire .
The following C function is registered as __index metamethod.
static int permL_index(lua_State *L) {
struct perm **pp = luaL_checkudata(L, 1, PERM_MT);
int i;
luaL_getmetatable(L, PERM_MT);
lua_pushvalue(L, 2);
lua_rawget(L, -2);
if ( lua_isnil(L, -1) ) {
/* found no method, so get value from userdata. */
i = luaL_checkint(L, 2);
luaL_argcheck(L, 1 <= i && i <= (*pp)->n, 2, "index out of range");
lua_pushinteger(L, (*pp)->v[i-1]);
};
return 1;
};
This is the code that does that,
int luaopen_perm(lua_State *L) {
luaL_newmetatable(L, PERM_MT);
luaL_setfuncs(L, permL_methods, 0);
luaL_setfuncs(L, permL_functions, 0);
lua_pop(L, 1);
luaL_newlib(L, permL_functions);
return 1;
};
where permL_methods is
static const struct luaL_Reg permL_methods[] = {
{ "__index", permL_index },
{ "__eq", permL_equal },
{ "__tostring", permL_tostring },
{ "__gc", permL_destroy },
[...]
{ NULL, NULL }
};
and permL_functions is
static const struct luaL_Reg permL_functions[] = {
{ "inverse", permL_new_inverse },
{ "product", permL_new_product },
{ "composition", permL_new_composition },
[...]
{ NULL, NULL }
};
This looks like a problem which can be solved with nested metatables. You need one metatable for the methods (like your sort() method), and a second one for index operations. That second metatable is actually the metatable of the methods metatable.
Let me write this as lua code. You need 3 tables:
-- the userdata object. I'm using a table here,
-- but it will work the same with a C userdata
u = {}
-- the "methods" metatable:
mt = {sort = function() print('sorting...') end}
-- the "operators" metatable:
op_mt = {__index = function() print('get') end}
Now, the tricky part is here: lua will first lookup u when you will call a method.
If it does not find it, it will lookup in the table pointed by the __index field of u's metatable... And Lua will repeat the process for that table!
-- first level metatable
mt.__index = mt
setmetatable(u, mt)
-- second level metatable
setmetatable(mt, op_mt)
You can now use your u like this:
> u:sort()
sorting...
> = u[1]
get
nil
EDIT: a better solution by using a function for the __index metamethod
Using a function for the __index metamethod is probably the right way to this:
u = {}
mt = {sort = function() print('sorting...') end}
setmetatable(u, mt)
mt.__index = function(t, key)
-- use rawget to avoid recursion
local mt_val = rawget(mt, key)
if mt_val ~=nil then
return mt_val
else
print('this is a get on object', t)
end
end
Usage:
> print(u)
table: 0x7fb1eb601c30
> u:sort()
sorting...
> = u[1]
this is a get on object table: 0x7fb1eb601c30
nil
>

LuaJit - Get metatable from module/package and assign it to userdata

Say I have this metatable for a custom struct vector2_t which is inside a module mymod like this:
local mymod = {}
local ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
typedef struct
{
double x;
double y;
} vector2_t;
]]
local vector2_t
vector2_t = ffi.metatype("vector2_t", {
__eq = function(lhs, rhs)
if lhs.x == rhs.x and lhs.y == rhs.y
then return true else return false end
end
-- Member functions follow...
})
vcmp.vector2 = vector2_t
-- I use this method because the script is integrated in C/C++ as a string and not
-- as a file and I need a constant name that isn't decided by the file name
package.preload["mymod"] = function(mod) return mymod end
And in another script I have this callback/event listener function which must receive a vector2_t as it's argument:
local mymod = require "mymod"
local some_pos = mymod.vector2(32, 29.7)
-- That pos argument must be an instance of mymod.vector2_t
function position_update(pos)
print("New Pos: " .. pos.x .. ", " .. pos.y .. "\n")
some_pos.x = pos.x
some_pos.y = pos.y
end
Now, I must call that callback/event listener function from C/C++ and pass an instance of that vector2_t (along with it's associated metatable) as the parameter to that function.
typedef struct
{
double x;
double y;
} vector2_t;
void call_position_update(lua_State* L, double x, double y)
{
// Retrieve the function
lua_getglobal(L, "position_update");
// Validate it
if (!lua_isfunction(L, lua_gettop(L)))
{
lua_pop(L, 1);
return;
}
// Create an instance of vector2_t
vector2_t *pos = (vector2_t *)lua_newuserdata(L, sizeof(vector2_t));
// Assign the values to the new instance
pos->x = x;
pos->y = y;
// How do I get the meta table vector2_t on to the stack?
// Reach to the module somehow...
// Get the meta table from the module
luaL_getmetatable(L, "vector2_t");
// Assign the meta table to the vector2_t instance already on the stack
lua_setmetatable(L, -2);
// I'm assuming that the vector2_t instance is already on the stack so there's nothing else to push
// Call the function with 1 argument which should be that vector2_t onto the stack
if (!lua_pcall(L, 1, 0, 0))
{
printf("Error calling function 'position_update': %s\n", lua_tostring(_Lua, -1));
}
}
I'm a bit lost and I don't know how to pas an instance of that vector2_t as the function parameter. I'm sorry for posting so much code bu I wanted to be sure that I explained correctly.
cdata and userdata are completely different things. The only interaction they have is that you can get an FFI void* pointer to userdata. Notably, there is no C API for cdata objects. Mixing them is sure to cause you a lot of headaches.
Pick either the Lua C API or the FFI, and stick to it as much as possible.
To directly answer your question:
how to pas [sic] an instance of that vector2_t as the function parameter
To a Lua C API function, it gets passed on the stack, just like other values. Note that it will be a cdata typed object, not a userdata object. Notably, you can't get a pointer to it.
How do I get the meta table vector2_t on to the stack?
You can't, since you don't make the metatable accessible to outside scripts in your first script. luaL_getmetatable only works with metatables created with luaL_newmetatable

Creating referenced table element

I created a more or less complex table in C. Now I want to create a reference on a lower level of the tree. Is this possible?
Idea:
ELEM000 +--> ELEM010
+--> ELEM020 +--> ELEM120
| +--> **ELEM121**
| +--> ELEM122
+--> ELEM030 +--> ELEM130
| +--> ELEM131
| +--> ELEM132
+--> **ELEM121**
The ELEM121 should also be visible one level above, i.e. be a reference
I added an example of what I wanted to to..
void PushL(lua_State *L, const char * str) {
char s[255];
strcpy(s, "ELEM"); strcat(s, str); lua_pushstring(L, s); // key
strcpy(s, "Value"); strcat(s, str); lua_pushstring(L, s); // value
lua_settable(L, -3);
}
void MakeTable( lua_State *L )
{
lua_pushstring(L, "TBL0"); // name of sub-table
lua_createtable(L, 0, 0);
lua_checkstack(L, 3);
{
PushL(L, "000");
lua_pushstring(L, "TBL1");
lua_createtable(L, 0, 0);
lua_checkstack(L, 3);
{
PushL(L, "010");
PushL(L, "020");
lua_pushstring(L, "TBL2");
lua_createtable(L, 0, 0);
lua_checkstack(L, 3);
{
PushL(L, "120");
PushL(L, "121");
PushL(L, "122");
lua_settable(L, -3);
}
PushL(L, "030");
lua_pushstring(L, "TBL3");
lua_createtable(L, 0, 0);
lua_checkstack(L, 3);
{
PushL(L, "130");
PushL(L, "131");
PushL(L, "132");
lua_settable(L, -3);
}
lua_settable(L, -3);
}
lua_pushstring(L, "ELEM121");
lua_pushstring(L, "SHOULD BE A REFERENCE TO ELEM121");
lua_settable(L, -3);
}
lua_setglobal(L,"____myTable");
}
Bottom line: In Lua, field variables cannot be referenced but there are ways of doing what you want.
Here is a comparison between C data structures and Lua data structures.
In C, you either:
have a copy of the value in two places
You can create copies in Lua, too.
or, have a pointer in one place that points to the other place.
In C, that means you'd have to access them differently, one with a pointer deference, the other without.
In Lua, you could have a function in one place that returns the value in the other place. That means you'd have to access them differently, one with a function call, the other without.
The following is equivalent to a read-only pointer:
local ELEM000 = {
ELEM010 = "ELEM010 value",
ELEM020 = {
ELEM120 = "ELEM120 value",
ELEM121 = "ELEM121 value",
ELEM122 = "ELEM122 value" },
ELEM030 = {
ELEM130 = "ELEM130 value",
ELEM131 = "ELEM131 value",
ELEM132 = "ELEM132 value" },
ELEM121 = function(self) return self.ELEM020.ELEM121 end }
print(ELEM000.ELEM020.ELEM121)
print(ELEM000:ELEM121())
ELEM000.ELEM020.ELEM121 = ELEM000.ELEM020.ELEM121 .. " updated"
print(ELEM000.ELEM020.ELEM121)
print(ELEM000:ELEM121())
If you need a writable pointer then another approach would be needed.
Update
A simple way for a writable pointer is to add an optional value parameter. This is commonly used in JavaScript APIs but JavaScript has the advantage of the undefined data type. In Lua, we'll have to use nil, which means that you can't write nil as a value.
ELEM121 = function(self, value)
if (value ~= nil) then self.ELEM020.ELEM121 = value end
return self.ELEM020.ELEM121
end
For true read-write field access, use the __index and __newindex metatmethods. This requires that the field actually not have a key in the table. The metamethods are invoked when indexing a non-existing field for reading (__index) or for writing (__newindex).
local ELEM000 = {
ELEM010 = "ELEM010 value",
ELEM020 = {
ELEM120 = "ELEM120 value",
ELEM121 = "ELEM121 value", -- captured as the initial value
ELEM122 = "ELEM122 value"},
ELEM030 = {
ELEM130 = "ELEM130 value",
ELEM131 = "ELEM131 value",
ELEM132 = "ELEM132 value" },
ELEM121 = nil -- ignored
}
setmetatable(ELEM000, {
__index = function(base, key)
if (key=="ELEM121") then return base.ELEM020.ELEM121
else return nil end end,
__newindex = function (base, key, value)
if (key=="ELEM121") then base.ELEM020.ELEM121 = value
else rawset(base, key, value) end end })
setmetatable(ELEM000.ELEM020, {
ELEM121 = ELEM000.ELEM020.ELEM121, --[[ backing storage for field,
initialized to existing value]]
__index = function(base, key)
if (key=="ELEM121") then return getmetatable(base).ELEM121
else return nil end end,
__newindex = function (base, key, value)
if (key=="ELEM121") then getmetatable(base).ELEM121 = value
else rawset(base, key, value) end end })
-- make sure metamethods will be invoked on these fields
rawset(ELEM000, "ELEM121", nil)
rawset(ELEM000.ELEM020, "ELEM121", nil)
Usage:
print(ELEM000.ELEM020.ELEM121)
print(ELEM000.ELEM121)
ELEM000.ELEM020.ELEM121 = ELEM000.ELEM020.ELEM121 .. " updated"
print(ELEM000.ELEM020.ELEM121)
print(ELEM000.ELEM121)
ELEM000.ELEM121 = ELEM000.ELEM121 .. " again"
print(ELEM000.ELEM020.ELEM121)
print(ELEM000.ELEM121)
There various places to store the backing field and different styles of coding the metamethods. This code is perhaps too concise. And, I leave it to you to code it in Lua C API, if you wish.

lua userdata with array access and object oriented access?

I'm writing a "C" userdata array structure.
As setter and getter i want normal array access (u[0] = 1 u[0]) like it's discussed here:
[c array share][1]Share Array between lua and C.
For that i need to set __index and __newindex to the set and get functions in c.
Additional i want object-oriented access, too, "like u:mymethod()". My trouble is, that i need to set now __index to the metatable itself.
Is there a way, to achieve both?
Just one of many possible ways to achieve this:
local userdata = { _array = { "A", "B", "C" } }
local mt = { }
local methods = { }
function mt.__index(userdata, k)
if methods[k] then
return methods[k]
else
return rawget(userdata, "_array")[k]
end
end
function mt.__newindex(userdata, k, v)
if methods[k] then
error "can't assign to method!"
else
rawget(userdata, "_array")[k] = v
end
end
function methods.count(userdata)
return #rawget(userdata, "_array")
end
setmetatable(userdata, mt)
userdata[3] = "Z"
print(userdata[1])
print(userdata[2])
print(userdata[3])
print(userdata:count())
userdata.count = 0
edit: As lhf pointed in his comment, it is not dangerous to use metatable as it's __index table at all, because c-methods should always check on what self they operate.

Is there something wrong with how I'm referencing my instances in this C extension?

I'm having some issues where when if I run this C extension outside of a Rails environment it works, but when I run inside Rails it gives me a stack dump.
I get this error message:
NoMethodError Exception: undefined method `evaluate' for #<String:0x00000103557db0>
This is presumably referring to the calls I am making within the EV::Counters evaluate function, to the "evaluate" functions that exist in the three instances that I am calling.
Strangely valgrind is not giving me any errors. But I think there is something basic I might be doing wrong with how I reference my instances?
VALUE rFlushInstance, rPairCounterInstance, rStraightInstance;
static VALUE
evaluate(VALUE self, VALUE val, VALUE suit, VALUE index)
{
rb_funcall(rFlushInstance, rb_intern("evaluate"), 3, val, suit, index);
rb_funcall(rStraightInstance, rb_intern("evaluate"), 2, val, index);
rb_funcall(rPairCounterInstance, rb_intern("evaluate"), 2, val, index);
return Qnil;
}
VALUE EV;
void Init_counters()
{
EV = rb_define_module("EV");
VALUE Counters = rb_define_class_under(EV, "Counters", rb_cObject);
init_pair_counter();
init_straight();
init_flush();
VALUE Flush = rb_const_get(EV, rb_intern("Flush"));
VALUE PairCounter = rb_const_get(EV, rb_intern("PairCounter"));
VALUE Straight = rb_const_get(EV, rb_intern("Straight"));
rFlushInstance = rb_class_new_instance(0, NULL, Flush);
rStraightInstance = rb_class_new_instance(0, NULL, Straight);
rPairCounterInstance = rb_class_new_instance(0, NULL, PairCounter);
rb_define_method(Counters, "initialize", initialize_counters, 2);
rb_define_method(Counters, "evaluate", evaluate, 3);
}
What I needed to do was to store the instances as instance variables, like:
VALUE rPairCounterInstance = rb_class_new_instance(0, NULL, PairCounter);
rb_ivar_set(self, rb_intern("#pair"), rPairCounterInstance);

Resources