7

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

在返回声明中,我收到错误消息。谁能告诉我如何从 V8 C++ 函数返回一个结构。

4

3 回答 3

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}
于 2013-01-25T03:20:18.153 回答
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();
}
于 2013-02-20T09:35:26.360 回答
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) 
于 2019-04-02T12:12:47.140 回答