638
edits
(finally cope with arrays sanely in ImplicitConvert) |
|||
| Line 12: | Line 12: | ||
:If ''rtype'' is an array type, this throws a <code>TypeError</code>. | :If ''rtype'' is an array type, this throws a <code>TypeError</code>. | ||
:If any ''argtypeN'' is an array type, the result is the same as if it had been the corresponding pointer type, <code>''argtypeN''.elementType.ptr</code>. ''(Rationale: This is how C and C++ treat array types in function declarations.)'' | |||
= Types = | = Types = | ||
| Line 395: | Line 397: | ||
<code>'''ImplicitConvert(''val'', ''t'')'''</code> - Convert the JavaScript value ''val'' to a C/C++ value of type ''t''. This is called whenever a JavaScript value of any kind is passed to a parameter of a ctypes-declared function, passed to <code>''cdata''.value = ''val''</code>, or assigned to an array element or struct member, as in <code>''carray''[''i''] = ''val''</code> or <code>''cstruct''.''member'' = ''val''</code>. This function is intended to lose precision only when there is no reasonable alternative. It generally does not coerce values of one type to another type. | <code>'''ImplicitConvert(''val'', ''t'')'''</code> - Convert the JavaScript value ''val'' to a C/C++ value of type ''t''. This is called whenever a JavaScript value of any kind is passed to a parameter of a ctypes-declared function, passed to <code>''cdata''.value = ''val''</code>, or assigned to an array element or struct member, as in <code>''carray''[''i''] = ''val''</code> or <code>''cstruct''.''member'' = ''val''</code>. This function is intended to lose precision only when there is no reasonable alternative. It generally does not coerce values of one type to another type. | ||
''t'' must not be <code>void</code>. | ''t'' must not be <code>void</code> or an array type with unspecified length. ''(Rationale: C/C++ variables and parameters cannot have such types. The parameter of a function declared <code>int f(int x[])</code> is <code>int *</code>, not <code>int[]</code>.)'' | ||
* First, if ''val'' is a <code>CData</code> object of type ''u'' and <code>SameType(''t'', ''u'')</code>, return the current value of the C/C++ object referred to by ''val''. Otherwise the behavior depends on the target type ''t''. | * First, if ''val'' is a <code>CData</code> object of type ''u'' and <code>SameType(''t'', ''u'')</code>, return the current value of the C/C++ object referred to by ''val''. Otherwise the behavior depends on the target type ''t''. | ||
| Line 436: | Line 438: | ||
:* If ''val'' is a <code>CData</code> object of array type and either ''t'' is <code>ctypes.voidptr_t</code> or the target type of ''t'' is the element type of the array, return a pointer to the first element of the array. | :* If ''val'' is a <code>CData</code> object of array type and either ''t'' is <code>ctypes.voidptr_t</code> or the target type of ''t'' is the element type of the array, return a pointer to the first element of the array. | ||
:* If ''t'' is <code>ctypes.voidptr_t</code> and ''val'' is a <code>CData</code> object of pointer type, return the value of the C/C++ pointer in ''val'', cast to <code>void *</code>. | :* If ''t'' is <code>ctypes.voidptr_t</code> and ''val'' is a <code>CData</code> object of pointer type, return the value of the C/C++ pointer in ''val'', cast to <code>void *</code>. | ||
:* Otherwise fail. ''(Rationale: We don't convert strings to pointers yet partly because we're lazy and partly because it would implicitly cast away const. We don't convert JavaScript arrays to pointers because this would have to allocate a C array implicitly, raising issues about who should deallocate it, and when, and how they know it's their responsibility.)'' | |||
:* Otherwise fail. ''(We don't convert strings to pointers yet | |||
* If ''t'' is an array type: | * If ''t'' is an array type: | ||
:* If ''val'' is not a JavaScript object, fail. ''(We could reasonably convert JS strings to arrays of char types, but let's skip it for now.)'' | :* If ''val'' is not a JavaScript object, fail. ''(We could reasonably convert JS strings to arrays of char types, but let's skip it for now.)'' | ||
:* If <code>''val''.length</code> is not a nonnegative integer, fail. | :* If <code>''val''.length</code> is not a nonnegative integer, fail. | ||
:* If | :* If <code>''val''.length !== ''t''.length</code>, fail. | ||
:* Otherwise, the result is a C/C++ array of <code>''val''.length</code> elements of type <code>''t''.elementType</code>. Element ''i'' of the result is <code>ImplicitConvert(''val''[''i''], ''t''.elementType)</code>. | :* Otherwise, the result is a C/C++ array of <code>''val''.length</code> elements of type <code>''t''.elementType</code>. Element ''i'' of the result is <code>ImplicitConvert(''val''[''i''], ''t''.elementType)</code>. | ||
edits