Skip to content
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

Binding a C++ member function to a static Wren method #15

Open
xSke opened this issue Jul 30, 2017 · 2 comments
Open

Binding a C++ member function to a static Wren method #15

xSke opened this issue Jul 30, 2017 · 2 comments

Comments

@xSke
Copy link

xSke commented Jul 30, 2017

Is it possible to bind a C++ member function (or closure, for that matter) to a static Wren method? I can't figure out a way to get any sort of instance state inside those function bindings. (For example, if I wanted to bind a Wren function to close a window, I'd need access to the window handle from a closure scope or member variable)

@xSke
Copy link
Author

xSke commented Jul 30, 2017

I managed to hack it to work with CFunctions:

template<class F> static void bind(wrenpp::ClassContext ctx, const std::string &sig, F const &callback) {
    static auto f = [=](WrenVM* w) {
        callback(w);
    };

    ctx.bindCFunction(true, sig, [](WrenVM* w) {
        f(w);
    }).endClass();
}

Callable with:

int counter = 42;
// ....
auto ctx = vm.beginModule("main").beginClass("Test");
bind(ctx, "count()", [&](WrenVM *w) {
    wrenSetSlotDouble(w, 0, counter++);
});

However, this is a really hacky solution and also doesn't work with the higher-level bindings.

@Nelarius
Copy link
Owner

Sorry that this response has taken forever!

You could try binding a Cfunction. It's a raw WrenForeignMethodFn (which is all that Wren sees of your class methods, see src/include/wren.h in the Wren repo) that you can bind to a class by doing vm.beginModule("main").beginClass("Test").bindCFunction(true, &wren_count, "count()");.

The function would then be something like:

int counter = 42;

void wren_count(WrenVM* vm)
{
  wrenSetSlotDouble(vm, 0, counter++);
}

Adding wrappers for lambdas is probably something I should add to my TO-DO list, since it's a bit unfortunate to have to write this sort of boiler-plate code manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants