-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathstrict_type_demo.cpp
More file actions
181 lines (133 loc) · 4.53 KB
/
Copy pathstrict_type_demo.cpp
File metadata and controls
181 lines (133 loc) · 4.53 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <node.h>
#include <string>
#include <algorithm>
#include <iostream>
using namespace v8;
/* Demonstrates converstions of v8:Value's passed as arguments.
See https://v8docs.nodesource.com/node-0.8/dc/d0a/classv8_1_1_value.html
for more types and conversion methods
*/
Local<Value> make_return(Isolate * isolate, const Local<Object> input ) ;
/* NOTE: Anytime you fail to set the return value - args.getReturnValue().Set(...) -
your addon will return undefined
*/
void PassNumber(const FunctionCallbackInfo<Value>& args) {
Isolate * isolate = args.GetIsolate();
if ( args.Length() < 1 ) {
return;
}
if ( !args[0]->IsNumber()) {
return;
}
double value = args[0]->NumberValue();
Local<Number> retval = Number::New(isolate, value + 42);
args.GetReturnValue().Set(retval);
}
void PassInteger(const FunctionCallbackInfo<Value>& args) {
Isolate * isolate = args.GetIsolate();
if ( args.Length() < 1 ) {
return;
}
if ( !args[0]->IsInt32()) {
return;
}
int value = args[0]->Int32Value();
Local<Number> retval = Int32::New(isolate, value + 42);
args.GetReturnValue().Set(retval);
}
void PassBoolean(const FunctionCallbackInfo<Value>& args) {
Isolate * isolate = args.GetIsolate();
if ( args.Length() < 1 || !args[0]->IsBoolean()) {
return;
}
bool value = args[0]->BooleanValue();
Local<Boolean> retval = Boolean::New(isolate, !value);
args.GetReturnValue().Set(retval);
}
void PassString(const FunctionCallbackInfo<Value>& args) {
Isolate * isolate = args.GetIsolate();
if ( args.Length() < 1 ) {
return;
}
else if ( args[0]->IsNull() ) {
return;
}
else if ( args[0]->IsUndefined() ) {
return;
}
else if (!args[0]->IsString()) {
// This clause would catch IsNull and IsUndefined too...
return ;
}
v8::String::Utf8Value s(args[0]);
std::string str(*s, s.length());
std::reverse(str.begin(), str.end());
Local<String> retval = String::NewFromUtf8(isolate, str.c_str());
args.GetReturnValue().Set(retval);
}
void PassObject(const FunctionCallbackInfo<Value>& args) {
Isolate * isolate = args.GetIsolate();
if ( args.Length() < 1 || ! args[0]->IsObject()) {
// just return undefined
return;
}
Local<Object> target = args[0]->ToObject();
Local<Value> obj = make_return(isolate, target);
args.GetReturnValue().Set(obj);
}
void PassArray(const FunctionCallbackInfo<Value>& args) {
Isolate * isolate = args.GetIsolate();
Local<Array> array = Local<Array>::Cast(args[0]);
if ( args.Length() < 1 || ! args[0]->IsArray()) {
return;
}
if (array->Length() < 3) {
return;
}
Local<String> prop = String::NewFromUtf8(isolate, "not_index");
if (array->Get(prop)->IsUndefined() ){
return;
}
for (unsigned int i = 0; i < 3; i++ ) {
if (array->Has(i)) {
Local<Value> v = array->Get(i);
if ( !v->IsNumber()) return;
double value = v->NumberValue();
array->Set(i, Number::New(isolate, value + 1));
}
else {
return;
}
}
Local<Array> a = Array::New(isolate);
a->Set(0, array->Get(0));
a->Set(1, array->Get(prop));
a->Set(2, array->Get(2));
args.GetReturnValue().Set(a);
}
Local<Value> make_return(Isolate * isolate, const Local<Object> input ) {
Local<String> x_prop = String::NewFromUtf8(isolate, "x");
Local<String> y_prop = String::NewFromUtf8(isolate, "y");
Local<String> sum_prop = String::NewFromUtf8(isolate, "sum");
Local<String> product_prop = String::NewFromUtf8(isolate, "product");
Local<Value> x_value = input->Get(x_prop);
Local<Value> y_value = input->Get(y_prop);
if ( !x_value->IsNumber() || !y_value->IsNumber()) {
return v8::Undefined(isolate);
}
double x = input->Get(x_prop)->NumberValue();
double y = input->Get(y_prop)->NumberValue();
Local<Object> obj = Object::New(isolate);
obj->Set(sum_prop, Number::New(isolate, x + y));
obj->Set(product_prop, Number::New(isolate, x * y));
return obj;
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "pass_number", PassNumber);
NODE_SET_METHOD(exports, "pass_integer", PassInteger);
NODE_SET_METHOD(exports, "pass_string", PassString);
NODE_SET_METHOD(exports, "pass_boolean", PassBoolean);
NODE_SET_METHOD(exports, "pass_object", PassObject);
NODE_SET_METHOD(exports, "pass_array", PassArray);
}
NODE_MODULE(strict_type_demo, init)