Debian Codegraph @

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

raw
1 // Type conversion error should report its type.
2
3 load(libdir + 'asserts.js');
4
5 function test() {
6 let test_struct = ctypes.StructType("test_struct", [{ "x": ctypes.int32_t },
7 { "bar": ctypes.int32_t }]);
8
9 // constructor
10 assertTypeErrorMessage(() => { new test_struct("foo"); },
11 "can't convert the string \"foo\" to the type test_struct");
12 assertTypeErrorMessage(() => { new test_struct("foo", "x"); },
13 "can't convert the string \"foo\" to the 'x' field (int32_t) of test_struct");
14 assertTypeErrorMessage(() => { new test_struct({ "x": "foo", "bar": 1 }); },
15 "can't convert the string \"foo\" to the 'x' field (int32_t) of test_struct");
16 assertTypeErrorMessage(() => { new test_struct({ 0: 1, "bar": 1 }); },
17 "property name the number 0 of the object ({0:1, bar:1}) is not a string");
18
19 // field setter
20 let struct_val = test_struct();
21 assertTypeErrorMessage(() => { struct_val.x = "foo"; },
22 "can't convert the string \"foo\" to the 'x' field (int32_t) of test_struct");
23 assertTypeErrorMessage(() => { struct_val.bar = "foo"; },
24 "can't convert the string \"foo\" to the 'bar' field (int32_t) of test_struct");
25
26 // value setter
27 assertTypeErrorMessage(() => { struct_val.value = { "x": "foo" }; },
28 "property count of the object ({x:\"foo\"}) does not match to field count of the type test_struct (expected 2, got 1)");
29 assertTypeErrorMessage(() => { struct_val.value = { "x": "foo", "bar": 1 }; },
30 "can't convert the string \"foo\" to the 'x' field (int32_t) of test_struct");
31 assertTypeErrorMessage(() => { struct_val.value = "foo"; },
32 "can't convert the string \"foo\" to the type test_struct");
33 }
34
35 if (typeof ctypes === "object")
36 test();