table to string

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

table to string

Brett Bibby
Hello,
When I encounter a return value after lua_pcall that is lua_istable, I want to get the table name as a string. Do I just call lua_tostring? Or is there some sort of "lua_totable"? (Although I don't want the table, just the name)
Thanks,
Brett

Reply | Threaded
Open this post in threaded view
|

RE: table to string

Nick Trout
> When I encounter a return value after lua_pcall that is lua_istable, I
> want to get the table name as a string. Do I just call lua_tostring?
Or is
> there some sort of "lua_totable"? (Although I don't want the table,
just
> the name)

Unfortunately the table name is ambiguous because several variables can
point to it, e.g.

A = {}
B = A

What is the name now? A or B, or both? (A: probably both) A and B are
really references to an object, which is a table. They are not the
"name" of the table.

You could add an entry in your table:

A = { name='foo' }

Then when you get your table back from lua_pcall you can

lua_pushliteral(L, "name");
lua_gettable(L, -2); // index to returned value
const char *name = lua_tostring(L, -1);
lua_pop(L, 1); // pop name

Or you could have a weak keyed table which has a reference to your
table, using the table reference as a key.

table_names = {}  -- make weak keyed...

A = {}
table_names[A] = 'foo'

-- get table name
print(table_names[a_table])