lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I think this must have been asked before, but for now I can’t find a
matching thread…

I’m doing a small binding to a C++ lib and I liked to copy it’s "named
parameter" style (and deferred initialisation)

Foo f;
f.setA( 1 )
 .setB( 2 )
 .setC( 3 );

(sorry for the lame simplification) to Lua as

Foo = require "FooLib"
f = Foo.Foo()
f:setA( 1 )
 :setB( 2 )
 :setC( 3 )

The implementation looks like this:

int foo_setA( lua_State *L ) {
    Foo **d = (Foo**) luaL_checkudata( L, 1, "Foo" );
    (*f)->setA( luaL_checkint( L, 2 ) );
    lua_pushvalue( L, 1 ); // lua_pushuserdata missing?
    return 1;
}

This is where I missed a lua_pushuserdata API function. I used
lua_pushvalue instead as shown.

Is this usage save? Or do I have to expect some problems gc-wise? The
manual says "Pushes a copy of the element at the given valid index
onto the stack." The word "copy" causes a slight feeling of unease. I
hope it is still the same userdata object from the Lua perspective.

Regards,
Matthias