java-bridge 2.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1107 @@
1
+ (function webpackUniversalModuleDefinition(root, factory) {
2
+ if(typeof exports === 'object' && typeof module === 'object')
3
+ module.exports = factory();
4
+ else if(typeof define === 'function' && define.amd)
5
+ define([], factory);
6
+ else if(typeof exports === 'object')
7
+ exports["java"] = factory();
8
+ else
9
+ root["java"] = factory();
10
+ })(global, () => {
11
+ return /******/ (() => { // webpackBootstrap
12
+ /******/ var __webpack_modules__ = ({
13
+
14
+ /***/ "./java.linux-x64-gnu.node":
15
+ /*!*********************************!*\
16
+ !*** ./java.linux-x64-gnu.node ***!
17
+ \*********************************/
18
+ /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
19
+
20
+ /* module decorator */ module = __webpack_require__.nmd(module);
21
+
22
+ try {
23
+ process.dlopen(module, __dirname + (__webpack_require__(/*! path */ "path").sep) + __webpack_require__.p + "java.linux-x64-gnu.node");
24
+ } catch (error) {
25
+ throw new Error('node-loader:\n' + error);
26
+ }
27
+
28
+
29
+ /***/ }),
30
+
31
+ /***/ "./native.js":
32
+ /*!*******************!*\
33
+ !*** ./native.js ***!
34
+ \*******************/
35
+ /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
36
+
37
+ const { existsSync, readFileSync } = __webpack_require__(/*! fs */ "fs")
38
+ const { join } = __webpack_require__(/*! path */ "path")
39
+
40
+ const { platform, arch } = process
41
+
42
+ let nativeBinding = null
43
+ let localFileExisted = false
44
+ let loadError = null
45
+
46
+ function isMusl() {
47
+ // For Node 10
48
+ if (!process.report || typeof process.report.getReport !== 'function') {
49
+ try {
50
+ return readFileSync('/usr/bin/ldd', 'utf8').includes('musl')
51
+ } catch (e) {
52
+ return true
53
+ }
54
+ } else {
55
+ const { glibcVersionRuntime } = process.report.getReport().header
56
+ return !glibcVersionRuntime
57
+ }
58
+ }
59
+
60
+ switch (platform) {
61
+ case 'android':
62
+ switch (arch) {
63
+ case 'arm64':
64
+ localFileExisted = existsSync(join(__dirname, 'java.android-arm64.node'))
65
+ try {
66
+ if (localFileExisted) {
67
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.android-arm64.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
68
+ } else {
69
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-android-arm64'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
70
+ }
71
+ } catch (e) {
72
+ loadError = e
73
+ }
74
+ break
75
+ case 'arm':
76
+ localFileExisted = existsSync(join(__dirname, 'java.android-arm-eabi.node'))
77
+ try {
78
+ if (localFileExisted) {
79
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.android-arm-eabi.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
80
+ } else {
81
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-android-arm-eabi'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
82
+ }
83
+ } catch (e) {
84
+ loadError = e
85
+ }
86
+ break
87
+ default:
88
+ throw new Error(`Unsupported architecture on Android ${arch}`)
89
+ }
90
+ break
91
+ case 'win32':
92
+ switch (arch) {
93
+ case 'x64':
94
+ localFileExisted = existsSync(
95
+ join(__dirname, 'java.win32-x64-msvc.node')
96
+ )
97
+ try {
98
+ if (localFileExisted) {
99
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.win32-x64-msvc.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
100
+ } else {
101
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-win32-x64-msvc'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
102
+ }
103
+ } catch (e) {
104
+ loadError = e
105
+ }
106
+ break
107
+ case 'ia32':
108
+ localFileExisted = existsSync(
109
+ join(__dirname, 'java.win32-ia32-msvc.node')
110
+ )
111
+ try {
112
+ if (localFileExisted) {
113
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.win32-ia32-msvc.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
114
+ } else {
115
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-win32-ia32-msvc'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
116
+ }
117
+ } catch (e) {
118
+ loadError = e
119
+ }
120
+ break
121
+ case 'arm64':
122
+ localFileExisted = existsSync(
123
+ join(__dirname, 'java.win32-arm64-msvc.node')
124
+ )
125
+ try {
126
+ if (localFileExisted) {
127
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.win32-arm64-msvc.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
128
+ } else {
129
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-win32-arm64-msvc'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
130
+ }
131
+ } catch (e) {
132
+ loadError = e
133
+ }
134
+ break
135
+ default:
136
+ throw new Error(`Unsupported architecture on Windows: ${arch}`)
137
+ }
138
+ break
139
+ case 'darwin':
140
+ switch (arch) {
141
+ case 'x64':
142
+ localFileExisted = existsSync(join(__dirname, 'java.darwin-x64.node'))
143
+ try {
144
+ if (localFileExisted) {
145
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.darwin-x64.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
146
+ } else {
147
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-darwin-x64'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
148
+ }
149
+ } catch (e) {
150
+ loadError = e
151
+ }
152
+ break
153
+ case 'arm64':
154
+ localFileExisted = existsSync(
155
+ join(__dirname, 'java.darwin-arm64.node')
156
+ )
157
+ try {
158
+ if (localFileExisted) {
159
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.darwin-arm64.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
160
+ } else {
161
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-darwin-arm64'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
162
+ }
163
+ } catch (e) {
164
+ loadError = e
165
+ }
166
+ break
167
+ default:
168
+ throw new Error(`Unsupported architecture on macOS: ${arch}`)
169
+ }
170
+ break
171
+ case 'freebsd':
172
+ if (arch !== 'x64') {
173
+ throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
174
+ }
175
+ localFileExisted = existsSync(join(__dirname, 'java.freebsd-x64.node'))
176
+ try {
177
+ if (localFileExisted) {
178
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.freebsd-x64.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
179
+ } else {
180
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-freebsd-x64'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
181
+ }
182
+ } catch (e) {
183
+ loadError = e
184
+ }
185
+ break
186
+ case 'linux':
187
+ switch (arch) {
188
+ case 'x64':
189
+ if (isMusl()) {
190
+ localFileExisted = existsSync(
191
+ join(__dirname, 'java.linux-x64-musl.node')
192
+ )
193
+ try {
194
+ if (localFileExisted) {
195
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.linux-x64-musl.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
196
+ } else {
197
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-linux-x64-musl'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
198
+ }
199
+ } catch (e) {
200
+ loadError = e
201
+ }
202
+ } else {
203
+ localFileExisted = existsSync(
204
+ join(__dirname, 'java.linux-x64-gnu.node')
205
+ )
206
+ try {
207
+ if (localFileExisted) {
208
+ nativeBinding = __webpack_require__(/*! ./java.linux-x64-gnu.node */ "./java.linux-x64-gnu.node")
209
+ } else {
210
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-linux-x64-gnu'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
211
+ }
212
+ } catch (e) {
213
+ loadError = e
214
+ }
215
+ }
216
+ break
217
+ case 'arm64':
218
+ if (isMusl()) {
219
+ localFileExisted = existsSync(
220
+ join(__dirname, 'java.linux-arm64-musl.node')
221
+ )
222
+ try {
223
+ if (localFileExisted) {
224
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.linux-arm64-musl.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
225
+ } else {
226
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-linux-arm64-musl'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
227
+ }
228
+ } catch (e) {
229
+ loadError = e
230
+ }
231
+ } else {
232
+ localFileExisted = existsSync(
233
+ join(__dirname, 'java.linux-arm64-gnu.node')
234
+ )
235
+ try {
236
+ if (localFileExisted) {
237
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.linux-arm64-gnu.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
238
+ } else {
239
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-linux-arm64-gnu'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
240
+ }
241
+ } catch (e) {
242
+ loadError = e
243
+ }
244
+ }
245
+ break
246
+ case 'arm':
247
+ localFileExisted = existsSync(
248
+ join(__dirname, 'java.linux-arm-gnueabihf.node')
249
+ )
250
+ try {
251
+ if (localFileExisted) {
252
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module './java.linux-arm-gnueabihf.node'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
253
+ } else {
254
+ nativeBinding = __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'java-bridge-linux-arm-gnueabihf'"); e.code = 'MODULE_NOT_FOUND'; throw e; }()))
255
+ }
256
+ } catch (e) {
257
+ loadError = e
258
+ }
259
+ break
260
+ default:
261
+ throw new Error(`Unsupported architecture on Linux: ${arch}`)
262
+ }
263
+ break
264
+ default:
265
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
266
+ }
267
+
268
+ if (!nativeBinding) {
269
+ if (loadError) {
270
+ throw loadError
271
+ }
272
+ throw new Error(`Failed to load native binding`)
273
+ }
274
+
275
+ const { Java, getClassFields, getField, setField, getStaticField, setStaticField, JavaInterfaceProxy, StdoutRedirect, getJavaLibPath } = nativeBinding
276
+
277
+ module.exports.Java = Java
278
+ module.exports.getClassFields = getClassFields
279
+ module.exports.getField = getField
280
+ module.exports.setField = setField
281
+ module.exports.getStaticField = getStaticField
282
+ module.exports.setStaticField = setStaticField
283
+ module.exports.JavaInterfaceProxy = JavaInterfaceProxy
284
+ module.exports.StdoutRedirect = StdoutRedirect
285
+ module.exports.getJavaLibPath = getJavaLibPath
286
+
287
+
288
+ /***/ }),
289
+
290
+ /***/ "./ts-src/definitions.ts":
291
+ /*!*******************************!*\
292
+ !*** ./ts-src/definitions.ts ***!
293
+ \*******************************/
294
+ /***/ ((__unused_webpack_module, exports) => {
295
+
296
+ "use strict";
297
+
298
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
299
+ exports.JavaObject = exports.JavaVersion = void 0;
300
+ /**
301
+ * The supported java versions.
302
+ * Your list of supported versions
303
+ * may differ if you use a different
304
+ * version of the jvm shared library.
305
+ */
306
+ var JavaVersion;
307
+ (function (JavaVersion) {
308
+ /** Java version 1.1 */
309
+ JavaVersion["VER_1_1"] = "1.1";
310
+ /** Java version 1.2 */
311
+ JavaVersion["VER_1_2"] = "1.2";
312
+ /** Java version 1.4 */
313
+ JavaVersion["VER_1_4"] = "1.4";
314
+ /** Java version 1.6 */
315
+ JavaVersion["VER_1_6"] = "1.6";
316
+ /** Java version 1.8 */
317
+ JavaVersion["VER_1_8"] = "1.8";
318
+ /** Java version 9 */
319
+ JavaVersion["VER_9"] = "9";
320
+ /** Java version 10 */
321
+ JavaVersion["VER_10"] = "10";
322
+ })(JavaVersion = exports.JavaVersion || (exports.JavaVersion = {}));
323
+ Object.freeze(JavaVersion);
324
+ /**
325
+ * A dummy java object class
326
+ */
327
+ class JavaObject {
328
+ }
329
+ exports.JavaObject = JavaObject;
330
+
331
+
332
+ /***/ }),
333
+
334
+ /***/ "./ts-src/index.ts":
335
+ /*!*************************!*\
336
+ !*** ./ts-src/index.ts ***!
337
+ \*************************/
338
+ /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
339
+
340
+ "use strict";
341
+
342
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
343
+ if (k2 === undefined) k2 = k;
344
+ var desc = Object.getOwnPropertyDescriptor(m, k);
345
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
346
+ desc = { enumerable: true, get: function() { return m[k]; } };
347
+ }
348
+ Object.defineProperty(o, k2, desc);
349
+ }) : (function(o, m, k, k2) {
350
+ if (k2 === undefined) k2 = k;
351
+ o[k2] = m[k];
352
+ }));
353
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
354
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
355
+ }) : function(o, v) {
356
+ o["default"] = v;
357
+ });
358
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
359
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
360
+ };
361
+ var __importStar = (this && this.__importStar) || function (mod) {
362
+ if (mod && mod.__esModule) return mod;
363
+ var result = {};
364
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
365
+ __setModuleDefault(result, mod);
366
+ return result;
367
+ };
368
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
369
+ exports.getJavaLibPath = exports.JavaClassProxy = exports.JavaClassInstance = exports.JavaObject = exports.JavaVersion = void 0;
370
+ var definitions_1 = __webpack_require__(/*! ./definitions */ "./ts-src/definitions.ts");
371
+ Object.defineProperty(exports, "JavaVersion", ({ enumerable: true, get: function () { return definitions_1.JavaVersion; } }));
372
+ Object.defineProperty(exports, "JavaObject", ({ enumerable: true, get: function () { return definitions_1.JavaObject; } }));
373
+ Object.defineProperty(exports, "JavaClassInstance", ({ enumerable: true, get: function () { return definitions_1.JavaClassInstance; } }));
374
+ Object.defineProperty(exports, "JavaClassProxy", ({ enumerable: true, get: function () { return definitions_1.JavaClassProxy; } }));
375
+ __exportStar(__webpack_require__(/*! ./java */ "./ts-src/java.ts"), exports);
376
+ const java = __importStar(__webpack_require__(/*! ./java */ "./ts-src/java.ts"));
377
+ exports["default"] = java;
378
+ var native_1 = __webpack_require__(/*! ../native */ "./native.js");
379
+ Object.defineProperty(exports, "getJavaLibPath", ({ enumerable: true, get: function () { return native_1.getJavaLibPath; } }));
380
+
381
+
382
+ /***/ }),
383
+
384
+ /***/ "./ts-src/java.ts":
385
+ /*!************************!*\
386
+ !*** ./ts-src/java.ts ***!
387
+ \************************/
388
+ /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
389
+
390
+ "use strict";
391
+
392
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
393
+ exports.getJavaInstance = exports.newProxy = exports.stdout = exports.classpath = exports.isInstanceOf = exports.appendClasspath = exports.importClassAsync = exports.importClass = exports.ensureJvm = void 0;
394
+ const native_1 = __webpack_require__(/*! ../native */ "./native.js");
395
+ const nativeLib_1 = __webpack_require__(/*! ./nativeLib */ "./ts-src/nativeLib.ts");
396
+ /**
397
+ * The static java instance
398
+ */
399
+ let javaInstance = null;
400
+ /**
401
+ * Ensure the java vm is created.
402
+ * If the jvm is already created, this does nothing.
403
+ * If the vm is not created yet, the jvm will be created upon this call.
404
+ * This method is also called every time with no arguments when any call
405
+ * to the jvm is done in another method.
406
+ *
407
+ * ## Examples
408
+ * Specify the path to jvm.(dylib|dll|so) manually,
409
+ * specify the java version to use and set to use daemon threads.
410
+ * ```ts
411
+ * import { ensureJvm, JavaVersion } from 'java-bridge';
412
+ *
413
+ * ensureJvm({
414
+ * libPath: 'path/to/jvm.dll',
415
+ * version: JavaVersion.VER_9,
416
+ * useDaemonThreads: true
417
+ * });
418
+ * ```
419
+ *
420
+ * Let the plugin find the jvm.(dylib|dll|so)
421
+ * ```ts
422
+ * ensureJvm({
423
+ * JavaVersion.VER_9,
424
+ * useDaemonThreads: true
425
+ * });
426
+ * ```
427
+ *
428
+ * Let the plugin find the jvm.(dylib|dll|so) and use the default options
429
+ * ```ts
430
+ * ensureJvm();
431
+ * ```
432
+ *
433
+ * @param options the options to use when creating the jvm
434
+ */
435
+ function ensureJvm(options) {
436
+ if (!javaInstance) {
437
+ javaInstance = new native_1.Java(options?.libPath, options?.version, options?.opts, options, (0, nativeLib_1.getJavaLibPath)(), (0, nativeLib_1.getNativeLibPath)());
438
+ }
439
+ }
440
+ exports.ensureJvm = ensureJvm;
441
+ function defineFields(object, getStatic) {
442
+ for (const field of (0, native_1.getClassFields)(object['class.proxy'], getStatic)) {
443
+ const getter = () => getStatic
444
+ ? (0, native_1.getStaticField)(object, field.name)
445
+ : (0, native_1.getField)(object, field.name);
446
+ if (field.isFinal) {
447
+ Object.defineProperty(object, field.name, {
448
+ get: getter,
449
+ enumerable: true,
450
+ });
451
+ }
452
+ else {
453
+ Object.defineProperty(object, field.name, {
454
+ get: getter,
455
+ set: (value) => getStatic
456
+ ? (0, native_1.setStaticField)(object, field.name, value)
457
+ : (0, native_1.setField)(object, field.name, value),
458
+ enumerable: true,
459
+ });
460
+ }
461
+ }
462
+ }
463
+ /**
464
+ * Import a class.
465
+ * Returns the constructor of the class to be created.
466
+ * For example, import "java.util.ArrayList" for a java Array List.
467
+ *
468
+ * Define a custom class type for the imported class and pass the
469
+ * constructor type of the class as the template parameter to get
470
+ * the proper type returned. You could also just cast the result.
471
+ *
472
+ * ## Examples
473
+ * ### Import ``java.util.ArrayList`` and create a new instance of it
474
+ * ```ts
475
+ * import { importClass } from 'java-bridge';
476
+ *
477
+ * // Import java.util.ArrayList
478
+ * const ArrayList = importClass('java.util.ArrayList');
479
+ *
480
+ * // Create a new instance of ArrayList
481
+ * const list = new ArrayList();
482
+ * ```
483
+ *
484
+ * ### Import ``java.util.ArrayList`` with types
485
+ * ```ts
486
+ * import { importClass, JavaClassInstance, JavaType } from 'java-bridge';
487
+ *
488
+ * /**
489
+ * * Definitions for class java.util.List
490
+ * *\/
491
+ * declare class List <T extends JavaType> extends JavaClassInstance {
492
+ * size(): Promise<number>;
493
+ * sizeSync(): number;
494
+ * add(e: T): Promise<void>;
495
+ * addSync(e: T): void;
496
+ * get(index: number): Promise<T>;
497
+ * getSync(index: number): T;
498
+ * toArray(): Promise<T[]>;
499
+ * toArraySync(): T[];
500
+ * isEmpty(): Promise<boolean>;
501
+ * isEmptySync(): boolean;
502
+ * }
503
+ *
504
+ * /**
505
+ * * Definitions for class java.util.ArrayList
506
+ * *\/
507
+ * declare class ArrayListClass<T extends JavaType> extends List<T> {
508
+ * public constructor(other: ArrayListClass<T>);
509
+ * public constructor();
510
+ * }
511
+ *
512
+ * // This causes the class to be import when the module is loaded.
513
+ * class ArrayList<T> extends importClass<typeof ArrayListClass>('java.util.ArrayList')<T> {}
514
+ *
515
+ * // Create a new ArrayList instance
516
+ * const list = new ArrayList<string>();
517
+ *
518
+ * // Add some contents to the list
519
+ * list.add('Hello');
520
+ * list.add('World');
521
+ *
522
+ * // Check the list contents
523
+ * assert.equals(list.sizeSync(), 2);
524
+ * assert.equals(list.getSync(0), 'Hello');
525
+ * assert.equals(list.getSync(1), 'World');
526
+ * ```
527
+ *
528
+ * @template T the type of the java class to import as a js type
529
+ * @param classname the name of the class to resolve
530
+ * @return the java class constructor
531
+ */
532
+ function importClass(classname) {
533
+ ensureJvm();
534
+ const constructor = javaInstance.importClass(classname);
535
+ defineFields(constructor, true);
536
+ constructor.constructor = function (...args) {
537
+ const object = new constructor.prototype.constructor(...args);
538
+ defineFields(object, false);
539
+ return object;
540
+ };
541
+ return constructor;
542
+ }
543
+ exports.importClass = importClass;
544
+ /**
545
+ * @inheritDoc importClass
546
+ */
547
+ async function importClassAsync(classname) {
548
+ ensureJvm();
549
+ const constructor = (await javaInstance.importClassAsync(classname));
550
+ defineFields(constructor, true);
551
+ constructor.constructor = function (...args) {
552
+ const object = new constructor.prototype.constructor(...args);
553
+ defineFields(object, false);
554
+ return object;
555
+ };
556
+ return constructor;
557
+ }
558
+ exports.importClassAsync = importClassAsync;
559
+ /**
560
+ * Append a single or multiple jars to the class path.
561
+ *
562
+ * Just replaces the old internal class loader with a new one containing the new jars.
563
+ * This doesn't check if the jars are valid and/or even exist.
564
+ * The new classpath will be available to all classes imported after this call.
565
+ *
566
+ * ## Example
567
+ * ```ts
568
+ * import { appendClasspath } from 'java-bridge';
569
+ *
570
+ * // Append a single jar to the class path
571
+ * appendClasspath('/path/to/jar.jar');
572
+ *
573
+ * // Append multiple jars to the class path
574
+ * appendClasspath(['/path/to/jar1.jar', '/path/to/jar2.jar']);
575
+ * ```
576
+ * or
577
+ * ```ts
578
+ * import { classpath } from 'java-bridge';
579
+ *
580
+ * // Append a single jar to the class path
581
+ * classpath.append('/path/to/jar.jar');
582
+ * ```
583
+ *
584
+ * @param path the path(s) to add
585
+ */
586
+ function appendClasspath(path) {
587
+ ensureJvm();
588
+ javaInstance.appendClasspath(path);
589
+ }
590
+ exports.appendClasspath = appendClasspath;
591
+ /**
592
+ * Check if `this_obj` is instance of `other`.
593
+ * This uses the native java `instanceof` operator.
594
+ * You may want to use this if {@link JavaClassInstance.instanceOf}
595
+ * is overridden, as that method itself does not override
596
+ * any method defined in the specific java class named 'instanceOf'.
597
+ *
598
+ * ## Example
599
+ * ```ts
600
+ * import { instanceOf, importClass } from 'java-bridge';
601
+ *
602
+ * const ArrayList = importClass('java.util.ArrayList');
603
+ * const list = new ArrayList();
604
+ *
605
+ * isInstanceOf(list, ArrayList); // true
606
+ * isInstanceOf(list, 'java.util.ArrayList'); // true
607
+ * isInstanceOf(list, 'java.util.List'); // true
608
+ * isInstanceOf(list, 'java.util.Collection'); // true
609
+ * isInstanceOf(list, 'java.lang.Object'); // true
610
+ * isInstanceOf(list, 'java.lang.String'); // false
611
+ *
612
+ * // You can also use the instanceOf method (if not overridden)
613
+ * list.instanceOf(ArrayList); // true
614
+ * list.instanceOf('java.util.ArrayList'); // true
615
+ * list.instanceOf('java.util.List'); // true
616
+ * list.instanceOf('java.util.Collection'); // true
617
+ * list.instanceOf('java.lang.Object'); // true
618
+ * list.instanceOf('java.lang.String'); // false
619
+ * ```
620
+ *
621
+ * @param this_obj the object to check
622
+ * @param other the class or class name to check against
623
+ * @return true if `this_obj` is an instance of `other`
624
+ */
625
+ function isInstanceOf(this_obj, other) {
626
+ ensureJvm();
627
+ return javaInstance.isInstanceOf(this_obj, other);
628
+ }
629
+ exports.isInstanceOf = isInstanceOf;
630
+ /**
631
+ * Methods for altering and querying the class path.
632
+ * @example
633
+ * import { classpath } from 'java-bridge';
634
+ *
635
+ * // Append a jar to the class path
636
+ * classpath.append('/path/to/jar.jar');
637
+ *
638
+ * assert.equal(classpath.get().length, 1);
639
+ * assert.equal(classpath.get()[0], '/path/to/jar.jar');
640
+ */
641
+ var classpath;
642
+ (function (classpath) {
643
+ /**
644
+ * @inheritDoc appendClasspath
645
+ */
646
+ function append(path) {
647
+ appendClasspath(path);
648
+ }
649
+ classpath.append = append;
650
+ /**
651
+ * Get the loaded jars in the class path
652
+ *
653
+ * @returns a list of the loaded jars
654
+ */
655
+ function get() {
656
+ ensureJvm();
657
+ return javaInstance.loadedJars;
658
+ }
659
+ classpath.get = get;
660
+ })(classpath = exports.classpath || (exports.classpath = {}));
661
+ /**
662
+ * A namespace containing methods for redirecting the stdout/stderr of the java process.
663
+ *
664
+ * ## See also
665
+ * * {@link StdoutRedirectGuard}
666
+ * * {@link stdout.enableRedirect}
667
+ */
668
+ var stdout;
669
+ (function (stdout_1) {
670
+ /**
671
+ * Enable stdout/stderr redirection.
672
+ *
673
+ * Pass methods for the stdout and stderr output to be redirected to.
674
+ * These methods must accept an error as the first argument,
675
+ * although this will probably never be set and can be ignored.
676
+ * The second argument is the data that was redirected.
677
+ *
678
+ * Setting any method to ``null`` or ``undefined`` will disable the redirect for that method.
679
+ * This also allows you not set any handler which does not make any sense at all.
680
+ *
681
+ * ## Examples
682
+ * ### Redirect all data to the js console
683
+ * ```ts
684
+ * import { stdout } from 'java-bridge';
685
+ *
686
+ * const guard = stdout.enableRedirect((_, data) => {
687
+ * console.log('Stdout:', data);
688
+ * }, (_, data) => {
689
+ * console.error('Stderr:', data);
690
+ * });
691
+ * ```
692
+ *
693
+ * ### Redirect stdout to the js console
694
+ * ```ts
695
+ * const guard = stdout.enableRedirect((_, data) => {
696
+ * console.log('Stdout:', data);
697
+ * });
698
+ * ```
699
+ *
700
+ * ### Redirect stderr to the js console
701
+ * ```ts
702
+ * const guard = stdout.enableRedirect(null, (_, data) => {
703
+ * console.error('Stderr:', data);
704
+ * });
705
+ * ```
706
+ *
707
+ * ### Redirect nothing to the js console (y tho)
708
+ * This enables you to print nothing to nowhere.
709
+ * ```ts
710
+ * // Why would you do this?
711
+ * const guard = stdout.enableRedirect(null, null);
712
+ *
713
+ * // Or
714
+ * const guard = stdout.enableRedirect();
715
+ * ```
716
+ *
717
+ * @see StdoutRedirectGuard
718
+ * @see StdoutCallback
719
+ * @param stdout the callback to be called when stdout is received
720
+ * @param stderr the callback to be called when stderr is received
721
+ * @returns a <code>StdoutRedirectGuard</code> instance. Keep this instance in scope to not lose the redirect.
722
+ */
723
+ function enableRedirect(stdout, stderr) {
724
+ ensureJvm();
725
+ return javaInstance.setStdoutCallbacks(stdout, stderr);
726
+ }
727
+ stdout_1.enableRedirect = enableRedirect;
728
+ })(stdout = exports.stdout || (exports.stdout = {}));
729
+ /**
730
+ * Create a new java interface proxy.
731
+ * This allows you to implement java interfaces in javascript.
732
+ *
733
+ * Pass an object as the second argument with the names of the
734
+ * methods you want to implement as keys and the implementations
735
+ * as values in order to expose these methods to the java process.
736
+ * Any arguments will be converted to javascript values and
737
+ * return values will be converted to java values.
738
+ *
739
+ * When the java process tries to call any method which is
740
+ * not implemented by the proxy, an error will be thrown.
741
+ *
742
+ * ## Examples
743
+ * ### Implement ``java.lang.Runnable``
744
+ * ```ts
745
+ * import { newProxy, importClass } from 'java-bridge';
746
+ *
747
+ * // Define the interface
748
+ * const runnable = newProxy('java.lang.Runnable', {
749
+ * run: (): void => {
750
+ * console.log('Hello World!');
751
+ * }
752
+ * });
753
+ *
754
+ * // Note: You can't do something like this:
755
+ * // runnable.run();
756
+ *
757
+ * // Pass the proxy to a java method instead:
758
+ * const Thread = importClass('java.lang.Thread');
759
+ * const thread = new Thread(runnable); // <- Pass the proxy here
760
+ *
761
+ * // NOTE: You don't have to call this asynchronously
762
+ * // as this call instantly returns.
763
+ * thread.startSync();
764
+ * ```
765
+ *
766
+ * ### Implement ``java.util.function.Function`` to transform a string
767
+ * ```ts
768
+ * const func = newProxy('java.util.function.Function', {
769
+ * // Any parameters and return types will be automatically converted
770
+ * apply: (str: string): string => {
771
+ * return str.toUpperCase();
772
+ * }
773
+ * });
774
+ *
775
+ * // Import the string class
776
+ * const JString = java.importClass('java.lang.String');
777
+ * const str = new JString('hello');
778
+ *
779
+ * // Pass the proxy.
780
+ * // NOTE: You must call this method async otherwise your program will hang.
781
+ * // See notes for more info.
782
+ * const transformed = await str.transform(func);
783
+ *
784
+ * assert.assertEquals(transformed, 'HELLO');
785
+ * ```
786
+ *
787
+ * Which is equivalent to the following java code:
788
+ * ```java
789
+ * Function<String, String> func = new Function<>() {
790
+ * @Override
791
+ * public String apply(String str) {
792
+ * return str.toUpperCase();
793
+ * }
794
+ * };
795
+ *
796
+ * String str = "hello";
797
+ * String transformed = str.transform(func);
798
+ * assert.assertEquals(transformed, "HELLO");
799
+ * ```
800
+ *
801
+ * #### Throwing exceptions
802
+ * Any exceptions thrown by the proxy will be converted to java exceptions
803
+ * and then rethrown in the java process. This may cause the exception
804
+ * to again be rethrown in the javascript process.
805
+ * ```ts
806
+ * const func = newProxy('java.util.function.Function', {
807
+ * apply: (str: string): string => {
808
+ * throw new Error('Something went wrong');
809
+ * }
810
+ * });
811
+ *
812
+ * const JString = java.importClass('java.lang.String');
813
+ * const str = new JString('hello');
814
+ *
815
+ * // This will re-throw the above error
816
+ * const transformed: never = await str.transform(func);
817
+ * ```
818
+ *
819
+ * ## Notes
820
+ * * Keep this instance in scope to not destroy the interface proxy.
821
+ * * Call {@link JavaInterfaceProxy.reset} to instantly destroy this instance.
822
+ * * If any method is queried by the java process and not implemented in here,
823
+ * an exception will be thrown in the java process.
824
+ * * Any errors thrown in the javascript process will be rethrown in the java process.
825
+ * * **When calling a java method that uses an interface defined by this, you must call
826
+ * that method using the interface asynchronously as Node.js is single threaded and can't
827
+ * wait for the java method to return while calling the proxy method at the same time.**
828
+ *
829
+ * ## See also
830
+ * * {@link JavaInterfaceProxy}
831
+ *
832
+ * @param interfaceName the name of the java interface to implement
833
+ * @param methods the methods to implement.
834
+ * @returns a proxy class to pass back to the java process
835
+ */
836
+ function newProxy(interfaceName, methods) {
837
+ ensureJvm();
838
+ const proxyMethods = Object.create(null);
839
+ for (const [name, method] of Object.entries(methods)) {
840
+ proxyMethods[name] = (err, callback, ...args) => {
841
+ if (err) {
842
+ throw err;
843
+ }
844
+ try {
845
+ const res = method(...args);
846
+ callback(null, res);
847
+ }
848
+ catch (e) {
849
+ if (e instanceof Error) {
850
+ callback(e);
851
+ }
852
+ else {
853
+ callback(new Error(e.toString()));
854
+ }
855
+ }
856
+ };
857
+ }
858
+ return javaInstance.createInterfaceProxy(interfaceName, proxyMethods);
859
+ }
860
+ exports.newProxy = newProxy;
861
+ /**
862
+ * Get the static java instance.
863
+ * This has no real use, all important methods are exported explicitly.
864
+ */
865
+ function getJavaInstance() {
866
+ return javaInstance;
867
+ }
868
+ exports.getJavaInstance = getJavaInstance;
869
+
870
+
871
+ /***/ }),
872
+
873
+ /***/ "./ts-src/nativeLib.ts":
874
+ /*!*****************************!*\
875
+ !*** ./ts-src/nativeLib.ts ***!
876
+ \*****************************/
877
+ /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
878
+
879
+ "use strict";
880
+
881
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
882
+ if (k2 === undefined) k2 = k;
883
+ var desc = Object.getOwnPropertyDescriptor(m, k);
884
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
885
+ desc = { enumerable: true, get: function() { return m[k]; } };
886
+ }
887
+ Object.defineProperty(o, k2, desc);
888
+ }) : (function(o, m, k, k2) {
889
+ if (k2 === undefined) k2 = k;
890
+ o[k2] = m[k];
891
+ }));
892
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
893
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
894
+ }) : function(o, v) {
895
+ o["default"] = v;
896
+ });
897
+ var __importStar = (this && this.__importStar) || function (mod) {
898
+ if (mod && mod.__esModule) return mod;
899
+ var result = {};
900
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
901
+ __setModuleDefault(result, mod);
902
+ return result;
903
+ };
904
+ var __importDefault = (this && this.__importDefault) || function (mod) {
905
+ return (mod && mod.__esModule) ? mod : { "default": mod };
906
+ };
907
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
908
+ exports.getJavaLibPath = exports.getNativeLibPath = void 0;
909
+ const path_1 = __importDefault(__webpack_require__(/*! path */ "path"));
910
+ const fs_1 = __importStar(__webpack_require__(/*! fs */ "fs"));
911
+ const glob_1 = __importDefault(__webpack_require__(/*! glob */ "glob"));
912
+ const { platform, arch } = process;
913
+ function getModule(base) {
914
+ const local = path_1.default.join(__dirname, base + '.node');
915
+ if (fs_1.default.existsSync(local)) {
916
+ return local;
917
+ }
918
+ else {
919
+ const module = base.replaceAll('.', '-').replace('java', 'java-bridge');
920
+ // @ts-ignore
921
+ if (require && require.resolve) {
922
+ // @ts-ignore
923
+ return require.resolve(module);
924
+ }
925
+ else {
926
+ return /*require.resolve*/(__webpack_require__("./ts-src sync recursive").resolve(module));
927
+ }
928
+ }
929
+ }
930
+ function UnsupportedPlatform() {
931
+ return new Error(`Unsupported platform: ${platform} ${arch}`);
932
+ }
933
+ function isMusl() {
934
+ // For Node 10
935
+ if (!process.report || typeof process.report.getReport !== 'function') {
936
+ try {
937
+ return (0, fs_1.readFileSync)('/usr/bin/ldd', 'utf8').includes('musl');
938
+ }
939
+ catch (e) {
940
+ return true;
941
+ }
942
+ }
943
+ else {
944
+ const { glibcVersionRuntime } = process.report.getReport()
945
+ .header;
946
+ return !glibcVersionRuntime;
947
+ }
948
+ }
949
+ function getNativeLibPath() {
950
+ switch (platform) {
951
+ case 'android':
952
+ switch (arch) {
953
+ case 'arm64':
954
+ return getModule('java.android-arm64');
955
+ case 'arm':
956
+ return getModule('java.android-arm-eabi');
957
+ default:
958
+ throw UnsupportedPlatform();
959
+ }
960
+ case 'win32':
961
+ return getModule(`java.win32-${arch}-msvc`);
962
+ case 'darwin':
963
+ return getModule(`java.darwin-${arch}`);
964
+ case 'freebsd':
965
+ return getModule(`java.freebsd-${arch}`);
966
+ case 'linux':
967
+ switch (arch) {
968
+ case 'x64':
969
+ case 'arm64':
970
+ return getModule(`java.linux-${arch}-${isMusl() ? 'musl' : 'gnu'}`);
971
+ case 'arm':
972
+ return getModule('java.linux-arm-gnueabihf');
973
+ default:
974
+ throw UnsupportedPlatform();
975
+ }
976
+ default:
977
+ throw UnsupportedPlatform();
978
+ }
979
+ }
980
+ exports.getNativeLibPath = getNativeLibPath;
981
+ function getJavaLibPath() {
982
+ const dir = path_1.default.join(__dirname, '..', 'java-src', 'build', 'libs');
983
+ let files = glob_1.default.sync('*.jar', { cwd: dir });
984
+ if (files.length === 0) {
985
+ throw new Error(`No java lib found in ${dir}`);
986
+ }
987
+ else {
988
+ return path_1.default.join(dir, files[0]);
989
+ }
990
+ }
991
+ exports.getJavaLibPath = getJavaLibPath;
992
+
993
+
994
+ /***/ }),
995
+
996
+ /***/ "./ts-src sync recursive":
997
+ /*!**********************!*\
998
+ !*** ./ts-src/ sync ***!
999
+ \**********************/
1000
+ /***/ ((module) => {
1001
+
1002
+ function webpackEmptyContext(req) {
1003
+ var e = new Error("Cannot find module '" + req + "'");
1004
+ e.code = 'MODULE_NOT_FOUND';
1005
+ throw e;
1006
+ }
1007
+ webpackEmptyContext.keys = () => ([]);
1008
+ webpackEmptyContext.resolve = webpackEmptyContext;
1009
+ webpackEmptyContext.id = "./ts-src sync recursive";
1010
+ module.exports = webpackEmptyContext;
1011
+
1012
+ /***/ }),
1013
+
1014
+ /***/ "glob":
1015
+ /*!***********************!*\
1016
+ !*** external "glob" ***!
1017
+ \***********************/
1018
+ /***/ ((module) => {
1019
+
1020
+ "use strict";
1021
+ module.exports = require("glob");
1022
+
1023
+ /***/ }),
1024
+
1025
+ /***/ "fs":
1026
+ /*!*********************!*\
1027
+ !*** external "fs" ***!
1028
+ \*********************/
1029
+ /***/ ((module) => {
1030
+
1031
+ "use strict";
1032
+ module.exports = require("fs");
1033
+
1034
+ /***/ }),
1035
+
1036
+ /***/ "path":
1037
+ /*!***********************!*\
1038
+ !*** external "path" ***!
1039
+ \***********************/
1040
+ /***/ ((module) => {
1041
+
1042
+ "use strict";
1043
+ module.exports = require("path");
1044
+
1045
+ /***/ })
1046
+
1047
+ /******/ });
1048
+ /************************************************************************/
1049
+ /******/ // The module cache
1050
+ /******/ var __webpack_module_cache__ = {};
1051
+ /******/
1052
+ /******/ // The require function
1053
+ /******/ function __webpack_require__(moduleId) {
1054
+ /******/ // Check if module is in cache
1055
+ /******/ var cachedModule = __webpack_module_cache__[moduleId];
1056
+ /******/ if (cachedModule !== undefined) {
1057
+ /******/ return cachedModule.exports;
1058
+ /******/ }
1059
+ /******/ // Create a new module (and put it into the cache)
1060
+ /******/ var module = __webpack_module_cache__[moduleId] = {
1061
+ /******/ id: moduleId,
1062
+ /******/ loaded: false,
1063
+ /******/ exports: {}
1064
+ /******/ };
1065
+ /******/
1066
+ /******/ // Execute the module function
1067
+ /******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
1068
+ /******/
1069
+ /******/ // Flag the module as loaded
1070
+ /******/ module.loaded = true;
1071
+ /******/
1072
+ /******/ // Return the exports of the module
1073
+ /******/ return module.exports;
1074
+ /******/ }
1075
+ /******/
1076
+ /************************************************************************/
1077
+ /******/ /* webpack/runtime/hasOwnProperty shorthand */
1078
+ /******/ (() => {
1079
+ /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
1080
+ /******/ })();
1081
+ /******/
1082
+ /******/ /* webpack/runtime/node module decorator */
1083
+ /******/ (() => {
1084
+ /******/ __webpack_require__.nmd = (module) => {
1085
+ /******/ module.paths = [];
1086
+ /******/ if (!module.children) module.children = [];
1087
+ /******/ return module;
1088
+ /******/ };
1089
+ /******/ })();
1090
+ /******/
1091
+ /******/ /* webpack/runtime/publicPath */
1092
+ /******/ (() => {
1093
+ /******/ __webpack_require__.p = "";
1094
+ /******/ })();
1095
+ /******/
1096
+ /************************************************************************/
1097
+ /******/
1098
+ /******/ // startup
1099
+ /******/ // Load entry module and return exports
1100
+ /******/ // This entry module is referenced by other modules so it can't be inlined
1101
+ /******/ var __webpack_exports__ = __webpack_require__("./ts-src/index.ts");
1102
+ /******/
1103
+ /******/ return __webpack_exports__;
1104
+ /******/ })()
1105
+ ;
1106
+ });
1107
+ //# sourceMappingURL=index.dev.min.js.map