title: Wrapping a C++ Object to a v8 Object in a Node Addon
body: <p>I'm currently writing a Node addon in C++, and I'm running into an issue where I need to make and return a v8 Array filled with v8 wrapped C++ object instances.</p>
  
  <p>At the moment, the code looks something like this</p>
  
  <pre class="lang-cpp prettyprint-override"><code>v8::Handle&lt;v8::Value&gt; Controller::nodeArray(const v8::Arguments&amp; args)
  {
      v8::HandleScope scope;
  
      Controller* controller= ObjectWrap::Unwrap&lt;Controller&gt;(args.This());
      const std::vector&lt;Foobar*&gt;* foobars = controller-&gt;getFoobars();
      unsigned int foobarCount = foobars-&gt;size();
  
      v8::Handle&lt;v8::Array&gt; foobarsArray = v8::Array::New(foobarCount);
      std::vector&lt;Foobar*&gt;::const_iterator foobar = foobars-&gt;begin();
  
      for(unsigned int i = 0; i &lt; foobarCount; i++)
      {
          // Need to create a v8 Object that wraps a single instance of a 
          // Foobar object in "foobars"
  
          // Then push the object to the v8 Array?
          // foobarsArray-&gt;Set(i, [some Foobar instance]);
          snake++;
      }
  
      return foobarsArray;
  }
  </code></pre>
  
  <p>I've tried several different things in the <code>for</code> loop to no avail, however. How exactly should I go about this?</p>
  
  <h2>Foobar and SuperFoo</h2>
  
  <p>Here are the <code>Init</code> initializer and <code>nodeNew</code> constructor functions defined in <code>Foobar</code>, as well as the header files for <code>Foobar</code> and <code>SuperFoo</code>.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#define BUILDING_NODE_EXTENSION
  
  #ifndef FOOBAR_H_
  #define FOOBAR_H_
  
  #include &lt;algorithm&gt;
  #include &lt;cmath&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  #include &lt;node.h&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  #include &lt;iostream&gt;
  
  #include "EnvironmentObject.h"
  #include "SuperFoo.h"
  #include "Grid.h"
  #include "GridSection.h"
  #include "Point.h"
  #include "Teams.h"
  #include "Vector.h"
  
  class SuperFoo;
  class Grid;
  
  class Foobar : public SuperFoo
  {
  public:
      Foobar();
      virtual ~Foobar();
      Foobar(int id, const Point&amp; location, const int&amp; team, const Grid&amp; world);
      Foobar(const Foobar&amp; snake);
  
      // Node Implementation
      static void Init(v8::Handle&lt;v8::Object&gt; target);
  
  private:
      // Node Implementation
      static v8::Handle&lt;v8::Value&gt; nodeNew(const v8::Arguments&amp; args);
      static v8::Handle&lt;v8::Value&gt; nodeGetID(const v8::Arguments&amp; args);
      static v8::Handle&lt;v8::Value&gt; nodeGetTeam(const v8::Arguments&amp; args);
      static v8::Handle&lt;v8::Value&gt; nodeGetState(const v8::Arguments&amp; args);
      static v8::Handle&lt;v8::Value&gt; nodeGetVelocity(const v8::Arguments&amp; args);
  
      int id_;
      int team_;
      int state_;
      Vector* velocity_;
      const Grid* world_;
  };
  
  #endif /* FOOBAR_H_ */
  </code></pre>
  
  <pre class="lang-cpp prettyprint-override"><code>#define BUILDING_NODE_EXTENSION
  
  #ifndef GAMEOBJECT_H_
  #define GAMEOBJECT_H_
  
  #include &lt;string&gt;
  
  #include &lt;node.h&gt;
  
  #include "Point.h"
  #include "Vector.h"
  
  class SuperFoo : public node::ObjectWrap
  {
  public:
      SuperFoo()
          : collidable_(true), stationary_(true) {}
      virtual ~SuperFoo();
      SuperFoo(const std::string&amp; type, const Point&amp; position);
      SuperFoo(const SuperFoo&amp; object);
  
      virtual bool collide(SuperFoo&amp; object) = 0;
  
      Point getPosition() const;
      std::string getType() const;
  
      void setPosition(const Point&amp; position);
  
      bool isCollidable() const;
      bool isStationary() const;
  
      void setCollidable(bool coolide);
      void setStationary(bool stationary);
  
  protected:
      Point* position_;
  
  private:
      // Node Implementation
      static v8::Handle&lt;v8::Value&gt; nodeGetPosition(const v8::Arguments&amp; args);
  
      std::string* type_;
      bool collidable_;
      bool stationary_;
  };
  
  #endif /* SUPERFOO_H_ */
  </code></pre>
  
  <pre class="lang-cpp prettyprint-override"><code>void Foobar::Init(v8::Handle&lt;v8::Object&gt; target)
  {
      // Prepare constructor template
      v8::Local&lt;v8::FunctionTemplate&gt; tpl = v8::FunctionTemplate::New(nodeNew);
      tpl-&gt;SetClassName(v8::String::NewSymbol("Foobar"));
      tpl-&gt;InstanceTemplate()-&gt;SetInternalFieldCount(1);
      // Prototype functions
      tpl-&gt;PrototypeTemplate()-&gt;Set(v8::String::NewSymbol("getID"), v8::FunctionTemplate::New(nodeGetID)-&gt;GetFunction());
      tpl-&gt;PrototypeTemplate()-&gt;Set(v8::String::NewSymbol("getVelocity"), v8::FunctionTemplate::New(nodeGetVelocity)-&gt;GetFunction());
      tpl-&gt;PrototypeTemplate()-&gt;Set(v8::String::NewSymbol("getState"), v8::FunctionTemplate::New(nodeGetState)-&gt;GetFunction());
  
      v8::Persistent&lt;v8::Function&gt; constructor = v8::Persistent&lt;v8::Function&gt;::New(tpl-&gt;GetFunction());
      target-&gt;Set(v8::String::NewSymbol("Foobar"), constructor);
  }
  
  v8::Handle&lt;v8::Value&gt; Foobar::nodeNew(const v8::Arguments&amp; args)
  {
      v8::HandleScope scope;
  
      int id = args[0]-&gt;NumberValue();
      Point* position = ObjectWrap::Unwrap&lt;Point&gt;(args[1]-&gt;ToObject());
      int team = args[2]-&gt;NumberValue();
      Grid* world = ObjectWrap::Unwrap&lt;Grid&gt;(args[3]-&gt;ToObject());
  
      Foobar* foobar = new Foobar(id, *position, team, *world);
  
      foobar-&gt;Wrap(args.This());
  
      return args.This();
  }
  </code></pre>
  
  <pre class="lang-cpp prettyprint-override"><code>v8::Handle&lt;v8::Value&gt; Controller::nodeSpawnFoobar(const v8::Arguments&amp; args)
  {
      Controller* gridController = ObjectWrap::Unwrap&lt;Controller&gt;(args.This());
  
      int team = args[0]-&gt;NumberValue();
      gridController-&gt;createFoobar(foobarID++, team);
  
      return v8::Undefined();
  }
  
  void Controller::spawnFoobar(int id, int team)
  {
      Point createPosition;
      if(team == Teams::kBlue)
      {
          createPosition = world_-&gt;getAreaPosition(Teams::kBlue)-&gt;getPosition();
      }
      else if (team == Teams::kRed)
      {
          createPosition = world_-&gt;getAreaPosition(Teams::kRed)-&gt;getPosition();
      }
  
      int xh = createPosition.get()[0] - EnvironmentObject::kAreaSize / 2
      int yh = createPosition.get()[1] + EnvironmentObject::kAreaSize / 2;
  
      int x = (rand() % EnvironmentObject::kAreaSize) + xh;
      int y = (rand() % EnvironmentObject::kAreaSize) + yh - EnvironmentObject::kAreaSize;
  
      foobars_-&gt;push_back(new Foobar(id, Point(x, y), team, *world_));
  }
  </code></pre>
  
  <h2>Javascript Call</h2>
  
  <p>Here's how the method is being used</p>
  
  <pre class="lang-js prettyprint-override"><code>this.update = function() {
      if([some event]) {
          controller.createFoobar();
          console.log(this.getFoobars())
      }
  }
  
  this.getFoobars = function() {
      var foobars = controller.getFoobars();
      var foobarsArray = new Array();
  
      for(i = 0; i &lt; foobars.length; i++) {
          var foobar = foobar[i];
  
          var position = foobar.getPosition();
          var posX = position.getX();
          var posY = position.getY();
          var positionPoint = new Point(posX, posY);
  
          var velocity = foobar.getVelocity();
          var toX = velocity.getToX();
          var toY = velocity.getToX();
          var velocityVector = new Vector(toX, toY);
  
          var foobarObj = {
                  id: foobar.getID(),
                  team: foobar.getTeam(),
                  position: positionPoint,
                  velocity: velocityVector,
                  state: foobar.getState()
          }
  
          foobarsArray.push(foobarObj);
      }
  
      return miniSnakesArray;
  };
  </code></pre>