JS_ValueToString

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_ValueToString
  • Revision title: JS_ValueToString
  • Revision id: 120531
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment 13 words added, 5 words removed

Revision Content

Convert a jsval to a JSString.

Syntax

JSString * JS_ValueToString(JSContext *cx, jsval v);
Name Type Description
cx JSContext * The context in which to perform the conversion. {{ Jsapi-requires-request() }}
v jsval The value to convert.

Description

JS_ValueToString converts a specified JavaScript value, v, to a string. It behaves like the JavaScript expression ""+v.

This function implements the ToString operator specified in {{ Es3_spec("9") }}. If v is an object, the actual conversion is performed by its JSClass.convert callback, which may call the JavaScript methods v.toString() and/or v.valueOf().

On success, JS_ValueToString returns a pointer to a string. On error or exception, it returns NULL. This happens, for example, if v is an object and v.toString() throws an exception.

The resulting JSString is subject to garbage collection. Protect it using a local root, an object property, or the JS_AddRoot function.

See Also

{{ LXRSearch("ident", "i", "JS_ValueToString") }}

JS_ConvertArguments, JS_ConvertValue, JS_GetTypeName, JS_TypeOfValue, JS_ValueToBoolean, JS_ValueToFunction, JS_ValueToInt32, JS_ValueToNumber, JS_ValueToObject

Revision Source

<p>Convert a <code>jsval</code> to a <code>JSString</code>.
</p>
<h2 name="Syntax"> Syntax </h2>
<pre class="eval"><a href="/en/SpiderMonkey/JSAPI_Reference/JSString" title="en/JSString">JSString</a> * <strong>JS_ValueToString</strong>(<a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/JSRuntime">JSContext</a> *cx, <a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a> v);
</pre>
<table class="fullwidth-table">
<tbody><tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td><code>cx</code></td>
<td><code><a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/JSRuntime">JSContext</a> *</code></td>
<td>The context in which to perform the conversion.
{{ Jsapi-requires-request() }}</td>
</tr>
<tr>
<td><code>v</code></td>
<td><code><a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a></code></td>
<td>The value to convert.</td>
</tr>
</tbody></table>
<h2 name="Description">Description</h2>
<p><strong><code>JS_ValueToString</code></strong> converts a specified JavaScript value, <code>v</code>, to a string. It behaves like the JavaScript expression <code>""+v</code>.</p>
<p>This function implements the ToString operator specified in {{ Es3_spec("9") }}. If <code>v</code> is an object, the actual conversion is performed by its <code><a href="/en/SpiderMonkey/JSAPI_Reference/JSObjectOps.defaultValue" title="en/JSObjectOps.defaultValue">JSClass.convert</a></code> callback, which may call the JavaScript methods <code>v.toString()</code> and/or <code>v.valueOf()</code>.</p>
<p>On success, <code>JS_ValueToString</code> returns a pointer to a string. On error or exception, it returns <code>NULL</code>. This happens, for example, if <code>v</code> is an object and <code>v.toString()</code> throws an exception.</p>
<p>The resulting <code>JSString</code> is subject to garbage collection. Protect it using a local root, an object property, or the <code><a href="/en/SpiderMonkey/JSAPI_Reference/JS_AddRoot" title="en/JS_AddRoot">JS_AddRoot</a></code> function.</p><h2 name="See_Also"> See Also </h2>
<p>{{ LXRSearch("ident", "i", "JS_ValueToString") }}
</p><p><a href="/en/SpiderMonkey/JSAPI_Reference/JS_ConvertArguments" title="en/JS_ConvertArguments">JS_ConvertArguments</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_ConvertValue" title="en/JS_ConvertValue">JS_ConvertValue</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_GetTypeName" title="en/JS_GetTypeName">JS_GetTypeName</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_TypeOfValue" title="en/JS_TypeOfValue">JS_TypeOfValue</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_ValueToBoolean" title="en/JS_ValueToBoolean">JS_ValueToBoolean</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_ValueToFunction" title="en/JS_ValueToFunction">JS_ValueToFunction</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_ValueToInt32" title="en/JS_ValueToInt32">JS_ValueToInt32</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_ValueToNumber" title="en/JS_ValueToNumber">JS_ValueToNumber</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_ValueToObject" title="en/JS_ValueToObject">JS_ValueToObject</a>
</p>
Revert to this revision