-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCollectionsConverter.h
280 lines (240 loc) · 10.2 KB
/
CollectionsConverter.h
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) The NodeRT Contributors
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the ""License""); you may
// not use this file except in compliance with the License. You may obtain a
// copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
#pragma once
#include <collection.h>
#include <v8.h>
#include "CollectionsConverterUtils.h"
#include "NodeRtUtils.h"
#include "nan.h"
namespace NodeRT {
namespace Collections {
Nan::Persistent<v8::String> g_keyProp;
Nan::Persistent<v8::String> g_valueProp;
static void initProps() {
if (g_keyProp.IsEmpty())
g_keyProp.Reset(Nan::New<v8::String>("key").ToLocalChecked());
if (g_valueProp.IsEmpty())
g_valueProp.Reset(Nan::New<v8::String>("value").ToLocalChecked());
}
static std::function<bool(v8::Local<v8::Value>)> checkStringFunc =
[](v8::Local<v8::Value> value) -> bool { return value->IsString(); };
template <class K, class V>
static ::Platform::Collections::Map<K, V> ^
JsArrayToWinrtMap(
v8::Local<v8::Array> arr,
const std::function<bool(v8::Local<v8::Value>)>& checkKeyTypeFunc,
const std::function<K(v8::Local<v8::Value>)>& convertToKeyTypeFunc,
const std::function<bool(v8::Local<v8::Value>)>& checkValueTypeFunc,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc) {
std::map<K, V> stdMap;
if (!FillMapFromJsArray(arr, checkKeyTypeFunc, convertToKeyTypeFunc,
checkValueTypeFunc, convertToValueTypeFunc,
stdMap)) {
return nullptr;
}
// TODO: michfa: consider using std::move (here & everywhere), e.g: return
// ref new ::Platform::Collections::Map<K, V>(std::move(stdMap));
// std::move will give a more efficient initialization from std::map, will
// invalidate stdMap however- some types will throw while moving
return ref new ::Platform::Collections::Map<K, V>(stdMap);
}
template <class K, class V>
static ::Platform::Collections::MapView<K, V> ^
JsArrayToWinrtMapView(
v8::Local<v8::Array> arr,
const std::function<bool(v8::Local<v8::Value>)>& checkKeyTypeFunc,
const std::function<K(v8::Local<v8::Value>)>& convertToKeyTypeFunc,
const std::function<bool(v8::Local<v8::Value>)>& checkValueTypeFunc,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc) {
std::map<K, V> stdMap;
if (!FillMapFromJsArray(arr, checkKeyTypeFunc, convertToKeyTypeFunc,
checkValueTypeFunc, convertToValueTypeFunc,
stdMap)) {
return nullptr;
}
return ref new ::Platform::Collections::MapView<K, V>(stdMap);
}
// A special implementation for the case were the map's keys are strings
// In this case we expect a non-array JS object.
template <class V>
static ::Platform::Collections::Map<Platform::String ^, V> ^
JsObjectToWinrtMap(
v8::Local<v8::Object> obj,
const std::function<bool(v8::Local<v8::Value>)>& checkValueTypeFunc,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc) {
std::map<::Platform::String ^, V> stdMap;
if (!FillMapFromJsObject(
obj, checkStringFunc, NodeRT::Utils::V8StringToPlatformString,
checkValueTypeFunc, convertToValueTypeFunc, stdMap)) {
return nullptr;
}
return ref new ::Platform::Collections::Map<::Platform::String ^, V>(
stdMap);
}
template <class V>
static ::Platform::Collections::MapView<Platform::String ^, V> ^
JsObjectToWinrtMapView(
v8::Local<v8::Object> obj,
const std::function<bool(v8::Local<v8::Value>)>& checkValueTypeFunc,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc) {
std::map<::Platform::String ^, V> stdMap;
if (!FillMapFromJsObject(
obj, checkStringFunc, NodeRT::Utils::V8StringToPlatformString,
checkValueTypeFunc, convertToValueTypeFunc, stdMap)) {
return nullptr;
}
return ref new ::Platform::Collections::MapView<::Platform::String ^, V>(
stdMap);
}
template <class V>
static ::Platform::Collections::Vector<V> ^
JsArrayToWinrtVector(
v8::Local<v8::Array> arr,
const std::function<bool(v8::Local<v8::Value>)>& checkValueTypeFunc,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc) {
std::vector<V> vec(arr->Length());
if (!FillVector<std::vector<V>&, V>(arr, checkValueTypeFunc,
convertToValueTypeFunc, vec)) {
return nullptr;
}
return ref new ::Platform::Collections::Vector<V>(vec);
}
template <class V>
static ::Platform::Collections::VectorView<V> ^
JsArrayToWinrtVectorView(
v8::Local<v8::Array> arr,
const std::function<bool(v8::Local<v8::Value>)>& checkValueTypeFunc,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc) {
std::vector<V> vec(arr->Length());
if (!FillVector<std::vector<V>&, V>(arr, checkValueTypeFunc,
convertToValueTypeFunc, vec)) {
return nullptr;
}
return ref new ::Platform::Collections::VectorView<V>(vec);
}
template <class V>
static ::Platform::Array<V> ^
JsArrayToWinrtArray(
v8::Local<v8::Array> arr,
const std::function<bool(v8::Local<v8::Value>)>& checkValueTypeFunc,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc) {
auto vec = ref new ::Platform::Array<V>(arr->Length());
if (!FillVector<::Platform::Array<V> ^, V>(arr, checkValueTypeFunc,
convertToValueTypeFunc, vec)) {
return nullptr;
}
return vec;
}
} // namespace Collections
template <class V>
static void InsertToVector(
uint32_t index,
v8::Local<v8::Value> value,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc,
std::vector<V>& vec) {
vec[index] = convertToValueTypeFunc(value);
}
template <class V>
static void InsertToVector(
uint32_t index,
v8::Local<v8::Value> value,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc,
::Platform::Array<V> ^ vec) {
vec->set(index, convertToValueTypeFunc(value));
}
// assumption: vec length >= arr length
template <class T, class V>
static bool FillVector(
v8::Local<v8::Array> arr,
const std::function<bool(v8::Local<v8::Value>)>& checkValueTypeFunc,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc,
T vec) {
for (uint32_t i = 0; i < arr->Length(); i++) {
Local<Value> value = Nan::Get(arr, i).ToLocalChecked();
if (!checkValueTypeFunc(value)) {
Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(
L"Received array with unexpected value type")));
return false;
}
InsertToVector(i, value, convertToValueTypeFunc, vec);
}
return true;
}
template <class K, class V>
static bool FillMapFromJsArray(
v8::Local<v8::Array> arr,
const std::function<bool(v8::Local<v8::Value>)>& checkKeyTypeFunc,
const std::function<K(v8::Local<v8::Value>)>& convertToKeyTypeFunc,
const std::function<bool(v8::Local<v8::Value>)>& checkValueTypeFunc,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc,
std::map<K, V>& stdMap) {
initProps();
// expect that each element in the array will be an object with 2 properties:
// key and value (with types that match K and V respectively)
for (uint32_t i = 0; i < arr->Length(); i++) {
Local<Value> curr = Nan::Get(arr, i).ToLocalChecked();
if (!curr->IsObject()) {
Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(
L"Array elements are expected to be javascript objects")));
return false;
}
v8::Local<v8::Object> obj = curr.As<v8::Object>();
if (!obj->Has(g_keyProp) || !obj->Has(g_valueProp)) {
Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(
L"Array elements are expected to be javascript objects with \'key\' "
L"and \'value\' properties")));
return false;
}
Local<Value> key = Nan::Get(obj, g_keyProp).ToLocalChecked();
Local<Value> value = Nan::Get(obj, g_valueProp).ToLocalChecked();
if (!checkKeyTypeFunc(key)) {
Nan::ThrowError(Nan::Error(
NodeRT::Utils::NewString(L"Array element has invalid key type")));
return false;
}
if (!checkValueTypeFunc(value)) {
Nan::ThrowError(Nan::Error(
NodeRT::Utils::NewString(L"Array element has invalid value type")));
return false;
}
stdMap.insert(std::pair<K, V>(convertToKeyTypeFunc(key),
convertToValueTypeFunc(value)));
}
return true;
}
template <class V>
static bool FillMapFromJsObject(
v8::Local<v8::Object> obj,
const std::function<bool(v8::Local<v8::Value>)>& checkKeyTypeFunc,
const std::function<::Platform::String ^ (v8::Local<v8::Value>)>&
convertToKeyTypeFunc,
const std::function<bool(v8::Local<v8::Value>)>& checkValueTypeFunc,
const std::function<V(v8::Local<v8::Value>)>& convertToValueTypeFunc,
std::map<::Platform::String ^, V>& stdMap) {
Local<Array> objProps = Nan::GetPropertyNames(obj).ToLocalChecked();
for (uint32_t i = 0; i < objProps->Length(); i++) {
Local<Value> key = Nan::Get(objProps, i).ToLocalChecked();
Local<Value> value = Nan::Get(obj, key).ToLocalChecked();
if (!checkValueTypeFunc(value)) {
Nan::ThrowError(Nan::Error(NodeRT::Utils::NewString(
L"Received object with unexpected value type")));
return false;
}
stdMap.insert(std::pair<::Platform::String ^, V>(
convertToKeyTypeFunc(key), convertToValueTypeFunc(value)));
}
return true;
}
}; // namespace NodeRT