I think your problem is that the constructor that is bound for the 
History_Tree class is one that returns raw pointers instead of smart 
pointers.  What you have right now is roughly equivalent to the following 
C++:

int main() {
    auto t = new History_Tree();
    t->append_next_move(15);
    return 0;
}

If you build a native executable of this and run it, you should get 
something like:

terminate called after throwing an instance of 'std::bad_weak_ptr'
  what():  std::bad_weak_ptr
Aborted

This is probably the cause of the exception you are seeing when running in 
JavaScript.  The reason this exception occurs in C++ is because 
shared_from_this() may only be called on objects that have already been 
stored in a std::shared_ptr:

http://en.cppreference.com/w/cpp/memory/enable_shared_from_this/shared_from_this#Notes

In this case, you want bindings that behave more like the following C++, 
which first puts the object in a std::shared_ptr:

int main() {
    auto t = std::make_shared<History_Tree>();
    t->append_next_move(15);
    return 0;
}

...in which case you probably want to bind the constructor like this:

EMSCRIPTEN_BINDINGS(History_Tree) {
  emscripten::class_<History_Tree>("History_Tree")
    .smart_ptr_constructor("History_TreePtr", 
&std::make_shared<History_Tree>)
    .function("append_next_move", &History_Tree::append_next_move)
    ;
}

On Monday, August 1, 2016 at 12:50:20 AM UTC-7, Jordan Bouchoucha wrote:
>
> Hi,
>
>
> Firstly, sorry for my bad English, I hope you will understand me clearly.
>
>
> I search solution for my issues this week without success... It's why i 
> post here now.
> I'm working in a C++ Tree module (very basic) and i need to use it in 
> Javascript.
>
>
> I show you the code resume :
>
>
> Tree.h
>
>
> #include <vector>
>> #include <memory>
>> #include <iostream>
>> #include <vector>
>> #include <string>
>> #include <emscripten/bind.h>
>>
>> class History_Tree : public std::enable_shared_from_this<History_Tree>
>>  {
>>  private:
>>
>>   struct Move {
>>    int move;
>>    std::shared_ptr<History_Tree> children;
>>   };
>>
>>   std::weak_ptr<History_Tree> parent;
>>   std::vector<Move> childrens;
>>
>>  public:
>>
>>  History_Tree()
>>   {}
>>
>>  History_Tree(std::weak_ptr<History_Tree> parent):  parent(parent)
>>   {}
>>
>>   void append_next_move(int move);
>>   std::shared_ptr<History_Tree> get_ptr();
>>
>>  };
>>
>>  
>
> Tree.cpp
>
>
> #include "Tree.h"
>>
>>  //////////////////////////////////////////////////////////////////////////
>>  void History_Tree::append_next_move(int move)
>>  //////////////////////////////////////////////////////////////////////////
>>  {
>>    Move m;
>>    m.move = move;
>>    m.children = std::make_shared<History_Tree>(get_ptr());
>>
>>    childrens.push_back(m);
>>  }
>>
>>  //////////////////////////////////////////////////////////////////////////
>>  std::shared_ptr<History_Tree> History_Tree::get_ptr()
>>  //////////////////////////////////////////////////////////////////////////
>>  {
>>   return shared_from_this();
>>  }
>>
>> EMSCRIPTEN_BINDINGS(History_Tree) {
>>   emscripten::class_<History_Tree>("History_Tree")
>>     .constructor<>()
>>     .smart_ptr<std::shared_ptr<History_Tree>>("History_TreePtr")
>>     .function("append_next_move", &History_Tree::append_next_move)
>>     ;
>> }
>>
>>  
>
> As you can see in the bottom of .cpp, I did EMSCRIPTEN_BINDINGS for try to 
> use my class in .js.
> After this i compile with this command : 
>
>
> em++ --bind -o Tree.js Tree.cpp
>
>  
>
> Seems to work even if i got a warning "unresolved symbol: 
> throwInternalError". I call my Tree.js in a html file, and try to do this : 
>
>
> var t = new Module.History_Tree(); 
>
>  
>
>> t.append_next_move(15);
>>
>  
>
> The "append_next_move()" doesn't work. I can read in my browser Javascript 
> console "uncaught exception: 5271640".
>
> After doing research, I guess that it is a variable address. The bug 
> appear when he read this line from Tree.cpp in "append_next_move()" : 
>
> m.children = std::make_shared<History_Tree>(get_ptr());
>
> I probably misunderstand something... I don't know what is wrong exactly 
> and how fix this. (In the begin, I was using WebIDL Binder but not work too 
> haha...)
>
> Maybe I need to say in EMSCRIPTEN_BINDINGS that History_Tree is 
> enable_shared_from_this ? I don't know... 
>
> Hope you will say to me "You're an idiot, the solution is ...". That would 
> be cool :D 
>
>
> For testing this, you just need to :
>
>    - Copy the Tree.cpp and Tree.h 
>    - Compile with the command em++
>    - Create this .html 
>
> <!doctype html>
>> <html lang="fr">
>> <head>
>>   <meta charset="utf-8">
>>   <title>Error test</title>
>>   <script type="text/javascript" src="test.js"></script>
>> </head>
>> <body>
>>
>> <script>
>> var t = new Module.History_Tree();
>> t.append_next_move(15);
>> </script>
>>
>> </body>
>> </html>
>>
>>
>    - Open it in your browser and look at console.
>
>
> Thanks for helping me.
>

-- 
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.

Reply via email to