[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: How to handle meta:__index read nil
- From: Alan Hightower <alan@...>
- Date: Sun, 27 May 2007 09:37:43 -0400 (EDT)
I have an application where I'm trying to shadow a Lua tree structure with
my own dynamic data structures using meta __index and __newindex methods
to get data in and out. However when I try to use a named index into a
table, I'm not sure how to handle the resulting Lua call to the __index
meta method. If I use a numerical index, it calls the __index method
with a usable key value. However when I use an alphanumeric index,
It calls me with the first argument 'nil'. I had assumed the symantics
were similar to lua_next in that it was trying a first/next/next/etc till
it found a matching index. But apparently whatever I return it treats as
a match and I can't get a desired index name from a nil. e.g.:
LUA release: Lua 5.1.2
pwl.scalar._children = {};
pwl.scalar._my_meta = {};
pwl.scalar._my_meta.__index = _proc_lua_read;
pwl.scalar._my_meta.__newindex = _proc_lua_assignment;
pwl.scalar._my_meta.__gc = _proc_lua_gcollect;
setmetatable (pwl.scalar, pwl.scalar._my_meta);
Then at this point I add an entry to pwl.scalar._children via the C API
and then send the following to the LUA interpreter:
pwl.scalar[mainvid]._children = {};
And get the follwing on the LUA stack when the __index method for
pwl.scalar is invoked:
------------------------------------------------
LUA-Stack: top/2 entries
-1 (unknown type = nil)
-2 (table)
- index 1
- (string) `_my_meta'
- (table)
- index 1
- (string) `__gc'
- (function) `0x2d3fbf'
- index 2
- (string) `__newindex'
- (function) `0x2d3fea'
- index 3
- (string) `__index'
- (function) `0x2d35a5'
- index 2
- (string) `_children'
- (table)
- index 1
- (string) `mainvid'
- (table)
- index 1
- (string) `_pwl_scalar_mainvid'
- (unknown type = userdata)
- index 3
- (string) `_pwl_scalar'
- (unknown type = userdata)
Whatever table entry I return, Lua assumes is the 'correct' table, never
calls __index again, and performs the above assignment on the result. And
if I return anything other than a table type, I get an error. So what
does LUA expect or how do I infer the table index Lua wants?
Thanks for any help.
--
-------------
Alan Hightower - alan@alanlee.org