Esempio n. 1
0
JS_XDRStringOrNull(JSXDRState *xdr, JSString **strp)
{
    uint32 null = (*strp == NULL);
    if (!JS_XDRUint32(xdr, &null))
        return JS_FALSE;
    if (null) {
        *strp = NULL;
        return JS_TRUE;
    }
    return JS_XDRString(xdr, strp);
}
Esempio n. 2
0
extern JSBool
js_XDRStringAtom(JSXDRState *xdr, JSAtom **atomp)
{
    JSString *str;
    uint32 nchars;
    JSAtom *atom;
    JSContext *cx;
    jschar *chars;
    jschar stackChars[256];

    if (xdr->mode == JSXDR_ENCODE) {
        JS_ASSERT(ATOM_IS_STRING(*atomp));
        str = ATOM_TO_STRING(*atomp);
        return JS_XDRString(xdr, &str);
    }

    /*
     * Inline JS_XDRString when decoding to avoid JSString allocation
     * for already existing atoms. See bug 321985.
     */
    if (!JS_XDRUint32(xdr, &nchars))
        return JS_FALSE;
    atom = NULL;
    cx = xdr->cx;
    if (nchars <= JS_ARRAY_LENGTH(stackChars)) {
        chars = stackChars;
    } else {
        /*
         * This is very uncommon. Don't use the tempPool arena for this as
         * most allocations here will be bigger than tempPool's arenasize.
         */
        chars = (jschar *) cx->malloc(nchars * sizeof(jschar));
        if (!chars)
            return JS_FALSE;
    }

    if (XDRChars(xdr, chars, nchars))
        atom = js_AtomizeChars(cx, chars, nchars, 0);
    if (chars != stackChars)
        cx->free(chars);

    if (!atom)
        return JS_FALSE;
    *atomp = atom;
    return JS_TRUE;
}
Esempio n. 3
0
extern JSBool
js_XDRStringAtom(JSXDRState *xdr, JSAtom **atomp)
{
    JSString *str;
    uint32 nchars;
    JSAtom *atom;
    JSContext *cx;
    void *mark;
    jschar *chars;

    if (xdr->mode == JSXDR_ENCODE) {
        JS_ASSERT(ATOM_IS_STRING(*atomp));
        str = ATOM_TO_STRING(*atomp);
        return JS_XDRString(xdr, &str);
    }

    /*
     * Inline JS_XDRString when decoding to avoid JSString allocation
     * for already existing atoms. See bug 321985.
     */
    if (!JS_XDRUint32(xdr, &nchars))
        return JS_FALSE;
    atom = NULL;
    cx = xdr->cx;
    mark = JS_ARENA_MARK(&cx->tempPool);
    JS_ARENA_ALLOCATE_CAST(chars, jschar *, &cx->tempPool,
                           nchars * sizeof(jschar));
    if (!chars)
        JS_ReportOutOfMemory(cx);
    else if (XDRChars(xdr, chars, nchars))
        atom = js_AtomizeChars(cx, chars, nchars, 0);
    JS_ARENA_RELEASE(&cx->tempPool, mark);
    if (!atom)
        return JS_FALSE;
    *atomp = atom;
    return JS_TRUE;
}
Esempio n. 4
0
static JSBool
XDRValueBody(JSXDRState *xdr, uint32 type, jsval *vp)
{
    switch (type) {
      case JSVAL_XDRNULL:
        *vp = JSVAL_NULL;
        break;
      case JSVAL_XDRVOID:
        *vp = JSVAL_VOID;
        break;
      case JSVAL_STRING: {
        JSString *str;
        if (xdr->mode == JSXDR_ENCODE)
            str = JSVAL_TO_STRING(*vp);
        if (!JS_XDRString(xdr, &str))
            return JS_FALSE;
        if (xdr->mode == JSXDR_DECODE)
            *vp = STRING_TO_JSVAL(str);
        break;
      }
      case JSVAL_DOUBLE: {
        jsdouble *dp;
        if (xdr->mode == JSXDR_ENCODE)
            dp = JSVAL_TO_DOUBLE(*vp);
        if (!JS_XDRDouble(xdr, &dp))
            return JS_FALSE;
        if (xdr->mode == JSXDR_DECODE)
            *vp = DOUBLE_TO_JSVAL(dp);
        break;
      }
      case JSVAL_OBJECT: {
        JSObject *obj;
        if (xdr->mode == JSXDR_ENCODE)
            obj = JSVAL_TO_OBJECT(*vp);
        if (!js_XDRObject(xdr, &obj))
            return JS_FALSE;
        if (xdr->mode == JSXDR_DECODE)
            *vp = OBJECT_TO_JSVAL(obj);
        break;
      }
      case JSVAL_BOOLEAN: {
        uint32 b;
        if (xdr->mode == JSXDR_ENCODE)
            b = (uint32) JSVAL_TO_BOOLEAN(*vp);
        if (!JS_XDRUint32(xdr, &b))
            return JS_FALSE;
        if (xdr->mode == JSXDR_DECODE)
            *vp = BOOLEAN_TO_JSVAL((JSBool) b);
        break;
      }
      default: {
        uint32 i;

        JS_ASSERT(type & JSVAL_INT);
        if (xdr->mode == JSXDR_ENCODE)
            i = (uint32) JSVAL_TO_INT(*vp);
        if (!JS_XDRUint32(xdr, &i))
            return JS_FALSE;
        if (xdr->mode == JSXDR_DECODE)
            *vp = INT_TO_JSVAL((int32) i);
        break;
      }
    }
    return JS_TRUE;
}