On Thu, May 7, 2015 at 6:08 AM, Jon Lederman <j...@thesoniccloud.com> wrote:
> I have the following two wrappers listed below.  I want the Api method of B
> to return an A in javascript.  How do I do this?  I can’t figure out how to
> construct the B class from the A Api method and return it?  Any help would
> be greatly appreciated.
>
> Jon
>
> class A : public node::ObjectWrap {
> public:
>  static void Init(v8::Handle<v8::Object> exports);
>
> private:
>  explicit A(Thing *thing);
>  ~A();
>
>  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
>  static void Api(const v8::FunctionCallbackInfo<v8::Value>& args);
>  static v8::Persistent<v8::Function> constructor;
>
>  ESLconnection* _esl_connection;
> };
>
> #endif
>
> class B : public node::ObjectWrap {
> public:
>  static void Init(v8::Handle<v8::Object> exports);
>
>
>
> private:
>
>  ~B();
>  explicit B(ESLevent *event);
>
> };
>
> #endif

The code below should work.  You mention both A from B and B from A in
your post.  This code implements the former.

  void B::F(const v8::FunctionCallbackInfo<v8::Value>& args) {
    v8::Local<v8::FunctionTemplate> function_template =
        v8::FunctionTemplate::New(args.GetIsolate(), A::New);
    v8::Local<v8::Function> function = function_template->GetFunction();
    v8::Local<v8::Value> object_v = function->NewInstance();  // Can pass args.
    if (object_v.IsEmpty()) return;  // Constructor threw.
    assert(object_v->IsObject());  // Should never fail.
    v8::Local<v8::Object> object = object_v.As<v8::Object>();
    // Do something with |object| here.
    args.GetReturnValue().Set(object);
  }

You may want to cache the v8::FunctionTemplate or the v8::Function in
a v8::Persistent so you don't have to recreate it all the time.  Good
luck!

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAHQurc9RrBTF%2Bz3euXE8a2bAqzw7BDZfRcp7JbuqZ5g8oj8MPQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to