Member function pointer to be used with a c library

heyho smart people,
maybe someone can help me out here.

i wanna add javascript support for an app of mine, using quickjs.
it is c libary and this is where i am a bit stuck now.
i am able to register static member functions, as you can see here:

whats the correct way to do this with non static member functions? I guess it should be done with a wrapper function, somehow passing the object and then casting it.

any pointers? any c nerds here?

Hey Thomas!
did you try using std::bind?

thanks Roy,
I think std::bind has been deprecated in favour of lambdas. I might give it a try later.
I got it working, using quickjs’ magic function type, it adds an additional parameter to the called function, that can be used to access data of static map for instance. feels a bit hacky, but for now, it works.
it would be nice if I could provide an api similar to ofAddListener, e.g. _js.register("name", this, &ofApp::js_memberFunction);

Yeah, that looks really hacky! :slight_smile:
But std::bind is not deprecated, it is heavilly used in OF’s core.
I think you can go for it, which would enable you to use it in a similar fashion as ofAddListener.
btw, inspect the ofAddListener function so you get an idea of how to implement such.

1 Like

thanks for clarifying that, and for the hint to check out how it’s done in the core.
I got it working. Still using that static wrapper function, but with a much nicer api.
here for the reference, in case someone stumbles across that post

1 Like

Just curious, what is the use case for this addon? I mean what it allows you to do that you can’t without using Javascript?

here are few

  • live reloading is the main point why i wanna add a scripting engine
  • i am quite familiar with js and so are people on my project (unfortunately they are not with c++)
  • maybe speed - in terms of development
  • maybe even embedding certain npm modules
  • not having to recompile/deploy an app if “content” changes
  • new possibilities for plugin systems
  • js is more accessible for certain groups, e.g. students/kids/beginners. i could hide away the complexity (setting up a project, compilation, and not to forget semicolons) and just provide a simple js interface

I also want to check if i can create oF bindings and expose them via a global of object to the js world. I think ofxLua does it in a similar fashion.

1 Like

I see. sounds like very useful features.
npm modules sounds like a very useful feature.

I got a first proof of concept working. memory leaks and inconsistent api included.
It live-reloads and has some oF bindings for testing purposes.

I am afraid I am entering now the maintenance hell if don’t autogenerate the bindings. Any thoughts on this?

btw. @roymacdonald, i think most npm modules depend on browser or node implementations. But since quickjs supports es6, modules and modern js features, it should be possible to use proper imports.

Looks promissing but yes, you should automate the bindings, otherwise it will be hell. No idea on how to do that.

Keep on the good work.

BTW, what happened with the OF package manager you were developing?

thanks Roy.
I started refactoring the package manager, it is now only one app, making use of ofxProjectGenerator code and an ofxImGui. i got a bit too busy with projects and teaching. I think I should find some time in December to get it to a state to publish an update.

I also uploaded a first (not very useable) version of the js oF app. In case you wanna give it a try to get an idea of what I wanna achieve.

brew tap thomasgeissl/tools
brew install ofxquickapp
curl >> example.js
ofxQuickApp /absolute/path/to/js/file.js
1 Like