Jsctypes/api: Difference between revisions

Jump to navigation Jump to search
1,630 bytes added ,  21 October 2009
Line 666: Line 666:


=Future directions=
=Future directions=
* 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.
==Callbacks==
 
The libffi part of this is presumably not too bad. Issues:
 
'''Lifetimes.''' C/C++ makes it impossible to track an object pointer. Both JavaScript's GC and experience with C/C++ function pointers will tend to discourage users from caring about function lifetimes.
 
I think the best solution to this problem is to put the burden of keeping the function alive entirely on the client.
 
'''Finding the right context to use.''' If we burn the cx right into the libffi closure, it will crash when called from a different thread or after the cx is destroyed. If we take a context at random from some internal JSAPI structure, it might be thread-safe, but the context's options and global will be random, which sounds dangerous. Perhaps ctypes itself can create a context per thread, on demand, for the use of function pointers. In a typical application, that would only create one context, if any.


==Converting strings==
==Converting strings==
Line 673: Line 681:
I think we want an explicit API for converting strings, very roughly:
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>CData</code> objects of certain pointer and array types have methods for reading and writing Unicode strings. These methods are present if the target or element type is an 8-bit character or integer type.


'''<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>''cdata''.readString(''[encoding[, length]]'')</code>''' - Read bytes from ''cdata'' and convert them to Unicode characters using the specified ''encoding'', returning a string. Specifically:
* If ''cdata'' is an array, let ''p'' = a pointer to the first element. Otherwise ''cdata'' is a pointer; let ''p'' = the value of ''cdata''.
* If ''encoding'' is <code>undefined</code> or omitted, the selected encoding is UTF-8. Otherwise, if ''encoding'' is a string naming a known character encoding, that encoding is selected. Otherwise throw a <code>TypeError</code>.
* If ''length'' is a size value, ''cdata'' is an array, and <code>''length'' &gt; <code>''cdata''.length</code>, then throw a <code>TypeError</code>.
* Otherwise, if ''length'' is a size value, take exactly ''length'' bytes starting at ''p'' and convert them to Unicode characters according to the selected encoding. ''(Open issue: Error handling.)'' Return a JavaScript string containing the Unicode characters, represented in UTF-16.
* Otherwise, if ''length'' is <code>undefined</code> or omitted, convert bytes starting at ''p'' to Unicode characters according to the selected encoding. Stop when the end of the array is reached (if ''cdata'' is an array) or when a null character (U+0000) is found. ''(Open issue: Error handling.)'' Return a JavaScript string containing the Unicode characters, represented in UTF-16.
* Otherwise throw a <code>TypeError</code>.


'''<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.
'''<code>''cdata''.writeString(''s'', ''[encoding[, length]]'')</code>''' - Determine the starting pointer ''p'' as above. If ''s'' is not a well-formed UTF-16 string, throw a <code>TypeError</code>. ''(Open issue: Error handling.)'' Otherwise convert ''s'' to bytes in the specified ''encoding'' (default: UTF-8) and write at most ''length'' - 1 bytes, or all the converted bytes, if ''length'' is <code>undefined</code> or omitted, to memory starting at ''p''. Write a converted null character after the data. Return the number of bytes of data written, not counting the terminating null character.


The following methods exist on <code>CData</code> objects of array-of-8-bit-character types:
''(Open issue: As proposed, these are not suitable for working with encodings where a zero byte might not indicate the end of text. For example, a string encoded in UTF-16 will typically contain a lot of zero bytes. Unfortunately, in the case of readString, the underlying library demands the length up front.)''


'''<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.
''(Open issue: These methods offer no error handling options, which is pretty weak. Real-world code often wants to allow a few characters to be garbled rather than fail. For now we will likely be limited to whatever the underlying codec library, <code>nsIScriptableUnicodeConverter</code>, can do.)''


'''<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: 16-bit versions too, for UTF-16?)''


''(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==
==isNull==
638

edits

Navigation menu