如何将关联数组从Node.js C ++插件传递给js代码?

时间:2013-03-24 12:25:14

标签: c++ node.js qt v8

所以,我想将一个映射从C ++代码传递给js脚本。键是数字,值是包含使用普通Node.js技术包装的对象的列表。这就是代码:

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 );

}

然后我在脚本中调用以下代码:

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;
        }
    }
}
...

问题是脚本无法识别'actor'对象,并且在调用'actor.isReady()'时会打印到控制台'undefined'。顺便说一下,这里是'ActorWrap'类的定义:

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;
};

并实施:

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() ) );
}

我怀疑在第一个片段中初始化'tier'对象时出现了问题。我没有设法找到任何关于将对象设置为另一个对象的属性的信息,因此当我调用“obj1-&gt; Set(string,obj2);”时可能是错的。“

1 个答案:

答案 0 :(得分:0)

嗯,这个问题很简单。由于我对js很新,我在代码中有一个错误,它会迭代我的关联数组。正确的变体如下

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;
        }
    }
}
...

所有C ++代码都是正确的,包装对象也是正确的。