lua-users home
lua-l archive

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

On Tue, Jun 23, 1998 at 02:15:49PM -0300, Norman Ramsey wrote:
>  > you can always store the pointer as userdata, but you won't be able to
>  > use it as string without duplicating the string in Lua.
> I'm saddened by this.  My code has two copies of every `unique'
> string: one for C code and one for Lua code.  I use the C interface
> because it's extremely fast, and I can't find an obvious way to use
> the Lua unique-string table from C.  (I don't want these strings
> garbage-collected.)  I'm probably missing something.  What?

Yeah, you are missing something. Just do this:

| lua_pushstring(my_string_pointer);
| my_string_ref = lua_ref(1);

Then you will have a handle to the unique string inside Lua. The "1"
argument to lua_ref tells it to lock the string so lua won't GC it.

I do this all the time because this:

| lua_pushobject(a_table);
| lua_pushobject(lua_getref(my_string_ref));
| foo = lua_gettable();

Is faster than this:

| lua_pushobject(a_table);
| lua_pushstring(my_string_pointer);
| foo = lua_gettable();

David Jeske (N9LCA) + +