-
Notifications
You must be signed in to change notification settings - Fork 0
/
addone.cc
123 lines (90 loc) · 3.75 KB
/
addone.cc
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// hello.cc
#include <node.h>
#include "addone_inner.cc"
#include <iostream>
//#include <cstddef>
using namespace std;
namespace simple {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Number;
using v8::Value;
using v8::ArrayBuffer;
void Addone(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
double res = addone(args[0]->NumberValue());
args.GetReturnValue().Set(Number::New(isolate, res));
}
void Addone_TA_F64(const FunctionCallbackInfo<Value>& args) {
// Get the input values, accounting for subarrays
v8::Local<v8::Float64Array> fa_input = args[0].As<v8::Float64Array>();
int l = fa_input -> ByteLength() / sizeof(double);
double *x = reinterpret_cast<double*>(static_cast<unsigned char*>(fa_input->Buffer()->GetContents().Data()) + fa_input->ByteOffset());
double* res = new double[l];
// Inner processing
for (int c = 0; c < l; c++) {
res[c] = addone(x[c]);
}
// Output a new typed array
args.GetReturnValue().Set(v8::Float64Array::New(v8::ArrayBuffer::New (args.GetIsolate(), res, l * sizeof(double)), 0, l));
}
void Addone_TA_I32(const FunctionCallbackInfo<Value>& args) {
// Get the input values, accounting for subarrays
v8::Local<v8::Int32Array> fa_input = args[0].As<v8::Int32Array>();
int l = fa_input -> ByteLength() / sizeof(int32_t);
int32_t *x = reinterpret_cast<int32_t*>(static_cast<unsigned char*>(fa_input->Buffer()->GetContents().Data()) + fa_input->ByteOffset());
int32_t* res = new int32_t[l];
// Inner processing
for (int c = 0; c < l; c++) {
res[c] = addone(x[c]);
}
// Output a new typed array
args.GetReturnValue().Set(v8::Int32Array::New(v8::ArrayBuffer::New (args.GetIsolate(), res, l * sizeof(int32_t)), 0, l));
}
/*
void _Addone_TA_F64(const FunctionCallbackInfo<Value>& args) {
v8::Local<v8::Float64Array> fa_input = args[0].As<v8::Float64Array>();
int l = fa_input -> ByteLength() / sizeof(double);
void *data = (fa_input->Buffer()->GetContents().Data());
size_t offset = fa_input->ByteOffset();
unsigned char* data_offset = static_cast<unsigned char*>(data + offset);
const double *x = reinterpret_cast<double*>(data_offset);
void *data = fa_input->Buffer()->GetContents().Data();
size_t offset = fa_input->ByteOffset();
unsigned char* data_offset = static_cast<unsigned char*>(data + offset);
double *x = reinterpret_cast<double*>(data_offset);
//v8::Local<v8::Float64Array> array = argument.As<v8::Float64Array>();
void *data = fa_input->Buffer()->GetContents().Data();
size_t offset = fa_input->ByteOffset();
//size_t data_ptr = reinterpret_cast<size_t>(data);
//data_ptr += offset;
//double *x = reinterpret_cast<double*>(data_ptr);
//unsigned char *data_offset = static_cast<unsigned char*>(data) + offset;
//double *x = reinterpret_cast<double*>(data_offset);
double *x = reinterpret_cast<double*>(static_cast<unsigned char*>(data) + offset);
//const double *x = reinterpret_cast<double*>
//const double *x = reinterpret_cast<double*>(fa_input -> Buffer() -> GetContents().Data());
//cout << "Length of array = " << (l) << endl;
double* res = new double[l];
for (int c = 0; c < l; c++) {
res[c] = addone(x[c]);
}
// Outputting a new typed array...
Local<ArrayBuffer> ab = v8::ArrayBuffer::New (args.GetIsolate(), res, l * sizeof(double));
//delete[] res;
//delete[] x;
delete[] data;
v8::Local<v8::Float64Array> f64a_res = v8::Float64Array::New(ab, 0, l);
args.GetReturnValue().Set(f64a_res);
}
*/
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "addone", Addone);
NODE_SET_METHOD(exports, "addone_ta_f64", Addone_TA_F64);
NODE_SET_METHOD(exports, "addone_ta_i32", Addone_TA_I32);
}
NODE_MODULE(addone, init)
} // namespace simple