actboy168 / lua-debug

Lua Debug Adapter for Visual Studio Code
MIT License
431 stars 93 forks source link

View class object property values when debugging? #237

Open Silverlan opened 1 year ago

Silverlan commented 1 year ago

I'm using luabind to bind some classes in C++ to make them available in Lua:

auto defVector = luabind::class_<Vector3>("Vector");
defVector.def(luabind::tostring(luabind::self));
defVector.def(luabind::constructor<>());
defVector.def(luabind::constructor<float, float, float>());
defVector.def_readwrite("x", &Vector3::x);
defVector.def_readwrite("y", &Vector3::y);
defVector.def_readwrite("z", &Vector3::z);
// ...

When debugging a Lua script, I would like to be able to see the values of the x, y and z properties for variables of this type, but this does not seem to be possible at the moment: Code_2023-03-19_15-38-10

Could something like this be implemented? Alternatively, it would be useful to see the __tostring text of the value. In the ZeroBrane IDE you can hover over a variable to view its __tostring text: zbstudio_2023-03-19_15-47-59

It's very useful for debugging, especially when working with math-type classes.

actboy168 commented 1 year ago

You can add a method __debugger_tostring to the metatable.

Silverlan commented 1 year ago

You can add a method __debugger_tostring to the metatable.

Works like a charm, thanks! I think it would be a good idea to put this in the readme, since it doesn't seem to be documented anywhere at the moment.

One more thing: Would it be possible to allow us to overwrite the displayed type of the values? At the moment, it just says "userdata" for custom types: Code_2023-03-20_10-55-29

The points table has values of types {Vector,Vector,EulerAngles}, but it just says {userdata,userdata,userdata}, which is not very helpful. Maybe another metatable function, like __debugger_gettypename?

actboy168 commented 1 year ago

You can set __name in metatable.

actboy168 commented 1 year ago

It really should be documented, but the problem right now is that there isn't any documentation.

Silverlan commented 1 year ago

You can set __name in metatable.

I did try setting __name, but it didn't seem to have any effect: Code_2023-03-21_09-54-54

__name is set in the metatable, but it still shows as userdata. Here's how I'm pushing the __name string into the table:

lua_pushstring(l, "__name");
lua_pushstring(l, "Test");
lua_settable(l, -3);

I also tried pushing a getter function instead (i.e. the same as __debugger_tostring), but that did not work either.

actboy168 commented 1 year ago

Well, it didn't work as expected. I'll fix it in the next version.

Silverlan commented 1 year ago

I tried commit b179c3c, but it doesn't seem to be working yet:

Code_2023-03-21_18-20-29

It still says userData for the types, and there is now an additional nil entry at the start. If the feature is still work-in-progress, just ignore me 😅.