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.4 KiB

name: duk_to_int
proto: |
duk_int_t duk_to_int(duk_context *ctx, duk_int_t 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>duk_int_t</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>
<div include="ref-custom-type-coercion.html" />
<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 = (double) 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>DUK_INT_MAX</code> will be returned from the API call.
</div>
example: |
printf("ToInteger() + int coercion: %ld\n", (long) duk_to_int(ctx, -3));
printf("ToInteger() coercion: %lf\n", (double) duk_get_number(ctx, -3));
tags:
- stack
introduced: 1.0.0