| 1 |
load(libdir + 'asserts.js'); |
| 2 |
|
| 3 |
function typedArrayMatchesString(ta, str, uptoStringLength = false) { |
| 4 |
if (ta.length != str.length && !uptoStringLength) |
| 5 |
return false; |
| 6 |
for (let i = 0; i < str.length; i++) { |
| 7 |
if (ta[i] != str.charCodeAt(i)) |
| 8 |
return false; |
| 9 |
} |
| 10 |
return true; |
| 11 |
} |
| 12 |
|
| 13 |
function test() { |
| 14 |
|
| 15 |
// Check that {signed,unsigned} -> {Int*Array, Uint*Array} for string types. |
| 16 |
|
| 17 |
const shortU8 = ctypes.unsigned_char.array(10)("abc\0\0\0\0\0\0\0").readTypedArray(); |
| 18 |
assertEq(shortU8 instanceof Uint8Array, true); |
| 19 |
assertEq(typedArrayMatchesString(shortU8, "abc\0\0\0\0\0\0\0"), true); |
| 20 |
|
| 21 |
const shortI8 = ctypes.signed_char.array(10)("abc\0\0\0\0\0\0\0").readTypedArray(); |
| 22 |
assertEq(shortI8 instanceof Int8Array, true); |
| 23 |
assertEq(typedArrayMatchesString(shortI8, "abc\0\0\0\0\0\0\0"), true); |
| 24 |
|
| 25 |
const shortU16 = ctypes.char16_t.array(10)("千").readTypedArray(); |
| 26 |
assertEq(shortU16 instanceof Uint16Array, true); |
| 27 |
assertEq(typedArrayMatchesString(shortU16, "千", 'ignore zero-padding, please'), true); |
| 28 |
|
| 29 |
// ...and for (other) numeric types. |
| 30 |
|
| 31 |
const I16 = ctypes.int16_t.array(10)().readTypedArray(); |
| 32 |
assertEq(I16 instanceof Int16Array, true); |
| 33 |
|
| 34 |
const U32 = ctypes.uint32_t.array(10)().readTypedArray(); |
| 35 |
assertEq(U32 instanceof Uint32Array, true); |
| 36 |
|
| 37 |
const I32 = ctypes.int32_t.array(10)().readTypedArray(); |
| 38 |
assertEq(I32 instanceof Int32Array, true); |
| 39 |
|
| 40 |
// Check that pointers without associated length get truncated to strlen(). |
| 41 |
|
| 42 |
const unsignedCharArray = ctypes.unsigned_char.array(10)("abc\0\0\0"); |
| 43 |
const shortU8cs = unsignedCharArray.addressOfElement(0).readTypedArray(); |
| 44 |
assertEq(shortU8cs instanceof Uint8Array, true); |
| 45 |
assertEq(shortU8cs.length, 3); |
| 46 |
assertEq(typedArrayMatchesString(shortU8cs, "abc", 'stop at NUL, please'), true); |
| 47 |
|
| 48 |
const signedCharArray = ctypes.signed_char.array(10)("abc\0\0\0"); |
| 49 |
const shortI8cs = signedCharArray.addressOfElement(0).readTypedArray(); |
| 50 |
assertEq(shortI8cs instanceof Int8Array, true); |
| 51 |
assertEq(shortI8cs.length, 3); |
| 52 |
assertEq(typedArrayMatchesString(shortI8cs, "abc", 'stop at NUL, please'), true); |
| 53 |
|
| 54 |
const char16Array = ctypes.char16_t.array(10)("千\0"); |
| 55 |
const shortU16cs = char16Array.addressOfElement(0).readTypedArray(); |
| 56 |
assertEq(shortU16cs instanceof Uint16Array, true); |
| 57 |
assertEq(shortU16cs.length, 1); |
| 58 |
assertEq(typedArrayMatchesString(shortU16cs, "千", 'ignore zero-padding, please'), true); |
| 59 |
|
| 60 |
// Other types should just fail if the length is not known. |
| 61 |
|
| 62 |
assertTypeErrorMessage(() => { ctypes.int32_t.array(3)().addressOfElement(0).readTypedArray(); }, |
| 63 |
/base type .* is not an 8-bit or 16-bit integer or character type/); |
| 64 |
assertTypeErrorMessage(() => { ctypes.float.array(3)().addressOfElement(0).readTypedArray(); }, |
| 65 |
/base type .* is not an 8-bit or 16-bit integer or character type/); |
| 66 |
assertTypeErrorMessage(() => { ctypes.double.array(3)().addressOfElement(0).readTypedArray(); }, |
| 67 |
/base type .* is not an 8-bit or 16-bit integer or character type/); |
| 68 |
// char16_t is unsigned, so this is not a string type. |
| 69 |
assertTypeErrorMessage(() => { ctypes.int16_t.array(3)().addressOfElement(0).readTypedArray(); }, |
| 70 |
/base type .* is not an 8-bit or 16-bit integer or character type/); |
| 71 |
|
| 72 |
// Wide string -> bytes -> UTF-8 encoded string in typed array |
| 73 |
|
| 74 |
const input2 = "千千千千千千千千千千千千千千千千千千千千千千千千千"; |
| 75 |
const encoded = ctypes.char.array(input2.length * 3)(input2).readTypedArray(); |
| 76 |
// Each 千 character is encoded as 3 bytes. |
| 77 |
assertEq(encoded.length, input2.length * 3); |
| 78 |
|
| 79 |
// Wide string -> char16_t -> 16-bit encoded string in typed array |
| 80 |
|
| 81 |
const encoded16 = ctypes.char16_t.array(input2.length)(input2).readTypedArray(); |
| 82 |
assertEq(encoded16.length, input2.length); |
| 83 |
|
| 84 |
// Floats |
| 85 |
|
| 86 |
const floats = ctypes.float.array(3)([10, 20, 30]).readTypedArray(); |
| 87 |
assertEq(floats instanceof Float32Array, true); |
| 88 |
assertEq(floats.toString(), "10,20,30"); |
| 89 |
|
| 90 |
const doubles = ctypes.double.array(3)([10, 20, 30]).readTypedArray(); |
| 91 |
assertEq(doubles instanceof Float64Array, true); |
| 92 |
assertEq(doubles.toString(), "10,20,30"); |
| 93 |
|
| 94 |
// Invalid |
| 95 |
|
| 96 |
assertTypeErrorMessage(() => { ctypes.int64_t.array(3)([10, 20, 30]).readTypedArray() }, |
| 97 |
/base type ctypes.int64_t.array.*is not compatible with a typed array element type/); |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
if (typeof ctypes === "object") |
| 102 |
test(); |