Пример #1
0
char*
enc_string(JSContext* cx, jsval arg, size_t* buflen)
{
    JSString* str = NULL;
    jschar* src = NULL;
    char* bytes = NULL;
    size_t srclen = 0;
    size_t byteslen = 0;

    str = JS_ValueToString(cx, arg);
    if(!str) goto error;

    src = JS_GetStringChars(str);
    srclen = JS_GetStringLength(str);

    if(!enc_charbuf(src, srclen, NULL, &byteslen)) goto error;

    bytes = JS_malloc(cx, (byteslen) + 1);
    bytes[byteslen] = 0;

    if(!enc_charbuf(src, srclen, bytes, &byteslen)) goto error;

    if(buflen) *buflen = byteslen;
    goto success;

error:
    if(bytes != NULL) JS_free(cx, bytes);
    bytes = NULL;

success:
    return bytes;
}
Пример #2
0
char*
enc_string(JSContext* cx, jsval arg, size_t* buflen)
{
    JSString* str = NULL;
    const jschar* src = NULL;
    char* bytes = NULL;
    size_t srclen = 0;
    size_t byteslen = 0;
    
    str = JS_ValueToString(cx, arg);
    if(!str) goto error;

#ifdef HAVE_JS_GET_STRING_CHARS_AND_LENGTH
    src = JS_GetStringCharsAndLength(cx, str, &srclen);
#else
    src = JS_GetStringChars(str);
    srclen = JS_GetStringLength(str);
#endif

    if(!enc_charbuf(src, srclen, NULL, &byteslen)) goto error;
    
    bytes = JS_malloc(cx, (byteslen) + 1);
    bytes[byteslen] = 0;
    
    if(!enc_charbuf(src, srclen, bytes, &byteslen)) goto error;

    if(buflen) *buflen = byteslen;
    goto success;

error:
    if(bytes != NULL) JS_free(cx, bytes);
    bytes = NULL;

success:
    return bytes;
}