Node.js 从C++; 我使用节点模块,并希望调用来自C++的ObjeWTrp子类的一些方法。我不完全清楚如何在函数定义中正确构造Arguments对象

Node.js 从C++; 我使用节点模块,并希望调用来自C++的ObjeWTrp子类的一些方法。我不完全清楚如何在函数定义中正确构造Arguments对象,node.js,v8,Node.js,V8,例如,我想调用以下方法(Context2d扩展ObjectWrap): 句柄 Context2d::LineTo(常量参数和参数){ 手镜镜; 如果(!args[0]->IsNumber()) return ThrowException(Exception::TypeError(String::New(“lineTo()x必须是一个数字”)); 如果(!args[1]->IsNumber()) return ThrowException(Exception::TypeError(String::

例如,我想调用以下方法(Context2d扩展ObjectWrap):

句柄
Context2d::LineTo(常量参数和参数){
手镜镜;
如果(!args[0]->IsNumber())
return ThrowException(Exception::TypeError(String::New(“lineTo()x必须是一个数字”));
如果(!args[1]->IsNumber())
return ThrowException(Exception::TypeError(String::New(“lineTo()y必须是一个数字”));
Context2d*context=ObjectWrap::Unwrap(args.This());
cairo_line_to(context->context(),args[0]->NumberValue(),args[1]->NumberValue());
返回未定义();
}

因此,简单地说:有一个未包装的Context2D,我如何调用静态LineTo,以便从args返回相同的实例?我当然意识到我可以通过深入研究v8来解决这个问题,但我希望有人能为我指出正确的方向。

你应该可以用这样的东西来称呼它。我假设函数是对象上的
toLine
。你还没有展示你的对象原型结构,所以你必须调整它来匹配

int x = 10;
int y = 20;
Context2D *context = ...;

// Get a reference to the function from the object.
Local<Value> toLineVal = context->handle_->Get(String::NewSymbol("toLine"));
if (!toLineVal->IsFunction()) return; // Error, toLine was replaced somehow.

// Cast the generic reference to a function.
Local<Function> toLine = Local<Function>::Cast(toLineVal);

// Call the function with two integer arguments.
Local<Value> args[2] = {
  Integer::New(x),
  Integer::New(y)
};
toLine->Call(context->handle_, 2, args);
int x = 10;
int y = 20;
Context2D *context = ...;

// Get a reference to the function from the object.
Local<Value> toLineVal = context->handle_->Get(String::NewSymbol("toLine"));
if (!toLineVal->IsFunction()) return; // Error, toLine was replaced somehow.

// Cast the generic reference to a function.
Local<Function> toLine = Local<Function>::Cast(toLineVal);

// Call the function with two integer arguments.
Local<Value> args[2] = {
  Integer::New(x),
  Integer::New(y)
};
toLine->Call(context->handle_, 2, args);
var toLineVal = context.toLine;
if (typeof toLineVal !== 'function') return; // Error

toLineVal(10, 20);