wren-lang / wren

The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.
http://wren.io
MIT License
6.86k stars 550 forks source link

Class reflection for embedding #1158

Open ConorDamery opened 1 year ago

ConorDamery commented 1 year ago

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.

mhermier commented 1 year ago

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 commented 1 year ago

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.

dollerama commented 1 month ago

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.