|
| 1 | +describe("Typed arrays passed to Java parameters", function () { |
| 2 | + var T = com.tns.tests.TypedArrayArgs; |
| 3 | + |
| 4 | + function toArray(javaArray) { |
| 5 | + var result = []; |
| 6 | + for (var i = 0; i < javaArray.length; i++) { |
| 7 | + result.push(javaArray[i]); |
| 8 | + } |
| 9 | + return result; |
| 10 | + } |
| 11 | + |
| 12 | + it("copies a Uint8Array into a byte[] parameter", function () { |
| 13 | + var u8 = new Uint8Array([1, 2, 127, 128, 255]); |
| 14 | + var bb = java.nio.ByteBuffer.wrap(u8); |
| 15 | + |
| 16 | + expect(bb.isDirect()).toBe(false); |
| 17 | + expect(bb.capacity()).toBe(5); |
| 18 | + expect(toArray(bb.array())).toEqual([1, 2, 127, -128, -1]); |
| 19 | + |
| 20 | + u8[0] = 9; |
| 21 | + expect(bb.array()[0]).toBe(1); |
| 22 | + bb.array()[1] = 42; |
| 23 | + expect(u8[1]).toBe(2); |
| 24 | + }); |
| 25 | + |
| 26 | + it("copies only the bytes a view covers", function () { |
| 27 | + var all = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]); |
| 28 | + expect(toArray(T.echoBytes(new Uint8Array(all.buffer, 2, 3)))).toEqual([2, 3, 4]); |
| 29 | + expect(toArray(T.echoBytes(all.subarray(6)))).toEqual([6, 7]); |
| 30 | + expect(toArray(T.echoInts(new Int32Array(new ArrayBuffer(16), 4, 2)))).toEqual([0, 0]); |
| 31 | + }); |
| 32 | + |
| 33 | + it("copies byte-sized arrays, ArrayBuffers and DataViews into byte[]", function () { |
| 34 | + var bytes = [1, -1, 127, -128]; |
| 35 | + var buffer = new Int8Array(bytes).buffer; |
| 36 | + |
| 37 | + expect(toArray(T.echoBytes(new Int8Array(bytes)))).toEqual(bytes); |
| 38 | + expect(toArray(T.echoBytes(new Uint8ClampedArray([1, 255, 127, 128])))).toEqual(bytes); |
| 39 | + expect(toArray(T.echoBytes(buffer))).toEqual(bytes); |
| 40 | + expect(toArray(T.echoBytes(new DataView(buffer, 1, 2)))).toEqual([-1, 127]); |
| 41 | + |
| 42 | + var shared = new SharedArrayBuffer(2); |
| 43 | + new Uint8Array(shared).set([5, 6]); |
| 44 | + expect(toArray(T.echoBytes(shared))).toEqual([5, 6]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("copies each typed array into the primitive array of its element type", function () { |
| 48 | + expect(toArray(T.echoShorts(new Int16Array([-32768, 1, 32767])))).toEqual([-32768, 1, 32767]); |
| 49 | + expect(toArray(T.echoShorts(new Uint16Array([65535, 2])))).toEqual([-1, 2]); |
| 50 | + expect(toArray(T.echoChars(new Uint16Array([0x41, 0x7a])))).toEqual(["A", "z"]); |
| 51 | + expect(toArray(T.echoInts(new Int32Array([-2147483648, 3, 2147483647])))).toEqual([-2147483648, 3, 2147483647]); |
| 52 | + expect(toArray(T.echoInts(new Uint32Array([4294967295, 4])))).toEqual([-1, 4]); |
| 53 | + expect(toArray(T.echoLongs(new BigInt64Array([-5n, 6n])))).toEqual([-5, 6]); |
| 54 | + expect(toArray(T.echoLongs(new BigUint64Array([7n])))).toEqual([7]); |
| 55 | + expect(toArray(T.echoFloats(new Float32Array([0.5, -1.25])))).toEqual([0.5, -1.25]); |
| 56 | + expect(toArray(T.echoDoubles(new Float64Array([0.1, -1e300])))).toEqual([0.1, -1e300]); |
| 57 | + }); |
| 58 | + |
| 59 | + it("converts other element types the way a plain array with the same values converts", function () { |
| 60 | + var values = [1.9, -2.5, 300, 70000, -70000]; |
| 61 | + expect(toArray(T.echoInts(new Float64Array(values)))).toEqual(toArray(T.echoInts(values))); |
| 62 | + expect(toArray(T.echoBytes(new Float64Array(values)))).toEqual(toArray(T.echoBytes(values))); |
| 63 | + expect(toArray(T.echoShorts(new Int32Array([70000, -1])))).toEqual(toArray(T.echoShorts([70000, -1]))); |
| 64 | + expect(toArray(T.echoBytes(new Int32Array([1, 255, 256, -1])))).toEqual([1, -1, 0, -1]); |
| 65 | + expect(toArray(T.echoLongs(new Int32Array([-5, 7])))).toEqual([-5, 7]); |
| 66 | + expect(toArray(T.echoDoubles(new Float32Array([0.5, 1.25])))).toEqual([0.5, 1.25]); |
| 67 | + expect(toArray(T.echoBooleans(new Uint8Array([0, 1, 2])))).toEqual([false, true, true]); |
| 68 | + }); |
| 69 | + |
| 70 | + it("converts a typed array for an object array parameter the way a plain array converts", function () { |
| 71 | + expect(T.describeObjects(new Int32Array([1, 2]))).toBe(T.describeObjects([1, 2])); |
| 72 | + expect(T.describeObjects(new Float64Array([0.5]))).toBe(T.describeObjects([0.5])); |
| 73 | + }); |
| 74 | + |
| 75 | + it("rejects element types that have no conversion to the parameter's", function () { |
| 76 | + expect(function () { T.echoInts(new BigInt64Array(2)); }).toThrow(); |
| 77 | + expect(function () { T.echoInts(new ArrayBuffer(8)); }).toThrow(); |
| 78 | + expect(function () { T.echoInts(new DataView(new ArrayBuffer(8))); }).toThrow(); |
| 79 | + expect(function () { T.describeObjects(new ArrayBuffer(8)); }).toThrow(); |
| 80 | + expect(function () { T.describeObjects(new BigInt64Array(1)); }).toThrow(); |
| 81 | + expect(function () { T.countRows(new Int32Array(4)); }).toThrow(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("copies empty typed arrays", function () { |
| 85 | + expect(T.echoBytes(new Uint8Array(0)).length).toBe(0); |
| 86 | + expect(T.echoDoubles(new Float64Array(0)).length).toBe(0); |
| 87 | + expect(T.echoBytes(new ArrayBuffer(0)).length).toBe(0); |
| 88 | + expect(T.echoInts([]).length).toBe(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it("still passes a typed array to an object parameter as a buffer over its memory", function () { |
| 92 | + var ints = new Int32Array(4); |
| 93 | + expect(T.capacityOf(ints)).toBe(4); |
| 94 | + expect(T.capacityOf(new Uint8Array(3))).toBe(3); |
| 95 | + // A typed array that is already linked to a buffer is still copied into byte[]. |
| 96 | + var bytes = new Uint8Array([3, 4]); |
| 97 | + expect(T.capacityOf(bytes)).toBe(2); |
| 98 | + expect(toArray(T.echoBytes(bytes))).toEqual([3, 4]); |
| 99 | + }); |
| 100 | + |
| 101 | + // Each of these used to pin a JNI global reference, and ART aborts the process once |
| 102 | + // its global reference table (51200 entries) overflows. |
| 103 | + it("passes more typed arrays to object parameters than fit in the global reference table", function () { |
| 104 | + var count = 60000; |
| 105 | + var total = 0; |
| 106 | + for (var i = 0; i < count; i++) { |
| 107 | + total += T.capacityOf(new Uint8Array(16)); |
| 108 | + } |
| 109 | + expect(total).toBe(count * 16); |
| 110 | + }); |
| 111 | + |
| 112 | + it("passes more typed arrays as Object[] elements than fit in the global reference table", function () { |
| 113 | + var count = 60000; |
| 114 | + var items = new Array(count); |
| 115 | + for (var i = 0; i < count; i++) { |
| 116 | + items[i] = new Float32Array(2); |
| 117 | + } |
| 118 | + expect(T.totalCapacity(items)).toBe(count * 2); |
| 119 | + }); |
| 120 | + |
| 121 | + it("lets Java collect the buffer once JS drops the typed array", function (done) { |
| 122 | + var weak = (function () { |
| 123 | + return new java.lang.ref.WeakReference(new Uint8Array(1024)); |
| 124 | + })(); |
| 125 | + var attempts = 0; |
| 126 | + function collect() { |
| 127 | + gc(); |
| 128 | + java.lang.System.gc(); |
| 129 | + if (weak.get() === null) { |
| 130 | + done(); |
| 131 | + } else if (++attempts >= 40) { |
| 132 | + expect(weak.get()).toBeNull(); |
| 133 | + done(); |
| 134 | + } else { |
| 135 | + setTimeout(collect, 50); |
| 136 | + } |
| 137 | + } |
| 138 | + setTimeout(collect, 0); |
| 139 | + }); |
| 140 | + |
| 141 | + it("keeps the typed array's memory alive while Java holds the buffer", function () { |
| 142 | + var holder = new com.tns.tests.ByteBufferHolder(); |
| 143 | + (function () { |
| 144 | + var bytes = new Uint8Array(4096); |
| 145 | + bytes[0] = 42; |
| 146 | + bytes[4095] = 7; |
| 147 | + holder.hold(bytes); |
| 148 | + })(); |
| 149 | + for (var i = 0; i < 3; i++) { |
| 150 | + gc(); |
| 151 | + java.lang.System.gc(); |
| 152 | + } |
| 153 | + expect(holder.get(0)).toBe(42); |
| 154 | + expect(holder.get(4095)).toBe(7); |
| 155 | + }); |
| 156 | + |
| 157 | + it("links a new buffer when Java has collected the one a reused typed array had", function () { |
| 158 | + var list = new java.util.ArrayList(); |
| 159 | + (function () { |
| 160 | + list.add(new Uint8Array([9, 8, 7])); |
| 161 | + })(); |
| 162 | + // Collecting the typed array makes Java hold its buffer weakly from then on. |
| 163 | + for (var i = 0; i < 3; i++) { |
| 164 | + gc(); |
| 165 | + java.lang.System.gc(); |
| 166 | + } |
| 167 | + var bytes = list.remove(0); |
| 168 | + expect(bytes instanceof Uint8Array).toBe(true); |
| 169 | + for (var i = 0; i < 3; i++) { |
| 170 | + java.lang.System.gc(); |
| 171 | + } |
| 172 | + |
| 173 | + list.add(bytes); |
| 174 | + expect(list.get(0)).toBe(bytes); |
| 175 | + expect(T.totalCapacity(list.toArray())).toBe(3); |
| 176 | + var copy = java.nio.ByteBuffer.allocate(3); |
| 177 | + copy.put(bytes); |
| 178 | + expect(toArray(copy.array())).toEqual([9, 8, 7]); |
| 179 | + }); |
| 180 | + |
| 181 | + function conversionError(call) { |
| 182 | + try { |
| 183 | + call(); |
| 184 | + } catch (e) { |
| 185 | + return e.message; |
| 186 | + } |
| 187 | + return "no error"; |
| 188 | + } |
| 189 | + |
| 190 | + it("throws for array elements that cannot convert to the element type", function () { |
| 191 | + expect(conversionError(function () { T.echoLongs([1n, 2n]); })) |
| 192 | + .toContain("Cannot convert array to [J at index 0"); |
| 193 | + expect(conversionError(function () { T.echoInts([1, 2n]); })).toContain("BigInt"); |
| 194 | + expect(conversionError(function () { T.echoInts([1, Symbol("s")]); })).toContain("Symbol"); |
| 195 | + expect(conversionError(function () { T.echoChars([Symbol("s")]); })).toContain("Symbol"); |
| 196 | + expect(conversionError(function () { |
| 197 | + T.echoDoubles([{ valueOf: function () { throw new Error("no value"); } }]); |
| 198 | + })).toContain("no value"); |
| 199 | + |
| 200 | + var withThrowingGetter = [1, 2]; |
| 201 | + Object.defineProperty(withThrowingGetter, 1, { get: function () { throw new Error("no element"); } }); |
| 202 | + expect(conversionError(function () { T.echoInts(withThrowingGetter); })).toContain("no element"); |
| 203 | + expect(conversionError(function () { T.describeObjects(withThrowingGetter); })).toContain("no element"); |
| 204 | + }); |
| 205 | + |
| 206 | + it("throws for a BigInt passed to a long parameter, as in an array", function () { |
| 207 | + expect(conversionError(function () { java.lang.Long.toHexString(5n); })) |
| 208 | + .toContain("Cannot convert bigint to J at index 0"); |
| 209 | + }); |
| 210 | + |
| 211 | + it("throws for an array longer than a Java array can be", function () { |
| 212 | + var sparse = []; |
| 213 | + sparse.length = 2147483648; |
| 214 | + expect(conversionError(function () { T.echoInts(sparse); })).toContain("Cannot convert array to [I"); |
| 215 | + }); |
| 216 | + |
| 217 | + it("converts a JS array too large for the thread's stack", function () { |
| 218 | + var count = 4 * 1024 * 1024; |
| 219 | + var values = new Array(count); |
| 220 | + var expected = 0; |
| 221 | + for (var i = 0; i < count; i++) { |
| 222 | + values[i] = i & 0xff; |
| 223 | + expected += i & 0xff; |
| 224 | + } |
| 225 | + expect(T.sumInts(values)).toBe(expected); |
| 226 | + }); |
| 227 | +}); |
0 commit comments