-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathnjsJsonBuffer.c
369 lines (339 loc) · 14.2 KB
/
njsJsonBuffer.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) 2020, 2025, Oracle and/or its affiliates.
//-----------------------------------------------------------------------------
//
// This software is dual-licensed to you under the Universal Permissive License
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
// either license.
//
// If you elect to accept the software under the Apache License, Version 2.0,
// the following applies:
//
// 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
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// NAME
// njsJsonBuffer.c
//
// DESCRIPTION
// Implementation of methods for managing buffers for binding JSON data.
//
//-----------------------------------------------------------------------------
#include "njsModule.h"
// forward declarations for functions only used in this file
static void njsJsonBuffer_freeNode(dpiJsonNode *node);
static bool njsJsonBuffer_getString(njsJsonBuffer *buf, njsBaton *baton,
napi_env env, napi_value inValue, char **outValue,
uint32_t *outValueLength);
static bool njsJsonBuffer_populateNode(njsJsonBuffer *buf, dpiJsonNode *node,
napi_env env, napi_value value, njsBaton *baton);
//-----------------------------------------------------------------------------
// njsJsonBuffer_freeNode()
//-----------------------------------------------------------------------------
static void njsJsonBuffer_freeNode(dpiJsonNode *node)
{
dpiJsonArray *array;
dpiJsonObject *obj;
uint32_t i;
switch (node->nativeTypeNum) {
case DPI_NATIVE_TYPE_JSON_ARRAY:
array = &node->value->asJsonArray;
if (array->elements) {
for (i = 0; i < array->numElements; i++) {
if (array->elements[i].value)
njsJsonBuffer_freeNode(&array->elements[i]);
}
free(array->elements);
array->elements = NULL;
}
if (array->elementValues) {
free(array->elementValues);
array->elementValues = NULL;
}
break;
case DPI_NATIVE_TYPE_JSON_OBJECT:
obj = &node->value->asJsonObject;
if (obj->fields) {
for (i = 0; i < obj->numFields; i++) {
if (obj->fields[i].value)
njsJsonBuffer_freeNode(&obj->fields[i]);
}
free(obj->fields);
obj->fields = NULL;
}
if (obj->fieldNames) {
free(obj->fieldNames);
obj->fieldNames = NULL;
}
if (obj->fieldNameLengths) {
free(obj->fieldNameLengths);
obj->fieldNameLengths = NULL;
}
if (obj->fieldValues) {
free(obj->fieldValues);
obj->fieldValues = NULL;
}
break;
}
}
//-----------------------------------------------------------------------------
// njsJsonBuffer_getString()
// Acquire a new buffer from the array of buffers and then allocate the
// requested space to store the string value. If an element in the array is not
// available, more space for the array is allocated in chunks.
//-----------------------------------------------------------------------------
static bool njsJsonBuffer_getString(njsJsonBuffer *buf, njsBaton *baton,
napi_env env, napi_value inValue, char **outValue,
uint32_t *outValueLength)
{
char **tempBuffers, *temp;
size_t tempLength;
*outValue = NULL;
*outValueLength = 0;
if (buf->numBuffers == buf->allocatedBuffers) {
buf->allocatedBuffers += 16;
tempBuffers = malloc(buf->allocatedBuffers * sizeof(char*));
if (!tempBuffers)
return njsBaton_setErrorInsufficientMemory(baton);
if (buf->numBuffers > 0) {
memcpy(tempBuffers, buf->buffers, buf->numBuffers * sizeof(char*));
free(buf->buffers);
}
buf->buffers = tempBuffers;
}
NJS_CHECK_NAPI(env, napi_get_value_string_utf8(env, inValue, NULL, 0,
&tempLength))
temp = malloc(tempLength + 1);
if (!temp)
return njsBaton_setErrorInsufficientMemory(baton);
buf->buffers[buf->numBuffers++] = temp;
NJS_CHECK_NAPI(env, napi_get_value_string_utf8(env, inValue, temp,
tempLength + 1, &tempLength))
*outValue = temp;
*outValueLength = (uint32_t) tempLength;
return true;
}
//-----------------------------------------------------------------------------
// njsJsonBuffer_populateNode()
// Populates a particular node with the contents of the JavaScript value.
//-----------------------------------------------------------------------------
static bool njsJsonBuffer_populateNode(njsJsonBuffer *buf, dpiJsonNode *node,
napi_env env, napi_value value, njsBaton *baton)
{
napi_value temp, name, fieldNames, fieldValues, vectorVal, global;
napi_value uint8Val, uint8Proto, valProto;
napi_valuetype valueType;
napi_typedarray_type type;
size_t tempBufferLength;
dpiJsonArray *array;
dpiJsonObject *obj;
char *tempBuffer;
uint32_t i;
bool check;
bool isTyped = false;
bool isSparse = false;
// determine type of value
NJS_CHECK_NAPI(env, napi_typeof(env, value, &valueType))
// nulls and undefined in JS are mapped to NULL in Oracle
if (valueType == napi_undefined || valueType == napi_null) {
node->oracleTypeNum = DPI_ORACLE_TYPE_NONE;
node->nativeTypeNum = DPI_NATIVE_TYPE_NULL;
return true;
}
// handle booleans
if (valueType == napi_boolean) {
node->oracleTypeNum = DPI_ORACLE_TYPE_BOOLEAN;
node->nativeTypeNum = DPI_NATIVE_TYPE_BOOLEAN;
NJS_CHECK_NAPI(env, napi_get_value_bool(env, value, &check))
node->value->asBoolean = (int) check;
return true;
}
// handle strings
if (valueType == napi_string) {
node->oracleTypeNum = DPI_ORACLE_TYPE_VARCHAR;
node->nativeTypeNum = DPI_NATIVE_TYPE_BYTES;
return njsJsonBuffer_getString(buf, baton, env, value,
&node->value->asBytes.ptr, &node->value->asBytes.length);
}
// handle numbers
if (valueType == napi_number) {
node->oracleTypeNum = DPI_ORACLE_TYPE_NUMBER;
node->nativeTypeNum = DPI_NATIVE_TYPE_DOUBLE;
NJS_CHECK_NAPI(env, napi_get_value_double(env, value,
&node->value->asDouble))
return true;
}
// handle arrays
NJS_CHECK_NAPI(env, napi_is_array(env, value, &check))
if (check) {
node->oracleTypeNum = DPI_ORACLE_TYPE_JSON_ARRAY;
node->nativeTypeNum = DPI_NATIVE_TYPE_JSON_ARRAY;
array = &node->value->asJsonArray;
NJS_CHECK_NAPI(env, napi_get_array_length(env, value,
&array->numElements))
array->elements = calloc(array->numElements, sizeof(dpiJsonNode));
array->elementValues = calloc(array->numElements,
sizeof(dpiDataBuffer));
if (!array->elements || !array->elementValues)
return njsBaton_setErrorInsufficientMemory(baton);
for (i = 0; i < array->numElements; i++) {
NJS_CHECK_NAPI(env, napi_get_element(env, value, i, &temp))
array->elements[i].value = &array->elementValues[i];
if (!njsJsonBuffer_populateNode(buf, &array->elements[i], env,
temp, baton))
return false;
}
return true;
}
// handle dates
NJS_CHECK_NAPI(env, napi_is_date(env, value, &check))
if (check) {
node->oracleTypeNum = DPI_ORACLE_TYPE_TIMESTAMP;
node->nativeTypeNum = DPI_NATIVE_TYPE_TIMESTAMP;
return njsUtils_setDateValue(DPI_ORACLE_TYPE_TIMESTAMP, env, value,
baton->jsGetDateComponentsFn, &node->value->asTimestamp);
}
// handle vectors
NJS_CHECK_NAPI(env, napi_is_typedarray(env, value, &isTyped))
NJS_CHECK_NAPI(env, napi_instanceof(env, value,
baton->jsSparseVectorConstructor, &isSparse))
if (isTyped || isSparse) { // typed array or sparse vector
NJS_CHECK_NAPI(env, napi_get_global(env, &global))
if (!isSparse) {
NJS_CHECK_NAPI(env, napi_get_typedarray_info(env, value, &type,
NULL, NULL, NULL, NULL))
if ((type == napi_float64_array) || (type == napi_float32_array)
|| (type == napi_int8_array)) {
check = true;
} else {
NJS_CHECK_NAPI(env, napi_get_named_property(env, global,
"Uint8Array", &uint8Val))
NJS_CHECK_NAPI(env, napi_get_named_property(env, uint8Val,
"prototype", &uint8Proto))
NJS_CHECK_NAPI(env, napi_get_prototype(env, value, &valProto))
// See if the value prototype matches with Uint8Array.
// The Buffer and JsonId return true for Uint8Array instance, so
// using prototype check to see if the value is
// instance of Uint8Array.
NJS_CHECK_NAPI(env, napi_strict_equals(env, uint8Proto,
valProto, &check))
}
}
if (check || isSparse) {
// value is a vector but not buffer.
node->oracleTypeNum = DPI_ORACLE_TYPE_VECTOR;
node->nativeTypeNum = DPI_NATIVE_TYPE_BYTES;
NJS_CHECK_NAPI(env, napi_call_function(env, global,
baton->jsEncodeVectorFn, 1, &value, &vectorVal))
NJS_CHECK_NAPI(env, napi_get_buffer_info(env, vectorVal,
(void**) &tempBuffer, &tempBufferLength))
node->value->asBytes.ptr = tempBuffer;
node->value->asBytes.length = (uint32_t) tempBufferLength;
return true;
}
}
// handle buffers
NJS_CHECK_NAPI(env, napi_is_buffer(env, value, &check))
if (check) {
NJS_CHECK_NAPI(env, napi_instanceof(env, value,
baton->jsJsonIdConstructor, &check))
NJS_CHECK_NAPI(env, napi_get_buffer_info(env, value,
(void**) &tempBuffer, &tempBufferLength))
if (check) {
// Handle JsonId
node->oracleTypeNum = DPI_ORACLE_TYPE_JSON_ID;
} else {
node->oracleTypeNum = DPI_ORACLE_TYPE_RAW;
}
node->nativeTypeNum = DPI_NATIVE_TYPE_BYTES;
node->value->asBytes.ptr = tempBuffer;
node->value->asBytes.length = (uint32_t) tempBufferLength;
return true;
}
// handle interval datatypes
NJS_CHECK_NAPI(env, napi_instanceof(env, value,
baton->jsIntervalYMConstructor, &check))
if (check) {
node->oracleTypeNum = DPI_ORACLE_TYPE_INTERVAL_YM;
node->nativeTypeNum = DPI_NATIVE_TYPE_INTERVAL_YM;
return njsUtils_setIntervalYM(env, value,
&(node->value->asIntervalYM));
}
NJS_CHECK_NAPI(env, napi_instanceof(env, value,
baton->jsIntervalDSConstructor, &check))
if (check) {
node->oracleTypeNum = DPI_ORACLE_TYPE_INTERVAL_DS;
node->nativeTypeNum = DPI_NATIVE_TYPE_INTERVAL_DS;
return njsUtils_setIntervalDS(env, value,
&(node->value->asIntervalDS));
}
// all other objects have been transformed to an object containing the
// key "fields" and the key "values"
node->oracleTypeNum = DPI_ORACLE_TYPE_JSON_OBJECT;
node->nativeTypeNum = DPI_NATIVE_TYPE_JSON_OBJECT;
obj = &node->value->asJsonObject;
NJS_CHECK_NAPI(env, napi_get_named_property(env, value, "fields",
&fieldNames))
NJS_CHECK_NAPI(env, napi_get_array_length(env, fieldNames,
&obj->numFields))
NJS_CHECK_NAPI(env, napi_get_named_property(env, value, "values",
&fieldValues))
obj->fieldNames = calloc(obj->numFields, sizeof(char*));
obj->fieldNameLengths = calloc(obj->numFields, sizeof(uint32_t));
obj->fields = calloc(obj->numFields, sizeof(dpiJsonNode));
obj->fieldValues = calloc(obj->numFields, sizeof(dpiDataBuffer));
if (!obj->fieldNames || !obj->fieldNameLengths || !obj->fields ||
!obj->fieldValues)
return njsBaton_setErrorInsufficientMemory(baton);
for (i = 0; i < obj->numFields; i++) {
NJS_CHECK_NAPI(env, napi_get_element(env, fieldNames, i, &name))
if (!njsJsonBuffer_getString(buf, baton, env, name,
&obj->fieldNames[i], &obj->fieldNameLengths[i]))
return false;
NJS_CHECK_NAPI(env, napi_get_element(env, fieldValues, i, &temp))
obj->fields[i].value = &obj->fieldValues[i];
if (!njsJsonBuffer_populateNode(buf, &obj->fields[i], env, temp,
baton))
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// njsJsonBuffer_free()
// Frees any memory allocated for the JSON buffer.
//-----------------------------------------------------------------------------
void njsJsonBuffer_free(njsJsonBuffer *buf)
{
uint32_t i;
if (buf->buffers) {
for (i = 0; i < buf->numBuffers; i++)
free(buf->buffers[i]);
free(buf->buffers);
buf->buffers = NULL;
}
njsJsonBuffer_freeNode(&buf->topNode);
}
//-----------------------------------------------------------------------------
// njsJsonBuffer_fromValue()
// Populates a JSON buffer from the specified JavaScript value.
//-----------------------------------------------------------------------------
bool njsJsonBuffer_fromValue(njsJsonBuffer *buf, napi_env env,
napi_value value, njsBaton *baton)
{
// initialize JSON buffer structure
buf->topNode.value = &buf->topNodeBuffer;
buf->allocatedBuffers = 0;
buf->numBuffers = 0;
buf->buffers = NULL;
// populate the top level node
return njsJsonBuffer_populateNode(buf, &buf->topNode, env, value, baton);
}