Debian Codegraph @

js/src/jit-test/tests/ctypes/conversion-array.js

raw
1 // Type conversion error should report its type.
2
3 load(libdir + 'asserts.js');
4
5 function test() {
6 // constructor
7 assertTypeErrorMessage(() => { ctypes.int32_t.array()("foo"); },
8 "can't convert the string \"foo\" to the type ctypes.int32_t.array()");
9 assertTypeErrorMessage(() => { ctypes.int32_t.array(10)("foo"); },
10 "can't convert the string \"foo\" to the type ctypes.int32_t.array(10)");
11 assertTypeErrorMessage(() => { ctypes.char.array(2)("foo"); },
12 "length of the string \"foo\" does not fit in the length of the type ctypes.char.array(2) (expected 2 or lower, got 3)");
13 assertTypeErrorMessage(() => { ctypes.char16_t.array(2)("foo"); },
14 "length of the string \"foo\" does not fit in the length of the type ctypes.char16_t.array(2) (expected 2 or lower, got 3)");
15 assertTypeErrorMessage(() => { ctypes.int8_t.array(2)(new ArrayBuffer(8)); },
16 "length of the array buffer ({}) does not match to the length of the type ctypes.int8_t.array(2) (expected 2, got 8)");
17 assertTypeErrorMessage(() => { ctypes.int8_t.array(2)(new Int8Array(8)); },
18 "length of the typed array ({0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0}) does not match to the length of the type ctypes.int8_t.array(2) (expected 2, got 8)");
19
20 // elem setter
21 assertTypeErrorMessage(() => { ctypes.int32_t.array(10)()[0] = "foo"; },
22 "can't convert the string \"foo\" to element 0 of the type ctypes.int32_t.array(10)");
23 assertTypeErrorMessage(() => { ctypes.int32_t.array(10)()[1] = "foo"; },
24 "can't convert the string \"foo\" to element 1 of the type ctypes.int32_t.array(10)");
25
26 // value setter
27 assertTypeErrorMessage(() => { ctypes.int32_t.array(1)().value = ["foo"]; },
28 "can't convert the string \"foo\" to element 0 of the type ctypes.int32_t.array(1)");
29 assertTypeErrorMessage(() => { ctypes.int32_t.array(1)().value = [2, "foo"]; },
30 "length of the array [2, \"foo\"] does not match to the length of the type ctypes.int32_t.array(1) (expected 1, got 2)");
31 assertTypeErrorMessage(() => { ctypes.int32_t.array(2)().value = [2, "foo"]; },
32 "can't convert the string \"foo\" to element 1 of the type ctypes.int32_t.array(2)");
33 }
34
35 if (typeof ctypes === "object")
36 test();