lua-users home
lua-l archive

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


Am 18.12.10 10:16, schrieb Marc Balmer:
> I am still fiddling with Lua - ClearSilver HDF interface.  I wrote index
> and newindex in C and for debugging purpose they output what is being
> indexed, newindexed.
> 
> The Lua code puts in a loop database results in a HDF container:
> 
> 	hdf.bset = {}
> 	for n = 1, res:ntuples() do
> 		hdf.bset[n] = {}
> 		hdf.bset[n].id = res:getvalue(n, 1)
> 		hdf.bset[n].name = res:getvalue(n, 2)
> 		hdf.bset[n].description = res:getvalue(n, 3)
> 	end
> 
> And as LHF pointed out, Lua wise the following snipped should be equivalent:
> 
> 	hdf.bset = {}
> 	for n = 1, res:ntuples() do
> 		hdf.bset[n] = {
> 			id = res:getvalue(n, 1),
> 			name = res:getvalue(n, 2),
> 			description = res:getvalue(n, 3)
> 		}
> 	end
> 
> Hower, the C functions are called differently, in the first case it
> looks like this:
> 
> lua: newindex 'bset'
> lua: index 'bset', no value found, returning object
> lua: newindex '1'
> lua: index 'bset', no value found, returning object
> lua: index '1', no value found, returning object
> lua: newindex 'id' (2)
> lua: index 'bset', no value found, returning object
> lua: index '1', no value found, returning object
> lua: newindex 'name' (gv-lf-zl)
> lua: index 'bset', no value found, returning object
> lua: index '1', no value found, returning object
> lua: newindex 'description' (Lieferung auf Ziel.)
> lua: index 'bset', no value found, returning object
> lua: newindex '2'
> lua: index 'bset', no value found, returning object
> lua: index '2', no value found, returning object
> lua: newindex 'id' (7)
> lua: index 'bset', no value found, returning object
> lua: index '2', no value found, returning object
> lua: newindex 'name' (test)
> lua: index 'bset', no value found, returning object
> lua: index '2', no value found, returning object
> lua: newindex 'description' (Test)
> lua: index 'bset', no value found, returning object
> lua: newindex '3'
> lua: index 'bset', no value found, returning object
> lua: index '3', no value found, returning object
> lua: newindex 'id' (6)
> lua: index 'bset', no value found, returning object
> lua: index '3', no value found, returning object
> lua: newindex 'name' (wh-vb)
> lua: index 'bset', no value found, returning object
> lua: index '3', no value found, returning object
> lua: newindex 'description' (Verkauf von Handelsware gegen bar)
> 
> In the second case, with the nicer looking constructor, at lot less C
> gets called:
> 
> lua: newindex 'bset'
> lua: index 'bset', no value found, returning object
> lua: newindex '1'
> lua: index 'bset', no value found, returning object
> lua: newindex '2'
> lua: index 'bset', no value found, returning object
> lua: newindex '3'
> 
> What am I missing here?  I can post the C code, if that helps.

After instrumenting res:getvalue() with some debug printfs, I see that
Lua actually calls this function before calling newindex, so it fetches
the values from the database, then calls newindex.  Are the names and
values of the fields in the constructor accessible somehow?  Here is the
new trace of the second case (direct initialization of the "table"):

lua: newindex 'bset'

lua: index 'bset', no value found, returning object
lua: getvalue 0, 0 = '2'
lua: getvalue 0, 1 = 'gv-lf-zl'
lua: getvalue 0, 2 = 'Lieferung auf Ziel.'
lua: newindex '1'

lua: index 'bset', no value found, returning object
lua: getvalue 1, 0 = '7'
lua: getvalue 1, 1 = 'test'
lua: getvalue 1, 2 = 'Test'
lua: newindex '2'

lua: index 'bset', no value found, returning object
lua: getvalue 2, 0 = '6'
lua: getvalue 2, 1 = 'wh-vb'
lua: getvalue 2, 2 = 'Verkauf von Handelsware gegen bar'
lua: newindex '3'