I managed to make everything work as expected. Your comments have been really helpful, Alexandre, thank you! 😀
On Wednesday, July 19, 2017 at 5:19:26 PM UTC+3, Alexandre Perrot wrote: > > Both are possible, but I'd say val is the best option. > > I did not test it, but it seems to me that since val has an operator(), it > can be used as a functor, and thus be directly passed as an std::function. > Otherwise, it is easy to create a lambda that will call it. > > Le lundi 17 juillet 2017 17:44:18 UTC+2, Vangelis Ts. a écrit : >> >> Thank you very much for your response, Alexandre. 🙂 >> >> Passing functions from JS to call later on from C++ is exactly what I am >> trying to do. I suspect that *emscripten::val* is the only way ―or is it >> also possible by using *std::function*? >> >> In any case, my C++ library uses *std::function* internally and >> (ideally) I would like to avoid making changes to the code that calls the >> callbacks. Is there the ability to "translate" an *emscripten::val* to >> *std::function*, in order to transparently pass the callbacks to the >> rest C++ code? >> >> >> On Monday, July 17, 2017 at 10:24:49 AM UTC+3, Alexandre Perrot wrote: >>> >>> Hi, >>> >>> std::functions are not bound by default, you need to do that yourself. >>> >>> Have a look at l.1775 of embind_test.cpp >>> >>> https://github.com/kripken/emscripten/blob/master/tests/embind/embind_test.cpp#L1775 >>> >>> This registers an std::function as a class, with the opcall method >>> binding the c++ () operator. >>> >>> Alternatively, you could use an emscripten::val to pass a function from >>> js. >>> >>> Le lundi 10 juillet 2017 14:58:08 UTC+2, Vangelis Ts. a écrit : >>>> >>>> Hello, >>>> >>>> I am trying to use a simple C++ program containing using >>>> *std::function* with Embind. However, the latter does not seem to >>>> recognize *std::function*. >>>> >>>> More specifically, here is the example I used: >>>> >>>> *Foo.cpp* >>>> >>>> >>>> #include <functional> >>>> #include <emscripten/bind.h> >>>> >>>> using namespace emscripten; >>>> >>>> int bar(std::function<int(int)> func, int num) { >>>> return func(num); >>>> } >>>> >>>> EMSCRIPTEN_BINDINGS(Foo) { >>>> function("bar", &bar); >>>> } >>>> >>>> >>>> *Foo.html* >>>> >>>> >>>> <!doctype html> >>>> <html> >>>> <script src="foo.js"></script> >>>> <script> >>>> console.log('Foo: ' + Module.bar); >>>> </script> >>>> </html> >>>> >>>> >>>> *Compilation* >>>> >>>> >>>> *$* emcc --bind foo.cpp -o foo.js >>>> >>>> >>>> The compilation succeeds with no errors or warnings. >>>> >>>> *Result* >>>> >>>> >>>> <https://lh3.googleusercontent.com/-cbSwX8ZiNag/WWNzyjMIwsI/AAAAAAAAKUA/06dRJmG-0C4b3_jHxB1c_Eu1Mny5947ZQCLcBGAs/s1600/Screen%2BShot%2B2017-07-10%2Bat%2B14.57.05.png> >>>> >>>> >>>> I also tried to compile and/or run the test files of Embind (see >>>> embind_test.cpp >>>> <https://github.com/kripken/emscripten/blob/master/tests/embind/embind_test.cpp> >>>> and embind.test.js >>>> <https://github.com/kripken/emscripten/blob/master/tests/embind/embind.test.js>), >>>> >>>> which also contain function that use *std::function* (e.g. >>>> emval_test_get_function_ptr >>>> (definition) >>>> <https://github.com/kripken/emscripten/blob/master/tests/embind/embind_test.cpp#L153> >>>> / emval_test_get_function_ptr (binding) >>>> <https://github.com/kripken/emscripten/blob/master/tests/embind/embind_test.cpp#L1780>), >>>> >>>> with exactly the same command and they *do work fine*: >>>> >>>> *Result* >>>> >>>> >>>> <https://lh3.googleusercontent.com/-9Cn4jyhAPzg/WWN1pN3B8TI/AAAAAAAAKUQ/x_t2dM-qyi8n6hTcIf968IQSaxmfFxoDACLcBGAs/s1600/Screen%2BShot%2B2017-07-10%2Bat%2B14.56.02.png> >>>> >>>> *Generated source code* >>>> >>>> >>>> <https://lh3.googleusercontent.com/-80B9SjRLHME/WWN16hbcMCI/AAAAAAAAKUU/ZzP2QEes3Pk0gacNafhtMMgyUAazTTEkwCLcBGAs/s1600/Screen%2BShot%2B2017-07-10%2Bat%2B14.55.53.png> >>>> >>>> I should also mention that I tried many different combinations, e.g. >>>> *std::function* with different types, as return type, as argument, as >>>> an independent function (like in the above example), as a class or struct >>>> method, but had no luck. >>>> >>>> Any ideas would prove of great help. >>>> >>>> Thank you very much, in advance. >>>> >>>> >>>> ― Vangelis >>>> >>> -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
