kikito/inspect.lua

support userdata

guileen opened this issue · 5 comments

If inspect can inspect userdata, will be wonderful.

for example a userdata point, point.x = 5, point.y = 6, we can inspect the userdata from metatable getters.

inspect(point) = <userdata id> {
   x = 5
   y = 6
}
    meta = getmetatable(userdata)
    meta['get']

Hi guileen,

I'm afraid that's not possible. The Lua API doesn't have userdata manipulation functions - they are visible only from the C side.

In your example, there's no way to know that point has properties x and y using Lua only.

If you know more about this, let me know. For now, I'm closing the issue.

I use print(inspect(getmetatable(point))) I got this:

{
  [".call"] = <function 1>,
  [".collector"] = <function 2>,
  [".get"] = {
    x = <function 3>,
    y = <function 4>
  },
  [".set"] = {
    x = <function 5>,
    y = <function 6>
  },
  __add = <function 7>,
  __call = <function 8>,
  __div = <function 9>,
  __eq = <function 10>,
  __gc = <function 11>,
  __index = <function 12>,
  __le = <function 13>,
  __lt = <function 14>,
  __mul = <function 15>,
  __newindex = <function 16>,
  __sub = <function 17>,
  equals = <function 18>,
  new = <function 19>,
  new_local = <function 20>,
  tolua_ubox = <1>{
    [<userdata 1>] = <userdata 2>,
    [<userdata 3>] = <userdata 4>,
    <metatable> = <2>{
      __mode = "v"
    }
  },
...

getmetatable(point)['.get'] is {x:<function>, y: <function>}, and this is just what we need, isn't it?

Hi guileen,

I'm afraid that that's application-specific. In other words, not all the userdatas in the world have their "properties" listed in a file called get in their metatable. That happens in your application because that's the way you have built it. Other people could have another field instead of get, or not have metatable at all. For that reason, I'm afraid I can not include getmetatable(userdata).get inside inspect.

If all your userdata is built the same way, you could create a custom inspect for your app.

Thank you for reply.

No problem! good luck with your project :)