On Thu, Oct 11, 2012 at 5:21 PM, Sam Grondahl <[email protected]> wrote:
> I routinely access compiled c++ modules in js modules by passing in the
> module name as an argument when creating the other module, like so:
>
> var cppModule = require('./cppModule');
> var cppModObject = new cppModule.SomeObject;
> var jsModule = require('./jsModule');
> var jsModObject = new jsModule.OtherObject(cppModObject);
>
> And in my js I have something like:
>
> var jsModule = function(otherModArg) {
>   var otherModuleObject = otherModArg;
>   ...
> }
>
> My question is whether I can do the reverse, i.e. pass in some instantiated
> js object into my c++ module and access that object within the c++. I'm
> happy to post more code if it would be helpful...I haven't been able to find
> any doc on this yet. Thanks!

You can and it's easy:

  // export this function as "binding"
  Handle<Value> Binding(const Arguments& args) {
    HandleScope scope;
    Local<Integer> val =
args[0]->ToObject()->Get(String::New("x"))->ToInteger();
    return scope.Close(val);
  }

And call it like this:

  var x = require('./cpp_module').binding({x: 42});
  console.log(x); // 42

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to