简体   繁体   English

Nan构建并循环数组

[英]Nan building and looping over an array

I'm able to execute a hello world example, but beyond that I'm new to nan and node add-ons .我能够执行一个 hello world 示例,但除此之外,我对nannode add-ons还是陌生的。

  1. I'm concerned about memory leaks so if I'm causing any please let me know.我担心 memory 泄漏,所以如果我造成任何泄漏,请告诉我。

  2. And how do I push an array onto that out array similar to [].push([0, 1]) .以及如何将数组推送到类似于[].push([0, 1])out数组上。 I'm not sure how to do it in the cleanest way possible without creating a new variable to store it - if possible.如果可能的话,我不确定如何在不创建新变量来存储它的情况下以最干净的方式进行操作。

  3. Also if there's anything else I'm doing that's not best practice please let me know.另外,如果我正在做的其他事情不是最佳做法,请告诉我。 I've been researching this for a while now.我已经研究了一段时间了。

Here's the code I have so far这是我到目前为止的代码

  #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();
                }
            }
       } 
  }

I was later able to write this.. If anyone can point out if I approached it correctly and/or if there are any memory issues I need to watch out for.我后来能够写这个。如果有人能指出我是否正确接近它和/或是否有任何 memory 问题我需要注意。

#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);
}

I don't think you have any leaks - in fact there is no implicit memory allocation at all in your code - but in case you need it, I suggest you check the gyp files of my addons for more information on how to build them with asan with g++ or clang .我不认为您有任何泄漏-实际上,您的代码中根本没有隐式 memory 分配-但是如果您需要它,我建议您查看我的插件的 gyp 文件以获取有关如何构建它们的更多信息asang++clang As far as I am concerned, it is a mandatory step when creating Node addons.就我而言,这是创建 Node 插件时必须执行的步骤。

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

The option is called --enable_asan该选项称为--enable_asan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM