3

Hi and thanks in advance. I'm trying to see if there's a way to avoid learning C (which I don't know at all) to turn userdata into a Lua table.

I'm using an application which lets users write addons using Lua scripts. These addons can query the application's underlying data. However the query results are returned as userdata and I need them to be available back in the script.

I haven't been able to find anything in the applications documentation about working with returned datasets. The only description given is that the operation I'm running:

Executes the currently assigned query string and uses the results to construct either a DataSet object if multiple result sets are returned or a DataTable if one or no result sets are returned.

In my case I'll be getting multiple result sets, I'm referring to it as userdata because that's what I get when I call type() on the query result.

I've looked at the Lua documentation which seems like it could be useful but lacking any familiarity with C I have no idea what I'm looking at. The metatable tells me I have access to the __index, __newindex, __tostring, and __gc metamethods. Calling tostring gives me a seven-digit integer which is (I think) completely unrelated to my data. It's possible I'm not even getting the data I want back at all, but I can't figure a way to check that. Online resources either say this can't be done or provide solutions in C which are probably clever, clean, and awesome but I don't understand them.

So I'm looking for some more definitive guidance on whether I can/should do this, if there's a way to do this without C (or just by blindly copying someone else's code), or -- if I need to use C for this -- if we're talking about the 90 minute or the 7 hour Lynda Introduction to C course to get where I need to be?

(and apologies if I've inadvertently violated any community norms with this question...it's my first time posting)

Liam
  • 33
  • 5
  • 5
    Usually a userdata has some properties and methods available to Lua script. Ask userdata's author for Lua API to access userdata. – Egor Skriptunoff Jan 05 '18 at 18:37
  • You should remove the `c` tag since this question appears to be concerned with how to interpret a generic Lua `userdata` object within a Lua script, which has nothing to do with C. – Mark Benningfield Jan 05 '18 at 18:47
  • Thank you for both for the comments. I've removed the c tag. Based on what you've both said it seems like there should be a way within lua to access userdata directly, but I've only been able to find resources online that describe how to write methods in C which then get attached to the Lua metadata table. At least that's what I took out of the answer provided [here](https://stackoverflow.com/questions/4329643/what-is-userdata-and-lightuserdata-in-lua). The only methods already existent on the data are the ones I referenced above. – Liam Jan 05 '18 at 18:56
  • As Egor pointed out, if the app you're using provides a `userdata` object, then the documentation for the scripting portion of the application should specify which methods are defined for use with that object. – Mark Benningfield Jan 05 '18 at 19:12
  • Regrettably the documentation provided by the application is a little spotty in this area. I've tried contacting the company directly but for a variety of unrelated reasons having to do with new cloud-based products they weren't interested in providing deeper support for this feature of this application. I've edited the question to include what is provided in the documentation and will keep looking through it to see if I've missed a solution. Thanks again! – Liam Jan 05 '18 at 19:31
  • There is no way in Lua to access a "userdata" object because these objects come from outside Lua. Either it is a "light userdata" in which case it is a pointer and won't be of much use to you, or a proper userdata which should have a metatable you can inspect - use *getmetatable* and look what's inside. – IS4 Jan 06 '18 at 23:24
  • Thanks @IllidanS4, looking at the metatable I see a function called `__newindex`, an empty table called `cache`, something with a key of type `userdata` and a value of `1`, the function `__tostring`, the function `__index`, and the function `__gc`. I think it might be time to tell my boss I won't be able to make this work but I appreciate everyone's time. – Liam Jan 07 '18 at 22:58
  • That is actually good. `__newindex` gets called when you assign `v[k]` and `__index` gets called when you retrieve `v[k]` (`v` is the value and `k` is the key). Sadly, the actual function will probably be implemented internally, so you can only guess the method names. Of course you can find the names if you used some debugging tools, but Lua itself provides no reflection for "outside" objects (as there cannot be one). It is at the mercy of the `__index` function whether it will give you a function or not, but its presence is good, it means there is *something* that can be used. – IS4 Jan 07 '18 at 23:08
  • Well that's encouraging! Thanks for the prompt response and I'll put some more time into investigating what might be there. – Liam Jan 08 '18 at 02:55

0 Answers0