楠在一个阵列上盘旋



我能够执行hello-world示例,但除此之外,我还不熟悉nannode add-ons

  1. 我担心内存泄漏,所以如果我造成了任何泄漏,请告诉我知道

  2. 如何将数组推送到out数组上,类似于[].push([0, 1])。如果可能的话,如果不创建一个新的变量来存储它,我不知道如何以最干净的方式完成它。

  3. 此外,如果我正在做的其他事情不是最佳实践,请告诉我!我已经研究了一段时间了。

这是我迄今为止拥有的代码

#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, 0, Nan::New(xPos + xArg));
(void) xy->Set(context, 1, Nan::New(yPos + yArg));
(void) out->Set(context, index++, xy);
}
}
}
}
info.GetReturnValue().Set(out);
}

我不认为您有任何泄漏-事实上,您的代码中根本没有隐式内存分配-但如果您需要,我建议您查看我的插件的gyp文件,以了解如何使用asang++clang构建它们的更多信息。就我而言,这是创建Node插件时必须执行的步骤。

https://github.com/mmomtchev/node-gdal-async/blob/master/binding.gyphttps://github.com/mmomtchev/exprtk.js/blob/main/binding.gyp

该选项称为--enable_asan

最新更新