-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathnjsModule.c
409 lines (359 loc) · 16.5 KB
/
njsModule.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright (c) 2019, 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
// njsModule.c
//
// DESCRIPTION
// Top-level module implementation.
//
//-----------------------------------------------------------------------------
#include "njsModule.h"
//-----------------------------------------------------------------------------
// njsModule_extendClass()
// Extends a class defined in JavaScript.
//-----------------------------------------------------------------------------
static bool njsModule_extendClass(napi_env env, napi_value module,
njsModuleGlobals *globals, const njsClassDef *classDef,
napi_ref *clsRef)
{
size_t numProperties, numBaseProperties, i;
napi_property_descriptor *allProperties;
napi_value cls, prototype, tempInstance;
// get the class from the module
NJS_CHECK_NAPI(env, napi_get_named_property(env, module, classDef->name,
&cls))
// create a new instance of the class (temporarily) and get its prototype
NJS_CHECK_NAPI(env, napi_new_instance(env, cls, 0, NULL, &tempInstance))
NJS_CHECK_NAPI(env, napi_get_prototype(env, tempInstance, &prototype))
// scan each of the class properties and constants to get the total number
// of properties to define
numProperties = 0;
if (!classDef->propertiesOnInstance) {
for (i = 0; classDef->properties[i].utf8name; i++, numProperties++);
}
numBaseProperties = numProperties;
// perform define if any properties are present
if (numProperties > 0) {
// allocate memory for all of the properties
allProperties = calloc(numProperties,
sizeof(napi_property_descriptor));
if (!allProperties)
return njsUtils_throwInsufficientMemory(env);
// populate the properties
memcpy(allProperties, classDef->properties,
sizeof(napi_property_descriptor) * numBaseProperties);
// store the instance on each of the properties as a convenience
for (i = 0; i < numProperties; i++)
allProperties[i].data = globals;
// define the properties on the prototype
if (napi_define_properties(env, prototype, numProperties,
allProperties) != napi_ok) {
free(allProperties);
return njsUtils_genericThrowError(env, __FILE__, __LINE__);
}
free(allProperties);
}
// store a reference to the constructor for later use
NJS_CHECK_NAPI(env, napi_create_reference(env, cls, 1, clsRef))
return true;
}
//-----------------------------------------------------------------------------
// njsModule_finalizeGlobals()
// Called when the module object goes out of scope and cleans up module
// globals.
// -----------------------------------------------------------------------------
static void njsModule_finalizeGlobals(napi_env env, void *finalize_data,
void *finalize_hint)
{
njsModuleGlobals *globals = (njsModuleGlobals*) finalize_data;
if (globals->context) {
dpiContext_destroy(globals->context);
globals->context = NULL;
}
NJS_DELETE_REF_AND_CLEAR(globals->jsAqDeqOptionsConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsAqEnqOptionsConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsAqMessageConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsAqQueueConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsDbObjectConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsConnectionConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsLobConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsPoolConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsResultSetConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsSodaCollectionConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsSodaDatabaseConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsSodaDocCursorConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsSodaDocumentConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsSodaOperationConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsGetDateComponentsFn);
NJS_DELETE_REF_AND_CLEAR(globals->jsMakeDateFn);
NJS_DELETE_REF_AND_CLEAR(globals->jsDecodeVectorFn);
NJS_DELETE_REF_AND_CLEAR(globals->jsEncodeVectorFn);
NJS_DELETE_REF_AND_CLEAR(globals->jsJsonIdConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsSparseVectorConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsIntervalYMConstructor);
NJS_DELETE_REF_AND_CLEAR(globals->jsIntervalDSConstructor);
free(globals);
}
//-----------------------------------------------------------------------------
// njsModule_populateGlobals()
// Populates the module globals used throughout the module. This includes
// extending the classes defined in JavaScript and storing references to the
// constructors for later use. It also includes keeping a reference to the
// global settings found in JavaScript and an object that stores active
// subscriptions.
//-----------------------------------------------------------------------------
static bool njsModule_populateGlobals(napi_env env, napi_value module,
napi_value settings, njsModuleGlobals *globals)
{
char versionString[40];
napi_value temp;
// extend classes
if (!njsModule_extendClass(env, module, globals, &njsClassDefAqDeqOptions,
&globals->jsAqDeqOptionsConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefAqEnqOptions,
&globals->jsAqEnqOptionsConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefAqMessage,
&globals->jsAqMessageConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefAqQueue,
&globals->jsAqQueueConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefDbObject,
&globals->jsDbObjectConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefConnection,
&globals->jsConnectionConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefLob,
&globals->jsLobConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefPool,
&globals->jsPoolConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefResultSet,
&globals->jsResultSetConstructor))
return false;
if (!njsModule_extendClass(env, module, globals,
&njsClassDefSodaCollection, &globals->jsSodaCollectionConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefSodaDatabase,
&globals->jsSodaDatabaseConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefSodaDocCursor,
&globals->jsSodaDocCursorConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefSodaDocument,
&globals->jsSodaDocumentConstructor))
return false;
if (!njsModule_extendClass(env, module, globals, &njsClassDefSodaOperation,
&globals->jsSodaOperationConstructor))
return false;
// get the JsonId class
NJS_CHECK_NAPI(env, napi_get_named_property(env, settings, "_JsonId",
&temp))
NJS_CHECK_NAPI(env, napi_create_reference(env, temp, 1,
&globals->jsJsonIdConstructor))
// get the SparseVector class
NJS_CHECK_NAPI(env, napi_get_named_property(env, settings, "_SparseVector",
&temp))
NJS_CHECK_NAPI(env, napi_create_reference(env, temp, 1,
&globals->jsSparseVectorConstructor))
// get the IntervalYM (year to month) class
NJS_CHECK_NAPI(env, napi_get_named_property(env, settings, "_IntervalYM",
&temp))
NJS_CHECK_NAPI(env, napi_create_reference(env, temp, 1,
&globals->jsIntervalYMConstructor))
// get the IntervalDS (day to second) class
NJS_CHECK_NAPI(env, napi_get_named_property(env, settings, "_IntervalDS",
&temp))
NJS_CHECK_NAPI(env, napi_create_reference(env, temp, 1,
&globals->jsIntervalDSConstructor))
// store a reference to the _makeDate() function
NJS_CHECK_NAPI(env, napi_get_named_property(env, settings,
"_getDateComponents", &temp))
NJS_CHECK_NAPI(env, napi_create_reference(env, temp, 1,
&globals->jsGetDateComponentsFn))
// store a reference to the _makeDate() function
NJS_CHECK_NAPI(env, napi_get_named_property(env, settings, "_makeDate",
&temp))
NJS_CHECK_NAPI(env, napi_create_reference(env, temp, 1,
&globals->jsMakeDateFn))
// store a reference to the _decodeVector() function
NJS_CHECK_NAPI(env, napi_get_named_property(env, settings, "_decodeVector",
&temp))
NJS_CHECK_NAPI(env, napi_create_reference(env, temp, 1,
&globals->jsDecodeVectorFn))
// store a reference to the _encodeVector() function
NJS_CHECK_NAPI(env, napi_get_named_property(env, settings, "_encodeVector",
&temp))
NJS_CHECK_NAPI(env, napi_create_reference(env, temp, 1,
&globals->jsEncodeVectorFn))
// acquire Oracle client version and store this in the settings object
if (dpiContext_getClientVersion(globals->context, &globals->clientVersionInfo) < 0)
return njsUtils_throwErrorDPI(env, globals);
NJS_CHECK_NAPI(env, napi_create_uint32(env, globals->clientVersionInfo.fullVersionNum,
&temp))
NJS_CHECK_NAPI(env, napi_set_named_property(env, settings,
"oracleClientVersion", temp))
(void) snprintf(versionString, sizeof(versionString), "%d.%d.%d.%d.%d",
globals->clientVersionInfo.versionNum,
globals->clientVersionInfo.releaseNum,
globals->clientVersionInfo.updateNum,
globals->clientVersionInfo.portReleaseNum,
globals->clientVersionInfo.portUpdateNum);
NJS_CHECK_NAPI(env, napi_create_string_utf8(env, versionString,
strlen(versionString), &temp))
NJS_CHECK_NAPI(env, napi_set_named_property(env, settings,
"oracleClientVersionString", temp))
return true;
}
//-----------------------------------------------------------------------------
// njsModule_initDPI()
// Initialize the ODPI-C library. This is done when the first standalone
// connection or session pool is created, or when a call to initOracleClient()
// is made, rather than when the module is first imported so that manipulating
// Oracle environment variables will work as expected. It also has the
// additional benefit of reducing the number of errors that can take place when
// the module is imported.
// -----------------------------------------------------------------------------
static bool njsModule_initDPI(napi_env env, napi_value *args,
njsModuleGlobals *globals, char **libDir, size_t *libDirLength,
char **configDir, size_t *configDirLength, char **errorUrl,
size_t *errorUrlLength, char **driverName, size_t *driverNameLength)
{
dpiContextCreateParams params;
napi_value error, message;
dpiErrorInfo errorInfo;
// get any arguments from JavaScript
if (!njsUtils_getNamedPropertyString(env, args[0], "libDir", libDir,
libDirLength))
return false;
if (!njsUtils_getNamedPropertyString(env, args[0], "configDir", configDir,
configDirLength))
return false;
if (!njsUtils_getNamedPropertyString(env, args[0], "errorUrl", errorUrl,
errorUrlLength))
return false;
if (!njsUtils_getNamedPropertyString(env, args[0], "driverName",
driverName, driverNameLength))
return false;
// initialize structure
memset(¶ms, 0, sizeof(params));
params.useJsonId = 1; // Enable JSONID
if (*libDirLength > 0)
params.oracleClientLibDir = *libDir;
if (*configDirLength > 0)
params.oracleClientConfigDir = *configDir;
if (*errorUrlLength > 0)
params.loadErrorUrl = *errorUrl;
if (*driverNameLength > 0)
params.defaultDriverName = *driverName;
// perform ODPI-C initialization
if (dpiContext_createWithParams(DPI_MAJOR_VERSION, DPI_MINOR_VERSION,
¶ms, &globals->context, &errorInfo) < 0) {
NJS_CHECK_NAPI(env, napi_create_string_utf8(env, errorInfo.message,
errorInfo.messageLength, &message))
NJS_CHECK_NAPI(env, napi_create_error(env, NULL, message, &error))
NJS_CHECK_NAPI(env, napi_throw(env, error))
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// njsModule_initOracleClient()
// Initialize the Oracle Client library.
//
// PARAMETERS
// - options
//-----------------------------------------------------------------------------
static napi_value njsModule_initOracleClient(napi_env env,
napi_callback_info info)
{
size_t libDirLength, configDirLength, errorUrlLength, driverNameLength;
char *libDir, *configDir, *errorUrl, *driverName;
njsModuleGlobals *globals;
napi_value args[3];
bool ok;
// iniitialize ODPI-C
libDir = configDir = errorUrl = driverName = NULL;
libDirLength = configDirLength = errorUrlLength = driverNameLength = 0;
if (!njsUtils_validateArgs(env, info, 3, args, &globals, NULL, NULL, NULL))
return NULL;
ok = njsModule_initDPI(env, args, globals, &libDir, &libDirLength,
&configDir, &configDirLength, &errorUrl, &errorUrlLength,
&driverName, &driverNameLength);
if (libDir)
free(libDir);
if (configDir)
free(configDir);
if (errorUrl)
free(errorUrl);
if (driverName)
free(driverName);
if (!ok)
return NULL;
// extend classes
njsModule_populateGlobals(env, args[1], args[2], globals);
return NULL;
}
//-----------------------------------------------------------------------------
// njsModule_initHelper()
// Initializer helper for the module. This defines the items exported by the
// module. A helper function is used to be able to take advantage of the
// NJS_CHECK_NAPI function and simplify code.
// -----------------------------------------------------------------------------
static bool njsModule_initHelper(napi_env env, napi_value exports)
{
njsModuleGlobals *globals;
napi_value fn, jsGlobals;
// create module globals and store an "external" object in JavaScript to
// prevent it from being collected; this value is also stored on every
// function definition so that it can be directly referenced
globals = calloc(1, sizeof(njsModuleGlobals));
if (!globals)
return njsUtils_throwInsufficientMemory(env);
NJS_CHECK_NAPI(env, napi_create_external(env, globals,
njsModule_finalizeGlobals, NULL, &jsGlobals))
NJS_CHECK_NAPI(env, napi_set_named_property(env, exports, "_globals",
jsGlobals))
// define function for initializing the Oracle client
NJS_CHECK_NAPI(env, napi_create_function(env, "initOracleClient",
NAPI_AUTO_LENGTH, njsModule_initOracleClient, globals, &fn))
NJS_CHECK_NAPI(env, napi_set_named_property(env, exports,
"initOracleClient", fn))
return true;
}
//-----------------------------------------------------------------------------
// njsModule_init()
// Initializer for the module. This defines the items exported by the module.
//-----------------------------------------------------------------------------
static napi_value njsModule_init(napi_env env, napi_value exports)
{
if (!njsModule_initHelper(env, exports))
return NULL;
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, njsModule_init)