Determines if a specified C integer is safe to pass to INT_TO_JSVAL.


#define INT_FITS_IN_JSVAL(i)  /* ... */
Name Type Description
i jsint The C integer value to check.


Determines if a specified C integer value, i, lies within the range allowed for integer jsvals. If so, INT_FITS_IN_JSVAL returns TRUE, and i can be cast to jsval by calling INT_TO_JSVAL(i). Otherwise, INT_FITS_IN_JSVAL returns FALSE. In this case, the application can still convert i to a jsval using JS_NewNumberValue.

Warning: The type of the argument i must be jsint, but the C/C++ compiler does not enforce type safety here. If i is an unsigned or 64-bit value, INT_FITS_IN_JSVAL can silently give the wrong answer.

JavaScript 1.8.5 note

Starting in SpiderMonkey 1.8.5, jsval can store a full 32-bit integer, so this check isn't needed any longer for 32-bit integers.


The following code snippet illustrates how a C variable, item, is conditionally tested in an if statement to see if it is a legal jsval integer value.

if (INT_FITS_IN_JSVAL(item)) {
    /* ... */
} else {
    JS_ReportError(cx, "Integer out of range: %d", item);


Document Tags and Contributors

Contributors to this page: Sheppy, Nickolay, Callek, MMondor, Dria, Jorend
Last updated by: Sheppy,