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