jbarnette-johnson 1.0.0.200806240111 → 1.0.0.200807291507

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. data/MANIFEST +1 -0
  2. data/Rakefile +3 -10
  3. data/bin/johnson +2 -1
  4. data/ext/spidermonkey/context.c +3 -4
  5. data/ext/spidermonkey/context.h +1 -1
  6. data/ext/spidermonkey/conversions.c +39 -33
  7. data/ext/spidermonkey/debugger.c +5 -5
  8. data/ext/spidermonkey/immutable_node.c.erb +11 -11
  9. data/ext/spidermonkey/jroot.h +4 -4
  10. data/ext/spidermonkey/js_land_proxy.c +9 -8
  11. data/ext/spidermonkey/ruby_land_proxy.c +5 -4
  12. data/ext/spidermonkey/runtime.c +1 -1
  13. data/johnson.gemspec +36 -0
  14. data/lib/hoe.rb +0 -7
  15. data/lib/johnson/cli/options.rb +10 -4
  16. data/lib/johnson/spidermonkey/runtime.rb +2 -2
  17. data/lib/johnson/version.rb +4 -2
  18. data/lib/johnson.rb +1 -0
  19. data/test/johnson/runtime_test.rb +11 -0
  20. data/test/johnson/spidermonkey/ruby_land_proxy_test.rb +6 -0
  21. data/vendor/spidermonkey/.cvsignore +9 -0
  22. data/vendor/spidermonkey/Makefile.in +462 -0
  23. data/vendor/spidermonkey/Makefile.ref +364 -0
  24. data/vendor/spidermonkey/README.html +820 -0
  25. data/vendor/spidermonkey/SpiderMonkey.rsp +12 -0
  26. data/vendor/spidermonkey/Y.js +19 -0
  27. data/vendor/spidermonkey/build.mk +43 -0
  28. data/vendor/spidermonkey/config/AIX4.1.mk +65 -0
  29. data/vendor/spidermonkey/config/AIX4.2.mk +64 -0
  30. data/vendor/spidermonkey/config/AIX4.3.mk +65 -0
  31. data/vendor/spidermonkey/config/Darwin.mk +83 -0
  32. data/vendor/spidermonkey/config/Darwin1.3.mk +81 -0
  33. data/vendor/spidermonkey/config/Darwin1.4.mk +41 -0
  34. data/vendor/spidermonkey/config/Darwin5.2.mk +81 -0
  35. data/vendor/spidermonkey/config/Darwin5.3.mk +81 -0
  36. data/vendor/spidermonkey/config/HP-UXB.10.10.mk +77 -0
  37. data/vendor/spidermonkey/config/HP-UXB.10.20.mk +77 -0
  38. data/vendor/spidermonkey/config/HP-UXB.11.00.mk +80 -0
  39. data/vendor/spidermonkey/config/IRIX.mk +87 -0
  40. data/vendor/spidermonkey/config/IRIX5.3.mk +44 -0
  41. data/vendor/spidermonkey/config/IRIX6.1.mk +44 -0
  42. data/vendor/spidermonkey/config/IRIX6.2.mk +44 -0
  43. data/vendor/spidermonkey/config/IRIX6.3.mk +44 -0
  44. data/vendor/spidermonkey/config/IRIX6.5.mk +44 -0
  45. data/vendor/spidermonkey/config/Linux_All.mk +103 -0
  46. data/vendor/spidermonkey/config/Mac_OS10.0.mk +82 -0
  47. data/vendor/spidermonkey/config/OSF1V4.0.mk +72 -0
  48. data/vendor/spidermonkey/config/OSF1V5.0.mk +69 -0
  49. data/vendor/spidermonkey/config/SunOS4.1.4.mk +101 -0
  50. data/vendor/spidermonkey/config/SunOS5.10.mk +50 -0
  51. data/vendor/spidermonkey/config/SunOS5.3.mk +91 -0
  52. data/vendor/spidermonkey/config/SunOS5.4.mk +92 -0
  53. data/vendor/spidermonkey/config/SunOS5.5.1.mk +44 -0
  54. data/vendor/spidermonkey/config/SunOS5.5.mk +87 -0
  55. data/vendor/spidermonkey/config/SunOS5.6.mk +89 -0
  56. data/vendor/spidermonkey/config/SunOS5.7.mk +44 -0
  57. data/vendor/spidermonkey/config/SunOS5.8.mk +44 -0
  58. data/vendor/spidermonkey/config/SunOS5.9.mk +44 -0
  59. data/vendor/spidermonkey/config/WINNT4.0.mk +117 -0
  60. data/vendor/spidermonkey/config/WINNT5.0.mk +117 -0
  61. data/vendor/spidermonkey/config/WINNT5.1.mk +117 -0
  62. data/vendor/spidermonkey/config/WINNT5.2.mk +117 -0
  63. data/vendor/spidermonkey/config/WINNT6.0.mk +117 -0
  64. data/vendor/spidermonkey/config/dgux.mk +64 -0
  65. data/vendor/spidermonkey/config.mk +192 -0
  66. data/vendor/spidermonkey/editline/Makefile.ref +144 -0
  67. data/vendor/spidermonkey/editline/README +83 -0
  68. data/vendor/spidermonkey/editline/editline.3 +175 -0
  69. data/vendor/spidermonkey/editline/editline.c +1369 -0
  70. data/vendor/spidermonkey/editline/editline.h +135 -0
  71. data/vendor/spidermonkey/editline/sysunix.c +182 -0
  72. data/vendor/spidermonkey/editline/unix.h +82 -0
  73. data/vendor/spidermonkey/fdlibm/.cvsignore +7 -0
  74. data/vendor/spidermonkey/fdlibm/Makefile.in +127 -0
  75. data/vendor/spidermonkey/fdlibm/Makefile.ref +192 -0
  76. data/vendor/spidermonkey/fdlibm/e_acos.c +147 -0
  77. data/vendor/spidermonkey/fdlibm/e_acosh.c +105 -0
  78. data/vendor/spidermonkey/fdlibm/e_asin.c +156 -0
  79. data/vendor/spidermonkey/fdlibm/e_atan2.c +165 -0
  80. data/vendor/spidermonkey/fdlibm/e_atanh.c +110 -0
  81. data/vendor/spidermonkey/fdlibm/e_cosh.c +133 -0
  82. data/vendor/spidermonkey/fdlibm/e_exp.c +202 -0
  83. data/vendor/spidermonkey/fdlibm/e_fmod.c +184 -0
  84. data/vendor/spidermonkey/fdlibm/e_gamma.c +71 -0
  85. data/vendor/spidermonkey/fdlibm/e_gamma_r.c +70 -0
  86. data/vendor/spidermonkey/fdlibm/e_hypot.c +173 -0
  87. data/vendor/spidermonkey/fdlibm/e_j0.c +524 -0
  88. data/vendor/spidermonkey/fdlibm/e_j1.c +523 -0
  89. data/vendor/spidermonkey/fdlibm/e_jn.c +315 -0
  90. data/vendor/spidermonkey/fdlibm/e_lgamma.c +71 -0
  91. data/vendor/spidermonkey/fdlibm/e_lgamma_r.c +347 -0
  92. data/vendor/spidermonkey/fdlibm/e_log.c +184 -0
  93. data/vendor/spidermonkey/fdlibm/e_log10.c +134 -0
  94. data/vendor/spidermonkey/fdlibm/e_pow.c +386 -0
  95. data/vendor/spidermonkey/fdlibm/e_rem_pio2.c +222 -0
  96. data/vendor/spidermonkey/fdlibm/e_remainder.c +120 -0
  97. data/vendor/spidermonkey/fdlibm/e_scalb.c +89 -0
  98. data/vendor/spidermonkey/fdlibm/e_sinh.c +122 -0
  99. data/vendor/spidermonkey/fdlibm/e_sqrt.c +497 -0
  100. data/vendor/spidermonkey/fdlibm/fdlibm.h +273 -0
  101. data/vendor/spidermonkey/fdlibm/fdlibm.mak +1453 -0
  102. data/vendor/spidermonkey/fdlibm/fdlibm.mdp +0 -0
  103. data/vendor/spidermonkey/fdlibm/k_cos.c +135 -0
  104. data/vendor/spidermonkey/fdlibm/k_rem_pio2.c +354 -0
  105. data/vendor/spidermonkey/fdlibm/k_sin.c +114 -0
  106. data/vendor/spidermonkey/fdlibm/k_standard.c +785 -0
  107. data/vendor/spidermonkey/fdlibm/k_tan.c +170 -0
  108. data/vendor/spidermonkey/fdlibm/s_asinh.c +101 -0
  109. data/vendor/spidermonkey/fdlibm/s_atan.c +175 -0
  110. data/vendor/spidermonkey/fdlibm/s_cbrt.c +133 -0
  111. data/vendor/spidermonkey/fdlibm/s_ceil.c +120 -0
  112. data/vendor/spidermonkey/fdlibm/s_copysign.c +72 -0
  113. data/vendor/spidermonkey/fdlibm/s_cos.c +118 -0
  114. data/vendor/spidermonkey/fdlibm/s_erf.c +356 -0
  115. data/vendor/spidermonkey/fdlibm/s_expm1.c +267 -0
  116. data/vendor/spidermonkey/fdlibm/s_fabs.c +70 -0
  117. data/vendor/spidermonkey/fdlibm/s_finite.c +71 -0
  118. data/vendor/spidermonkey/fdlibm/s_floor.c +121 -0
  119. data/vendor/spidermonkey/fdlibm/s_frexp.c +99 -0
  120. data/vendor/spidermonkey/fdlibm/s_ilogb.c +85 -0
  121. data/vendor/spidermonkey/fdlibm/s_isnan.c +74 -0
  122. data/vendor/spidermonkey/fdlibm/s_ldexp.c +66 -0
  123. data/vendor/spidermonkey/fdlibm/s_lib_version.c +73 -0
  124. data/vendor/spidermonkey/fdlibm/s_log1p.c +211 -0
  125. data/vendor/spidermonkey/fdlibm/s_logb.c +79 -0
  126. data/vendor/spidermonkey/fdlibm/s_matherr.c +64 -0
  127. data/vendor/spidermonkey/fdlibm/s_modf.c +132 -0
  128. data/vendor/spidermonkey/fdlibm/s_nextafter.c +124 -0
  129. data/vendor/spidermonkey/fdlibm/s_rint.c +131 -0
  130. data/vendor/spidermonkey/fdlibm/s_scalbn.c +107 -0
  131. data/vendor/spidermonkey/fdlibm/s_signgam.c +40 -0
  132. data/vendor/spidermonkey/fdlibm/s_significand.c +68 -0
  133. data/vendor/spidermonkey/fdlibm/s_sin.c +118 -0
  134. data/vendor/spidermonkey/fdlibm/s_tan.c +112 -0
  135. data/vendor/spidermonkey/fdlibm/s_tanh.c +122 -0
  136. data/vendor/spidermonkey/fdlibm/w_acos.c +78 -0
  137. data/vendor/spidermonkey/fdlibm/w_acosh.c +78 -0
  138. data/vendor/spidermonkey/fdlibm/w_asin.c +80 -0
  139. data/vendor/spidermonkey/fdlibm/w_atan2.c +79 -0
  140. data/vendor/spidermonkey/fdlibm/w_atanh.c +81 -0
  141. data/vendor/spidermonkey/fdlibm/w_cosh.c +77 -0
  142. data/vendor/spidermonkey/fdlibm/w_exp.c +88 -0
  143. data/vendor/spidermonkey/fdlibm/w_fmod.c +78 -0
  144. data/vendor/spidermonkey/fdlibm/w_gamma.c +85 -0
  145. data/vendor/spidermonkey/fdlibm/w_gamma_r.c +81 -0
  146. data/vendor/spidermonkey/fdlibm/w_hypot.c +78 -0
  147. data/vendor/spidermonkey/fdlibm/w_j0.c +105 -0
  148. data/vendor/spidermonkey/fdlibm/w_j1.c +106 -0
  149. data/vendor/spidermonkey/fdlibm/w_jn.c +128 -0
  150. data/vendor/spidermonkey/fdlibm/w_lgamma.c +85 -0
  151. data/vendor/spidermonkey/fdlibm/w_lgamma_r.c +81 -0
  152. data/vendor/spidermonkey/fdlibm/w_log.c +78 -0
  153. data/vendor/spidermonkey/fdlibm/w_log10.c +81 -0
  154. data/vendor/spidermonkey/fdlibm/w_pow.c +99 -0
  155. data/vendor/spidermonkey/fdlibm/w_remainder.c +77 -0
  156. data/vendor/spidermonkey/fdlibm/w_scalb.c +95 -0
  157. data/vendor/spidermonkey/fdlibm/w_sinh.c +77 -0
  158. data/vendor/spidermonkey/fdlibm/w_sqrt.c +77 -0
  159. data/vendor/spidermonkey/javascript-trace.d +73 -0
  160. data/vendor/spidermonkey/js.c +3951 -0
  161. data/vendor/spidermonkey/js.mak +4438 -0
  162. data/vendor/spidermonkey/js.mdp +0 -0
  163. data/vendor/spidermonkey/js.msg +307 -0
  164. data/vendor/spidermonkey/js.pkg +2 -0
  165. data/vendor/spidermonkey/js3240.rc +79 -0
  166. data/vendor/spidermonkey/jsOS240.def +654 -0
  167. data/vendor/spidermonkey/jsapi.c +5836 -0
  168. data/vendor/spidermonkey/jsapi.h +2624 -0
  169. data/vendor/spidermonkey/jsarena.c +450 -0
  170. data/vendor/spidermonkey/jsarena.h +318 -0
  171. data/vendor/spidermonkey/jsarray.c +2988 -0
  172. data/vendor/spidermonkey/jsarray.h +124 -0
  173. data/vendor/spidermonkey/jsatom.c +1045 -0
  174. data/vendor/spidermonkey/jsatom.h +442 -0
  175. data/vendor/spidermonkey/jsbit.h +253 -0
  176. data/vendor/spidermonkey/jsbool.c +176 -0
  177. data/vendor/spidermonkey/jsbool.h +73 -0
  178. data/vendor/spidermonkey/jsclist.h +139 -0
  179. data/vendor/spidermonkey/jscntxt.c +1348 -0
  180. data/vendor/spidermonkey/jscntxt.h +1120 -0
  181. data/vendor/spidermonkey/jscompat.h +57 -0
  182. data/vendor/spidermonkey/jsconfig.h +248 -0
  183. data/vendor/spidermonkey/jsconfig.mk +181 -0
  184. data/vendor/spidermonkey/jscpucfg.c +383 -0
  185. data/vendor/spidermonkey/jscpucfg.h +212 -0
  186. data/vendor/spidermonkey/jsdate.c +2398 -0
  187. data/vendor/spidermonkey/jsdate.h +124 -0
  188. data/vendor/spidermonkey/jsdbgapi.c +1799 -0
  189. data/vendor/spidermonkey/jsdbgapi.h +464 -0
  190. data/vendor/spidermonkey/jsdhash.c +868 -0
  191. data/vendor/spidermonkey/jsdhash.h +592 -0
  192. data/vendor/spidermonkey/jsdtoa.c +3167 -0
  193. data/vendor/spidermonkey/jsdtoa.h +130 -0
  194. data/vendor/spidermonkey/jsdtracef.c +317 -0
  195. data/vendor/spidermonkey/jsdtracef.h +77 -0
  196. data/vendor/spidermonkey/jsemit.c +6909 -0
  197. data/vendor/spidermonkey/jsemit.h +741 -0
  198. data/vendor/spidermonkey/jsexn.c +1371 -0
  199. data/vendor/spidermonkey/jsexn.h +96 -0
  200. data/vendor/spidermonkey/jsfile.c +2736 -0
  201. data/vendor/spidermonkey/jsfile.h +56 -0
  202. data/vendor/spidermonkey/jsfile.msg +90 -0
  203. data/vendor/spidermonkey/jsfun.c +2634 -0
  204. data/vendor/spidermonkey/jsfun.h +254 -0
  205. data/vendor/spidermonkey/jsgc.c +3554 -0
  206. data/vendor/spidermonkey/jsgc.h +403 -0
  207. data/vendor/spidermonkey/jshash.c +476 -0
  208. data/vendor/spidermonkey/jshash.h +151 -0
  209. data/vendor/spidermonkey/jsify.pl +485 -0
  210. data/vendor/spidermonkey/jsinterp.c +6981 -0
  211. data/vendor/spidermonkey/jsinterp.h +521 -0
  212. data/vendor/spidermonkey/jsinvoke.c +43 -0
  213. data/vendor/spidermonkey/jsiter.c +1067 -0
  214. data/vendor/spidermonkey/jsiter.h +122 -0
  215. data/vendor/spidermonkey/jskeyword.tbl +124 -0
  216. data/vendor/spidermonkey/jskwgen.c +460 -0
  217. data/vendor/spidermonkey/jslibmath.h +266 -0
  218. data/vendor/spidermonkey/jslock.c +1309 -0
  219. data/vendor/spidermonkey/jslock.h +313 -0
  220. data/vendor/spidermonkey/jslocko.asm +60 -0
  221. data/vendor/spidermonkey/jslog2.c +94 -0
  222. data/vendor/spidermonkey/jslong.c +264 -0
  223. data/vendor/spidermonkey/jslong.h +412 -0
  224. data/vendor/spidermonkey/jsmath.c +568 -0
  225. data/vendor/spidermonkey/jsmath.h +57 -0
  226. data/vendor/spidermonkey/jsnum.c +1228 -0
  227. data/vendor/spidermonkey/jsnum.h +283 -0
  228. data/vendor/spidermonkey/jsobj.c +5266 -0
  229. data/vendor/spidermonkey/jsobj.h +709 -0
  230. data/vendor/spidermonkey/jsopcode.c +5245 -0
  231. data/vendor/spidermonkey/jsopcode.h +394 -0
  232. data/vendor/spidermonkey/jsopcode.tbl +523 -0
  233. data/vendor/spidermonkey/jsotypes.h +202 -0
  234. data/vendor/spidermonkey/jsparse.c +6680 -0
  235. data/vendor/spidermonkey/jsparse.h +511 -0
  236. data/vendor/spidermonkey/jsprf.c +1262 -0
  237. data/vendor/spidermonkey/jsprf.h +150 -0
  238. data/vendor/spidermonkey/jsproto.tbl +128 -0
  239. data/vendor/spidermonkey/jsprvtd.h +267 -0
  240. data/vendor/spidermonkey/jspubtd.h +744 -0
  241. data/vendor/spidermonkey/jsregexp.c +4352 -0
  242. data/vendor/spidermonkey/jsregexp.h +183 -0
  243. data/vendor/spidermonkey/jsreops.tbl +145 -0
  244. data/vendor/spidermonkey/jsscan.c +2003 -0
  245. data/vendor/spidermonkey/jsscan.h +387 -0
  246. data/vendor/spidermonkey/jsscope.c +1948 -0
  247. data/vendor/spidermonkey/jsscope.h +418 -0
  248. data/vendor/spidermonkey/jsscript.c +1832 -0
  249. data/vendor/spidermonkey/jsscript.h +287 -0
  250. data/vendor/spidermonkey/jsshell.msg +50 -0
  251. data/vendor/spidermonkey/jsstddef.h +83 -0
  252. data/vendor/spidermonkey/jsstr.c +5004 -0
  253. data/vendor/spidermonkey/jsstr.h +641 -0
  254. data/vendor/spidermonkey/jstypes.h +475 -0
  255. data/vendor/spidermonkey/jsutil.c +345 -0
  256. data/vendor/spidermonkey/jsutil.h +157 -0
  257. data/vendor/spidermonkey/jsxdrapi.c +800 -0
  258. data/vendor/spidermonkey/jsxdrapi.h +218 -0
  259. data/vendor/spidermonkey/jsxml.c +8471 -0
  260. data/vendor/spidermonkey/jsxml.h +349 -0
  261. data/vendor/spidermonkey/lock_SunOS.s +119 -0
  262. data/vendor/spidermonkey/perfect.js +39 -0
  263. data/vendor/spidermonkey/plify_jsdhash.sed +36 -0
  264. data/vendor/spidermonkey/prmjtime.c +846 -0
  265. data/vendor/spidermonkey/prmjtime.h +103 -0
  266. data/vendor/spidermonkey/resource.h +15 -0
  267. data/vendor/spidermonkey/rules.mk +197 -0
  268. data/vendor/spidermonkey/win32.order +384 -0
  269. metadata +4 -3
