JavaScript-1.5 Reference

JS_ConvertValue Function

Summary

Converts a JS value to a value of a specific JS type.

Syntax

JSBool JS_ConvertValue(JSContext *cx, jsval v, JSType type,
    jsval *vp);
NameTypeDescription
cxJSContext *Pointer to a JS context from which to derive runtime information.
vjsvalThe JS value to convert.
typeJSTypeThe type to which to convert the value. type must be one of JSTYPE_VOID, JSTYPE_OBJECT, JSTYPE_FUNCTION, JSTYPE_STRING, JSTYPE_NUMBER, or JSTYPE_BOOLEAN. Otherwise JS_ConvertValue reports an error.
vpjsval *Pointer to the JS value that contains the converted value when the function returns.

Description

JS_ConvertValue converts a specified JS value, v, to a specified JS type, type. The converted value is stored in the jsval pointed to by vp. Typically users of this function set vp to point to v, so that if conversion is successful, v now contains the converted value.

JS_ConvertValue calls other, type-specific conversion routines based on what you specify in type. These include JS_ValueToFunction, JS_ValueToString, JS_ValueToNumber, and JS_ValueToBoolean.

Converting any JS value to JSTYPE_VOID always succeeds.

Converting to JSTYPE_OBJECT is successful if the JS value to convert is one of JSVAL_INT, JSVAL_DOUBLE, JSVAL_STRING, JSVAL_BOOLEAN, or JSVAL_OBJECT.

Converting to JSTYPE_FUNCTION is successful if the JS value to convert is an object for which a function class has been defined, or if the JS value is already a function.

Converting any JS value to JSTYPE_STRING always succeeds.

Converting a JS value to JSTYPE_NUMBER succeeds if the JS value to convert is a JSVAL_INT, JSVAL_DOUBLE, or JSVAL_BOOLEAN. If the JS value is a JSVAL_STRING that contains numeric values and signs only, conversion also succeeds. If the JS value is a JSVAL_OBJECT, conversion is successful if the object supports its own conversion function.

Converting any JS value to JSTYPE_BOOLEAN always succeeds, except when the JS value is a JSVAL_OBJECT that does not support its own conversion routine.

If the conversion is successful, JS_ConvertValue returns JS_TRUE, and vp points to the converted value. Otherwise, it returns JS_FALSE, and vp is either undefined, or points to the current value of v, depending on how you implement your code.

Notes

Converting a JS value from one type to another does not change the actual data value stored in the item.

See Also

Groups [ Function ]
Documents [ LXR ID Search ]
Entries [ JS_ConvertArguments | JS_GetTypeName | JS_TypeOfValue | JS_ValueToBoolean | JS_ValueToFunction | JS_ValueToInt32 | JS_ValueToNumber | JS_ValueToObject | JS_ValueToString ]

This page was generated by APIDOC