Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Alessandro Pignotti
    @alexp-sssup
    Well, no, you could do something like
    [[cheerp::genericjs]] client::EventListener* getNewCallback()
    {
    C* a = new C(); 
    return cheerp::Callback([a](float x, float y) { a->method(x, y); }) 
    }
    In this way for every call to getNewCallback you will also get an independently allocated class
    Or you can allocate 1 instance of the class during initialization and then store the callback in the global JS namespace, for example
    Eugene Mikhailovski
    @Eugenexz
    @alexp-sssup I need "a" to be instantiated only once elsewhere
    Alessandro Pignotti
    @alexp-sssup
    namespace [[cheerp::genericjs]] client
    {
    client::EventListener* wasmCallback;
    }
    [[cheerp::genericjs]] void webMain()
    {
    ...
    C* a = new C();
    client::wasmCallback = cheerp::Callback([a](float x, float y)...);
    ...
    }
    And now from JS code you can use the wasmCallback(x,y) function
    No, as usual you will need a [[cheerp::genericjs]] function, I tagged webMain in the example before.
    Eugene Mikhailovski
    @Eugenexz
    @alexp-sssup but now webMain is not wasm
    Alessandro Pignotti
    @alexp-sssup
    No indeed, if you have heavy computation code in webMain you may create a smaller function with [[cheerp::genericjs]] just to allocate the callback you need
    There is most often no negative effect in having some code tagged with [[cheerp::genericjs]], it is fairly performant anyway
    Eugene Mikhailovski
    @Eugenexz
    @alexp-sssup If I have a smaller function with [[cheerp::genericjs]] just to allocate the callback then I will still need the globally allocated wasm class.
    Alessandro Pignotti
    @alexp-sssup
    @Eugenexz I am not following
    You can allocate the instance they way you prefer and then pass it an argument to the small genericjs function (both pointer and reference arguments will work)
    In case you have bugs with using the C++ lambda as in the example you should also try the std::bind method which returns an std::function
    Eugene Mikhailovski
    @Eugenexz
    Ultimately, I just don't see where the separation is between wasm and JS in your examples if I want to instantiate my wasm class in the wasm section
    Alessandro Pignotti
    @alexp-sssup
    The separation is well defined, cheerp::genericjs]] functions will be in JS, the rest will be in wasm
    You can allocate wasm classes also in genericjs function, they will still be fully wasm
    Eugene Mikhailovski
    @Eugenexz
    @alexp-sssup I can pass [[cheerp::genericjs]] or [[cheerp::jsexport]] arguments into wasm functions?
    Alessandro Pignotti
    @alexp-sssup
    [[cheerp::jsexport]] cannot be applied to arguments, what do you mean?
    wasm functions cannot manipulate genericjs objects, but the other way around is fully supported
    Eugene Mikhailovski
    @Eugenexz
    Then what did you mean by "You can allocate the instance they way you prefer and then pass it an argument to the small genericjs function"?
    Alessandro Pignotti
    @alexp-sssup
    You can pass wasm objects into genericjs code
    not the other way around
    So you can allocate your wasm instance the way you like
    and then pass it to genericjs only to forge the callback
    Eugene Mikhailovski
    @Eugenexz
    Ok I understand now
    @alexp-sssup Thanks
    Alessandro Pignotti
    @alexp-sssup
    You are welcome :-)
    Eugene Mikhailovski
    @Eugenexz
    @alexp-sssup But again, I can't use cheerp 2.0 unless I know how to properly import functions into wasm
    Alessandro Pignotti
    @alexp-sssup
    @Eugenexz Can you make an example of what you want to achieve?
    Eugene Mikhailovski
    @Eugenexz
    C++: extern "C" { int createBuffer(); }; JS: WebAssembly.instantiate(r, {_createBuffer: () => {...}});
    Alessandro Pignotti
    @alexp-sssup
    The easiest way is to declare the functions in the client namespace and call those from a small genericjs function
    Eugene Mikhailovski
    @Eugenexz
    How do I declare them in the client namespace? And I will have to in turn call these genericjs functions from the wasm section, right?
    Alessandro Pignotti
    @alexp-sssup
    namespace [[cheerp::genericjs]] client
    {
    int createBuffer();
    }
    And as you said you will need to call the genericjs from wasm
    Eugene Mikhailovski
    @Eugenexz
    @alexp-sssup great, that works, thanks!
    Eugene Mikhailovski
    @Eugenexz
    @alexp-sssup although now I have to go through two function calls instead of one
    Alessandro Pignotti
    @alexp-sssup
    Yeah, that's not great, in the next release calling directly client functions from wasm will be supported in some cases
    This one should be fine
    Eugene Mikhailovski
    @Eugenexz
    Sounds good
    Carlo Piovesan
    @carlopi
    I wrote an article on how to port a C library to JS, it may be of interest: https://medium.com/leaningtech/a-fast-bigint-js-in-an-evening-compiling-c-to-javascript-db61ae733512
    Eugene Mikhailovski
    @Eugenexz
    Hello, inside a constructor of a class, I have two calls with different parameters to a member method of another class and everything works, but when I remove the second call, I get RuntimeError: function signature mismatch
    Alessandro Pignotti
    @alexp-sssup
    @Eugenexz There is nothing wrong in what you described. There may be a compiler bug but we need a test case to be able to understand it. It is also likely that the problem is already fixed in current master, do you have the possibility of testing using Linux + our nightly PPA, for example on a virtual machine?
    Eugene Mikhailovski
    @Eugenexz
    @alexp-sssup How is there nothing wrong? Yes, I may try
    Alessandro Pignotti
    @alexp-sssup
    I meant that you described something which is supposed to work, the error you received is obviously not normal
    Eugene Mikhailovski
    @Eugenexz
    @alexp-sssup that makes more sense haha
    Timothée Jeannin
    @TimotheeJeannin
    Hi, is there anybody here?
    Alessandro Pignotti
    @alexp-sssup
    Sure
    Alessandro Pignotti
    @alexp-sssup
    @TimotheeJeannin what's your question?