我是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;
}
在return语句中,我收到错误。谁能告诉我如何从V8 C ++函数返回结构。
答案 0 :(得分:6)
您想要创建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);
}
从Javascript调用:
log(JSON.stringify(example_object()))
输出
{"name":"Stackoverflow","url":"http://stackoverflow.com","javascript_tagged":317566}
答案 1 :(得分:1)
如果要创建node.js模块,
npm install ref
npm install ref-array
npm install ref-struct
在您的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());
模块源中的:
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();
}
答案 2 :(得分:0)
C ++
struct empDetails {
int empNo;
string empName;
};
void getDetails(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
if(args.Length() != 1) {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[0]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
int empId = args[0]->NumberValue();
// You can call your c++ function here instead of a returning a dummy struct
empDetails e;
e.empNo = empId;
e.empName "Hello World";
Local<Object> result = Object::New(isolate);
result->Set(String::NewFromUtf8(isolate, "empNo"), Number::New(isolate, e.empNo));
result->Set(String::NewFromUtf8(isolate, "empName"), String::NewFromUtf8(isolate, e.empName.c_str()));
args.GetReturnValue().Set(result);
}
JavaScript
const emp = require('your-module')
var empDetails = obj.getDetails(123)
console.log("number:" + empDetails.empNo + " name: " + empDetails.empNo)