JS_XDRString(JSXDRState *xdr, JSString **strp) { uint32 nchars; jschar *chars; if (xdr->mode == JSXDR_ENCODE) nchars = JSSTRING_LENGTH(*strp); if (!JS_XDRUint32(xdr, &nchars)) return JS_FALSE; if (xdr->mode == JSXDR_DECODE) { chars = (jschar *) JS_malloc(xdr->cx, (nchars + 1) * sizeof(jschar)); if (!chars) return JS_FALSE; } else { chars = JSSTRING_CHARS(*strp); } if (!XDRChars(xdr, chars, nchars)) goto bad; if (xdr->mode == JSXDR_DECODE) { chars[nchars] = 0; *strp = JS_NewUCString(xdr->cx, chars, nchars); if (!*strp) goto bad; } return JS_TRUE; bad: if (xdr->mode == JSXDR_DECODE) JS_free(xdr->cx, chars); return JS_FALSE; }
JS_XDRString(JSXDRState *xdr, JSString **strp) { uint32 nchars; jschar *chars; if (xdr->mode == JSXDR_ENCODE) nchars = (*strp)->length(); if (!JS_XDRUint32(xdr, &nchars)) return JS_FALSE; if (xdr->mode == JSXDR_DECODE) { chars = (jschar *) xdr->cx->malloc((nchars + 1) * sizeof(jschar)); if (!chars) return JS_FALSE; } else { chars = (*strp)->chars(); } if (!XDRChars(xdr, chars, nchars)) goto bad; if (xdr->mode == JSXDR_DECODE) { chars[nchars] = 0; *strp = JS_NewUCString(xdr->cx, chars, nchars); if (!*strp) goto bad; } return JS_TRUE; bad: if (xdr->mode == JSXDR_DECODE) xdr->cx->free(chars); return JS_FALSE; }
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; }
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; }