-
-
Notifications
You must be signed in to change notification settings - Fork 497
Expand file tree
/
Copy patharray.cc
More file actions
40 lines (32 loc) · 1.05 KB
/
array.cc
File metadata and controls
40 lines (32 loc) · 1.05 KB
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;
}