cod*_*der 6 javascript v8 visual-c++
我是Javascript和V8库的新手.我的要求是调用C++函数并将C结构返回给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;
}
Run Code Online (Sandbox Code Playgroud)
在return语句中,我收到错误.谁能告诉我如何从V8 C++函数返回一个结构.
您想要创建Javascript对象并使用您的数据填充它的每个成员.
#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);
}
Run Code Online (Sandbox Code Playgroud)
从Javascript调用:
log(JSON.stringify(example_object()))
Run Code Online (Sandbox Code Playgroud)
产量
{"name":"Stackoverflow","url":"http://stackoverflow.com","javascript_tagged":317566}
Run Code Online (Sandbox Code Playgroud)