~ubuntu-branches/ubuntu/raring/gjs/raring

« back to all changes in this revision

Viewing changes to gjs/jsapi-util-string.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-03-13 19:15:08 UTC
  • mfrom: (1.6.7)
  • Revision ID: package-import@ubuntu.com-20120313191508-k44let6s97mb45uv
Tags: 1.31.20-0ubuntu1
* New upstream release.
* Add gir package
* debian/control.in: Require minimum glib 2.31
* Drop all patches since they've been applied upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
        return FALSE;
51
51
    }
52
52
 
53
 
#ifdef HAVE_JS_GETSTRINGCHARS
54
 
    s = JS_GetStringChars(JSVAL_TO_STRING(string_val));
55
 
    s_length = JS_GetStringLength(JSVAL_TO_STRING(string_val));
56
 
#else
57
53
    s = JS_GetStringCharsAndLength(context, JSVAL_TO_STRING(string_val), &s_length);
58
54
    if (s == NULL) {
59
55
        JS_EndRequest(context);
60
56
        return FALSE;
61
57
    }
62
 
#endif
63
58
 
64
59
    utf8_string = g_utf16_to_utf8(s,
65
60
                                  (glong)s_length,
259
254
gjs_string_get_ascii(JSContext       *context,
260
255
                     jsval            value)
261
256
{
262
 
    JSString *str;
 
257
    char *ascii;
263
258
 
264
259
    if (!JSVAL_IS_STRING(value)) {
265
260
        gjs_throw(context, "A string was expected, but value was not a string");
266
261
        return NULL;
267
262
    }
268
263
 
269
 
    str = JSVAL_TO_STRING(value);
270
 
 
271
 
#ifdef HAVE_JS_GETSTRINGBYTES
272
 
    return g_strdup(JS_GetStringBytes(str));
273
 
#else
274
 
    char *ascii;
275
 
    size_t len = JS_GetStringEncodingLength(context, str);
276
 
    if (len == (size_t)(-1))
277
 
        return NULL;
278
 
 
279
 
    ascii = g_malloc((len + 1) * sizeof(char));
280
 
    JS_EncodeStringToBuffer(str, ascii, len);
281
 
    ascii[len] = '\0';
 
264
    gjs_string_get_binary_data(context, value, &ascii, NULL);
 
265
 
282
266
    return ascii;
283
 
#endif
284
267
}
285
268
 
286
269
static JSBool
316
299
                           gsize           *len_p)
317
300
{
318
301
    JSString *str;
 
302
    gsize len;
 
303
    char *bytes;
319
304
 
320
305
    JS_BeginRequest(context);
321
306
 
333
318
 
334
319
    str = JSVAL_TO_STRING(value);
335
320
 
336
 
#ifdef HAVE_JS_GETSTRINGBYTES
337
 
    const char *js_data = JS_GetStringBytes(str);
338
 
 
339
 
    /* GetStringLength returns number of 16-bit jschar;
340
 
     * we stored binary data as 1 byte per jschar
341
 
     */
342
 
    *len_p = JS_GetStringLength(str);
343
 
    *data_p = g_memdup(js_data, *len_p);
344
 
#else
345
 
    *len_p = JS_GetStringEncodingLength(context, str);
346
 
    if (*len_p == (gsize)(-1))
 
321
    len = JS_GetStringEncodingLength(context, str);
 
322
    if (len == (gsize)(-1))
347
323
        return JS_FALSE;
348
324
 
349
 
    *data_p = g_malloc((*len_p + 1) * sizeof(char));
350
 
    JS_EncodeStringToBuffer(str, *data_p, *len_p);
351
 
    (*data_p)[*len_p] = '\0';
352
 
#endif
 
325
    if (data_p) {
 
326
        bytes = g_malloc((len + 1) * sizeof(char));
 
327
        JS_EncodeStringToBuffer(str, bytes, len);
 
328
        bytes[len] = '\0';
 
329
        *data_p = bytes;
 
330
    }
 
331
 
 
332
    if (len_p)
 
333
        *len_p = len;
353
334
 
354
335
    JS_EndRequest(context);
355
336
 
429
410
        goto out;
430
411
    }
431
412
 
432
 
#ifdef HAVE_JS_GETSTRINGCHARS
433
 
    js_data = JS_GetStringChars(JSVAL_TO_STRING(value));
434
 
    *len_p = JS_GetStringLength(JSVAL_TO_STRING(value));
435
 
#else
436
413
    js_data = JS_GetStringCharsAndLength(context, JSVAL_TO_STRING(value), len_p);
437
414
    if (js_data == NULL)
438
415
        goto out;
439
 
#endif
 
416
 
440
417
    *data_p = g_memdup(js_data, sizeof(*js_data)*(*len_p));
441
418
 
442
419
    retval = JS_TRUE;
462
439
                   char           **name_p)
463
440
{
464
441
    jsval id_val;
465
 
    JSString *str;
466
442
 
467
443
    if (!JS_IdToValue(context, id, &id_val))
468
444
        return JS_FALSE;
469
445
 
470
446
    if (JSVAL_IS_STRING(id_val)) {
471
 
        str = JSVAL_TO_STRING(id_val);
472
 
#ifdef HAVE_JS_GETSTRINGBYTES
473
 
        *name_p = g_strdup(JS_GetStringBytes(str));
474
 
#else
475
 
        size_t len = JS_GetStringEncodingLength(context, str);
476
 
        if (len == (size_t)(-1))
477
 
            return JS_FALSE;
478
 
 
479
 
        *name_p = g_malloc((len + 1) * sizeof(char));
480
 
        JS_EncodeStringToBuffer(str, *name_p, len);
481
 
        (*name_p)[len] = '\0';
482
 
#endif
 
447
        gjs_string_get_binary_data(context, id_val, name_p, NULL);
483
448
        return JS_TRUE;
484
449
    } else {
485
450
        *name_p = NULL;