cocos2d / bindings-generator

JSBindings generator for C++
170 stars 153 forks source link

Generate conversion from JS functions to std::function. #16

Closed colesbury closed 11 years ago

colesbury commented 11 years ago

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<void ()> 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) ;

dumganhar commented 11 years ago

Thanks.