title: How to return a new V8 javascript "class" instance from within C++?
body: <p>When using V8 as a scripting engine I expose a C++ function called <code>construct_with_ec6_syntax</code> to Javascript. This function, when invoked, should simply return an instance of <code>some_ec6_class</code>.</p>
  
  <p>This C++ function should basically do the following equivalent Javascript:</p>
  
  <pre><code>return new some_ec6_class(111, 222);
  </code></pre>
  
  <p>This class will be defined in Javascript as follows <strong>with</strong> EC6 syntax:</p>
  
  <pre><code>class some_ec6_class
  {
      constructor(x, y) {
          this.result = x + y;
      }
  }
  </code></pre>
  
  <p>My goal is to run the following in Javascript...</p>
  
  <pre><code>var the_instance = construct_with_ec6_syntax(111, 222);
  the_instance.result ; // .. and get 333 here.
  </code></pre>
  
  <p>My current implementation for the C++ function is this:</p>
  
  <pre><code>void construct_with_ec6_syntax(const FunctionCallbackInfo&lt;Value&gt;&amp; args) {
      Handle&lt;Object&gt; global = args.GetIsolate()-&gt;GetCurrentContext()-&gt;Global();
      std::string newvecfunc = "some_ec6_class";
      Handle&lt;Value&gt; value = global-&gt;Get(String::NewFromUtf8(args.GetIsolate(), newvecfunc.c_str(), String::kNormalString, newvecfunc.length()));
      Local&lt;Value&gt; result;
      if (value-&gt;IsFunction()) {
          Handle&lt;Function&gt; func = Handle&lt;Function&gt;::Cast(value);
          Handle&lt;Value&gt; args2[2];
          args2[0] = Number::New(args.GetIsolate(), 111);
          args2[1] = Number::New(args.GetIsolate(), 222);
          result = func-&gt;CallAsConstructor(args.GetIsolate()-&gt;GetCurrentContext(), 2, args2).ToLocalChecked();
      }   
      args.GetReturnValue().Set(result);
  }   
  </code></pre>
  
  <p>Running this function from Javascript has it return <code>undefined</code>! Instead of the object I expect. As xaxxon pointed out to me, it's because <code>value-&gt;IsFunction()</code> returns false, but <code>value-&gt;IsUndefined()</code> returns true. If I had defined the class using the <strong>with non</strong> EC6 syntax, the above function <em>does</em> return an instance..</p>
  
  <pre><code>function some_non_ec6_class(x, y) // this guy would work with the above function
  {
      this.result = x + y;
  }
  </code></pre>
  
  <p>So I'm a bit confused! Do I somehow need to be more specific, like get the <code>constructor</code> function from the object first, and then invoke <code>CallAsConstructor</code>?</p>
  
  <p>Any hint is appreciated!</p>
  
  <p>(This question looks similar to
  <a href="https://stackoverflow.com/questions/11387015/calling-a-v8-javascript-function-from-c-with-an-argument">Calling a v8 javascript function from c++ with an argument</a> but is different.)</p>
  
  <p>I'm using a V8 checkout from 22nd of October 2016.
  Complete testcase: </p>
  
  <p><a href="https://gist.github.com/rayburgemeestre/c0abd528f6f67edbfe686d484c45ddbb" rel="nofollow noreferrer">https://gist.github.com/rayburgemeestre/c0abd528f6f67edbfe686d484c45ddbb</a></p>
  
  <p><strong>Minor update:</strong></p>
  
  <p>As you can see in the comments, I also made a more specific test-case regarding "fetching" the class from the context here: <a href="https://gist.github.com/rayburgemeestre/df6193d532c7b7908fe27c89799bfa3a" rel="nofollow noreferrer">https://gist.github.com/rayburgemeestre/df6193d532c7b7908fe27c89799bfa3a</a></p>
  
  <p>Also I posted to the v8-users mailinglist: <a href="https://groups.google.com/forum/#!topic/v8-users/Hj2j4rJMwBw" rel="nofollow noreferrer">https://groups.google.com/forum/#!topic/v8-users/Hj2j4rJMwBw</a></p>