You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

25 lines
1.5 KiB

<h1 id="ctypes">API C types</h1>
<p>Duktape API uses typedef-wrapped C types such as <code>duk_int_t</code>,
<code>duk_idx_t</code>, and <code>duk_ret_t</code> almost exclusively to ensure
portability to exotic platforms. On most platforms these map directly to signed
or unsigned <code>int</code>; the wrappers make it possible to support platforms
where usual type assumptions (like having a 32-bit <code>int</code>) don't hold.
See Wiki article <a href="http://wiki.duktape.org/ApiCTypes.html">API C types</a>
for a detailed discussion.</p>
<p>Summary of best practices:</p>
<ul>
<li>Use Duktape API types such as <code>duk_idx_t</code> and <code>duk_ret_t</code>
when declaring variables for maximum portability. Alternatively you may
use plain types (like <code>long</code>) but your code will be less portable
and you may need to use casts to avoid warnings.</li>
<li>In <code>printf()</code> and <code>duk_push_sprintf()</code> formatting
cast Duktape types to a wide integer type and use a standard format specifier.
Example: <code>printf("Result: %ld\n", (long) duk_get_int(ctx, -3));</code>.</li>
<li>Use the <code>L</code> (or <code>UL</code>) suffix for constants which
are larger than 16 bits to maximize portability. Like the <code>int</code>
type, integer constants without a suffix are only guaranteed to be 16 bits
wide. With the <code>L</code> suffix constants are guaranteed to be at least
32 bits wide. Example: <code>duk_push_int(ctx, 1234567L);</code>.</li>
</ul>