2020年12月22日星期二

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.

  1. I'm concerned about memory leaks so if I'm causing any please let me know.

  2. And how do I push an array onto that out array similar to [].push([0, 1]). 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.

#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);  }  
https://stackoverflow.com/questions/65401082/nan-building-and-looping-over-an-array December 22, 2020 at 06:56AM

没有评论:

发表评论