@@ -0,0 +1,800 @@
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 "jsconfig.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
+
63
+ typedef 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
+
107
+ static JSBool
108
+ mem_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
+
116
+ static JSBool
117
+ mem_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
+
125
+ static JSBool
126
+ mem_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
+
134
+ static JSBool
135
+ mem_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
+
143
+ static void *
144
+ mem_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
+
157
+ static JSBool
158
+ mem_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
+
210
+ static uint32
211
+ mem_tell(JSXDRState *xdr)
212
+ {
213
+ return MEM_COUNT(xdr);
214
+ }
215
+
216
+ static void
217
+ mem_finalize(JSXDRState *xdr)
218
+ {
219
+ JS_free(xdr->cx, MEM_BASE(xdr));
220
+ }
221
+
222
+ static JSXDROps xdrmem_ops = {
223
+ mem_get32, mem_set32, mem_getbytes, mem_setbytes,
224
+ mem_raw, mem_seek, mem_tell, mem_finalize
225
+ };
226
+
227
+ JS_PUBLIC_API(void)
228
+ JS_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
+
239
+ JS_PUBLIC_API(JSXDRState *)
240
+ JS_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
+
261
+ JS_PUBLIC_API(void *)
262
+ JS_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
+
270
+ JS_PUBLIC_API(void)
271
+ JS_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
+
280
+ JS_PUBLIC_API(uint32)
281
+ JS_XDRMemDataLeft(JSXDRState *xdr)
282
+ {
283
+ if (xdr->ops != &xdrmem_ops)
284
+ return 0;
285
+ return MEM_LIMIT(xdr) - MEM_COUNT(xdr);
286
+ }
287
+
288
+ JS_PUBLIC_API(void)
289
+ JS_XDRMemResetData(JSXDRState *xdr)
290
+ {
291
+ if (xdr->ops != &xdrmem_ops)
292
+ return;
293
+ MEM_COUNT(xdr) = 0;
294
+ }
295
+
296
+ JS_PUBLIC_API(void)
297
+ JS_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
+
309
+ JS_PUBLIC_API(JSBool)
310
+ JS_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
+
319
+ JS_PUBLIC_API(JSBool)
320
+ JS_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
+
329
+ JS_PUBLIC_API(JSBool)
330
+ JS_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
+
343
+ JS_PUBLIC_API(JSBool)
344
+ JS_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
+ */
375
+ JS_PUBLIC_API(JSBool)
376
+ JS_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
+
401
+ JS_PUBLIC_API(JSBool)
402
+ JS_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
+
414
+ static JSBool
415
+ XDRChars(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
+ */
443
+ JS_PUBLIC_API(JSBool)
444
+ JS_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
+
472
+ bad:
473
+ if (xdr->mode == JSXDR_DECODE)
474
+ JS_free(xdr->cx, chars);
475
+ return JS_FALSE;
476
+ }
477
+
478
+ JS_PUBLIC_API(JSBool)
479
+ JS_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
+
491
+ static JSBool
492
+ XDRDoubleValue(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
+
505
+ JS_PUBLIC_API(JSBool)
506
+ JS_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
+
526
+ static JSBool
527
+ XDRValueBody(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((JSBool) 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
+
592
+ JS_PUBLIC_API(JSBool)
593
+ JS_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
+
608
+ JSBool
609
+ js_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
+
640
+ extern JSBool
641
+ js_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
+
687
+ JS_PUBLIC_API(JSBool)
688
+ JS_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
+
701
+ typedef struct JSRegHashEntry {
702
+ JSDHashEntryHdr hdr;
703
+ const char *name;
704
+ uint32 index;
705
+ } JSRegHashEntry;
706
+
707
+ JS_PUBLIC_API(JSBool)
708
+ JS_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
+
745
+ JS_PUBLIC_API(uint32)
746
+ JS_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
+
790
+ JS_PUBLIC_API(JSClass *)
791
+ JS_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 */