wren icon indicating copy to clipboard operation
wren copied to clipboard

Class reflection for embedding

Open ConorDamery opened this issue 2 years ago • 3 comments

Currently there doesn't seem to be a way to inspect a class and its methods/properties from C/Cpp. Lua uses a table structure that can be inspected, and while it may be possible to add a properties map in the class with a specific name that can be used to manage the handles, this is at best an iffy workaround.

Any plans or suggestions on how this could/should be handled?

My first guess (which I'm not sure is the case already), would be to follow the same pattern Lua does by making the class handle also a map, so as long as you know the class name you can inspect its properties. This could also allow for checking if a handle has a callable method before using wrenCall and avoiding a runtime error.

ConorDamery avatar Mar 21 '23 16:03 ConorDamery

There is a pull request/draft for a reflection API at #1006. I should update it sooner or later. I'm currently busy with other stuff, but I updated the change set last week to improve it.

mhermier avatar Mar 21 '23 17:03 mhermier

Forgot to say, a quick and dirty way to do it would be to make Class iterable (by implementing iterate(_) and iteratorValue) so it output the symbols, but that will be not accepted in main for various reasons. But if you need it, you can easily implement it for yourself.

mhermier avatar Mar 21 '23 17:03 mhermier

Just wanted to add that attributes were added at some point. I suppose it might be a little time consuming but if you tag the methods/props you want to reflect though then you can access them elsewhere or in wren. I'm using attributes to serialize/deserialize.

dollerama avatar Jul 23 '24 05:07 dollerama