1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
16 * The Original Code is Mozilla Communicator client code, released
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.
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.
38 * ***** END LICENSE BLOCK ***** */
39 #include "jsversion.h"
46 #include "jsutil.h" /* Added by JSIFY */
52 #include "jsobj.h" /* js_XDRObject */
53 #include "jsscript.h" /* js_XDRScript */
60 #define DBG(x) ((void)0)
63 typedef struct JSXDRMemState
{
70 #define MEM_BLOCK 8192
71 #define MEM_PRIV(xdr) ((JSXDRMemState *)(xdr))
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)
77 #define MEM_LEFT(xdr, bytes) \
79 if ((xdr)->mode == JSXDR_DECODE && \
80 MEM_COUNT(xdr) + bytes > MEM_LIMIT(xdr)) { \
81 JS_ReportErrorNumber((xdr)->cx, js_GetErrorMessage, NULL, \
87 #define MEM_NEED(xdr, bytes) \
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_ = (xdr)->cx->realloc(MEM_BASE(xdr), limit_); \
96 MEM_BASE(xdr) = (char *) data_; \
97 MEM_LIMIT(xdr) = limit_; \
100 MEM_LEFT(xdr, bytes); \
104 #define MEM_DATA(xdr) ((void *)(MEM_BASE(xdr) + MEM_COUNT(xdr)))
105 #define MEM_INCR(xdr,bytes) (MEM_COUNT(xdr) += (bytes))
108 mem_get32(JSXDRState
*xdr
, uint32
*lp
)
111 *lp
= *(uint32
*)MEM_DATA(xdr
);
117 mem_set32(JSXDRState
*xdr
, uint32
*lp
)
120 *(uint32
*)MEM_DATA(xdr
) = *lp
;
126 mem_getbytes(JSXDRState
*xdr
, char *bytes
, uint32 len
)
129 memcpy(bytes
, MEM_DATA(xdr
), len
);
135 mem_setbytes(JSXDRState
*xdr
, char *bytes
, uint32 len
)
138 memcpy(MEM_DATA(xdr
), bytes
, len
);
144 mem_raw(JSXDRState
*xdr
, uint32 len
)
147 if (xdr
->mode
== JSXDR_ENCODE
) {
149 } else if (xdr
->mode
== JSXDR_DECODE
) {
152 data
= MEM_DATA(xdr
);
158 mem_seek(JSXDRState
*xdr
, int32 offset
, JSXDRWhence whence
)
162 if ((int32
)MEM_COUNT(xdr
) + offset
< 0) {
163 JS_ReportErrorNumber(xdr
->cx
, js_GetErrorMessage
, NULL
,
164 JSMSG_SEEK_BEYOND_START
);
168 MEM_NEED(xdr
, offset
);
169 MEM_COUNT(xdr
) += offset
;
173 JS_ReportErrorNumber(xdr
->cx
, js_GetErrorMessage
, NULL
,
174 JSMSG_SEEK_BEYOND_START
);
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
;
182 if ((uint32
)offset
> MEM_LIMIT(xdr
)) {
183 JS_ReportErrorNumber(xdr
->cx
, js_GetErrorMessage
, NULL
,
184 JSMSG_SEEK_BEYOND_END
);
187 MEM_COUNT(xdr
) = offset
;
192 xdr
->mode
== JSXDR_ENCODE
||
193 (int32
)MEM_LIMIT(xdr
) + offset
< 0) {
194 JS_ReportErrorNumber(xdr
->cx
, js_GetErrorMessage
, NULL
,
198 MEM_COUNT(xdr
) = MEM_LIMIT(xdr
) + offset
;
202 JS_snprintf(numBuf
, sizeof numBuf
, "%d", whence
);
203 JS_ReportErrorNumber(xdr
->cx
, js_GetErrorMessage
, NULL
,
204 JSMSG_WHITHER_WHENCE
, numBuf
);
211 mem_tell(JSXDRState
*xdr
)
213 return MEM_COUNT(xdr
);
217 mem_finalize(JSXDRState
*xdr
)
219 xdr
->cx
->free(MEM_BASE(xdr
));
222 static JSXDROps xdrmem_ops
= {
223 mem_get32
, mem_set32
, mem_getbytes
, mem_setbytes
,
224 mem_raw
, mem_seek
, mem_tell
, mem_finalize
228 JS_XDRInitBase(JSXDRState
*xdr
, JSXDRMode mode
, JSContext
*cx
)
232 xdr
->registry
= NULL
;
233 xdr
->numclasses
= xdr
->maxclasses
= 0;
235 xdr
->userdata
= NULL
;
239 JS_PUBLIC_API(JSXDRState
*)
240 JS_XDRNewMem(JSContext
*cx
, JSXDRMode mode
)
242 JSXDRState
*xdr
= (JSXDRState
*) cx
->malloc(sizeof(JSXDRMemState
));
245 JS_XDRInitBase(xdr
, mode
, cx
);
246 if (mode
== JSXDR_ENCODE
) {
247 if (!(MEM_BASE(xdr
) = (char *) cx
->malloc(MEM_BLOCK
))) {
252 /* XXXbe ok, so better not deref MEM_BASE(xdr) if not ENCODE */
253 MEM_BASE(xdr
) = NULL
;
255 xdr
->ops
= &xdrmem_ops
;
257 MEM_LIMIT(xdr
) = MEM_BLOCK
;
261 JS_PUBLIC_API(void *)
262 JS_XDRMemGetData(JSXDRState
*xdr
, uint32
*lp
)
264 if (xdr
->ops
!= &xdrmem_ops
)
266 *lp
= MEM_COUNT(xdr
);
267 return MEM_BASE(xdr
);
271 JS_XDRMemSetData(JSXDRState
*xdr
, void *data
, uint32 len
)
273 if (xdr
->ops
!= &xdrmem_ops
)
275 MEM_LIMIT(xdr
) = len
;
276 MEM_BASE(xdr
) = (char *) data
;
280 JS_PUBLIC_API(uint32
)
281 JS_XDRMemDataLeft(JSXDRState
*xdr
)
283 if (xdr
->ops
!= &xdrmem_ops
)
285 return MEM_LIMIT(xdr
) - MEM_COUNT(xdr
);
289 JS_XDRMemResetData(JSXDRState
*xdr
)
291 if (xdr
->ops
!= &xdrmem_ops
)
297 JS_XDRDestroy(JSXDRState
*xdr
)
299 JSContext
*cx
= xdr
->cx
;
300 xdr
->ops
->finalize(xdr
);
302 cx
->free(xdr
->registry
);
304 JS_DHashTableDestroy((JSDHashTable
*) xdr
->reghash
);
309 JS_PUBLIC_API(JSBool
)
310 JS_XDRUint8(JSXDRState
*xdr
, uint8
*b
)
313 if (!JS_XDRUint32(xdr
, &l
))
319 JS_PUBLIC_API(JSBool
)
320 JS_XDRUint16(JSXDRState
*xdr
, uint16
*s
)
323 if (!JS_XDRUint32(xdr
, &l
))
329 JS_PUBLIC_API(JSBool
)
330 JS_XDRUint32(JSXDRState
*xdr
, uint32
*lp
)
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
);
343 JS_PUBLIC_API(JSBool
)
344 JS_XDRBytes(JSXDRState
*xdr
, char *bytes
, uint32 len
)
347 static char padbuf
[JSXDR_ALIGN
-1];
349 if (xdr
->mode
== JSXDR_ENCODE
) {
350 if (!xdr
->ops
->setbytes(xdr
, bytes
, len
))
353 if (!xdr
->ops
->getbytes(xdr
, bytes
, len
))
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
))
363 if (!xdr
->ops
->seek(xdr
, padlen
, JSXDR_SEEK_CUR
))
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.
375 JS_PUBLIC_API(JSBool
)
376 JS_XDRCString(JSXDRState
*xdr
, char **sp
)
380 if (xdr
->mode
== JSXDR_ENCODE
)
382 JS_XDRUint32(xdr
, &len
);
383 if (xdr
->mode
== JSXDR_DECODE
) {
384 if (!(*sp
= (char *) xdr
->cx
->malloc(len
+ 1)))
387 if (!JS_XDRBytes(xdr
, *sp
, len
)) {
388 if (xdr
->mode
== JSXDR_DECODE
)
392 if (xdr
->mode
== JSXDR_DECODE
) {
394 } else if (xdr
->mode
== JSXDR_FREE
) {
401 JS_PUBLIC_API(JSBool
)
402 JS_XDRCStringOrNull(JSXDRState
*xdr
, char **sp
)
404 uint32 null
= (*sp
== NULL
);
405 if (!JS_XDRUint32(xdr
, &null
))
411 return JS_XDRCString(xdr
, sp
);
415 XDRChars(JSXDRState
*xdr
, jschar
*chars
, uint32 nchars
)
417 uint32 i
, padlen
, nbytes
;
420 nbytes
= nchars
* sizeof(jschar
);
421 padlen
= nbytes
% JSXDR_ALIGN
;
423 padlen
= JSXDR_ALIGN
- padlen
;
426 if (!(raw
= (jschar
*) xdr
->ops
->raw(xdr
, nbytes
)))
428 if (xdr
->mode
== JSXDR_ENCODE
) {
429 for (i
= 0; i
!= nchars
; i
++)
430 raw
[i
] = JSXDR_SWAB16(chars
[i
]);
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
]);
441 * Convert between a JS (Unicode) string and the XDR representation.
443 JS_PUBLIC_API(JSBool
)
444 JS_XDRString(JSXDRState
*xdr
, JSString
**strp
)
449 if (xdr
->mode
== JSXDR_ENCODE
)
450 nchars
= (*strp
)->length();
451 if (!JS_XDRUint32(xdr
, &nchars
))
454 if (xdr
->mode
== JSXDR_DECODE
) {
455 chars
= (jschar
*) xdr
->cx
->malloc((nchars
+ 1) * sizeof(jschar
));
459 chars
= (*strp
)->chars();
462 if (!XDRChars(xdr
, chars
, nchars
))
464 if (xdr
->mode
== JSXDR_DECODE
) {
466 *strp
= JS_NewUCString(xdr
->cx
, chars
, nchars
);
473 if (xdr
->mode
== JSXDR_DECODE
)
474 xdr
->cx
->free(chars
);
478 JS_PUBLIC_API(JSBool
)
479 JS_XDRStringOrNull(JSXDRState
*xdr
, JSString
**strp
)
481 uint32 null
= (*strp
== NULL
);
482 if (!JS_XDRUint32(xdr
, &null
))
488 return JS_XDRString(xdr
, strp
);
492 XDRDoubleValue(JSXDRState
*xdr
, jsdouble
*dp
)
496 u
.d
= (xdr
->mode
== JSXDR_ENCODE
) ? *dp
: 0.0;
497 if (!JS_XDRUint32(xdr
, &u
.s
.lo
) || !JS_XDRUint32(xdr
, &u
.s
.hi
))
499 if (xdr
->mode
== JSXDR_DECODE
)
504 JS_PUBLIC_API(JSBool
)
505 JS_XDRDouble(JSXDRState
*xdr
, jsdouble
**dpp
)
507 jsdouble d
= (xdr
->mode
== JSXDR_ENCODE
) ? **dpp
: 0.0;
508 if (!XDRDoubleValue(xdr
, &d
))
510 if (xdr
->mode
== JSXDR_DECODE
) {
511 *dpp
= JS_NewDouble(xdr
->cx
, d
);
518 /* These are magic pseudo-tags: see jsapi.h, near the top, for real tags. */
519 #define JSVAL_XDRNULL 0x8
520 #define JSVAL_XDRVOID 0xA
523 XDRValueBody(JSXDRState
*xdr
, uint32 type
, jsval
*vp
)
534 if (xdr
->mode
== JSXDR_ENCODE
)
535 str
= JSVAL_TO_STRING(*vp
);
536 if (!JS_XDRString(xdr
, &str
))
538 if (xdr
->mode
== JSXDR_DECODE
)
539 *vp
= STRING_TO_JSVAL(str
);
543 jsdouble
*dp
= (xdr
->mode
== JSXDR_ENCODE
) ? JSVAL_TO_DOUBLE(*vp
) : NULL
;
544 if (!JS_XDRDouble(xdr
, &dp
))
546 if (xdr
->mode
== JSXDR_DECODE
)
547 *vp
= DOUBLE_TO_JSVAL(dp
);
552 if (xdr
->mode
== JSXDR_ENCODE
)
553 obj
= JSVAL_TO_OBJECT(*vp
);
554 if (!js_XDRObject(xdr
, &obj
))
556 if (xdr
->mode
== JSXDR_DECODE
)
557 *vp
= OBJECT_TO_JSVAL(obj
);
560 case JSVAL_SPECIAL
: {
562 if (xdr
->mode
== JSXDR_ENCODE
)
563 b
= (uint32
) JSVAL_TO_BOOLEAN(*vp
);
564 if (!JS_XDRUint32(xdr
, &b
))
566 if (xdr
->mode
== JSXDR_DECODE
)
567 *vp
= BOOLEAN_TO_JSVAL(!!b
);
573 JS_ASSERT(type
& JSVAL_INT
);
574 if (xdr
->mode
== JSXDR_ENCODE
)
575 i
= (uint32
) JSVAL_TO_INT(*vp
);
576 if (!JS_XDRUint32(xdr
, &i
))
578 if (xdr
->mode
== JSXDR_DECODE
)
579 *vp
= INT_TO_JSVAL((int32
) i
);
586 JS_PUBLIC_API(JSBool
)
587 JS_XDRValue(JSXDRState
*xdr
, jsval
*vp
)
591 if (xdr
->mode
== JSXDR_ENCODE
) {
592 if (JSVAL_IS_NULL(*vp
))
593 type
= JSVAL_XDRNULL
;
594 else if (JSVAL_IS_VOID(*vp
))
595 type
= JSVAL_XDRVOID
;
597 type
= JSVAL_TAG(*vp
);
599 return JS_XDRUint32(xdr
, &type
) && XDRValueBody(xdr
, type
, vp
);
603 js_XDRAtom(JSXDRState
*xdr
, JSAtom
**atomp
)
608 if (xdr
->mode
== JSXDR_ENCODE
) {
609 v
= ATOM_KEY(*atomp
);
610 return JS_XDRValue(xdr
, &v
);
614 * Inline JS_XDRValue when decoding to avoid ceation of GC things when
615 * then corresponding atom already exists. See bug 321985.
617 if (!JS_XDRUint32(xdr
, &type
))
619 if (type
== JSVAL_STRING
)
620 return js_XDRStringAtom(xdr
, atomp
);
622 if (type
== JSVAL_DOUBLE
) {
624 if (!XDRDoubleValue(xdr
, &d
))
626 *atomp
= js_AtomizeDouble(xdr
->cx
, d
);
627 return *atomp
!= NULL
;
630 return XDRValueBody(xdr
, type
, &v
) &&
631 js_AtomizePrimitiveValue(xdr
->cx
, v
, atomp
);
635 js_XDRStringAtom(JSXDRState
*xdr
, JSAtom
**atomp
)
642 jschar stackChars
[256];
644 if (xdr
->mode
== JSXDR_ENCODE
) {
645 JS_ASSERT(ATOM_IS_STRING(*atomp
));
646 str
= ATOM_TO_STRING(*atomp
);
647 return JS_XDRString(xdr
, &str
);
651 * Inline JS_XDRString when decoding to avoid JSString allocation
652 * for already existing atoms. See bug 321985.
654 if (!JS_XDRUint32(xdr
, &nchars
))
658 if (nchars
<= JS_ARRAY_LENGTH(stackChars
)) {
662 * This is very uncommon. Don't use the tempPool arena for this as
663 * most allocations here will be bigger than tempPool's arenasize.
665 chars
= (jschar
*) cx
->malloc(nchars
* sizeof(jschar
));
670 if (XDRChars(xdr
, chars
, nchars
))
671 atom
= js_AtomizeChars(cx
, chars
, nchars
, 0);
672 if (chars
!= stackChars
)
681 JS_PUBLIC_API(JSBool
)
682 JS_XDRScript(JSXDRState
*xdr
, JSScript
**scriptp
)
684 if (!js_XDRScript(xdr
, scriptp
, true, NULL
))
686 if (xdr
->mode
== JSXDR_DECODE
)
687 js_CallNewScriptHook(xdr
->cx
, *scriptp
, NULL
);
691 #define CLASS_REGISTRY_MIN 8
692 #define CLASS_INDEX_TO_ID(i) ((i)+1)
693 #define CLASS_ID_TO_INDEX(id) ((id)-1)
695 typedef struct JSRegHashEntry
{
701 JS_PUBLIC_API(JSBool
)
702 JS_XDRRegisterClass(JSXDRState
*xdr
, JSClass
*clasp
, uint32
*idp
)
704 uintN numclasses
, maxclasses
;
707 numclasses
= xdr
->numclasses
;
708 maxclasses
= xdr
->maxclasses
;
709 if (numclasses
== maxclasses
) {
710 maxclasses
= (maxclasses
== 0) ? CLASS_REGISTRY_MIN
: maxclasses
<< 1;
711 registry
= (JSClass
**)
712 xdr
->cx
->realloc(xdr
->registry
, maxclasses
* sizeof(JSClass
*));
715 xdr
->registry
= registry
;
716 xdr
->maxclasses
= maxclasses
;
718 JS_ASSERT(numclasses
&& numclasses
< maxclasses
);
719 registry
= xdr
->registry
;
722 registry
[numclasses
] = clasp
;
724 JSRegHashEntry
*entry
= (JSRegHashEntry
*)
725 JS_DHashTableOperate((JSDHashTable
*) xdr
->reghash
,
726 clasp
->name
, JS_DHASH_ADD
);
728 JS_ReportOutOfMemory(xdr
->cx
);
731 entry
->name
= clasp
->name
;
732 entry
->index
= numclasses
;
734 *idp
= CLASS_INDEX_TO_ID(numclasses
);
735 xdr
->numclasses
= ++numclasses
;
739 JS_PUBLIC_API(uint32
)
740 JS_XDRFindClassIdByName(JSXDRState
*xdr
, const char *name
)
744 numclasses
= xdr
->numclasses
;
745 if (numclasses
>= 10) {
746 JSRegHashEntry
*entry
;
748 /* Bootstrap reghash from registry on first overpopulated Find. */
751 JS_NewDHashTable(JS_DHashGetStubOps(), NULL
,
752 sizeof(JSRegHashEntry
),
753 JS_DHASH_DEFAULT_CAPACITY(numclasses
));
755 for (i
= 0; i
< numclasses
; i
++) {
756 JSClass
*clasp
= xdr
->registry
[i
];
757 entry
= (JSRegHashEntry
*)
758 JS_DHashTableOperate((JSDHashTable
*) xdr
->reghash
,
759 clasp
->name
, JS_DHASH_ADD
);
760 entry
->name
= clasp
->name
;
766 /* If we managed to create reghash, use it for O(1) Find. */
768 entry
= (JSRegHashEntry
*)
769 JS_DHashTableOperate((JSDHashTable
*) xdr
->reghash
,
770 name
, JS_DHASH_LOOKUP
);
771 if (JS_DHASH_ENTRY_IS_BUSY(&entry
->hdr
))
772 return CLASS_INDEX_TO_ID(entry
->index
);
776 /* Only a few classes, or we couldn't malloc reghash: use linear search. */
777 for (i
= 0; i
< numclasses
; i
++) {
778 if (!strcmp(name
, xdr
->registry
[i
]->name
))
779 return CLASS_INDEX_TO_ID(i
);
784 JS_PUBLIC_API(JSClass
*)
785 JS_XDRFindClassById(JSXDRState
*xdr
, uint32 id
)
787 uintN i
= CLASS_ID_TO_INDEX(id
);
789 if (i
>= xdr
->numclasses
)
791 return xdr
->registry
[i
];
794 #endif /* JS_HAS_XDR */