Jsctypes/api: Difference between revisions

Jump to navigation Jump to search
→‎Calling types: Give up and switch to a bulleted list
(→‎Calling types: Add auto-sizing ctypes.jschar.array(10)("hello") and ctypes.char.array(10)("hello"))
(→‎Calling types: Give up and switch to a bulleted list)
Line 189: Line 189:
:If <code>''t''.size</code> is <code>undefined</code>, this throws a <code>TypeError</code>.
:If <code>''t''.size</code> is <code>undefined</code>, this throws a <code>TypeError</code>.


:'''<code>new ''t''(''val'')</code>''' or '''<code>''t''(''val'')</code>''' - If <code>''t''.size</code> is not <code>undefined</code>: Convert ''val'' to type ''t'' by calling <code>ExplicitConvert(''val'', ''t'')</code>, throwing a <code>TypeError</code> if the conversion is impossible. Allocate a new buffer of <code>''t''.size</code> bytes, populated with the converted value. Return a new <code>CData</code> object of type ''t'' referring to the complete object in that buffer. (When ''val'' is a <code>CData</code> object of type ''t'', the behavior is like <code>malloc</code> followed by <code>memcpy</code>.)
:'''<code>new ''t''(''val'')</code>''' or '''<code>''t''(''val'')</code>''' - Create a new <code>CData</code> object as follows:


:If ''t'' is an array type of unspecified length and ''val'' is a size value (defined above): Let ''u'' = <code>ArrayType(''t''.elementType, ''val'')</code> and return <code>new ''u''</code>.
:* If <code>''t''.size</code> is not <code>undefined</code>: Convert ''val'' to type ''t'' by calling <code>ExplicitConvert(''val'', ''t'')</code>, throwing a <code>TypeError</code> if the conversion is impossible. Allocate a new buffer of <code>''t''.size</code> bytes, populated with the converted value. Return a new <code>CData</code> object of type ''t'' referring to the complete object in that buffer. (When ''val'' is a <code>CData</code> object of type ''t'', the behavior is like <code>malloc</code> followed by <code>memcpy</code>.)


:If <code>SameType(''t'', ArrayType(ctypes.jschar))</code> and ''val'' is a string: Return a new <code>CData</code> object of type <code>ArrayType(ctypes.jschar, ''val''.length&nbsp;+&nbsp;1)</code> containing the contents of ''val'' followed by a null character.
:* If ''t'' is an array type of unspecified length:


:If ''t'' is an array type of unspecified length, <code>''t''.elementType</code> is an 8-bit character type, and ''val'' is a string: If ''val'' is not a well-formed UTF-16 string, throw a <code>TypeError</code>. Otherwise, let ''s'' = a sequence of bytes, the result of converting ''val'' from UTF-16 to UTF-8, and let ''n'' = the number of bytes in ''s''. Return a new <code>CData</code> object of type <code>ArrayType(''t''.elementType, ''n'' + 1)</code> containing the bytes in ''s'' followed by a null character.
::* If ''val'' is a size value (defined above): Let ''u'' = <code>ArrayType(''t''.elementType, ''val'')</code> and return <code>new ''u''</code>.


:If ''t'' is an array type of unspecified length and ''val'' is not a size value: If ''val'' is not an object, or it does not have a <code>.length</code> property whose value is a nonnegative integer, throw a <code>TypeError</code>. Otherwise, let ''u'' = <code>ArrayType(''t''.elementType, ''val''.length)</code> and return <code>new ''u''(''val'')</code>.
::* If <code>''t''.elementType</code> is <code>jschar</code> and ''val'' is a string: Return a new <code>CData</code> object of type <code>ArrayType(ctypes.jschar, ''val''.length&nbsp;+&nbsp;1)</code> containing the contents of ''val'' followed by a null character.


:''(Array <code>CData</code> objects created in this way have <code>''cdata''.constructor === ''u''</code>, not ''t''. Rationale: For all <code>CData</code> objects, <code>cdata.constructor.size</code> gives the size in bytes, unless a struct field shadows <code>cdata.constructor</code>.)''
::* If <code>''t''.elementType</code> is an 8-bit character type and ''val'' is a string: If ''val'' is not a well-formed UTF-16 string, throw a <code>TypeError</code>. Otherwise, let ''s'' = a sequence of bytes, the result of converting ''val'' from UTF-16 to UTF-8, and let ''n'' = the number of bytes in ''s''. Return a new <code>CData</code> object of type <code>ArrayType(''t''.elementType, ''n'' + 1)</code> containing the bytes in ''s'' followed by a null character.


:Otherwise, ''t'' is <code>void_t</code>. Throw a <code>TypeError</code>.
::* If ''val'' is an object and <code>''val''.length</code>  is a nonnegative integer, let ''u'' = <code>ArrayType(''t''.elementType, ''val''.length)</code> and return <code>new ''u''(''val'')</code>. ''(Array <code>CData</code> objects created in this way have <code>''cdata''.constructor === ''u''</code>, not ''t''. Rationale: For all <code>CData</code> objects, <code>cdata.constructor.size</code> gives the size in bytes, unless a struct field shadows <code>cdata.constructor</code>.)''
 
::* Otherwise, throw a <code>TypeError</code>.
 
:* Otherwise, ''t'' is <code>void_t</code>. Throw a <code>TypeError</code>.


  let a_t = ctypes.ArrayType(ctypes.int32_t);
  let a_t = ctypes.ArrayType(ctypes.int32_t);
638

edits

Navigation menu