Re: [nodejs] Access js modules from a C++ module

35 views
Skip to first unread message

Ben Noordhuis

unread,
Oct 11, 2012, 11:29:51 AM10/11/12
to nod...@googlegroups.com
On Thu, Oct 11, 2012 at 5:21 PM, Sam Grondahl <samgr...@gmail.com> 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
Reply all
Reply to author
Forward
0 new messages