Emscripten Class Constructor Taking std::vector












0















I was wondering if anyone could help me with binding for a C++ class, which takes an std::vector<T> as a constructor, in Emscripten. I would like something along the lines of the following:



EMSCRIPTEN_BINDINGS(my_class) {

emscripten::class_<test_class>("test_class")
.constructor<std::vector<float>>()
.property("x", &test_class::get_x, &test_class::set_x)
;
}


I read up on this post, and implemented a proxy function to take my JS float array created by var inputArray = new Float32Array([1,2,3], to an std::vector<float>.



However, when I use the inputArray as a parameter to the class constructor I get the following warning:



5258048 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.


I have added the DISABLE_EXCEPTION_CATCHING=2 flag to the emcc step, however, this doesn't produce any different output.



Has anyone else come up with a solution?










share|improve this question

























  • Did you try adding one of those suggested compiler flags?

    – ChrisD
    Nov 1 '18 at 19:35











  • @ChrisD See the edit.

    – 9301293
    Nov 1 '18 at 19:37
















0















I was wondering if anyone could help me with binding for a C++ class, which takes an std::vector<T> as a constructor, in Emscripten. I would like something along the lines of the following:



EMSCRIPTEN_BINDINGS(my_class) {

emscripten::class_<test_class>("test_class")
.constructor<std::vector<float>>()
.property("x", &test_class::get_x, &test_class::set_x)
;
}


I read up on this post, and implemented a proxy function to take my JS float array created by var inputArray = new Float32Array([1,2,3], to an std::vector<float>.



However, when I use the inputArray as a parameter to the class constructor I get the following warning:



5258048 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.


I have added the DISABLE_EXCEPTION_CATCHING=2 flag to the emcc step, however, this doesn't produce any different output.



Has anyone else come up with a solution?










share|improve this question

























  • Did you try adding one of those suggested compiler flags?

    – ChrisD
    Nov 1 '18 at 19:35











  • @ChrisD See the edit.

    – 9301293
    Nov 1 '18 at 19:37














0












0








0








I was wondering if anyone could help me with binding for a C++ class, which takes an std::vector<T> as a constructor, in Emscripten. I would like something along the lines of the following:



EMSCRIPTEN_BINDINGS(my_class) {

emscripten::class_<test_class>("test_class")
.constructor<std::vector<float>>()
.property("x", &test_class::get_x, &test_class::set_x)
;
}


I read up on this post, and implemented a proxy function to take my JS float array created by var inputArray = new Float32Array([1,2,3], to an std::vector<float>.



However, when I use the inputArray as a parameter to the class constructor I get the following warning:



5258048 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.


I have added the DISABLE_EXCEPTION_CATCHING=2 flag to the emcc step, however, this doesn't produce any different output.



Has anyone else come up with a solution?










share|improve this question
















I was wondering if anyone could help me with binding for a C++ class, which takes an std::vector<T> as a constructor, in Emscripten. I would like something along the lines of the following:



EMSCRIPTEN_BINDINGS(my_class) {

emscripten::class_<test_class>("test_class")
.constructor<std::vector<float>>()
.property("x", &test_class::get_x, &test_class::set_x)
;
}


I read up on this post, and implemented a proxy function to take my JS float array created by var inputArray = new Float32Array([1,2,3], to an std::vector<float>.



However, when I use the inputArray as a parameter to the class constructor I get the following warning:



5258048 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.


I have added the DISABLE_EXCEPTION_CATCHING=2 flag to the emcc step, however, this doesn't produce any different output.



Has anyone else come up with a solution?







javascript c++ emscripten






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 2 '18 at 17:25







9301293

















asked Nov 1 '18 at 19:31









93012939301293

364215




364215













  • Did you try adding one of those suggested compiler flags?

    – ChrisD
    Nov 1 '18 at 19:35











  • @ChrisD See the edit.

    – 9301293
    Nov 1 '18 at 19:37



















  • Did you try adding one of those suggested compiler flags?

    – ChrisD
    Nov 1 '18 at 19:35











  • @ChrisD See the edit.

    – 9301293
    Nov 1 '18 at 19:37

















Did you try adding one of those suggested compiler flags?

– ChrisD
Nov 1 '18 at 19:35





Did you try adding one of those suggested compiler flags?

– ChrisD
Nov 1 '18 at 19:35













@ChrisD See the edit.

– 9301293
Nov 1 '18 at 19:37





@ChrisD See the edit.

– 9301293
Nov 1 '18 at 19:37












1 Answer
1






active

oldest

votes


















0














The key thing is to ensure that you've defined a mapping for std::vector using register_vector so you can pass the vector your copy function has created back to JavaScript and then back into C++.



This code seems to work for me, if I understand your problem correctly:



#include <vector>

#include <emscripten.h>
#include <emscripten/bind.h>

class test_class {
float x;

public:
test_class(std::vector<float> arr);
float get_x() const;
void set_x(float val);
};

test_class::test_class(std::vector<float> arr) {
x = 0;
for (size_t i = 0; i < arr.size(); i++) {
x += arr[i];
}
x = x / arr.size();
}

float test_class::get_x() const {
return x;
}

void test_class::set_x(float val) {
x = val;
}

EMSCRIPTEN_BINDINGS(my_class) {

emscripten::register_vector<float>("VectorFloat");

emscripten::class_<test_class>("test_class")
.constructor<std::vector<float>>()
.property("x", &test_class::get_x, &test_class::set_x)
;
}

int main() {
EM_ASM(
var arr = new Float32Array([1.0, 2.0, 0.5]);
var vec = new Module.VectorFloat();
for (var i = 0; i < arr.length; i++) {
vec.push_back(arr[i]);
}
var obj = new Module.test_class(vec);
console.log('obj.x is ' + obj.x);
);
}


This sample code does an inefficient copy from the Float32Array to the std::vector (represented as the VectorFloat proxy object in JS), assuming you've got that part working, and concentrates on passing the vector into the constructor.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53108165%2femscripten-class-constructor-taking-stdvectort%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The key thing is to ensure that you've defined a mapping for std::vector using register_vector so you can pass the vector your copy function has created back to JavaScript and then back into C++.



    This code seems to work for me, if I understand your problem correctly:



    #include <vector>

    #include <emscripten.h>
    #include <emscripten/bind.h>

    class test_class {
    float x;

    public:
    test_class(std::vector<float> arr);
    float get_x() const;
    void set_x(float val);
    };

    test_class::test_class(std::vector<float> arr) {
    x = 0;
    for (size_t i = 0; i < arr.size(); i++) {
    x += arr[i];
    }
    x = x / arr.size();
    }

    float test_class::get_x() const {
    return x;
    }

    void test_class::set_x(float val) {
    x = val;
    }

    EMSCRIPTEN_BINDINGS(my_class) {

    emscripten::register_vector<float>("VectorFloat");

    emscripten::class_<test_class>("test_class")
    .constructor<std::vector<float>>()
    .property("x", &test_class::get_x, &test_class::set_x)
    ;
    }

    int main() {
    EM_ASM(
    var arr = new Float32Array([1.0, 2.0, 0.5]);
    var vec = new Module.VectorFloat();
    for (var i = 0; i < arr.length; i++) {
    vec.push_back(arr[i]);
    }
    var obj = new Module.test_class(vec);
    console.log('obj.x is ' + obj.x);
    );
    }


    This sample code does an inefficient copy from the Float32Array to the std::vector (represented as the VectorFloat proxy object in JS), assuming you've got that part working, and concentrates on passing the vector into the constructor.






    share|improve this answer




























      0














      The key thing is to ensure that you've defined a mapping for std::vector using register_vector so you can pass the vector your copy function has created back to JavaScript and then back into C++.



      This code seems to work for me, if I understand your problem correctly:



      #include <vector>

      #include <emscripten.h>
      #include <emscripten/bind.h>

      class test_class {
      float x;

      public:
      test_class(std::vector<float> arr);
      float get_x() const;
      void set_x(float val);
      };

      test_class::test_class(std::vector<float> arr) {
      x = 0;
      for (size_t i = 0; i < arr.size(); i++) {
      x += arr[i];
      }
      x = x / arr.size();
      }

      float test_class::get_x() const {
      return x;
      }

      void test_class::set_x(float val) {
      x = val;
      }

      EMSCRIPTEN_BINDINGS(my_class) {

      emscripten::register_vector<float>("VectorFloat");

      emscripten::class_<test_class>("test_class")
      .constructor<std::vector<float>>()
      .property("x", &test_class::get_x, &test_class::set_x)
      ;
      }

      int main() {
      EM_ASM(
      var arr = new Float32Array([1.0, 2.0, 0.5]);
      var vec = new Module.VectorFloat();
      for (var i = 0; i < arr.length; i++) {
      vec.push_back(arr[i]);
      }
      var obj = new Module.test_class(vec);
      console.log('obj.x is ' + obj.x);
      );
      }


      This sample code does an inefficient copy from the Float32Array to the std::vector (represented as the VectorFloat proxy object in JS), assuming you've got that part working, and concentrates on passing the vector into the constructor.






      share|improve this answer


























        0












        0








        0







        The key thing is to ensure that you've defined a mapping for std::vector using register_vector so you can pass the vector your copy function has created back to JavaScript and then back into C++.



        This code seems to work for me, if I understand your problem correctly:



        #include <vector>

        #include <emscripten.h>
        #include <emscripten/bind.h>

        class test_class {
        float x;

        public:
        test_class(std::vector<float> arr);
        float get_x() const;
        void set_x(float val);
        };

        test_class::test_class(std::vector<float> arr) {
        x = 0;
        for (size_t i = 0; i < arr.size(); i++) {
        x += arr[i];
        }
        x = x / arr.size();
        }

        float test_class::get_x() const {
        return x;
        }

        void test_class::set_x(float val) {
        x = val;
        }

        EMSCRIPTEN_BINDINGS(my_class) {

        emscripten::register_vector<float>("VectorFloat");

        emscripten::class_<test_class>("test_class")
        .constructor<std::vector<float>>()
        .property("x", &test_class::get_x, &test_class::set_x)
        ;
        }

        int main() {
        EM_ASM(
        var arr = new Float32Array([1.0, 2.0, 0.5]);
        var vec = new Module.VectorFloat();
        for (var i = 0; i < arr.length; i++) {
        vec.push_back(arr[i]);
        }
        var obj = new Module.test_class(vec);
        console.log('obj.x is ' + obj.x);
        );
        }


        This sample code does an inefficient copy from the Float32Array to the std::vector (represented as the VectorFloat proxy object in JS), assuming you've got that part working, and concentrates on passing the vector into the constructor.






        share|improve this answer













        The key thing is to ensure that you've defined a mapping for std::vector using register_vector so you can pass the vector your copy function has created back to JavaScript and then back into C++.



        This code seems to work for me, if I understand your problem correctly:



        #include <vector>

        #include <emscripten.h>
        #include <emscripten/bind.h>

        class test_class {
        float x;

        public:
        test_class(std::vector<float> arr);
        float get_x() const;
        void set_x(float val);
        };

        test_class::test_class(std::vector<float> arr) {
        x = 0;
        for (size_t i = 0; i < arr.size(); i++) {
        x += arr[i];
        }
        x = x / arr.size();
        }

        float test_class::get_x() const {
        return x;
        }

        void test_class::set_x(float val) {
        x = val;
        }

        EMSCRIPTEN_BINDINGS(my_class) {

        emscripten::register_vector<float>("VectorFloat");

        emscripten::class_<test_class>("test_class")
        .constructor<std::vector<float>>()
        .property("x", &test_class::get_x, &test_class::set_x)
        ;
        }

        int main() {
        EM_ASM(
        var arr = new Float32Array([1.0, 2.0, 0.5]);
        var vec = new Module.VectorFloat();
        for (var i = 0; i < arr.length; i++) {
        vec.push_back(arr[i]);
        }
        var obj = new Module.test_class(vec);
        console.log('obj.x is ' + obj.x);
        );
        }


        This sample code does an inefficient copy from the Float32Array to the std::vector (represented as the VectorFloat proxy object in JS), assuming you've got that part working, and concentrates on passing the vector into the constructor.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 1:51









        yjjjnlsyjjjnls

        445




        445
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53108165%2femscripten-class-constructor-taking-stdvectort%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents