Note: JS_NewString() was removed in SpiderMonkey 1.8.5. Use JS_NewStringCopyN and JS_NewUCStringCopyN instead.

Syntax

JSString *
JS_NewUCString(JSContext *cx, char16_t *chars, size_t length);

JSString *
JS_NewString(JSContext *cx, char *buf, size_t length); // Obsolete since JSAPI 1.8.5
Name Type Description
cx JSContext * The context in which to create the new string. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
buf char * or char16_t * Pointer to a character array, allocated with JS_malloc, containing the characters for the JS string to create. The JavaScript engine adopts the buffer.
length size_t Number of characters in the text string.

Description

JS_NewString creates and returns a new string, using the memory starting at buf and ending at buf + length as the character storage. JS_NewUCString is the Unicode version of the function.

The character array, buf, must be allocated on the heap using JS_malloc. On success, the JavaScript engine adopts responsibility for memory management of this region. The application must not read, write, or free the buffer. This allows the JavaScript engine to avoid needless data copying.

On success, JS_NewString and JS_NewUCString return a pointer to the new string. On error or exception, they return NULL.

See Also