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,39 @@
1
+ // Some simple testing of new, eval and some string stuff.
2
+
3
+ // constructor -- expression array initialization
4
+ function ExprArray(n,v)
5
+ {
6
+ // Initializes n values to v coerced to a string.
7
+ for (var i = 0; i < n; i++) {
8
+ this[i] = "" + v;
9
+ }
10
+ }
11
+
12
+
13
+ // Print the perfect numbers up to n and the sum expression for n's divisors.
14
+ function perfect(n)
15
+ {
16
+ print("The perfect numbers up to " + n + " are:");
17
+
18
+ // We build sumOfDivisors[i] to hold a string expression for
19
+ // the sum of the divisors of i, excluding i itself.
20
+ var sumOfDivisors = new ExprArray(n+1,1);
21
+ for (var divisor = 2; divisor <= n; divisor++) {
22
+ for (var j = divisor + divisor; j <= n; j += divisor) {
23
+ sumOfDivisors[j] += " + " + divisor;
24
+ }
25
+ // At this point everything up to 'divisor' has its sumOfDivisors
26
+ // expression calculated, so we can determine whether it's perfect
27
+ // already by evaluating.
28
+ if (eval(sumOfDivisors[divisor]) == divisor) {
29
+ print("" + divisor + " = " + sumOfDivisors[divisor]);
30
+ }
31
+ }
32
+ print("That's all.");
33
+ }
34
+
35
+
36
+ print("\nA number is 'perfect' if it is equal to the sum of its")
37
+ print("divisors (excluding itself).\n");
38
+ perfect(500);
39
+
@@ -0,0 +1,36 @@
1
+ / * Double hashing implementation./a\
2
+ * GENERATED BY js/src/plify_jsdhash.sed -- DO NOT EDIT!!!
3
+ / * Double hashing, a la Knuth 6./a\
4
+ * GENERATED BY js/src/plify_jsdhash.sed -- DO NOT EDIT!!!
5
+ s/jsdhash_h___/pldhash_h___/
6
+ s/jsdhash\.bigdump/pldhash.bigdump/
7
+ s/jstypes\.h/nscore.h/
8
+ s/jsbit\.h/prbit.h/
9
+ s/jsdhash\.h/pldhash.h/
10
+ s/jsdhash\.c/pldhash.c/
11
+ s/jsdhash:/pldhash:/
12
+ s/jsutil\.h/nsDebug.h/
13
+ s/JS_DHASH/PL_DHASH/g
14
+ s/JS_DHash/PL_DHash/g
15
+ s/JSDHash/PLDHash/g
16
+ s/JSHash/PLHash/g
17
+ s/uint32 /PRUint32/g
18
+ s/\([^U]\)int32 /\1PRInt32/g
19
+ s/uint16 /PRUint16/g
20
+ s/\([^U]\)int16 /\1PRInt16/g
21
+ s/uint32/PRUint32/g
22
+ s/\([^U]\)int32/\1PRInt32/g
23
+ s/uint16/PRUint16/g
24
+ s/\([^U]\)int16/\1PRInt16/g
25
+ s/JSBool/PRBool/g
26
+ s/extern JS_PUBLIC_API(\([^()]*\))/NS_COM_GLUE \1/
27
+ s/JS_PUBLIC_API(\([^()]*\))/\1/
28
+ s/JS_DLL_CALLBACK/PR_CALLBACK/
29
+ s/JS_STATIC_DLL_CALLBACK/PR_STATIC_CALLBACK/
30
+ s/JS_NewDHashTable/PL_NewDHashTable/
31
+ s/JS_ASSERT(0)/NS_NOTREACHED("0")/
32
+ s/\( *\)JS_ASSERT(\(.*\));/\1NS_ASSERTION(\2,\n\1 "\2");/
33
+ s/JS_UNLIKELY/NS_UNLIKELY/g
34
+ s/JS_LIKELY/NS_LIKELY/g
35
+ s/JS_/PR_/g
36
+ s/fprintf(stderr,/printf_stderr(/
@@ -0,0 +1,846 @@
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
+
40
+ /*
41
+ * PR time code.
42
+ */
43
+ #include "jsstddef.h"
44
+ #ifdef SOLARIS
45
+ #define _REENTRANT 1
46
+ #endif
47
+ #include <string.h>
48
+ #include <time.h>
49
+ #include "jstypes.h"
50
+ #include "jsutil.h"
51
+
52
+ #include "jsprf.h"
53
+ #include "jslock.h"
54
+ #include "prmjtime.h"
55
+
56
+ #define PRMJ_DO_MILLISECONDS 1
57
+
58
+ #ifdef XP_OS2
59
+ #include <sys/timeb.h>
60
+ #endif
61
+ #ifdef XP_WIN
62
+ #include <windef.h>
63
+ #include <winbase.h>
64
+ #include <math.h> /* for fabs */
65
+ #include <mmsystem.h> /* for timeBegin/EndPeriod */
66
+ /* VC++ 8.0 or later, and not WINCE */
67
+ #if _MSC_VER >= 1400 && !defined(WINCE)
68
+ #define NS_HAVE_INVALID_PARAMETER_HANDLER 1
69
+ #endif
70
+ #ifdef NS_HAVE_INVALID_PARAMETER_HANDLER
71
+ #include <stdlib.h> /* for _set_invalid_parameter_handler */
72
+ #include <crtdbg.h> /* for _CrtSetReportMode */
73
+ #endif
74
+
75
+ #ifdef JS_THREADSAFE
76
+ #include <prinit.h>
77
+ #endif
78
+
79
+ #endif
80
+
81
+ #if defined(XP_UNIX) || defined(XP_BEOS)
82
+
83
+ #ifdef _SVID_GETTOD /* Defined only on Solaris, see Solaris <sys/types.h> */
84
+ extern int gettimeofday(struct timeval *tv);
85
+ #endif
86
+
87
+ #include <sys/time.h>
88
+
89
+ #endif /* XP_UNIX */
90
+
91
+ #define PRMJ_YEAR_DAYS 365L
92
+ #define PRMJ_FOUR_YEARS_DAYS (4 * PRMJ_YEAR_DAYS + 1)
93
+ #define PRMJ_CENTURY_DAYS (25 * PRMJ_FOUR_YEARS_DAYS - 1)
94
+ #define PRMJ_FOUR_CENTURIES_DAYS (4 * PRMJ_CENTURY_DAYS + 1)
95
+ #define PRMJ_HOUR_SECONDS 3600L
96
+ #define PRMJ_DAY_SECONDS (24L * PRMJ_HOUR_SECONDS)
97
+ #define PRMJ_YEAR_SECONDS (PRMJ_DAY_SECONDS * PRMJ_YEAR_DAYS)
98
+ #define PRMJ_MAX_UNIX_TIMET 2145859200L /*time_t value equiv. to 12/31/2037 */
99
+
100
+ /* function prototypes */
101
+ static void PRMJ_basetime(JSInt64 tsecs, PRMJTime *prtm);
102
+ /*
103
+ * get the difference in seconds between this time zone and UTC (GMT)
104
+ */
105
+ JSInt32
106
+ PRMJ_LocalGMTDifference()
107
+ {
108
+ struct tm ltime;
109
+
110
+ /* get the difference between this time zone and GMT */
111
+ memset((char *)&ltime,0,sizeof(ltime));
112
+ ltime.tm_mday = 2;
113
+ ltime.tm_year = 70;
114
+ return (JSInt32)mktime(&ltime) - (24L * 3600L);
115
+ }
116
+
117
+ /* Constants for GMT offset from 1970 */
118
+ #define G1970GMTMICROHI 0x00dcdcad /* micro secs to 1970 hi */
119
+ #define G1970GMTMICROLOW 0x8b3fa000 /* micro secs to 1970 low */
120
+
121
+ #define G2037GMTMICROHI 0x00e45fab /* micro secs to 2037 high */
122
+ #define G2037GMTMICROLOW 0x7a238000 /* micro secs to 2037 low */
123
+
124
+ /* Convert from base time to extended time */
125
+ static JSInt64
126
+ PRMJ_ToExtendedTime(JSInt32 base_time)
127
+ {
128
+ JSInt64 exttime;
129
+ JSInt64 g1970GMTMicroSeconds;
130
+ JSInt64 low;
131
+ JSInt32 diff;
132
+ JSInt64 tmp;
133
+ JSInt64 tmp1;
134
+
135
+ diff = PRMJ_LocalGMTDifference();
136
+ JSLL_UI2L(tmp, PRMJ_USEC_PER_SEC);
137
+ JSLL_I2L(tmp1,diff);
138
+ JSLL_MUL(tmp,tmp,tmp1);
139
+
140
+ JSLL_UI2L(g1970GMTMicroSeconds,G1970GMTMICROHI);
141
+ JSLL_UI2L(low,G1970GMTMICROLOW);
142
+ #ifndef JS_HAVE_LONG_LONG
143
+ JSLL_SHL(g1970GMTMicroSeconds,g1970GMTMicroSeconds,16);
144
+ JSLL_SHL(g1970GMTMicroSeconds,g1970GMTMicroSeconds,16);
145
+ #else
146
+ JSLL_SHL(g1970GMTMicroSeconds,g1970GMTMicroSeconds,32);
147
+ #endif
148
+ JSLL_ADD(g1970GMTMicroSeconds,g1970GMTMicroSeconds,low);
149
+
150
+ JSLL_I2L(exttime,base_time);
151
+ JSLL_ADD(exttime,exttime,g1970GMTMicroSeconds);
152
+ JSLL_SUB(exttime,exttime,tmp);
153
+ return exttime;
154
+ }
155
+
156
+ #ifdef XP_WIN
157
+ typedef struct CalibrationData
158
+ {
159
+ long double freq; /* The performance counter frequency */
160
+ long double offset; /* The low res 'epoch' */
161
+ long double timer_offset; /* The high res 'epoch' */
162
+
163
+ /* The last high res time that we returned since recalibrating */
164
+ JSInt64 last;
165
+
166
+ JSBool calibrated;
167
+
168
+ #ifdef JS_THREADSAFE
169
+ CRITICAL_SECTION data_lock;
170
+ CRITICAL_SECTION calibration_lock;
171
+ #endif
172
+ } CalibrationData;
173
+
174
+ static const JSInt64 win2un = JSLL_INIT(0x19DB1DE, 0xD53E8000);
175
+
176
+ static CalibrationData calibration = { 0 };
177
+
178
+ #define FILETIME2INT64(ft) (((JSInt64)ft.dwHighDateTime) << 32LL | (JSInt64)ft.dwLowDateTime)
179
+
180
+ static void
181
+ NowCalibrate()
182
+ {
183
+ FILETIME ft, ftStart;
184
+ LARGE_INTEGER liFreq, now;
185
+
186
+ if (calibration.freq == 0.0) {
187
+ if(!QueryPerformanceFrequency(&liFreq)) {
188
+ /* High-performance timer is unavailable */
189
+ calibration.freq = -1.0;
190
+ } else {
191
+ calibration.freq = (long double) liFreq.QuadPart;
192
+ }
193
+ }
194
+ if (calibration.freq > 0.0) {
195
+ JSInt64 calibrationDelta = 0;
196
+
197
+ /* By wrapping a timeBegin/EndPeriod pair of calls around this loop,
198
+ the loop seems to take much less time (1 ms vs 15ms) on Vista. */
199
+ timeBeginPeriod(1);
200
+ GetSystemTimeAsFileTime(&ftStart);
201
+ do {
202
+ GetSystemTimeAsFileTime(&ft);
203
+ } while (memcmp(&ftStart,&ft, sizeof(ft)) == 0);
204
+ timeEndPeriod(1);
205
+
206
+ /*
207
+ calibrationDelta = (FILETIME2INT64(ft) - FILETIME2INT64(ftStart))/10;
208
+ fprintf(stderr, "Calibration delta was %I64d us\n", calibrationDelta);
209
+ */
210
+
211
+ QueryPerformanceCounter(&now);
212
+
213
+ calibration.offset = (long double) FILETIME2INT64(ft);
214
+ calibration.timer_offset = (long double) now.QuadPart;
215
+
216
+ /* The windows epoch is around 1600. The unix epoch is around
217
+ 1970. win2un is the difference (in windows time units which
218
+ are 10 times more highres than the JS time unit) */
219
+ calibration.offset -= win2un;
220
+ calibration.offset *= 0.1;
221
+ calibration.last = 0;
222
+
223
+ calibration.calibrated = JS_TRUE;
224
+ }
225
+ }
226
+
227
+ #define CALIBRATIONLOCK_SPINCOUNT 0
228
+ #define DATALOCK_SPINCOUNT 4096
229
+ #define LASTLOCK_SPINCOUNT 4096
230
+
231
+ #ifdef JS_THREADSAFE
232
+ static PRStatus PR_CALLBACK
233
+ NowInit(void)
234
+ {
235
+ memset(&calibration, 0, sizeof(calibration));
236
+ NowCalibrate();
237
+ InitializeCriticalSectionAndSpinCount(&calibration.calibration_lock, CALIBRATIONLOCK_SPINCOUNT);
238
+ InitializeCriticalSectionAndSpinCount(&calibration.data_lock, DATALOCK_SPINCOUNT);
239
+ return PR_SUCCESS;
240
+ }
241
+
242
+ void
243
+ PRMJ_NowShutdown()
244
+ {
245
+ DeleteCriticalSection(&calibration.calibration_lock);
246
+ DeleteCriticalSection(&calibration.data_lock);
247
+ }
248
+
249
+ #define MUTEX_LOCK(m) EnterCriticalSection(m)
250
+ #define MUTEX_TRYLOCK(m) TryEnterCriticalSection(m)
251
+ #define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
252
+ #define MUTEX_SETSPINCOUNT(m, c) SetCriticalSectionSpinCount((m),(c))
253
+
254
+ static PRCallOnceType calibrationOnce = { 0 };
255
+
256
+ #else
257
+
258
+ #define MUTEX_LOCK(m)
259
+ #define MUTEX_TRYLOCK(m) 1
260
+ #define MUTEX_UNLOCK(m)
261
+ #define MUTEX_SETSPINCOUNT(m, c)
262
+
263
+ #endif
264
+
265
+
266
+ #endif /* XP_WIN */
267
+
268
+ /*
269
+
270
+ Win32 python-esque pseudo code
271
+ Please see bug 363258 for why the win32 timing code is so complex.
272
+
273
+ calibration mutex : Win32CriticalSection(spincount=0)
274
+ data mutex : Win32CriticalSection(spincount=4096)
275
+
276
+ def NowInit():
277
+ init mutexes
278
+ PRMJ_NowCalibration()
279
+
280
+ def NowCalibration():
281
+ expensive up-to-15ms call
282
+
283
+ def PRMJ_Now():
284
+ returnedTime = 0
285
+ needCalibration = False
286
+ cachedOffset = 0.0
287
+ calibrated = False
288
+ PR_CallOnce(PRMJ_NowInit)
289
+ do
290
+ if not global.calibrated or needCalibration:
291
+ acquire calibration mutex
292
+ acquire data mutex
293
+
294
+ // Only recalibrate if someone didn't already
295
+ if cachedOffset == calibration.offset:
296
+ // Have all waiting threads immediately wait
297
+ set data mutex spin count = 0
298
+ PRMJ_NowCalibrate()
299
+ calibrated = 1
300
+
301
+ set data mutex spin count = default
302
+ release data mutex
303
+ release calibration mutex
304
+
305
+ calculate lowres time
306
+
307
+ if highres timer available:
308
+ acquire data mutex
309
+ calculate highres time
310
+ cachedOffset = calibration.offset
311
+ highres time = calibration.last = max(highres time, calibration.last)
312
+ release data mutex
313
+
314
+ get kernel tick interval
315
+
316
+ if abs(highres - lowres) < kernel tick:
317
+ returnedTime = highres time
318
+ needCalibration = False
319
+ else:
320
+ if calibrated:
321
+ returnedTime = lowres
322
+ needCalibration = False
323
+ else:
324
+ needCalibration = True
325
+ else:
326
+ returnedTime = lowres
327
+ while needCalibration
328
+
329
+ */
330
+
331
+ JSInt64
332
+ PRMJ_Now(void)
333
+ {
334
+ #ifdef XP_OS2
335
+ JSInt64 s, us, ms2us, s2us;
336
+ struct timeb b;
337
+ #endif
338
+ #ifdef XP_WIN
339
+ static int nCalls = 0;
340
+ long double lowresTime, highresTimerValue;
341
+ FILETIME ft;
342
+ LARGE_INTEGER now;
343
+ JSBool calibrated = JS_FALSE;
344
+ JSBool needsCalibration = JS_FALSE;
345
+ JSInt64 returnedTime;
346
+ long double cachedOffset = 0.0;
347
+ #endif
348
+ #if defined(XP_UNIX) || defined(XP_BEOS)
349
+ struct timeval tv;
350
+ JSInt64 s, us, s2us;
351
+ #endif /* XP_UNIX */
352
+
353
+ #ifdef XP_OS2
354
+ ftime(&b);
355
+ JSLL_UI2L(ms2us, PRMJ_USEC_PER_MSEC);
356
+ JSLL_UI2L(s2us, PRMJ_USEC_PER_SEC);
357
+ JSLL_UI2L(s, b.time);
358
+ JSLL_UI2L(us, b.millitm);
359
+ JSLL_MUL(us, us, ms2us);
360
+ JSLL_MUL(s, s, s2us);
361
+ JSLL_ADD(s, s, us);
362
+ return s;
363
+ #endif
364
+ #ifdef XP_WIN
365
+
366
+ /* To avoid regressing startup time (where high resolution is likely
367
+ not needed), give the old behavior for the first few calls.
368
+ This does not appear to be needed on Vista as the timeBegin/timeEndPeriod
369
+ calls seem to immediately take effect. */
370
+ int thiscall = JS_ATOMIC_INCREMENT(&nCalls);
371
+ /* 10 seems to be the number of calls to load with a blank homepage */
372
+ if (thiscall <= 10) {
373
+ GetSystemTimeAsFileTime(&ft);
374
+ return (FILETIME2INT64(ft)-win2un)/10L;
375
+ }
376
+
377
+ /* For non threadsafe platforms, NowInit is not necessary */
378
+ #ifdef JS_THREADSAFE
379
+ PR_CallOnce(&calibrationOnce, NowInit);
380
+ #endif
381
+ do {
382
+ if (!calibration.calibrated || needsCalibration) {
383
+ MUTEX_LOCK(&calibration.calibration_lock);
384
+ MUTEX_LOCK(&calibration.data_lock);
385
+
386
+ /* Recalibrate only if no one else did before us */
387
+ if(calibration.offset == cachedOffset) {
388
+ /* Since calibration can take a while, make any other
389
+ threads immediately wait */
390
+ MUTEX_SETSPINCOUNT(&calibration.data_lock, 0);
391
+
392
+ NowCalibrate();
393
+
394
+ calibrated = JS_TRUE;
395
+
396
+ /* Restore spin count */
397
+ MUTEX_SETSPINCOUNT(&calibration.data_lock, DATALOCK_SPINCOUNT);
398
+ }
399
+ MUTEX_UNLOCK(&calibration.data_lock);
400
+ MUTEX_UNLOCK(&calibration.calibration_lock);
401
+ }
402
+
403
+
404
+ /* Calculate a low resolution time */
405
+ GetSystemTimeAsFileTime(&ft);
406
+ lowresTime = 0.1*(long double)(FILETIME2INT64(ft) - win2un);
407
+
408
+ if (calibration.freq > 0.0) {
409
+ long double highresTime, diff;
410
+
411
+ DWORD timeAdjustment, timeIncrement;
412
+ BOOL timeAdjustmentDisabled;
413
+
414
+ /* Default to 15.625 ms if the syscall fails */
415
+ long double skewThreshold = 15625.25;
416
+ /* Grab high resolution time */
417
+ QueryPerformanceCounter(&now);
418
+ highresTimerValue = (long double)now.QuadPart;
419
+
420
+ MUTEX_LOCK(&calibration.data_lock);
421
+ highresTime = calibration.offset + PRMJ_USEC_PER_SEC*
422
+ (highresTimerValue-calibration.timer_offset)/calibration.freq;
423
+ cachedOffset = calibration.offset;
424
+
425
+ /* On some dual processor/core systems, we might get an earlier time
426
+ so we cache the last time that we returned */
427
+ calibration.last = max(calibration.last,(JSInt64)highresTime);
428
+ returnedTime = calibration.last;
429
+ MUTEX_UNLOCK(&calibration.data_lock);
430
+
431
+ /* Rather than assume the NT kernel ticks every 15.6ms, ask it */
432
+ if (GetSystemTimeAdjustment(&timeAdjustment,
433
+ &timeIncrement,
434
+ &timeAdjustmentDisabled)) {
435
+ if (timeAdjustmentDisabled) {
436
+ /* timeAdjustment is in units of 100ns */
437
+ skewThreshold = timeAdjustment/10.0;
438
+ } else {
439
+ /* timeIncrement is in units of 100ns */
440
+ skewThreshold = timeIncrement/10.0;
441
+ }
442
+ }
443
+
444
+ /* Check for clock skew */
445
+ diff = lowresTime - highresTime;
446
+
447
+ /* For some reason that I have not determined, the skew can be
448
+ up to twice a kernel tick. This does not seem to happen by
449
+ itself, but I have only seen it triggered by another program
450
+ doing some kind of file I/O. The symptoms are a negative diff
451
+ followed by an equally large positive diff. */
452
+ if (fabs(diff) > 2*skewThreshold) {
453
+ /*fprintf(stderr,"Clock skew detected (diff = %f)!\n", diff);*/
454
+
455
+ if (calibrated) {
456
+ /* If we already calibrated once this instance, and the
457
+ clock is still skewed, then either the processor(s) are
458
+ wildly changing clockspeed or the system is so busy that
459
+ we get switched out for long periods of time. In either
460
+ case, it would be infeasible to make use of high
461
+ resolution results for anything, so let's resort to old
462
+ behavior for this call. It's possible that in the
463
+ future, the user will want the high resolution timer, so
464
+ we don't disable it entirely. */
465
+ returnedTime = (JSInt64)lowresTime;
466
+ needsCalibration = JS_FALSE;
467
+ } else {
468
+ /* It is possible that when we recalibrate, we will return a
469
+ value less than what we have returned before; this is
470
+ unavoidable. We cannot tell the different between a
471
+ faulty QueryPerformanceCounter implementation and user
472
+ changes to the operating system time. Since we must
473
+ respect user changes to the operating system time, we
474
+ cannot maintain the invariant that Date.now() never
475
+ decreases; the old implementation has this behavior as
476
+ well. */
477
+ needsCalibration = JS_TRUE;
478
+ }
479
+ } else {
480
+ /* No detectable clock skew */
481
+ returnedTime = (JSInt64)highresTime;
482
+ needsCalibration = JS_FALSE;
483
+ }
484
+ } else {
485
+ /* No high resolution timer is available, so fall back */
486
+ returnedTime = (JSInt64)lowresTime;
487
+ }
488
+ } while (needsCalibration);
489
+
490
+ return returnedTime;
491
+ #endif
492
+
493
+ #if defined(XP_UNIX) || defined(XP_BEOS)
494
+ #ifdef _SVID_GETTOD /* Defined only on Solaris, see Solaris <sys/types.h> */
495
+ gettimeofday(&tv);
496
+ #else
497
+ gettimeofday(&tv, 0);
498
+ #endif /* _SVID_GETTOD */
499
+ JSLL_UI2L(s2us, PRMJ_USEC_PER_SEC);
500
+ JSLL_UI2L(s, tv.tv_sec);
501
+ JSLL_UI2L(us, tv.tv_usec);
502
+ JSLL_MUL(s, s, s2us);
503
+ JSLL_ADD(s, s, us);
504
+ return s;
505
+ #endif /* XP_UNIX */
506
+ }
507
+
508
+ /* Get the DST timezone offset for the time passed in */
509
+ JSInt64
510
+ PRMJ_DSTOffset(JSInt64 local_time)
511
+ {
512
+ JSInt64 us2s;
513
+ time_t local;
514
+ JSInt32 diff;
515
+ JSInt64 maxtimet;
516
+ struct tm tm;
517
+ PRMJTime prtm;
518
+ #ifndef HAVE_LOCALTIME_R
519
+ struct tm *ptm;
520
+ #endif
521
+
522
+
523
+ JSLL_UI2L(us2s, PRMJ_USEC_PER_SEC);
524
+ JSLL_DIV(local_time, local_time, us2s);
525
+
526
+ /* get the maximum of time_t value */
527
+ JSLL_UI2L(maxtimet,PRMJ_MAX_UNIX_TIMET);
528
+
529
+ if(JSLL_CMP(local_time,>,maxtimet)){
530
+ JSLL_UI2L(local_time,PRMJ_MAX_UNIX_TIMET);
531
+ } else if(!JSLL_GE_ZERO(local_time)){
532
+ /*go ahead a day to make localtime work (does not work with 0) */
533
+ JSLL_UI2L(local_time,PRMJ_DAY_SECONDS);
534
+ }
535
+ JSLL_L2UI(local,local_time);
536
+ PRMJ_basetime(local_time,&prtm);
537
+ #ifndef HAVE_LOCALTIME_R
538
+ ptm = localtime(&local);
539
+ if(!ptm){
540
+ return 0;
541
+ }
542
+ tm = *ptm;
543
+ #else
544
+ localtime_r(&local,&tm); /* get dst information */
545
+ #endif
546
+
547
+ diff = ((tm.tm_hour - prtm.tm_hour) * PRMJ_HOUR_SECONDS) +
548
+ ((tm.tm_min - prtm.tm_min) * 60);
549
+
550
+ if(diff < 0){
551
+ diff += PRMJ_DAY_SECONDS;
552
+ }
553
+
554
+ JSLL_UI2L(local_time,diff);
555
+
556
+ JSLL_MUL(local_time,local_time,us2s);
557
+
558
+ return(local_time);
559
+ }
560
+
561
+ #ifdef NS_HAVE_INVALID_PARAMETER_HANDLER
562
+ JS_STATIC_DLL_CALLBACK(void)
563
+ PRMJ_InvalidParameterHandler(const wchar_t *expression,
564
+ const wchar_t *function,
565
+ const wchar_t *file,
566
+ unsigned int line,
567
+ uintptr_t pReserved)
568
+ {
569
+ /* empty */
570
+ }
571
+ #endif
572
+
573
+ /* Format a time value into a buffer. Same semantics as strftime() */
574
+ size_t
575
+ PRMJ_FormatTime(char *buf, int buflen, const char *fmt, PRMJTime *prtm)
576
+ {
577
+ size_t result = 0;
578
+ #if defined(XP_UNIX) || defined(XP_WIN) || defined(XP_OS2) || defined(XP_BEOS)
579
+ struct tm a;
580
+ int fake_tm_year = 0;
581
+ #ifdef NS_HAVE_INVALID_PARAMETER_HANDLER
582
+ _invalid_parameter_handler oldHandler;
583
+ int oldReportMode;
584
+ #endif
585
+
586
+ /* Zero out the tm struct. Linux, SunOS 4 struct tm has extra members int
587
+ * tm_gmtoff, char *tm_zone; when tm_zone is garbage, strftime gets
588
+ * confused and dumps core. NSPR20 prtime.c attempts to fill these in by
589
+ * calling mktime on the partially filled struct, but this doesn't seem to
590
+ * work as well; the result string has "can't get timezone" for ECMA-valid
591
+ * years. Might still make sense to use this, but find the range of years
592
+ * for which valid tz information exists, and map (per ECMA hint) from the
593
+ * given year into that range.
594
+
595
+ * N.B. This hasn't been tested with anything that actually _uses_
596
+ * tm_gmtoff; zero might be the wrong thing to set it to if you really need
597
+ * to format a time. This fix is for jsdate.c, which only uses
598
+ * JS_FormatTime to get a string representing the time zone. */
599
+ memset(&a, 0, sizeof(struct tm));
600
+
601
+ a.tm_sec = prtm->tm_sec;
602
+ a.tm_min = prtm->tm_min;
603
+ a.tm_hour = prtm->tm_hour;
604
+ a.tm_mday = prtm->tm_mday;
605
+ a.tm_mon = prtm->tm_mon;
606
+ a.tm_wday = prtm->tm_wday;
607
+
608
+ /*
609
+ * Years before 1900 and after 9999 cause strftime() to abort on Windows.
610
+ * To avoid that we replace it with FAKE_YEAR_BASE + year % 100 and then
611
+ * replace matching substrings in the strftime() result with the real year.
612
+ * Note that FAKE_YEAR_BASE should be a multiple of 100 to make 2-digit
613
+ * year formats (%y) work correctly (since we won't find the fake year
614
+ * in that case).
615
+ * e.g. new Date(1873, 0).toLocaleFormat('%Y %y') => "1873 73"
616
+ * See bug 327869.
617
+ */
618
+ #define FAKE_YEAR_BASE 9900
619
+ if (prtm->tm_year < 1900 || prtm->tm_year > 9999) {
620
+ fake_tm_year = FAKE_YEAR_BASE + prtm->tm_year % 100;
621
+ a.tm_year = fake_tm_year - 1900;
622
+ }
623
+ else {
624
+ a.tm_year = prtm->tm_year - 1900;
625
+ }
626
+ a.tm_yday = prtm->tm_yday;
627
+ a.tm_isdst = prtm->tm_isdst;
628
+
629
+ /*
630
+ * Even with the above, SunOS 4 seems to detonate if tm_zone and tm_gmtoff
631
+ * are null. This doesn't quite work, though - the timezone is off by
632
+ * tzoff + dst. (And mktime seems to return -1 for the exact dst
633
+ * changeover time.)
634
+ */
635
+
636
+ #ifdef NS_HAVE_INVALID_PARAMETER_HANDLER
637
+ oldHandler = _set_invalid_parameter_handler(PRMJ_InvalidParameterHandler);
638
+ oldReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
639
+ #endif
640
+
641
+ result = strftime(buf, buflen, fmt, &a);
642
+
643
+ #ifdef NS_HAVE_INVALID_PARAMETER_HANDLER
644
+ _set_invalid_parameter_handler(oldHandler);
645
+ _CrtSetReportMode(_CRT_ASSERT, oldReportMode);
646
+ #endif
647
+
648
+ if (fake_tm_year && result) {
649
+ char real_year[16];
650
+ char fake_year[16];
651
+ size_t real_year_len;
652
+ size_t fake_year_len;
653
+ char* p;
654
+
655
+ sprintf(real_year, "%d", prtm->tm_year);
656
+ real_year_len = strlen(real_year);
657
+ sprintf(fake_year, "%d", fake_tm_year);
658
+ fake_year_len = strlen(fake_year);
659
+
660
+ /* Replace the fake year in the result with the real year. */
661
+ for (p = buf; (p = strstr(p, fake_year)); p += real_year_len) {
662
+ size_t new_result = result + real_year_len - fake_year_len;
663
+ if ((int)new_result >= buflen) {
664
+ return 0;
665
+ }
666
+ memmove(p + real_year_len, p + fake_year_len, strlen(p + fake_year_len));
667
+ memcpy(p, real_year, real_year_len);
668
+ result = new_result;
669
+ *(buf + result) = '\0';
670
+ }
671
+ }
672
+ #endif
673
+ return result;
674
+ }
675
+
676
+ /* table for number of days in a month */
677
+ static int mtab[] = {
678
+ /* jan, feb,mar,apr,may,jun */
679
+ 31,28,31,30,31,30,
680
+ /* july,aug,sep,oct,nov,dec */
681
+ 31,31,30,31,30,31
682
+ };
683
+
684
+ /*
685
+ * basic time calculation functionality for localtime and gmtime
686
+ * setups up prtm argument with correct values based upon input number
687
+ * of seconds.
688
+ */
689
+ static void
690
+ PRMJ_basetime(JSInt64 tsecs, PRMJTime *prtm)
691
+ {
692
+ /* convert tsecs back to year,month,day,hour,secs */
693
+ JSInt32 year = 0;
694
+ JSInt32 month = 0;
695
+ JSInt32 yday = 0;
696
+ JSInt32 mday = 0;
697
+ JSInt32 wday = 6; /* start on a Sunday */
698
+ JSInt32 days = 0;
699
+ JSInt32 seconds = 0;
700
+ JSInt32 minutes = 0;
701
+ JSInt32 hours = 0;
702
+ JSInt32 isleap = 0;
703
+
704
+ /* Temporaries used for various computations */
705
+ JSInt64 result;
706
+ JSInt64 result1;
707
+ JSInt64 result2;
708
+
709
+ JSInt64 base;
710
+
711
+ /* Some variables for intermediate result storage to make computing isleap
712
+ easier/faster */
713
+ JSInt32 fourCenturyBlocks;
714
+ JSInt32 centuriesLeft;
715
+ JSInt32 fourYearBlocksLeft;
716
+ JSInt32 yearsLeft;
717
+
718
+ /* Since leap years work by 400/100/4 year intervals, precompute the length
719
+ of those in seconds if they start at the beginning of year 1. */
720
+ JSInt64 fourYears;
721
+ JSInt64 century;
722
+ JSInt64 fourCenturies;
723
+
724
+ JSLL_UI2L(result, PRMJ_DAY_SECONDS);
725
+
726
+ JSLL_I2L(fourYears, PRMJ_FOUR_YEARS_DAYS);
727
+ JSLL_MUL(fourYears, fourYears, result);
728
+
729
+ JSLL_I2L(century, PRMJ_CENTURY_DAYS);
730
+ JSLL_MUL(century, century, result);
731
+
732
+ JSLL_I2L(fourCenturies, PRMJ_FOUR_CENTURIES_DAYS);
733
+ JSLL_MUL(fourCenturies, fourCenturies, result);
734
+
735
+ /* get the base time via UTC */
736
+ base = PRMJ_ToExtendedTime(0);
737
+ JSLL_UI2L(result, PRMJ_USEC_PER_SEC);
738
+ JSLL_DIV(base,base,result);
739
+ JSLL_ADD(tsecs,tsecs,base);
740
+
741
+ /* Compute our |year|, |isleap|, and part of |days|. When this part is
742
+ done, |year| should hold the year our date falls in (number of whole
743
+ years elapsed before our date), isleap should hold 1 if the year the
744
+ date falls in is a leap year and 0 otherwise. */
745
+
746
+ /* First do year 0; it's special and nonleap. */
747
+ JSLL_UI2L(result, PRMJ_YEAR_SECONDS);
748
+ if (!JSLL_CMP(tsecs,<,result)) {
749
+ days = PRMJ_YEAR_DAYS;
750
+ year = 1;
751
+ JSLL_SUB(tsecs, tsecs, result);
752
+ }
753
+
754
+ /* Now use those constants we computed above */
755
+ JSLL_UDIVMOD(&result1, &result2, tsecs, fourCenturies);
756
+ JSLL_L2I(fourCenturyBlocks, result1);
757
+ year += fourCenturyBlocks * 400;
758
+ days += fourCenturyBlocks * PRMJ_FOUR_CENTURIES_DAYS;
759
+ tsecs = result2;
760
+
761
+ JSLL_UDIVMOD(&result1, &result2, tsecs, century);
762
+ JSLL_L2I(centuriesLeft, result1);
763
+ year += centuriesLeft * 100;
764
+ days += centuriesLeft * PRMJ_CENTURY_DAYS;
765
+ tsecs = result2;
766
+
767
+ JSLL_UDIVMOD(&result1, &result2, tsecs, fourYears);
768
+ JSLL_L2I(fourYearBlocksLeft, result1);
769
+ year += fourYearBlocksLeft * 4;
770
+ days += fourYearBlocksLeft * PRMJ_FOUR_YEARS_DAYS;
771
+ tsecs = result2;
772
+
773
+ /* Recall that |result| holds PRMJ_YEAR_SECONDS */
774
+ JSLL_UDIVMOD(&result1, &result2, tsecs, result);
775
+ JSLL_L2I(yearsLeft, result1);
776
+ year += yearsLeft;
777
+ days += yearsLeft * PRMJ_YEAR_DAYS;
778
+ tsecs = result2;
779
+
780
+ /* now compute isleap. Note that we don't have to use %, since we've
781
+ already computed those remainders. Also note that they're all offset by
782
+ 1 because of the 1 for year 0. */
783
+ isleap =
784
+ (yearsLeft == 3) && (fourYearBlocksLeft != 24 || centuriesLeft == 3);
785
+ JS_ASSERT(isleap ==
786
+ ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)));
787
+
788
+ JSLL_UI2L(result1,PRMJ_DAY_SECONDS);
789
+
790
+ JSLL_DIV(result,tsecs,result1);
791
+ JSLL_L2I(mday,result);
792
+
793
+ /* let's find the month */
794
+ while(((month == 1 && isleap) ?
795
+ (mday >= mtab[month] + 1) :
796
+ (mday >= mtab[month]))){
797
+ yday += mtab[month];
798
+ days += mtab[month];
799
+
800
+ mday -= mtab[month];
801
+
802
+ /* it's a Feb, check if this is a leap year */
803
+ if(month == 1 && isleap != 0){
804
+ yday++;
805
+ days++;
806
+ mday--;
807
+ }
808
+ month++;
809
+ }
810
+
811
+ /* now adjust tsecs */
812
+ JSLL_MUL(result,result,result1);
813
+ JSLL_SUB(tsecs,tsecs,result);
814
+
815
+ mday++; /* day of month always start with 1 */
816
+ days += mday;
817
+ wday = (days + wday) % 7;
818
+
819
+ yday += mday;
820
+
821
+ /* get the hours */
822
+ JSLL_UI2L(result1,PRMJ_HOUR_SECONDS);
823
+ JSLL_DIV(result,tsecs,result1);
824
+ JSLL_L2I(hours,result);
825
+ JSLL_MUL(result,result,result1);
826
+ JSLL_SUB(tsecs,tsecs,result);
827
+
828
+ /* get minutes */
829
+ JSLL_UI2L(result1,60);
830
+ JSLL_DIV(result,tsecs,result1);
831
+ JSLL_L2I(minutes,result);
832
+ JSLL_MUL(result,result,result1);
833
+ JSLL_SUB(tsecs,tsecs,result);
834
+
835
+ JSLL_L2I(seconds,tsecs);
836
+
837
+ prtm->tm_usec = 0L;
838
+ prtm->tm_sec = (JSInt8)seconds;
839
+ prtm->tm_min = (JSInt8)minutes;
840
+ prtm->tm_hour = (JSInt8)hours;
841
+ prtm->tm_mday = (JSInt8)mday;
842
+ prtm->tm_mon = (JSInt8)month;
843
+ prtm->tm_wday = (JSInt8)wday;
844
+ prtm->tm_year = (JSInt16)year;
845
+ prtm->tm_yday = (JSInt16)yday;
846
+ }