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.
 
 
 
 
 
 

42 lines
1.5 KiB

=proto
int duk_to_int(duk_context *ctx, int index);
=stack
[ ... val! ... ] -> [ ... ToNumber(val)! ... ]
=summary
<p>Replace the value at <code>index</code> with an Ecmascript
<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.4">ToInteger()</a>
coerced value. Returns an <code>int</code> further coerced from the <code>ToInteger()</code>
result using the algorithm described in
<code><a href="#duk_get_int">duk_get_int()</a></code> (this second coercion is not
reflected in the new stack value). If <code>index</code> is invalid, throws an error.</p>
<p>Custom type coercion:</p>
<ul>
<li>Buffer: <code>0</code> for zero-size buffer, <code>1</code> otherwise</li>
<li>Pointer: <code>0</code> for <code>NULL</code> pointer, <code>1</code> otherwise</li>
</ul>
<p>If you want the <code>double</code> version of the <code>ToInteger()</code> coerced value,
use:</p>
<pre class="c-code">
double d;
(void) duk_to_int(ctx, -3);
d = duk_get_number(ctx, -3);
</pre>
<div class="note">
<code>duk_get_int()</code> int coercion is applied only to the return value, it is not
reflected on the value stack. For instance, if value stack contains the string
<code>"Infinity"</code>, the value on the stack will be coerced to the number
<code>Infinity</code> and <code>INT_MAX</code> will be returned from the API call.
</div>
=example
printf("ToInteger() + int coercion: %d\n", duk_to_int(ctx, -3));
printf("ToInteger() coercion: %lf\n", duk_get_number(ctx, -3));
=tags
stack