cocos2d/bindings-generator — pull request #16 by colesbury (2013-07-16 23:38:41+00:00)
Title: Generate conversion from JS functions to std::function.

This patch generates binding code for C++ methods that contains std::function parameters. This depends on an implementation of "JSFunctionWrapper", which is added to cocos2d-x in another pull-request.

Unfortunately, libclang doesn't give structured type information for template arguments, so this binding code will not work for some complex arguments. For example, it won't generate binding code for a std::function that contains std::functions as parameters or return values. It will work for most simple arguments: void, int, Dictionary*, objects, etc.

For example for:

class Foo {
static void addHandler(const std::function handler);
};

This would generate something like:

do {
std::shared_ptr func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), argv[0]));
auto lambda = [=]() -> void {
jsval largv[0];
jsval rval;
JSBool ok = func->invoke(0, &largv[0], rval);
if (!ok && JS_IsExceptionPending(cx)) {
JS_ReportPendingException(cx);
}
};
arg0 = lambda;
} while(0)
;