Jsctypes/api: Difference between revisions

2,224 bytes added ,  20 October 2009
(→‎Calling types: Give up and switch to a bulleted list)
Line 668: Line 668:
* Callbacks (JITting native wrappers that conform to a given C/C++ function-pointer type and call a JS function. Finding the right cx to use will be tricky.)
* Callbacks (JITting native wrappers that conform to a given C/C++ function-pointer type and call a JS function. Finding the right cx to use will be tricky.)
* Array slices, a way to get a <code>CData</code> object that acts like a view on a window of an array. E.g. ''carray''.slice(start, stop). Assigning one slice to another would memcpy.
* Array slices, a way to get a <code>CData</code> object that acts like a view on a window of an array. E.g. ''carray''.slice(start, stop). Assigning one slice to another would memcpy.
==Converting strings==
I think we want an explicit API for converting strings, very roughly:
The following methods are avialable on <code>CData</code> objects of pointer-to-8-bit-character types:
'''<code>''cptr''.readString(''[encoding[, length]]'')</code>''' - Return a JS string, the result of reading bytes starting at the byte ''cptr'' points to, converting them to Unicode characters using the specified ''encoding'' (default: UTF-8), and then converting the result to UTF-16. This reads exactly ''length'' bytes, or if ''length'' is missing or <code>undefined</code>, an unlimited number of bytes until a 0 byte is encountered.
'''<code>''cptr''.writeString(''s'', ''[encoding[, length]]'')</code>''' - If ''s'' is not a well-formed UTF-16 string, throw a <code>TypeError</code>. Otherwise convert ''s'' to bytes in the specified ''encoding'' (default: UTF-8) and write at most ''length'' - 1 (or an unlimited number, if ''length'' is missing or <code>undefined</code>) bytes to memory starting at ''cptr''. Write a 0 byte after the data. Return the number of bytes of data written, not counting the terminating 0.
The following methods exist on <code>CData</code> objects of array-of-8-bit-character types:
'''<code>''carray''.readString(''[encoding[, length]]'')</code>''' - Same as <code>''cptr''.readString()</code> except it stops at the end of the array, even if ''length'' is missing or <code>undefined</code> and the array contains no 0 bytes.
'''<code>''carray''.writeString(''s'', ''[encoding[, length]]'')</code>''' - Same as <code>''carray''.writeString()</code> except if ''length'' is missing or <code>undefined</code> it defaults to <code>''carray''.length</code>, and if ''length'' is present but greater than <code>''carray''.length</code>, throw a <code>TypeError</code>.
''(Open issue: this offers no error handling options, which is pretty weak. Real-world code often wants to allow errors rather than die horribly.)''
==isNull==
If we do not convert NULL pointers to JS <code>null</code> (and I may have changed my mind about this) then we need:
'''<code>''cptr''.isNull()</code>''' - Return <code>true</code> if ''cptr''&apos;s value is a null pointer, <code>false</code> otherwise.


==Auto-converting strings==
==Auto-converting strings==
638

edits