-
Notifications
You must be signed in to change notification settings - Fork 460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion: How to avoid manually setting descriptors for each attribute? #1550
Comments
There's nothing I've found, but if you don't mind doing a bit of debugging & adapting, you can look at: https://github.com/broken/napi-wrapper-gen It's a small app I wrote that uses antlr to read a c++ header file and generate the corresponding Napi header & source files. It works well for my use-case, and while I've tried to keep it generic, it is not all encompassing and there are likely some app specific things that will need to be adjusted. Example: from Song.h, it will generate Song_wrap.h & Song_wrap.cpp. |
Hi @edoCsItahW ,
What is the reasoning behind this, as that would determine a solution. If it's because you don't want to replicate a lot of "boilerplate" code, then I would suggest taking a look at the solution above. If it's because you don't want your class prototype to have a bunch of instance methods on it, you could try looking at exposing only one instance method which takes the intended method name + args as its own argument, and use a |
Hey @broken and @KevinEady, Thanks a lot for your help! To start, @KevinEady, you’re right. Maybe I should provide a bit more context. So here’s the deal: I’m trying to whip up an adapter in C++ to act as middleware between Node and another language (let’s just call it a server for brevity). I had already made the call to create a base class and use @broken's tools to add property descriptors—definitely better than doing it for every single class. But, due to some health stuff, I haven’t gotten around to implementing it. @KevinEady, I’m interested in trying out your approach. I really hadn’t thought about breaking through from the Node side, and it seems promising in theory. That said, using decorators sounds super tempting, as it could elegantly accomplish a lot with minimal code. So I hope it’s not too forward of me to ask: is there a specific reason for the existence of "T::*"? |
How to avoid manually setting descriptors for each attribute
There is a class that has many properties, but I have a way to dynamically collect these properties, so I use Maps to store them and hope they can be exposed to Nodejs. That's where the problem lies.
Assuming that according to the process, it should be like this
But I don't want to set descriptors for every attribute, so I made the following attempts
This doesn't work because the
T::*
inInstanceGetterCallback
restricts my return type from being a free functionBut info does not contain a key, it contains the same value as value, and the key passed in by the user is unknown what has taken it away
So do you have any good methods?
The text was updated successfully, but these errors were encountered: