2012-11-01 8 views
5

Jestem nowicjuszem w bibliotece JavaScript i V8. Moje wymaganie to wywołanie funkcji C++ i zwrócenie struktury C z powrotem do modułu JavaScript.jak przywrócić strukturę z V8 C++ do modułu javascript

struct empDetails { 
    int empNo; 
    string empName; 
}; 

v8::Handle<v8::Value> getDetails(const v8::Arguments &args) { 
    if ((args.Length() != 1) || !args[0]->IsUint32()) { 
     return v8::ThrowException(v8::Exception::Error  
       (v8::String::New("Invalid> arguments."))); 
    } 
    uint32_t userId = args[0]->ToUint32()->Value(); 
    empDetails e; 
    company::GetEmpdetails(userId, e); // other static function in my project 
    return e; 
} 

W oświadczeniu zwrotnym pojawia się błąd. Czy ktoś może mi powiedzieć, jak zwrócić struct z V8 C++ funkcji.

+2

Co to znaczy, aby powrócić do C struct do JavaScript? Czy nie chcesz (trzeba?) Zawinąć go w obiekcie JavaScript, jak np. '{EmpNo: 1, empName:" John Doe "}'? – maerics

+1

[Poradnik dla programistów osadzonych w V8 na temat szablonów obiektów] (https://developers.google.com/v8/embed#templates) prawdopodobnie sprawi, że znajdziesz się we właściwym miejscu. – maerics

+0

wygląda na to, że nie przeczytałem wystarczająco dużo o szablonach obiektów v8, dziękuję za odpowiedź – coder

Odpowiedz

6

Chcesz utworzyć obiekt Javascript i zapełniać go wszystkimi swoimi danymi.

#define function(name) v8::Handle<v8::Value> name(const v8::Arguments& a) 

    function (example_object) { 
     v8::HandleScope handle_scope; 
     Handle<Object> Result = Object::New(); 
     Result->Set(String::New("name"), String::New("Stackoverflow")); 
     Result->Set(String::New("url"), String::New("http://stackoverflow.com")); 
     Result->Set(String::New("javascript_tagged"), Number::New(317566)); 
     return handle_scope.Close(Result); 
    } 

połączeń z javascript:

log(JSON.stringify(example_object())) 

Wyjście

{"name":"Stackoverflow","url":"http://stackoverflow.com","javascript_tagged":317566} 
1

Gdy chcesz utworzyć moduł node.js,

npm install ref 
npm install ref-array 
npm install ref-struct 

w źródle JS:

var ref = require('ref'); 
var ArrayType = require('ref-array') 
var StructType = require('ref-struct'); 
var empDetails = StructType({ 
    empNo: ref.types.int, 
    empName: ArrayType('char', STRING_LENGTH) 
}); 
var result = new empDetails; 
getDetails(999, result.ref()); 

w źródle module:

struct empDetails { 
    int empNo; 
    char empName[STRING_LENGTH]; 
}; 
v8::Handle<v8::Value> getDetails(const v8::Arguments &args) { 
    if((args.Length() != 2) || !args[0]->IsUint32()){ 
     return v8::ThrowException(v8::Exception::Error  
      (v8::String::New("Invalid> arguments."))); 
    } 
    uint32_t userId = args[0]->ToUint32()->Value(); 
    struct empDetails src; 
    company::GetEmpdetails(userId, src); 
    v8::Handle<v8::Object> dst = args[1]->ToObject(); 
    if(node::Buffer::Length(dst) >= sizeof(struct empDetails)) 
     memcpy(node::Buffer::Data(dst), &src, sizeof(struct empDetails)); 
    return args.This(); 
} 
Powiązane problemy