问题描述
我可以执行一个 hello world 示例,但除此之外,我还是 nan
和 node add-ons
的新手。
-
我担心内存泄漏,所以如果我造成任何问题,请让我 知道。
-
以及如何将数组推送到该
out
数组上,类似于[].push([0,1])
。如果可能的话,我不知道如何在不创建新变量来存储它的情况下以最干净的方式做到这一点。 -
另外,如果我正在做的其他事情不是最佳实践,请告诉我!我已经研究了一段时间了。
这是我目前的代码
#include <nan.h>
void Method(const Nan::FunctionCallbackInfo <v8::Value> &info) {
v8::Local <v8::Context> context = info.GetIsolate()->GetCurrentContext();
v8::Local <v8::Array> coordinate = v8::Local<v8::Array>::Cast(info[0]);
unsigned int radius = info[2]->Uint32Value(context).FromJust();
// Also if creating the array is wasteful this way by giving it the max possible size
v8::Local <v8::Array> out = Nan::New<v8::Array>(x * y);
for (int x = -radius; x <= radius; ++x) {
for (int y = -radius; y <= radius; ++y) {
if (x * x + y * y <= radius * radius) {
// I need to push something like [x + coordinate->Get(context,0),y + coordinate->Get(context,0)];
out->push_back();
}
}
}
}
我后来能够写这个..如果有人能指出我是否正确地接近它和/或是否有任何我需要注意的记忆问题。
#include <nan.h>
void Method(const Nan::FunctionCallbackInfo <v8::Value> &info) {
v8::Local <v8::Context> context = info.GetIsolate()->GetCurrentContext();
v8::Local <v8::Array> coordinates v8::Local<v8::Array>::Cast(info[0]);
int radius = info[1]->Int32Value(context).FromJust();
v8::Local <v8::Array> out = Nan::New<v8::Array>();
int index = 0;
for (unsigned int i = 0; i < coordinates->Length(); i++) {
v8::Local <v8::Array> coordinate = v8::Local<v8::Array>::Cast(coordinates->Get(context,i).ToLocalChecked());
int xArg = coordinate->Get(context,0).ToLocalChecked()->Int32Value(context).FromJust();
int yArg = coordinate->Get(context,1).ToLocalChecked()->Int32Value(context).FromJust();
for (int xPos = -radius; xPos <= radius; ++xPos) {
for (int yPos = -radius; yPos <= radius; ++yPos) {
if (xPos * xPos + yPos * yPos <= radius * radius) {
v8::Local <v8::Array> xy = Nan::New<v8::Array>();
(void) xy->Set(context,Nan::New(xPos + xArg));
(void) xy->Set(context,1,Nan::New(yPos + yArg));
(void) out->Set(context,index++,xy);
}
}
}
}
info.GetReturnValue().Set(out);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)