PageRenderTime 216ms CodeModel.GetById 156ms app.highlight 54ms RepoModel.GetById 1ms app.codeStats 1ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jsxdrapi.cpp

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C++ | 800 lines | 663 code | 74 blank | 63 comment | 185 complexity | 7c67e0668f44f3d819284097e5b0cc7b MD5 | raw file
  1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2 *
  3 * ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is Mozilla Communicator client code, released
 17 * March 31, 1998.
 18 *
 19 * The Initial Developer of the Original Code is
 20 * Netscape Communications Corporation.
 21 * Portions created by the Initial Developer are Copyright (C) 1998
 22 * the Initial Developer. All Rights Reserved.
 23 *
 24 * Contributor(s):
 25 *
 26 * Alternatively, the contents of this file may be used under the terms of
 27 * either of the GNU General Public License Version 2 or later (the "GPL"),
 28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 29 * in which case the provisions of the GPL or the LGPL are applicable instead
 30 * of those above. If you wish to allow use of your version of this file only
 31 * under the terms of either the GPL or the LGPL, and not to allow others to
 32 * use your version of this file under the terms of the MPL, indicate your
 33 * decision by deleting the provisions above and replace them with the notice
 34 * and other provisions required by the GPL or the LGPL. If you do not delete
 35 * the provisions above, a recipient may use your version of this file under
 36 * the terms of any one of the MPL, the GPL or the LGPL.
 37 *
 38 * ***** END LICENSE BLOCK ***** */
 39#include "jsstddef.h"
 40#include "jsversion.h"
 41
 42#if JS_HAS_XDR
 43
 44#include <string.h>
 45#include "jstypes.h"
 46#include "jsutil.h" /* Added by JSIFY */
 47#include "jsdhash.h"
 48#include "jsprf.h"
 49#include "jsapi.h"
 50#include "jscntxt.h"
 51#include "jsnum.h"
 52#include "jsobj.h"              /* js_XDRObject */
 53#include "jsscript.h"           /* js_XDRScript */
 54#include "jsstr.h"
 55#include "jsxdrapi.h"
 56
 57#ifdef DEBUG
 58#define DBG(x) x
 59#else
 60#define DBG(x) ((void)0)
 61#endif
 62
 63typedef struct JSXDRMemState {
 64    JSXDRState  state;
 65    char        *base;
 66    uint32      count;
 67    uint32      limit;
 68} JSXDRMemState;
 69
 70#define MEM_BLOCK       8192
 71#define MEM_PRIV(xdr)   ((JSXDRMemState *)(xdr))
 72
 73#define MEM_BASE(xdr)   (MEM_PRIV(xdr)->base)
 74#define MEM_COUNT(xdr)  (MEM_PRIV(xdr)->count)
 75#define MEM_LIMIT(xdr)  (MEM_PRIV(xdr)->limit)
 76
 77#define MEM_LEFT(xdr, bytes)                                                  \
 78    JS_BEGIN_MACRO                                                            \
 79        if ((xdr)->mode == JSXDR_DECODE &&                                    \
 80            MEM_COUNT(xdr) + bytes > MEM_LIMIT(xdr)) {                        \
 81            JS_ReportErrorNumber((xdr)->cx, js_GetErrorMessage, NULL,         \
 82                                 JSMSG_END_OF_DATA);                          \
 83            return 0;                                                         \
 84        }                                                                     \
 85    JS_END_MACRO
 86
 87#define MEM_NEED(xdr, bytes)                                                  \
 88    JS_BEGIN_MACRO                                                            \
 89        if ((xdr)->mode == JSXDR_ENCODE) {                                    \
 90            if (MEM_LIMIT(xdr) &&                                             \
 91                MEM_COUNT(xdr) + bytes > MEM_LIMIT(xdr)) {                    \
 92                uint32 limit_ = JS_ROUNDUP(MEM_COUNT(xdr) + bytes, MEM_BLOCK);\
 93                void *data_ = JS_realloc((xdr)->cx, MEM_BASE(xdr), limit_);   \
 94                if (!data_)                                                   \
 95                    return 0;                                                 \
 96                MEM_BASE(xdr) = (char *) data_;                               \
 97                MEM_LIMIT(xdr) = limit_;                                      \
 98            }                                                                 \
 99        } else {                                                              \
100            MEM_LEFT(xdr, bytes);                                             \
101        }                                                                     \
102    JS_END_MACRO
103
104#define MEM_DATA(xdr)        ((void *)(MEM_BASE(xdr) + MEM_COUNT(xdr)))
105#define MEM_INCR(xdr,bytes)  (MEM_COUNT(xdr) += (bytes))
106
107static JSBool
108mem_get32(JSXDRState *xdr, uint32 *lp)
109{
110    MEM_LEFT(xdr, 4);
111    *lp = *(uint32 *)MEM_DATA(xdr);
112    MEM_INCR(xdr, 4);
113    return JS_TRUE;
114}
115
116static JSBool
117mem_set32(JSXDRState *xdr, uint32 *lp)
118{
119    MEM_NEED(xdr, 4);
120    *(uint32 *)MEM_DATA(xdr) = *lp;
121    MEM_INCR(xdr, 4);
122    return JS_TRUE;
123}
124
125static JSBool
126mem_getbytes(JSXDRState *xdr, char *bytes, uint32 len)
127{
128    MEM_LEFT(xdr, len);
129    memcpy(bytes, MEM_DATA(xdr), len);
130    MEM_INCR(xdr, len);
131    return JS_TRUE;
132}
133
134static JSBool
135mem_setbytes(JSXDRState *xdr, char *bytes, uint32 len)
136{
137    MEM_NEED(xdr, len);
138    memcpy(MEM_DATA(xdr), bytes, len);
139    MEM_INCR(xdr, len);
140    return JS_TRUE;
141}
142
143static void *
144mem_raw(JSXDRState *xdr, uint32 len)
145{
146    void *data;
147    if (xdr->mode == JSXDR_ENCODE) {
148        MEM_NEED(xdr, len);
149    } else if (xdr->mode == JSXDR_DECODE) {
150        MEM_LEFT(xdr, len);
151    }
152    data = MEM_DATA(xdr);
153    MEM_INCR(xdr, len);
154    return data;
155}
156
157static JSBool
158mem_seek(JSXDRState *xdr, int32 offset, JSXDRWhence whence)
159{
160    switch (whence) {
161      case JSXDR_SEEK_CUR:
162        if ((int32)MEM_COUNT(xdr) + offset < 0) {
163            JS_ReportErrorNumber(xdr->cx, js_GetErrorMessage, NULL,
164                                 JSMSG_SEEK_BEYOND_START);
165            return JS_FALSE;
166        }
167        if (offset > 0)
168            MEM_NEED(xdr, offset);
169        MEM_COUNT(xdr) += offset;
170        return JS_TRUE;
171      case JSXDR_SEEK_SET:
172        if (offset < 0) {
173            JS_ReportErrorNumber(xdr->cx, js_GetErrorMessage, NULL,
174                                 JSMSG_SEEK_BEYOND_START);
175            return JS_FALSE;
176        }
177        if (xdr->mode == JSXDR_ENCODE) {
178            if ((uint32)offset > MEM_COUNT(xdr))
179                MEM_NEED(xdr, offset - MEM_COUNT(xdr));
180            MEM_COUNT(xdr) = offset;
181        } else {
182            if ((uint32)offset > MEM_LIMIT(xdr)) {
183                JS_ReportErrorNumber(xdr->cx, js_GetErrorMessage, NULL,
184                                     JSMSG_SEEK_BEYOND_END);
185                return JS_FALSE;
186            }
187            MEM_COUNT(xdr) = offset;
188        }
189        return JS_TRUE;
190      case JSXDR_SEEK_END:
191        if (offset >= 0 ||
192            xdr->mode == JSXDR_ENCODE ||
193            (int32)MEM_LIMIT(xdr) + offset < 0) {
194            JS_ReportErrorNumber(xdr->cx, js_GetErrorMessage, NULL,
195                                 JSMSG_END_SEEK);
196            return JS_FALSE;
197        }
198        MEM_COUNT(xdr) = MEM_LIMIT(xdr) + offset;
199        return JS_TRUE;
200      default: {
201        char numBuf[12];
202        JS_snprintf(numBuf, sizeof numBuf, "%d", whence);
203        JS_ReportErrorNumber(xdr->cx, js_GetErrorMessage, NULL,
204                             JSMSG_WHITHER_WHENCE, numBuf);
205        return JS_FALSE;
206      }
207    }
208}
209
210static uint32
211mem_tell(JSXDRState *xdr)
212{
213    return MEM_COUNT(xdr);
214}
215
216static void
217mem_finalize(JSXDRState *xdr)
218{
219    JS_free(xdr->cx, MEM_BASE(xdr));
220}
221
222static JSXDROps xdrmem_ops = {
223    mem_get32,      mem_set32,      mem_getbytes,   mem_setbytes,
224    mem_raw,        mem_seek,       mem_tell,       mem_finalize
225};
226
227JS_PUBLIC_API(void)
228JS_XDRInitBase(JSXDRState *xdr, JSXDRMode mode, JSContext *cx)
229{
230    xdr->mode = mode;
231    xdr->cx = cx;
232    xdr->registry = NULL;
233    xdr->numclasses = xdr->maxclasses = 0;
234    xdr->reghash = NULL;
235    xdr->userdata = NULL;
236    xdr->script = NULL;
237}
238
239JS_PUBLIC_API(JSXDRState *)
240JS_XDRNewMem(JSContext *cx, JSXDRMode mode)
241{
242    JSXDRState *xdr = (JSXDRState *) JS_malloc(cx, sizeof(JSXDRMemState));
243    if (!xdr)
244        return NULL;
245    JS_XDRInitBase(xdr, mode, cx);
246    if (mode == JSXDR_ENCODE) {
247        if (!(MEM_BASE(xdr) = (char *) JS_malloc(cx, MEM_BLOCK))) {
248            JS_free(cx, xdr);
249            return NULL;
250        }
251    } else {
252        /* XXXbe ok, so better not deref MEM_BASE(xdr) if not ENCODE */
253        MEM_BASE(xdr) = NULL;
254    }
255    xdr->ops = &xdrmem_ops;
256    MEM_COUNT(xdr) = 0;
257    MEM_LIMIT(xdr) = MEM_BLOCK;
258    return xdr;
259}
260
261JS_PUBLIC_API(void *)
262JS_XDRMemGetData(JSXDRState *xdr, uint32 *lp)
263{
264    if (xdr->ops != &xdrmem_ops)
265        return NULL;
266    *lp = MEM_COUNT(xdr);
267    return MEM_BASE(xdr);
268}
269
270JS_PUBLIC_API(void)
271JS_XDRMemSetData(JSXDRState *xdr, void *data, uint32 len)
272{
273    if (xdr->ops != &xdrmem_ops)
274        return;
275    MEM_LIMIT(xdr) = len;
276    MEM_BASE(xdr) = (char *) data;
277    MEM_COUNT(xdr) = 0;
278}
279
280JS_PUBLIC_API(uint32)
281JS_XDRMemDataLeft(JSXDRState *xdr)
282{
283    if (xdr->ops != &xdrmem_ops)
284        return 0;
285    return MEM_LIMIT(xdr) - MEM_COUNT(xdr);
286}
287
288JS_PUBLIC_API(void)
289JS_XDRMemResetData(JSXDRState *xdr)
290{
291    if (xdr->ops != &xdrmem_ops)
292        return;
293    MEM_COUNT(xdr) = 0;
294}
295
296JS_PUBLIC_API(void)
297JS_XDRDestroy(JSXDRState *xdr)
298{
299    JSContext *cx = xdr->cx;
300    xdr->ops->finalize(xdr);
301    if (xdr->registry) {
302        JS_free(cx, xdr->registry);
303        if (xdr->reghash)
304            JS_DHashTableDestroy((JSDHashTable *) xdr->reghash);
305    }
306    JS_free(cx, xdr);
307}
308
309JS_PUBLIC_API(JSBool)
310JS_XDRUint8(JSXDRState *xdr, uint8 *b)
311{
312    uint32 l = *b;
313    if (!JS_XDRUint32(xdr, &l))
314        return JS_FALSE;
315    *b = (uint8) l;
316    return JS_TRUE;
317}
318
319JS_PUBLIC_API(JSBool)
320JS_XDRUint16(JSXDRState *xdr, uint16 *s)
321{
322    uint32 l = *s;
323    if (!JS_XDRUint32(xdr, &l))
324        return JS_FALSE;
325    *s = (uint16) l;
326    return JS_TRUE;
327}
328
329JS_PUBLIC_API(JSBool)
330JS_XDRUint32(JSXDRState *xdr, uint32 *lp)
331{
332    JSBool ok = JS_TRUE;
333    if (xdr->mode == JSXDR_ENCODE) {
334        uint32 xl = JSXDR_SWAB32(*lp);
335        ok = xdr->ops->set32(xdr, &xl);
336    } else if (xdr->mode == JSXDR_DECODE) {
337        ok = xdr->ops->get32(xdr, lp);
338        *lp = JSXDR_SWAB32(*lp);
339    }
340    return ok;
341}
342
343JS_PUBLIC_API(JSBool)
344JS_XDRBytes(JSXDRState *xdr, char *bytes, uint32 len)
345{
346    uint32 padlen;
347    static char padbuf[JSXDR_ALIGN-1];
348
349    if (xdr->mode == JSXDR_ENCODE) {
350        if (!xdr->ops->setbytes(xdr, bytes, len))
351            return JS_FALSE;
352    } else {
353        if (!xdr->ops->getbytes(xdr, bytes, len))
354            return JS_FALSE;
355    }
356    len = xdr->ops->tell(xdr);
357    if (len % JSXDR_ALIGN) {
358        padlen = JSXDR_ALIGN - (len % JSXDR_ALIGN);
359        if (xdr->mode == JSXDR_ENCODE) {
360            if (!xdr->ops->setbytes(xdr, padbuf, padlen))
361                return JS_FALSE;
362        } else {
363            if (!xdr->ops->seek(xdr, padlen, JSXDR_SEEK_CUR))
364                return JS_FALSE;
365        }
366    }
367    return JS_TRUE;
368}
369
370/**
371 * Convert between a C string and the XDR representation:
372 * leading 32-bit count, then counted vector of chars,
373 * then possibly \0 padding to multiple of 4.
374 */
375JS_PUBLIC_API(JSBool)
376JS_XDRCString(JSXDRState *xdr, char **sp)
377{
378    uint32 len;
379
380    if (xdr->mode == JSXDR_ENCODE)
381        len = strlen(*sp);
382    JS_XDRUint32(xdr, &len);
383    if (xdr->mode == JSXDR_DECODE) {
384        if (!(*sp = (char *) JS_malloc(xdr->cx, len + 1)))
385            return JS_FALSE;
386    }
387    if (!JS_XDRBytes(xdr, *sp, len)) {
388        if (xdr->mode == JSXDR_DECODE)
389            JS_free(xdr->cx, *sp);
390        return JS_FALSE;
391    }
392    if (xdr->mode == JSXDR_DECODE) {
393        (*sp)[len] = '\0';
394    } else if (xdr->mode == JSXDR_FREE) {
395        JS_free(xdr->cx, *sp);
396        *sp = NULL;
397    }
398    return JS_TRUE;
399}
400
401JS_PUBLIC_API(JSBool)
402JS_XDRCStringOrNull(JSXDRState *xdr, char **sp)
403{
404    uint32 null = (*sp == NULL);
405    if (!JS_XDRUint32(xdr, &null))
406        return JS_FALSE;
407    if (null) {
408        *sp = NULL;
409        return JS_TRUE;
410    }
411    return JS_XDRCString(xdr, sp);
412}
413
414static JSBool
415XDRChars(JSXDRState *xdr, jschar *chars, uint32 nchars)
416{
417    uint32 i, padlen, nbytes;
418    jschar *raw;
419
420    nbytes = nchars * sizeof(jschar);
421    padlen = nbytes % JSXDR_ALIGN;
422    if (padlen) {
423        padlen = JSXDR_ALIGN - padlen;
424        nbytes += padlen;
425    }
426    if (!(raw = (jschar *) xdr->ops->raw(xdr, nbytes)))
427        return JS_FALSE;
428    if (xdr->mode == JSXDR_ENCODE) {
429        for (i = 0; i != nchars; i++)
430            raw[i] = JSXDR_SWAB16(chars[i]);
431        if (padlen)
432            memset((char *)raw + nbytes - padlen, 0, padlen);
433    } else if (xdr->mode == JSXDR_DECODE) {
434        for (i = 0; i != nchars; i++)
435            chars[i] = JSXDR_SWAB16(raw[i]);
436    }
437    return JS_TRUE;
438}
439
440/*
441 * Convert between a JS (Unicode) string and the XDR representation.
442 */
443JS_PUBLIC_API(JSBool)
444JS_XDRString(JSXDRState *xdr, JSString **strp)
445{
446    uint32 nchars;
447    jschar *chars;
448
449    if (xdr->mode == JSXDR_ENCODE)
450        nchars = JSSTRING_LENGTH(*strp);
451    if (!JS_XDRUint32(xdr, &nchars))
452        return JS_FALSE;
453
454    if (xdr->mode == JSXDR_DECODE) {
455        chars = (jschar *) JS_malloc(xdr->cx, (nchars + 1) * sizeof(jschar));
456        if (!chars)
457            return JS_FALSE;
458    } else {
459        chars = JSSTRING_CHARS(*strp);
460    }
461
462    if (!XDRChars(xdr, chars, nchars))
463        goto bad;
464    if (xdr->mode == JSXDR_DECODE) {
465        chars[nchars] = 0;
466        *strp = JS_NewUCString(xdr->cx, chars, nchars);
467        if (!*strp)
468            goto bad;
469    }
470    return JS_TRUE;
471
472bad:
473    if (xdr->mode == JSXDR_DECODE)
474        JS_free(xdr->cx, chars);
475    return JS_FALSE;
476}
477
478JS_PUBLIC_API(JSBool)
479JS_XDRStringOrNull(JSXDRState *xdr, JSString **strp)
480{
481    uint32 null = (*strp == NULL);
482    if (!JS_XDRUint32(xdr, &null))
483        return JS_FALSE;
484    if (null) {
485        *strp = NULL;
486        return JS_TRUE;
487    }
488    return JS_XDRString(xdr, strp);
489}
490
491static JSBool
492XDRDoubleValue(JSXDRState *xdr, jsdouble *dp)
493{
494    jsdpun u;
495
496    if (xdr->mode == JSXDR_ENCODE)
497        u.d = *dp;
498    if (!JS_XDRUint32(xdr, &u.s.lo) || !JS_XDRUint32(xdr, &u.s.hi))
499        return JS_FALSE;
500    if (xdr->mode == JSXDR_DECODE)
501        *dp = u.d;
502    return JS_TRUE;
503}
504
505JS_PUBLIC_API(JSBool)
506JS_XDRDouble(JSXDRState *xdr, jsdouble **dpp)
507{
508    jsdouble d;
509
510    if (xdr->mode == JSXDR_ENCODE)
511        d = **dpp;
512    if (!XDRDoubleValue(xdr, &d))
513        return JS_FALSE;
514    if (xdr->mode == JSXDR_DECODE) {
515        *dpp = JS_NewDouble(xdr->cx, d);
516        if (!*dpp)
517            return JS_FALSE;
518    }
519    return JS_TRUE;
520}
521
522/* These are magic pseudo-tags: see jsapi.h, near the top, for real tags. */
523#define JSVAL_XDRNULL   0x8
524#define JSVAL_XDRVOID   0xA
525
526static JSBool
527XDRValueBody(JSXDRState *xdr, uint32 type, jsval *vp)
528{
529    switch (type) {
530      case JSVAL_XDRNULL:
531        *vp = JSVAL_NULL;
532        break;
533      case JSVAL_XDRVOID:
534        *vp = JSVAL_VOID;
535        break;
536      case JSVAL_STRING: {
537        JSString *str;
538        if (xdr->mode == JSXDR_ENCODE)
539            str = JSVAL_TO_STRING(*vp);
540        if (!JS_XDRString(xdr, &str))
541            return JS_FALSE;
542        if (xdr->mode == JSXDR_DECODE)
543            *vp = STRING_TO_JSVAL(str);
544        break;
545      }
546      case JSVAL_DOUBLE: {
547        jsdouble *dp;
548        if (xdr->mode == JSXDR_ENCODE)
549            dp = JSVAL_TO_DOUBLE(*vp);
550        if (!JS_XDRDouble(xdr, &dp))
551            return JS_FALSE;
552        if (xdr->mode == JSXDR_DECODE)
553            *vp = DOUBLE_TO_JSVAL(dp);
554        break;
555      }
556      case JSVAL_OBJECT: {
557        JSObject *obj;
558        if (xdr->mode == JSXDR_ENCODE)
559            obj = JSVAL_TO_OBJECT(*vp);
560        if (!js_XDRObject(xdr, &obj))
561            return JS_FALSE;
562        if (xdr->mode == JSXDR_DECODE)
563            *vp = OBJECT_TO_JSVAL(obj);
564        break;
565      }
566      case JSVAL_BOOLEAN: {
567        uint32 b;
568        if (xdr->mode == JSXDR_ENCODE)
569            b = (uint32) JSVAL_TO_BOOLEAN(*vp);
570        if (!JS_XDRUint32(xdr, &b))
571            return JS_FALSE;
572        if (xdr->mode == JSXDR_DECODE)
573            *vp = BOOLEAN_TO_JSVAL(!!b);
574        break;
575      }
576      default: {
577        uint32 i;
578
579        JS_ASSERT(type & JSVAL_INT);
580        if (xdr->mode == JSXDR_ENCODE)
581            i = (uint32) JSVAL_TO_INT(*vp);
582        if (!JS_XDRUint32(xdr, &i))
583            return JS_FALSE;
584        if (xdr->mode == JSXDR_DECODE)
585            *vp = INT_TO_JSVAL((int32) i);
586        break;
587      }
588    }
589    return JS_TRUE;
590}
591
592JS_PUBLIC_API(JSBool)
593JS_XDRValue(JSXDRState *xdr, jsval *vp)
594{
595    uint32 type;
596
597    if (xdr->mode == JSXDR_ENCODE) {
598        if (JSVAL_IS_NULL(*vp))
599            type = JSVAL_XDRNULL;
600        else if (JSVAL_IS_VOID(*vp))
601            type = JSVAL_XDRVOID;
602        else
603            type = JSVAL_TAG(*vp);
604    }
605    return JS_XDRUint32(xdr, &type) && XDRValueBody(xdr, type, vp);
606}
607
608JSBool
609js_XDRAtom(JSXDRState *xdr, JSAtom **atomp)
610{
611    jsval v;
612    uint32 type;
613    jsdouble d;
614
615    if (xdr->mode == JSXDR_ENCODE) {
616        v = ATOM_KEY(*atomp);
617        return JS_XDRValue(xdr, &v);
618    }
619
620    /*
621     * Inline JS_XDRValue when decoding to avoid ceation of GC things when
622     * then corresponding atom already exists. See bug 321985.
623     */
624    if (!JS_XDRUint32(xdr, &type))
625        return JS_FALSE;
626    if (type == JSVAL_STRING)
627        return js_XDRStringAtom(xdr, atomp);
628
629    if (type == JSVAL_DOUBLE) {
630        if (!XDRDoubleValue(xdr, &d))
631            return JS_FALSE;
632        *atomp = js_AtomizeDouble(xdr->cx, d);
633        return *atomp != NULL;
634    }
635
636    return XDRValueBody(xdr, type, &v) &&
637           js_AtomizePrimitiveValue(xdr->cx, v, atomp);
638}
639
640extern JSBool
641js_XDRStringAtom(JSXDRState *xdr, JSAtom **atomp)
642{
643    JSString *str;
644    uint32 nchars;
645    JSAtom *atom;
646    JSContext *cx;
647    jschar *chars;
648    jschar stackChars[256];
649
650    if (xdr->mode == JSXDR_ENCODE) {
651        JS_ASSERT(ATOM_IS_STRING(*atomp));
652        str = ATOM_TO_STRING(*atomp);
653        return JS_XDRString(xdr, &str);
654    }
655
656    /*
657     * Inline JS_XDRString when decoding to avoid JSString allocation
658     * for already existing atoms. See bug 321985.
659     */
660    if (!JS_XDRUint32(xdr, &nchars))
661        return JS_FALSE;
662    atom = NULL;
663    cx = xdr->cx;
664    if (nchars <= JS_ARRAY_LENGTH(stackChars)) {
665        chars = stackChars;
666    } else {
667        /*
668         * This is very uncommon. Don't use the tempPool arena for this as
669         * most allocations here will be bigger than tempPool's arenasize.
670         */
671        chars = (jschar *) JS_malloc(cx, nchars * sizeof(jschar));
672        if (!chars)
673            return JS_FALSE;
674    }
675
676    if (XDRChars(xdr, chars, nchars))
677        atom = js_AtomizeChars(cx, chars, nchars, 0);
678    if (chars != stackChars)
679        JS_free(cx, chars);
680
681    if (!atom)
682        return JS_FALSE;
683    *atomp = atom;
684    return JS_TRUE;
685}
686
687JS_PUBLIC_API(JSBool)
688JS_XDRScript(JSXDRState *xdr, JSScript **scriptp)
689{
690    if (!js_XDRScript(xdr, scriptp, NULL))
691        return JS_FALSE;
692    if (xdr->mode == JSXDR_DECODE)
693        js_CallNewScriptHook(xdr->cx, *scriptp, NULL);
694    return JS_TRUE;
695}
696
697#define CLASS_REGISTRY_MIN      8
698#define CLASS_INDEX_TO_ID(i)    ((i)+1)
699#define CLASS_ID_TO_INDEX(id)   ((id)-1)
700
701typedef struct JSRegHashEntry {
702    JSDHashEntryHdr hdr;
703    const char      *name;
704    uint32          index;
705} JSRegHashEntry;
706
707JS_PUBLIC_API(JSBool)
708JS_XDRRegisterClass(JSXDRState *xdr, JSClass *clasp, uint32 *idp)
709{
710    uintN numclasses, maxclasses;
711    JSClass **registry;
712
713    numclasses = xdr->numclasses;
714    maxclasses = xdr->maxclasses;
715    if (numclasses == maxclasses) {
716        maxclasses = (maxclasses == 0) ? CLASS_REGISTRY_MIN : maxclasses << 1;
717        registry = (JSClass **)
718            JS_realloc(xdr->cx, xdr->registry, maxclasses * sizeof(JSClass *));
719        if (!registry)
720            return JS_FALSE;
721        xdr->registry = registry;
722        xdr->maxclasses = maxclasses;
723    } else {
724        JS_ASSERT(numclasses && numclasses < maxclasses);
725        registry = xdr->registry;
726    }
727
728    registry[numclasses] = clasp;
729    if (xdr->reghash) {
730        JSRegHashEntry *entry = (JSRegHashEntry *)
731            JS_DHashTableOperate((JSDHashTable *) xdr->reghash,
732                                 clasp->name, JS_DHASH_ADD);
733        if (!entry) {
734            JS_ReportOutOfMemory(xdr->cx);
735            return JS_FALSE;
736        }
737        entry->name = clasp->name;
738        entry->index = numclasses;
739    }
740    *idp = CLASS_INDEX_TO_ID(numclasses);
741    xdr->numclasses = ++numclasses;
742    return JS_TRUE;
743}
744
745JS_PUBLIC_API(uint32)
746JS_XDRFindClassIdByName(JSXDRState *xdr, const char *name)
747{
748    uintN i, numclasses;
749
750    numclasses = xdr->numclasses;
751    if (numclasses >= 10) {
752        JSRegHashEntry *entry;
753
754        /* Bootstrap reghash from registry on first overpopulated Find. */
755        if (!xdr->reghash) {
756            xdr->reghash =
757                JS_NewDHashTable(JS_DHashGetStubOps(), NULL,
758                                 sizeof(JSRegHashEntry),
759                                 JS_DHASH_DEFAULT_CAPACITY(numclasses));
760            if (xdr->reghash) {
761                for (i = 0; i < numclasses; i++) {
762                    JSClass *clasp = xdr->registry[i];
763                    entry = (JSRegHashEntry *)
764                        JS_DHashTableOperate((JSDHashTable *) xdr->reghash,
765                                             clasp->name, JS_DHASH_ADD);
766                    entry->name = clasp->name;
767                    entry->index = i;
768                }
769            }
770        }
771
772        /* If we managed to create reghash, use it for O(1) Find. */
773        if (xdr->reghash) {
774            entry = (JSRegHashEntry *)
775                JS_DHashTableOperate((JSDHashTable *) xdr->reghash,
776                                     name, JS_DHASH_LOOKUP);
777            if (JS_DHASH_ENTRY_IS_BUSY(&entry->hdr))
778                return CLASS_INDEX_TO_ID(entry->index);
779        }
780    }
781
782    /* Only a few classes, or we couldn't malloc reghash: use linear search. */
783    for (i = 0; i < numclasses; i++) {
784        if (!strcmp(name, xdr->registry[i]->name))
785            return CLASS_INDEX_TO_ID(i);
786    }
787    return 0;
788}
789
790JS_PUBLIC_API(JSClass *)
791JS_XDRFindClassById(JSXDRState *xdr, uint32 id)
792{
793    uintN i = CLASS_ID_TO_INDEX(id);
794
795    if (i >= xdr->numclasses)
796        return NULL;
797    return xdr->registry[i];
798}
799
800#endif /* JS_HAS_XDR */