So, I want to pass a map from C++ code to js script. Keys are numbers and values are lists with objects that are wrapped using ordinary Node.js techniques. That's the code:
Handle<Value> topologicalSortedGraph( const Arguments &args ) {
HandleScope scope;
...
QMap<int, QList<Actor *> > topologicalSortedGraph = scriptContext->getTopologicalSortedGraph();
const int schemeTiersCount = topologicalSortedGraph.size();
Local<Object> scheme = Object::New( );
for ( int i = 0; schemeTiersCount > i; ++i ) {
Local<Object> tier = Object::New( );
foreach ( Actor *actor, topologicalSortedGraph[i] ) {
Handle<Value> actorInitData[] = { Int32::New( reinterpret_cast<int>( actor ) ) };
Handle<Value> wrappedActor = ActorWrap::newInstance( 1, actorInitData );
tier->Set( String::NewSymbol( actor->getId( ).toLocal8Bit( ).constData( ) ),
wrappedActor );
}
scheme->Set( i, tier );
}
return scope.Close( scheme );
}
Then I invoke following code in the script:
var addon = require('./Addon');
...
var scheme = addon.topologicalSortedGraph();
for (var number in scheme) {
for (var actor in scheme[number]) {
if (actor.isReady()) {
addon.tick(actor);
break;
}
}
}
...
The issue is the script cannot recognize an 'actor' object and it prints to console 'undefined' when 'actor.isReady()' is being invoked. BTW, here is the definition of 'ActorWrap' class:
class ActorWrap : public node::ObjectWrap {
public:
static void init( );
static Handle<Value> newInstance( int argc, const Handle<Value> *argv );
private:
ActorWrap( const Actor *initActor );
~ActorWrap( );
static Handle<Value> newObject( const Arguments &args );
static Handle<Value> id( const Arguments &args );
static Handle<Value> label( const Arguments &args );
static Handle<Value> isDone( const Arguments &args );
static Handle<Value> isReady( const Arguments &args );
static Persistent<Function> CONSTRUCTOR;
static const char * CLASS_NAME;
const Actor * actor;
};
And implementation:
Persistent<Function> ActorWrap::CONSTRUCTOR;
const char *ActorWrap::CLASS_NAME = "Actor";
ActorWrap::ActorWrap( const Actor *initActor ) : actor( initActor ) {
}
ActorWrap::~ActorWrap( ) {
}
void ActorWrap::init( ) {
Local<FunctionTemplate> tpl = FunctionTemplate::New( newObject );
tpl->SetClassName( String::NewSymbol( CLASS_NAME ) );
tpl->InstanceTemplate()->SetInternalFieldCount( 1 );
tpl->PrototypeTemplate( )->Set( String::NewSymbol( "id" ),
FunctionTemplate::New( id )->GetFunction( ) );
tpl->PrototypeTemplate( )->Set( String::NewSymbol( "label" ),
FunctionTemplate::New( label )->GetFunction( ) );
tpl->PrototypeTemplate( )->Set( String::NewSymbol( "isDone" ),
FunctionTemplate::New( isDone )->GetFunction( ) );
tpl->PrototypeTemplate( )->Set( String::NewSymbol( "isReady" ),
FunctionTemplate::New( isReady )->GetFunction( ) );
CONSTRUCTOR = Persistent<Function>::New( tpl->GetFunction( ) );
}
Handle<Value> ActorWrap::newInstance( int argc, const Handle<Value> *argv ) {
HandleScope scope;
Handle<Value> objectInitData[] = { argv[0] };
Local<Object> instance = CONSTRUCTOR->NewInstance( 1, objectInitData );
return scope.Close( instance );
}
Handle<Value> ActorWrap::newObject( const Arguments &args ) {
HandleScope scope;
const Actor *actor = reinterpret_cast<Actor *>( args[0]->Int32Value( ) );
Q_ASSERT( NULL != actor );
ActorWrap *obj = new ActorWrap( actor );
obj->Wrap( args.This( ) );
return args.This( );
}
Handle<Value> ActorWrap::id( const Arguments &args ) {
HandleScope scope;
ActorWrap* obj = ObjectWrap::Unwrap<ActorWrap>( args.This( ) );
return scope.Close( String::New( obj->actor->getId( ).toLocal8Bit( ).constData( ) ) );
}
Handle<Value> ActorWrap::label( const Arguments &args ) {
HandleScope scope;
ActorWrap* obj = ObjectWrap::Unwrap<ActorWrap>( args.This( ) );
return scope.Close( String::New( obj->actor->getLabel( ).toLocal8Bit( ).constData() ) );
}
Handle<Value> ActorWrap::isDone( const Arguments &args ) {
HandleScope scope;
ActorWrap* obj = ObjectWrap::Unwrap<ActorWrap>( args.This( ) );
LocalWorkflow::BaseWorker *worker = obj->actor->castPeer<BaseWorker>( );
Q_ASSERT( NULL != worker );
return scope.Close( Boolean::New( worker->isDone() ) );
}
Handle<Value> ActorWrap::isReady( const Arguments &args ) {
HandleScope scope;
ActorWrap* obj = ObjectWrap::Unwrap<ActorWrap>( args.This( ) );
LocalWorkflow::BaseWorker *worker = obj->actor->castPeer<BaseWorker>( );
Q_ASSERT( NULL != worker );
return scope.Close( Boolean::New( worker->isReady() ) );
}
I suspect that something goes wrong when I initialize a 'tier' object in the first snippet. I didn't managed to find any information about setting an object as a property of another one, so may be I was wrong when called just "obj1->Set( string, obj2 );"
Well, the issue was trivial. Since I quite new to js I had a mistake in the code that iterates my associative array. Correct variant follows
var addon = require('./Addon');
...
var scheme = addon.topologicalSortedGraph();
for (var number in scheme) {
for (var actor in scheme[number]) {
if (scheme[number][actor].isReady()) {
addon.tick(actor);
break;
}
}
}
...
All the C++ code is correct and wrapped objects are correct too.