/js/src/jsxdrapi.h

http://github.com/zpao/v8monkey · C Header · 239 lines · 122 code · 43 blank · 74 comment · 0 complexity · 06dfa9c00015acef40d6294118c50ace MD5 · raw file

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sw=4 et tw=78:
  3. *
  4. * ***** BEGIN LICENSE BLOCK *****
  5. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is Mozilla Communicator client code, released
  18. * March 31, 1998.
  19. *
  20. * The Initial Developer of the Original Code is
  21. * Netscape Communications Corporation.
  22. * Portions created by the Initial Developer are Copyright (C) 1998
  23. * the Initial Developer. All Rights Reserved.
  24. *
  25. * Contributor(s):
  26. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either of the GNU General Public License Version 2 or later (the "GPL"),
  29. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. #ifndef jsxdrapi_h___
  41. #define jsxdrapi_h___
  42. /*
  43. * JS external data representation interface API.
  44. *
  45. * The XDR system is comprised of three major parts:
  46. *
  47. * - the state serialization/deserialization APIs, which allow consumers
  48. * of the API to serialize JS runtime state (script bytecodes, atom maps,
  49. * object graphs, etc.) for later restoration. These portions
  50. * are implemented in various appropriate files, such as jsscript.c
  51. * for the script portions and jsobj.c for object state.
  52. * - the callback APIs through which the runtime requests an opaque
  53. * representation of a native object, and through which the runtime
  54. * constructs a live native object from an opaque representation. These
  55. * portions are the responsibility of the native object implementor.
  56. * - utility functions for en/decoding of primitive types, such as
  57. * JSStrings. This portion is implemented in jsxdrapi.c.
  58. *
  59. * Spiritually guided by Sun's XDR, where appropriate.
  60. */
  61. #include "jspubtd.h"
  62. #include "jsprvtd.h"
  63. JS_BEGIN_EXTERN_C
  64. /* We use little-endian byteorder for all encoded data */
  65. #if defined IS_LITTLE_ENDIAN
  66. #define JSXDR_SWAB32(x) x
  67. #define JSXDR_SWAB16(x) x
  68. #elif defined IS_BIG_ENDIAN
  69. #define JSXDR_SWAB32(x) (((uint32_t)(x) >> 24) | \
  70. (((uint32_t)(x) >> 8) & 0xff00) | \
  71. (((uint32_t)(x) << 8) & 0xff0000) | \
  72. ((uint32_t)(x) << 24))
  73. #define JSXDR_SWAB16(x) (((uint16_t)(x) >> 8) | ((uint16_t)(x) << 8))
  74. #else
  75. #error "unknown byte order"
  76. #endif
  77. #define JSXDR_ALIGN 4
  78. typedef enum JSXDRMode {
  79. JSXDR_ENCODE,
  80. JSXDR_DECODE
  81. } JSXDRMode;
  82. typedef enum JSXDRWhence {
  83. JSXDR_SEEK_SET,
  84. JSXDR_SEEK_CUR,
  85. JSXDR_SEEK_END
  86. } JSXDRWhence;
  87. typedef struct JSXDROps {
  88. JSBool (*get32)(JSXDRState *, uint32_t *);
  89. JSBool (*set32)(JSXDRState *, uint32_t *);
  90. JSBool (*getbytes)(JSXDRState *, char *, uint32_t);
  91. JSBool (*setbytes)(JSXDRState *, char *, uint32_t);
  92. void * (*raw)(JSXDRState *, uint32_t);
  93. JSBool (*seek)(JSXDRState *, int32_t, JSXDRWhence);
  94. uint32_t (*tell)(JSXDRState *);
  95. void (*finalize)(JSXDRState *);
  96. } JSXDROps;
  97. struct JSXDRState;
  98. namespace js {
  99. class XDRScriptState {
  100. public:
  101. XDRScriptState(JSXDRState *x);
  102. ~XDRScriptState();
  103. JSXDRState *xdr;
  104. const char *filename;
  105. bool filenameSaved;
  106. };
  107. } /* namespace JS */
  108. struct JSXDRState {
  109. JSXDRMode mode;
  110. JSXDROps *ops;
  111. JSContext *cx;
  112. JSClass **registry;
  113. uintN numclasses;
  114. uintN maxclasses;
  115. void *reghash;
  116. void *userdata;
  117. JSScript *script;
  118. js::XDRScriptState *state;
  119. };
  120. extern JS_PUBLIC_API(void)
  121. JS_XDRInitBase(JSXDRState *xdr, JSXDRMode mode, JSContext *cx);
  122. extern JS_PUBLIC_API(JSXDRState *)
  123. JS_XDRNewMem(JSContext *cx, JSXDRMode mode);
  124. extern JS_PUBLIC_API(void *)
  125. JS_XDRMemGetData(JSXDRState *xdr, uint32_t *lp);
  126. extern JS_PUBLIC_API(void)
  127. JS_XDRMemSetData(JSXDRState *xdr, void *data, uint32_t len);
  128. extern JS_PUBLIC_API(uint32_t)
  129. JS_XDRMemDataLeft(JSXDRState *xdr);
  130. extern JS_PUBLIC_API(void)
  131. JS_XDRMemResetData(JSXDRState *xdr);
  132. extern JS_PUBLIC_API(void)
  133. JS_XDRDestroy(JSXDRState *xdr);
  134. extern JS_PUBLIC_API(JSBool)
  135. JS_XDRUint8(JSXDRState *xdr, uint8_t *b);
  136. extern JS_PUBLIC_API(JSBool)
  137. JS_XDRUint16(JSXDRState *xdr, uint16_t *s);
  138. extern JS_PUBLIC_API(JSBool)
  139. JS_XDRUint32(JSXDRState *xdr, uint32_t *lp);
  140. extern JS_PUBLIC_API(JSBool)
  141. JS_XDRBytes(JSXDRState *xdr, char *bytes, uint32_t len);
  142. extern JS_PUBLIC_API(JSBool)
  143. JS_XDRCString(JSXDRState *xdr, char **sp);
  144. extern JS_PUBLIC_API(JSBool)
  145. JS_XDRCStringOrNull(JSXDRState *xdr, char **sp);
  146. extern JS_PUBLIC_API(JSBool)
  147. JS_XDRString(JSXDRState *xdr, JSString **strp);
  148. extern JS_PUBLIC_API(JSBool)
  149. JS_XDRStringOrNull(JSXDRState *xdr, JSString **strp);
  150. extern JS_PUBLIC_API(JSBool)
  151. JS_XDRDouble(JSXDRState *xdr, jsdouble *dp);
  152. extern JS_PUBLIC_API(JSBool)
  153. JS_XDRValue(JSXDRState *xdr, jsval *vp);
  154. extern JS_PUBLIC_API(JSBool)
  155. JS_XDRFunctionObject(JSXDRState *xdr, JSObject **objp);
  156. extern JS_PUBLIC_API(JSBool)
  157. JS_XDRScript(JSXDRState *xdr, JSScript **scriptp);
  158. extern JS_PUBLIC_API(JSBool)
  159. JS_XDRRegisterClass(JSXDRState *xdr, JSClass *clasp, uint32_t *lp);
  160. extern JS_PUBLIC_API(uint32_t)
  161. JS_XDRFindClassIdByName(JSXDRState *xdr, const char *name);
  162. extern JS_PUBLIC_API(JSClass *)
  163. JS_XDRFindClassById(JSXDRState *xdr, uint32_t id);
  164. /*
  165. * Magic numbers.
  166. */
  167. #define JSXDR_MAGIC_SCRIPT_1 0xdead0001
  168. #define JSXDR_MAGIC_SCRIPT_2 0xdead0002
  169. #define JSXDR_MAGIC_SCRIPT_3 0xdead0003
  170. #define JSXDR_MAGIC_SCRIPT_4 0xdead0004
  171. #define JSXDR_MAGIC_SCRIPT_5 0xdead0005
  172. #define JSXDR_MAGIC_SCRIPT_6 0xdead0006
  173. #define JSXDR_MAGIC_SCRIPT_7 0xdead0007
  174. #define JSXDR_MAGIC_SCRIPT_8 0xdead0008
  175. #define JSXDR_MAGIC_SCRIPT_9 0xdead0009
  176. #define JSXDR_MAGIC_SCRIPT_10 0xdead000a
  177. #define JSXDR_MAGIC_SCRIPT_11 0xdead000b
  178. #define JSXDR_MAGIC_SCRIPT_12 0xdead000c
  179. #define JSXDR_MAGIC_SCRIPT_CURRENT JSXDR_MAGIC_SCRIPT_12
  180. /*
  181. * Bytecode version number. Increment the subtrahend whenever JS bytecode
  182. * changes incompatibly.
  183. *
  184. * This version number is XDR'd near the front of xdr bytecode and
  185. * aborts deserialization if there is a mismatch between the current
  186. * and saved versions. If deserialization fails, the data should be
  187. * invalidated if possible.
  188. */
  189. #define JSXDR_BYTECODE_VERSION (0xb973c0de - 107)
  190. /*
  191. * Library-private functions.
  192. */
  193. extern JSBool
  194. js_XDRAtom(JSXDRState *xdr, JSAtom **atomp);
  195. JS_END_EXTERN_C
  196. #endif /* ! jsxdrapi_h___ */