File: array.cc

package info (click to toggle)
node-addon-api 8.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,248 kB
  • sloc: cpp: 15,431; javascript: 5,631; ansic: 157; makefile: 7
file content (40 lines) | stat: -rw-r--r-- 1,075 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "napi.h"

using namespace Napi;

Value CreateArray(const CallbackInfo& info) {
  if (info.Length() > 0) {
    size_t length = info[0].As<Number>().Uint32Value();
    return Array::New(info.Env(), length);
  } else {
    return Array::New(info.Env());
  }
}

Value GetLength(const CallbackInfo& info) {
  Array array = info[0].As<Array>();
  return Number::New(info.Env(), static_cast<uint32_t>(array.Length()));
}

Value GetElement(const CallbackInfo& info) {
  Array array = info[0].As<Array>();
  size_t index = info[1].As<Number>().Uint32Value();
  return array[index];
}

void SetElement(const CallbackInfo& info) {
  Array array = info[0].As<Array>();
  size_t index = info[1].As<Number>().Uint32Value();
  array[index] = info[2].As<Value>();
}

Object InitBasicTypesArray(Env env) {
  Object exports = Object::New(env);

  exports["createArray"] = Function::New(env, CreateArray);
  exports["getLength"] = Function::New(env, GetLength);
  exports["get"] = Function::New(env, GetElement);
  exports["set"] = Function::New(env, SetElement);

  return exports;
}