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,43 @@
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
+
41
+ #define js_invoke_c__
42
+
43
+ #include "jsinterp.c"
@@ -0,0 +1,1067 @@
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
+
41
+ /*
42
+ * JavaScript iterators.
43
+ */
44
+ #include "jsstddef.h"
45
+ #include <string.h> /* for memcpy */
46
+ #include "jstypes.h"
47
+ #include "jsutil.h"
48
+ #include "jsarena.h"
49
+ #include "jsapi.h"
50
+ #include "jsarray.h"
51
+ #include "jsatom.h"
52
+ #include "jsbool.h"
53
+ #include "jscntxt.h"
54
+ #include "jsconfig.h"
55
+ #include "jsexn.h"
56
+ #include "jsfun.h"
57
+ #include "jsgc.h"
58
+ #include "jsinterp.h"
59
+ #include "jsiter.h"
60
+ #include "jslock.h"
61
+ #include "jsnum.h"
62
+ #include "jsobj.h"
63
+ #include "jsopcode.h"
64
+ #include "jsscan.h"
65
+ #include "jsscope.h"
66
+ #include "jsscript.h"
67
+
68
+ #if JS_HAS_XML_SUPPORT
69
+ #include "jsxml.h"
70
+ #endif
71
+
72
+ #define JSSLOT_ITER_STATE (JSSLOT_PRIVATE)
73
+ #define JSSLOT_ITER_FLAGS (JSSLOT_PRIVATE + 1)
74
+
75
+ #if JSSLOT_ITER_FLAGS >= JS_INITIAL_NSLOTS
76
+ #error JS_INITIAL_NSLOTS must be greater than JSSLOT_ITER_FLAGS.
77
+ #endif
78
+
79
+ #if JS_HAS_GENERATORS
80
+
81
+ static JSBool
82
+ CloseGenerator(JSContext *cx, JSObject *genobj);
83
+
84
+ #endif
85
+
86
+ /*
87
+ * Shared code to close iterator's state either through an explicit call or
88
+ * when GC detects that the iterator is no longer reachable.
89
+ */
90
+ void
91
+ js_CloseNativeIterator(JSContext *cx, JSObject *iterobj)
92
+ {
93
+ jsval state;
94
+ JSObject *iterable;
95
+
96
+ JS_ASSERT(STOBJ_GET_CLASS(iterobj) == &js_IteratorClass);
97
+
98
+ /* Avoid double work if js_CloseNativeIterator was called on obj. */
99
+ state = STOBJ_GET_SLOT(iterobj, JSSLOT_ITER_STATE);
100
+ if (JSVAL_IS_NULL(state))
101
+ return;
102
+
103
+ /* Protect against failure to fully initialize obj. */
104
+ iterable = STOBJ_GET_PARENT(iterobj);
105
+ if (iterable) {
106
+ #if JS_HAS_XML_SUPPORT
107
+ uintN flags = JSVAL_TO_INT(STOBJ_GET_SLOT(iterobj, JSSLOT_ITER_FLAGS));
108
+ if ((flags & JSITER_FOREACH) && OBJECT_IS_XML(cx, iterable)) {
109
+ ((JSXMLObjectOps *) iterable->map->ops)->
110
+ enumerateValues(cx, iterable, JSENUMERATE_DESTROY, &state,
111
+ NULL, NULL);
112
+ } else
113
+ #endif
114
+ OBJ_ENUMERATE(cx, iterable, JSENUMERATE_DESTROY, &state, NULL);
115
+ }
116
+ STOBJ_SET_SLOT(iterobj, JSSLOT_ITER_STATE, JSVAL_NULL);
117
+ }
118
+
119
+ JSClass js_IteratorClass = {
120
+ "Iterator",
121
+ JSCLASS_HAS_RESERVED_SLOTS(2) | /* slots for state and flags */
122
+ JSCLASS_HAS_CACHED_PROTO(JSProto_Iterator),
123
+ JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
124
+ JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
125
+ JSCLASS_NO_OPTIONAL_MEMBERS
126
+ };
127
+
128
+ static JSBool
129
+ InitNativeIterator(JSContext *cx, JSObject *iterobj, JSObject *obj, uintN flags)
130
+ {
131
+ jsval state;
132
+ JSBool ok;
133
+
134
+ JS_ASSERT(STOBJ_GET_CLASS(iterobj) == &js_IteratorClass);
135
+
136
+ /* Initialize iterobj in case of enumerate hook failure. */
137
+ STOBJ_SET_PARENT(iterobj, obj);
138
+ STOBJ_SET_SLOT(iterobj, JSSLOT_ITER_STATE, JSVAL_NULL);
139
+ STOBJ_SET_SLOT(iterobj, JSSLOT_ITER_FLAGS, INT_TO_JSVAL(flags));
140
+ if (!js_RegisterCloseableIterator(cx, iterobj))
141
+ return JS_FALSE;
142
+ if (!obj)
143
+ return JS_TRUE;
144
+
145
+ ok =
146
+ #if JS_HAS_XML_SUPPORT
147
+ ((flags & JSITER_FOREACH) && OBJECT_IS_XML(cx, obj))
148
+ ? ((JSXMLObjectOps *) obj->map->ops)->
149
+ enumerateValues(cx, obj, JSENUMERATE_INIT, &state, NULL, NULL)
150
+ :
151
+ #endif
152
+ OBJ_ENUMERATE(cx, obj, JSENUMERATE_INIT, &state, NULL);
153
+ if (!ok)
154
+ return JS_FALSE;
155
+
156
+ STOBJ_SET_SLOT(iterobj, JSSLOT_ITER_STATE, state);
157
+ if (flags & JSITER_ENUMERATE) {
158
+ /*
159
+ * The enumerating iterator needs the original object to suppress
160
+ * enumeration of deleted or shadowed prototype properties. Since the
161
+ * enumerator never escapes to scripts, we use the prototype slot to
162
+ * store the original object.
163
+ */
164
+ JS_ASSERT(obj != iterobj);
165
+ STOBJ_SET_PROTO(iterobj, obj);
166
+ }
167
+ return JS_TRUE;
168
+ }
169
+
170
+ static JSBool
171
+ Iterator(JSContext *cx, JSObject *iterobj, uintN argc, jsval *argv, jsval *rval)
172
+ {
173
+ JSBool keyonly;
174
+ uintN flags;
175
+ JSObject *obj;
176
+
177
+ keyonly = js_ValueToBoolean(argv[1]);
178
+ flags = keyonly ? 0 : JSITER_FOREACH;
179
+
180
+ if (cx->fp->flags & JSFRAME_CONSTRUCTING) {
181
+ /* XXX work around old valueOf call hidden beneath js_ValueToObject */
182
+ if (!JSVAL_IS_PRIMITIVE(argv[0])) {
183
+ obj = JSVAL_TO_OBJECT(argv[0]);
184
+ } else {
185
+ obj = js_ValueToNonNullObject(cx, argv[0]);
186
+ if (!obj)
187
+ return JS_FALSE;
188
+ argv[0] = OBJECT_TO_JSVAL(obj);
189
+ }
190
+ return InitNativeIterator(cx, iterobj, obj, flags);
191
+ }
192
+
193
+ *rval = argv[0];
194
+ return js_ValueToIterator(cx, flags, rval);
195
+ }
196
+
197
+ static JSBool
198
+ NewKeyValuePair(JSContext *cx, jsid key, jsval val, jsval *rval)
199
+ {
200
+ jsval vec[2];
201
+ JSTempValueRooter tvr;
202
+ JSObject *aobj;
203
+
204
+ vec[0] = ID_TO_VALUE(key);
205
+ vec[1] = val;
206
+
207
+ JS_PUSH_TEMP_ROOT(cx, 2, vec, &tvr);
208
+ aobj = js_NewArrayObject(cx, 2, vec);
209
+ *rval = OBJECT_TO_JSVAL(aobj);
210
+ JS_POP_TEMP_ROOT(cx, &tvr);
211
+
212
+ return aobj != NULL;
213
+ }
214
+
215
+ static JSBool
216
+ IteratorNextImpl(JSContext *cx, JSObject *obj, jsval *rval)
217
+ {
218
+ JSObject *iterable;
219
+ jsval state;
220
+ uintN flags;
221
+ JSBool foreach, ok;
222
+ jsid id;
223
+
224
+ JS_ASSERT(OBJ_GET_CLASS(cx, obj) == &js_IteratorClass);
225
+
226
+ iterable = OBJ_GET_PARENT(cx, obj);
227
+ JS_ASSERT(iterable);
228
+ state = OBJ_GET_SLOT(cx, obj, JSSLOT_ITER_STATE);
229
+ if (JSVAL_IS_NULL(state))
230
+ goto stop;
231
+
232
+ flags = JSVAL_TO_INT(OBJ_GET_SLOT(cx, obj, JSSLOT_ITER_FLAGS));
233
+ JS_ASSERT(!(flags & JSITER_ENUMERATE));
234
+ foreach = (flags & JSITER_FOREACH) != 0;
235
+ ok =
236
+ #if JS_HAS_XML_SUPPORT
237
+ (foreach && OBJECT_IS_XML(cx, iterable))
238
+ ? ((JSXMLObjectOps *) iterable->map->ops)->
239
+ enumerateValues(cx, iterable, JSENUMERATE_NEXT, &state,
240
+ &id, rval)
241
+ :
242
+ #endif
243
+ OBJ_ENUMERATE(cx, iterable, JSENUMERATE_NEXT, &state, &id);
244
+ if (!ok)
245
+ return JS_FALSE;
246
+
247
+ STOBJ_SET_SLOT(obj, JSSLOT_ITER_STATE, state);
248
+ if (JSVAL_IS_NULL(state))
249
+ goto stop;
250
+
251
+ if (foreach) {
252
+ #if JS_HAS_XML_SUPPORT
253
+ if (!OBJECT_IS_XML(cx, iterable) &&
254
+ !OBJ_GET_PROPERTY(cx, iterable, id, rval)) {
255
+ return JS_FALSE;
256
+ }
257
+ #endif
258
+ if (!NewKeyValuePair(cx, id, *rval, rval))
259
+ return JS_FALSE;
260
+ } else {
261
+ *rval = ID_TO_VALUE(id);
262
+ }
263
+ return JS_TRUE;
264
+
265
+ stop:
266
+ JS_ASSERT(OBJ_GET_SLOT(cx, obj, JSSLOT_ITER_STATE) == JSVAL_NULL);
267
+ *rval = JSVAL_HOLE;
268
+ return JS_TRUE;
269
+ }
270
+
271
+ JSBool
272
+ js_ThrowStopIteration(JSContext *cx)
273
+ {
274
+ jsval v;
275
+
276
+ JS_ASSERT(!JS_IsExceptionPending(cx));
277
+ if (js_FindClassObject(cx, NULL, INT_TO_JSID(JSProto_StopIteration), &v))
278
+ JS_SetPendingException(cx, v);
279
+ return JS_FALSE;
280
+ }
281
+
282
+ static JSBool
283
+ iterator_next(JSContext *cx, uintN argc, jsval *vp)
284
+ {
285
+ JSObject *obj;
286
+
287
+ obj = JS_THIS_OBJECT(cx, vp);
288
+ if (!JS_InstanceOf(cx, obj, &js_IteratorClass, vp + 2))
289
+ return JS_FALSE;
290
+
291
+ if (!IteratorNextImpl(cx, obj, vp))
292
+ return JS_FALSE;
293
+
294
+ if (*vp == JSVAL_HOLE) {
295
+ *vp = JSVAL_NULL;
296
+ js_ThrowStopIteration(cx);
297
+ return JS_FALSE;
298
+ }
299
+ return JS_TRUE;
300
+ }
301
+
302
+ static JSBool
303
+ iterator_self(JSContext *cx, uintN argc, jsval *vp)
304
+ {
305
+ *vp = JS_THIS(cx, vp);
306
+ return !JSVAL_IS_NULL(*vp);
307
+ }
308
+
309
+ #define JSPROP_ROPERM (JSPROP_READONLY | JSPROP_PERMANENT)
310
+
311
+ static JSFunctionSpec iterator_methods[] = {
312
+ JS_FN(js_iterator_str, iterator_self, 0,0,JSPROP_ROPERM),
313
+ JS_FN(js_next_str, iterator_next, 0,0,JSPROP_ROPERM),
314
+ JS_FS_END
315
+ };
316
+
317
+ uintN
318
+ js_GetNativeIteratorFlags(JSContext *cx, JSObject *iterobj)
319
+ {
320
+ if (OBJ_GET_CLASS(cx, iterobj) != &js_IteratorClass)
321
+ return 0;
322
+ return JSVAL_TO_INT(OBJ_GET_SLOT(cx, iterobj, JSSLOT_ITER_FLAGS));
323
+ }
324
+
325
+ /*
326
+ * Call ToObject(v).__iterator__(keyonly) if ToObject(v).__iterator__ exists.
327
+ * Otherwise construct the default iterator.
328
+ */
329
+ JS_FRIEND_API(JSBool)
330
+ js_ValueToIterator(JSContext *cx, uintN flags, jsval *vp)
331
+ {
332
+ JSObject *obj;
333
+ JSTempValueRooter tvr;
334
+ JSAtom *atom;
335
+ JSClass *clasp;
336
+ JSExtendedClass *xclasp;
337
+ JSBool ok;
338
+ JSObject *iterobj;
339
+ jsval arg;
340
+
341
+ JS_ASSERT(!(flags & ~(JSITER_ENUMERATE |
342
+ JSITER_FOREACH |
343
+ JSITER_KEYVALUE)));
344
+
345
+ /* JSITER_KEYVALUE must always come with JSITER_FOREACH */
346
+ JS_ASSERT(!(flags & JSITER_KEYVALUE) || (flags & JSITER_FOREACH));
347
+
348
+ /* XXX work around old valueOf call hidden beneath js_ValueToObject */
349
+ if (!JSVAL_IS_PRIMITIVE(*vp)) {
350
+ obj = JSVAL_TO_OBJECT(*vp);
351
+ } else {
352
+ /*
353
+ * Enumerating over null and undefined gives an empty enumerator.
354
+ * This is contrary to ECMA-262 9.9 ToObject, invoked from step 3 of
355
+ * the first production in 12.6.4 and step 4 of the second production,
356
+ * but it's "web JS" compatible.
357
+ */
358
+ if ((flags & JSITER_ENUMERATE)) {
359
+ if (!js_ValueToObject(cx, *vp, &obj))
360
+ return JS_FALSE;
361
+ if (!obj)
362
+ goto default_iter;
363
+ } else {
364
+ obj = js_ValueToNonNullObject(cx, *vp);
365
+ if (!obj)
366
+ return JS_FALSE;
367
+ }
368
+ }
369
+
370
+ JS_ASSERT(obj);
371
+ JS_PUSH_TEMP_ROOT_OBJECT(cx, obj, &tvr);
372
+
373
+ clasp = OBJ_GET_CLASS(cx, obj);
374
+ if ((clasp->flags & JSCLASS_IS_EXTENDED) &&
375
+ (xclasp = (JSExtendedClass *) clasp)->iteratorObject) {
376
+ iterobj = xclasp->iteratorObject(cx, obj, !(flags & JSITER_FOREACH));
377
+ if (!iterobj)
378
+ goto bad;
379
+ *vp = OBJECT_TO_JSVAL(iterobj);
380
+ } else {
381
+ atom = cx->runtime->atomState.iteratorAtom;
382
+ #if JS_HAS_XML_SUPPORT
383
+ if (OBJECT_IS_XML(cx, obj)) {
384
+ if (!js_GetXMLFunction(cx, obj, ATOM_TO_JSID(atom), vp))
385
+ goto bad;
386
+ } else
387
+ #endif
388
+ {
389
+ if (!OBJ_GET_PROPERTY(cx, obj, ATOM_TO_JSID(atom), vp))
390
+ goto bad;
391
+ }
392
+
393
+ if (JSVAL_IS_VOID(*vp)) {
394
+ default_iter:
395
+ /*
396
+ * Fail over to the default enumerating native iterator.
397
+ *
398
+ * Create iterobj with a NULL parent to ensure that we use the
399
+ * correct scope chain to lookup the iterator's constructor. Since
400
+ * we use the parent slot to keep track of the iterable, we must
401
+ * fix it up after.
402
+ */
403
+ iterobj = js_NewObject(cx, &js_IteratorClass, NULL, NULL, 0);
404
+ if (!iterobj)
405
+ goto bad;
406
+
407
+ /* Store in *vp to protect it from GC (callers must root vp). */
408
+ *vp = OBJECT_TO_JSVAL(iterobj);
409
+
410
+ if (!InitNativeIterator(cx, iterobj, obj, flags))
411
+ goto bad;
412
+ } else {
413
+ arg = BOOLEAN_TO_JSVAL((flags & JSITER_FOREACH) == 0);
414
+ if (!js_InternalInvoke(cx, obj, *vp, JSINVOKE_ITERATOR, 1, &arg,
415
+ vp)) {
416
+ goto bad;
417
+ }
418
+ if (JSVAL_IS_PRIMITIVE(*vp)) {
419
+ const char *printable = js_AtomToPrintableString(cx, atom);
420
+ if (printable) {
421
+ js_ReportValueError2(cx, JSMSG_BAD_ITERATOR_RETURN,
422
+ JSDVG_SEARCH_STACK, *vp, NULL,
423
+ printable);
424
+ }
425
+ goto bad;
426
+ }
427
+ }
428
+ }
429
+
430
+ ok = JS_TRUE;
431
+ out:
432
+ if (obj)
433
+ JS_POP_TEMP_ROOT(cx, &tvr);
434
+ return ok;
435
+ bad:
436
+ ok = JS_FALSE;
437
+ goto out;
438
+ }
439
+
440
+ JS_FRIEND_API(JSBool)
441
+ js_CloseIterator(JSContext *cx, jsval v)
442
+ {
443
+ JSObject *obj;
444
+ JSClass *clasp;
445
+
446
+ JS_ASSERT(!JSVAL_IS_PRIMITIVE(v));
447
+ obj = JSVAL_TO_OBJECT(v);
448
+ clasp = OBJ_GET_CLASS(cx, obj);
449
+
450
+ if (clasp == &js_IteratorClass) {
451
+ js_CloseNativeIterator(cx, obj);
452
+ }
453
+ #if JS_HAS_GENERATORS
454
+ else if (clasp == &js_GeneratorClass) {
455
+ if (!CloseGenerator(cx, obj))
456
+ return JS_FALSE;
457
+ }
458
+ #endif
459
+ return JS_TRUE;
460
+ }
461
+
462
+ static JSBool
463
+ CallEnumeratorNext(JSContext *cx, JSObject *iterobj, uintN flags, jsval *rval)
464
+ {
465
+ JSObject *obj, *origobj;
466
+ jsval state;
467
+ JSBool foreach;
468
+ jsid id;
469
+ JSObject *obj2;
470
+ JSBool cond;
471
+ JSClass *clasp;
472
+ JSExtendedClass *xclasp;
473
+ JSProperty *prop;
474
+ JSString *str;
475
+
476
+ JS_ASSERT(flags & JSITER_ENUMERATE);
477
+ JS_ASSERT(STOBJ_GET_CLASS(iterobj) == &js_IteratorClass);
478
+
479
+ obj = STOBJ_GET_PARENT(iterobj);
480
+ origobj = STOBJ_GET_PROTO(iterobj);
481
+ state = STOBJ_GET_SLOT(iterobj, JSSLOT_ITER_STATE);
482
+ if (JSVAL_IS_NULL(state))
483
+ goto stop;
484
+
485
+ foreach = (flags & JSITER_FOREACH) != 0;
486
+ #if JS_HAS_XML_SUPPORT
487
+ /*
488
+ * Treat an XML object specially only when it starts the prototype chain.
489
+ * Otherwise we need to do the usual deleted and shadowed property checks.
490
+ */
491
+ if (obj == origobj && OBJECT_IS_XML(cx, obj)) {
492
+ if (foreach) {
493
+ JSXMLObjectOps *xmlops = (JSXMLObjectOps *) obj->map->ops;
494
+
495
+ if (!xmlops->enumerateValues(cx, obj, JSENUMERATE_NEXT, &state,
496
+ &id, rval)) {
497
+ return JS_FALSE;
498
+ }
499
+ } else {
500
+ if (!OBJ_ENUMERATE(cx, obj, JSENUMERATE_NEXT, &state, &id))
501
+ return JS_FALSE;
502
+ }
503
+ STOBJ_SET_SLOT(iterobj, JSSLOT_ITER_STATE, state);
504
+ if (JSVAL_IS_NULL(state))
505
+ goto stop;
506
+ } else
507
+ #endif
508
+ {
509
+ restart:
510
+ if (!OBJ_ENUMERATE(cx, obj, JSENUMERATE_NEXT, &state, &id))
511
+ return JS_FALSE;
512
+
513
+ STOBJ_SET_SLOT(iterobj, JSSLOT_ITER_STATE, state);
514
+ if (JSVAL_IS_NULL(state)) {
515
+ #if JS_HAS_XML_SUPPORT
516
+ if (OBJECT_IS_XML(cx, obj)) {
517
+ /*
518
+ * We just finished enumerating an XML obj that is present on
519
+ * the prototype chain of a non-XML origobj. Stop further
520
+ * prototype chain searches because XML objects don't
521
+ * enumerate prototypes.
522
+ */
523
+ JS_ASSERT(origobj != obj);
524
+ JS_ASSERT(!OBJECT_IS_XML(cx, origobj));
525
+ } else
526
+ #endif
527
+ {
528
+ obj = OBJ_GET_PROTO(cx, obj);
529
+ if (obj) {
530
+ STOBJ_SET_PARENT(iterobj, obj);
531
+ if (!OBJ_ENUMERATE(cx, obj, JSENUMERATE_INIT, &state, NULL))
532
+ return JS_FALSE;
533
+ STOBJ_SET_SLOT(iterobj, JSSLOT_ITER_STATE, state);
534
+ if (!JSVAL_IS_NULL(state))
535
+ goto restart;
536
+ }
537
+ }
538
+ goto stop;
539
+ }
540
+
541
+ /* Skip properties not in obj when looking from origobj. */
542
+ if (!OBJ_LOOKUP_PROPERTY(cx, origobj, id, &obj2, &prop))
543
+ return JS_FALSE;
544
+ if (!prop)
545
+ goto restart;
546
+ OBJ_DROP_PROPERTY(cx, obj2, prop);
547
+
548
+ /*
549
+ * If the id was found in a prototype object or an unrelated object
550
+ * (specifically, not in an inner object for obj), skip it. This step
551
+ * means that all OBJ_LOOKUP_PROPERTY implementations must return an
552
+ * object further along on the prototype chain, or else possibly an
553
+ * object returned by the JSExtendedClass.outerObject optional hook.
554
+ */
555
+ if (obj != obj2) {
556
+ cond = JS_FALSE;
557
+ clasp = OBJ_GET_CLASS(cx, obj2);
558
+ if (clasp->flags & JSCLASS_IS_EXTENDED) {
559
+ xclasp = (JSExtendedClass *) clasp;
560
+ cond = xclasp->outerObject &&
561
+ xclasp->outerObject(cx, obj2) == obj;
562
+ }
563
+ if (!cond)
564
+ goto restart;
565
+ }
566
+
567
+ if (foreach) {
568
+ /* Get property querying the original object. */
569
+ if (!OBJ_GET_PROPERTY(cx, origobj, id, rval))
570
+ return JS_FALSE;
571
+ }
572
+ }
573
+
574
+ if (foreach) {
575
+ if (flags & JSITER_KEYVALUE) {
576
+ if (!NewKeyValuePair(cx, id, *rval, rval))
577
+ return JS_FALSE;
578
+ }
579
+ } else {
580
+ /* Make rval a string for uniformity and compatibility. */
581
+ str = js_ValueToString(cx, ID_TO_VALUE(id));
582
+ if (!str)
583
+ return JS_FALSE;
584
+ *rval = STRING_TO_JSVAL(str);
585
+ }
586
+ return JS_TRUE;
587
+
588
+ stop:
589
+ JS_ASSERT(STOBJ_GET_SLOT(iterobj, JSSLOT_ITER_STATE) == JSVAL_NULL);
590
+ *rval = JSVAL_HOLE;
591
+ return JS_TRUE;
592
+ }
593
+
594
+ JS_FRIEND_API(JSBool)
595
+ js_CallIteratorNext(JSContext *cx, JSObject *iterobj, jsval *rval)
596
+ {
597
+ uintN flags;
598
+
599
+ /* Fast path for native iterators */
600
+ if (OBJ_GET_CLASS(cx, iterobj) == &js_IteratorClass) {
601
+ flags = JSVAL_TO_INT(OBJ_GET_SLOT(cx, iterobj, JSSLOT_ITER_FLAGS));
602
+ if (flags & JSITER_ENUMERATE)
603
+ return CallEnumeratorNext(cx, iterobj, flags, rval);
604
+
605
+ /*
606
+ * Call next directly as all the methods of the native iterator are
607
+ * read-only and permanent.
608
+ */
609
+ if (!IteratorNextImpl(cx, iterobj, rval))
610
+ return JS_FALSE;
611
+ } else {
612
+ jsid id = ATOM_TO_JSID(cx->runtime->atomState.nextAtom);
613
+
614
+ if (!JS_GetMethodById(cx, iterobj, id, &iterobj, rval))
615
+ return JS_FALSE;
616
+ if (!js_InternalCall(cx, iterobj, *rval, 0, NULL, rval)) {
617
+ /* Check for StopIteration. */
618
+ if (!cx->throwing ||
619
+ JSVAL_IS_PRIMITIVE(cx->exception) ||
620
+ OBJ_GET_CLASS(cx, JSVAL_TO_OBJECT(cx->exception))
621
+ != &js_StopIterationClass) {
622
+ return JS_FALSE;
623
+ }
624
+
625
+ /* Inline JS_ClearPendingException(cx). */
626
+ cx->throwing = JS_FALSE;
627
+ cx->exception = JSVAL_VOID;
628
+ *rval = JSVAL_HOLE;
629
+ return JS_TRUE;
630
+ }
631
+ }
632
+
633
+ return JS_TRUE;
634
+ }
635
+
636
+ static JSBool
637
+ stopiter_hasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp)
638
+ {
639
+ *bp = !JSVAL_IS_PRIMITIVE(v) &&
640
+ OBJ_GET_CLASS(cx, JSVAL_TO_OBJECT(v)) == &js_StopIterationClass;
641
+ return JS_TRUE;
642
+ }
643
+
644
+ JSClass js_StopIterationClass = {
645
+ js_StopIteration_str,
646
+ JSCLASS_HAS_CACHED_PROTO(JSProto_StopIteration),
647
+ JS_PropertyStub, JS_PropertyStub,
648
+ JS_PropertyStub, JS_PropertyStub,
649
+ JS_EnumerateStub, JS_ResolveStub,
650
+ JS_ConvertStub, JS_FinalizeStub,
651
+ NULL, NULL,
652
+ NULL, NULL,
653
+ NULL, stopiter_hasInstance,
654
+ NULL, NULL
655
+ };
656
+
657
+ #if JS_HAS_GENERATORS
658
+
659
+ static void
660
+ generator_finalize(JSContext *cx, JSObject *obj)
661
+ {
662
+ JSGenerator *gen;
663
+
664
+ gen = (JSGenerator *) JS_GetPrivate(cx, obj);
665
+ if (gen) {
666
+ /*
667
+ * gen can be open on shutdown when close hooks are ignored or when
668
+ * the embedding cancels scheduled close hooks.
669
+ */
670
+ JS_ASSERT(gen->state == JSGEN_NEWBORN || gen->state == JSGEN_CLOSED ||
671
+ gen->state == JSGEN_OPEN);
672
+ JS_free(cx, gen);
673
+ }
674
+ }
675
+
676
+ static void
677
+ generator_trace(JSTracer *trc, JSObject *obj)
678
+ {
679
+ JSGenerator *gen;
680
+
681
+ gen = (JSGenerator *) JS_GetPrivate(trc->context, obj);
682
+ if (!gen)
683
+ return;
684
+
685
+ /*
686
+ * js_TraceStackFrame does not recursively trace the down-linked frame
687
+ * chain, so we insist that gen->frame has no parent to trace when the
688
+ * generator is not running.
689
+ */
690
+ JS_ASSERT_IF(gen->state != JSGEN_RUNNING && gen->state != JSGEN_CLOSING,
691
+ !gen->frame.down);
692
+
693
+ /*
694
+ * FIXME be 390950. Generator's frame is a part of the JS stack when the
695
+ * generator is running or closing. Thus tracing the frame in this case
696
+ * here duplicates the work done in js_TraceContext.
697
+ */
698
+ js_TraceStackFrame(trc, &gen->frame);
699
+ }
700
+
701
+ JSClass js_GeneratorClass = {
702
+ js_Generator_str,
703
+ JSCLASS_HAS_PRIVATE | JSCLASS_IS_ANONYMOUS |
704
+ JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Generator),
705
+ JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
706
+ JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, generator_finalize,
707
+ NULL, NULL, NULL, NULL,
708
+ NULL, NULL, JS_CLASS_TRACE(generator_trace), NULL
709
+ };
710
+
711
+ /*
712
+ * Called from the JSOP_GENERATOR case in the interpreter, with fp referring
713
+ * to the frame by which the generator function was activated. Create a new
714
+ * JSGenerator object, which contains its own JSStackFrame that we populate
715
+ * from *fp. We know that upon return, the JSOP_GENERATOR opcode will return
716
+ * from the activation in fp, so we can steal away fp->callobj and fp->argsobj
717
+ * if they are non-null.
718
+ */
719
+ JSObject *
720
+ js_NewGenerator(JSContext *cx, JSStackFrame *fp)
721
+ {
722
+ JSObject *obj;
723
+ uintN argc, nargs, nvars, nslots;
724
+ JSGenerator *gen;
725
+ jsval *newsp;
726
+
727
+ /* After the following return, failing control flow must goto bad. */
728
+ obj = js_NewObject(cx, &js_GeneratorClass, NULL, NULL, 0);
729
+ if (!obj)
730
+ return NULL;
731
+
732
+ /* Load and compute stack slot counts. */
733
+ argc = fp->argc;
734
+ nargs = JS_MAX(argc, fp->fun->nargs);
735
+ nvars = fp->nvars;
736
+ nslots = 2 + nargs + nvars + fp->script->depth;
737
+
738
+ /* Allocate obj's private data struct. */
739
+ gen = (JSGenerator *)
740
+ JS_malloc(cx, sizeof(JSGenerator) + (nslots - 1) * sizeof(jsval));
741
+ if (!gen)
742
+ goto bad;
743
+
744
+ gen->obj = obj;
745
+
746
+ /* Steal away objects reflecting fp and point them at gen->frame. */
747
+ gen->frame.callobj = fp->callobj;
748
+ if (fp->callobj) {
749
+ JS_SetPrivate(cx, fp->callobj, &gen->frame);
750
+ fp->callobj = NULL;
751
+ }
752
+ gen->frame.argsobj = fp->argsobj;
753
+ if (fp->argsobj) {
754
+ JS_SetPrivate(cx, fp->argsobj, &gen->frame);
755
+ fp->argsobj = NULL;
756
+ }
757
+
758
+ /* These two references can be shared with fp until it goes away. */
759
+ gen->frame.varobj = fp->varobj;
760
+ gen->frame.thisp = fp->thisp;
761
+
762
+ /* Copy call-invariant script and function references. */
763
+ gen->frame.script = fp->script;
764
+ gen->frame.callee = fp->callee;
765
+ gen->frame.fun = fp->fun;
766
+
767
+ /* Use newsp to carve space out of gen->stack. */
768
+ newsp = gen->stack;
769
+ gen->arena.next = NULL;
770
+ gen->arena.base = (jsuword) newsp;
771
+ gen->arena.limit = gen->arena.avail = (jsuword) (newsp + nslots);
772
+
773
+ #define COPY_STACK_ARRAY(vec,cnt,num) \
774
+ JS_BEGIN_MACRO \
775
+ gen->frame.cnt = cnt; \
776
+ gen->frame.vec = newsp; \
777
+ newsp += (num); \
778
+ memcpy(gen->frame.vec, fp->vec, (num) * sizeof(jsval)); \
779
+ JS_END_MACRO
780
+
781
+ /* Copy argv, rval, and vars. */
782
+ *newsp++ = fp->argv[-2];
783
+ *newsp++ = fp->argv[-1];
784
+ COPY_STACK_ARRAY(argv, argc, nargs);
785
+ gen->frame.rval = fp->rval;
786
+ COPY_STACK_ARRAY(vars, nvars, nvars);
787
+
788
+ #undef COPY_STACK_ARRAY
789
+
790
+ /* Initialize or copy virtual machine state. */
791
+ gen->frame.down = NULL;
792
+ gen->frame.annotation = NULL;
793
+ gen->frame.scopeChain = fp->scopeChain;
794
+
795
+ gen->frame.spbase = newsp;
796
+ JS_ASSERT(fp->spbase == fp->regs->sp);
797
+ gen->savedRegs.sp = newsp;
798
+ gen->savedRegs.pc = fp->regs->pc;
799
+ gen->frame.regs = &gen->savedRegs;
800
+
801
+ /* Copy remaining state (XXX sharp* and xml* should be local vars). */
802
+ gen->frame.sharpDepth = 0;
803
+ gen->frame.sharpArray = NULL;
804
+ gen->frame.flags = (fp->flags & ~JSFRAME_ROOTED_ARGV) | JSFRAME_GENERATOR;
805
+ gen->frame.dormantNext = NULL;
806
+ gen->frame.xmlNamespace = NULL;
807
+ gen->frame.blockChain = NULL;
808
+
809
+ /* Note that gen is newborn. */
810
+ gen->state = JSGEN_NEWBORN;
811
+
812
+ if (!JS_SetPrivate(cx, obj, gen)) {
813
+ JS_free(cx, gen);
814
+ goto bad;
815
+ }
816
+ return obj;
817
+
818
+ bad:
819
+ cx->weakRoots.newborn[GCX_OBJECT] = NULL;
820
+ return NULL;
821
+ }
822
+
823
+ typedef enum JSGeneratorOp {
824
+ JSGENOP_NEXT,
825
+ JSGENOP_SEND,
826
+ JSGENOP_THROW,
827
+ JSGENOP_CLOSE
828
+ } JSGeneratorOp;
829
+
830
+ /*
831
+ * Start newborn or restart yielding generator and perform the requested
832
+ * operation inside its frame.
833
+ */
834
+ static JSBool
835
+ SendToGenerator(JSContext *cx, JSGeneratorOp op, JSObject *obj,
836
+ JSGenerator *gen, jsval arg)
837
+ {
838
+ JSStackFrame *fp;
839
+ JSArena *arena;
840
+ JSBool ok;
841
+
842
+ if (gen->state == JSGEN_RUNNING || gen->state == JSGEN_CLOSING) {
843
+ js_ReportValueError(cx, JSMSG_NESTING_GENERATOR,
844
+ JSDVG_SEARCH_STACK, OBJECT_TO_JSVAL(obj),
845
+ JS_GetFunctionId(gen->frame.fun));
846
+ return JS_FALSE;
847
+ }
848
+
849
+ JS_ASSERT(gen->state == JSGEN_NEWBORN || gen->state == JSGEN_OPEN);
850
+ switch (op) {
851
+ case JSGENOP_NEXT:
852
+ case JSGENOP_SEND:
853
+ if (gen->state == JSGEN_OPEN) {
854
+ /*
855
+ * Store the argument to send as the result of the yield
856
+ * expression.
857
+ */
858
+ gen->savedRegs.sp[-1] = arg;
859
+ }
860
+ gen->state = JSGEN_RUNNING;
861
+ break;
862
+
863
+ case JSGENOP_THROW:
864
+ JS_SetPendingException(cx, arg);
865
+ gen->state = JSGEN_RUNNING;
866
+ break;
867
+
868
+ default:
869
+ JS_ASSERT(op == JSGENOP_CLOSE);
870
+ JS_SetPendingException(cx, JSVAL_ARETURN);
871
+ gen->state = JSGEN_CLOSING;
872
+ break;
873
+ }
874
+
875
+ /* Extend the current stack pool with gen->arena. */
876
+ arena = cx->stackPool.current;
877
+ JS_ASSERT(!arena->next);
878
+ JS_ASSERT(!gen->arena.next);
879
+ JS_ASSERT(cx->stackPool.current != &gen->arena);
880
+ cx->stackPool.current = arena->next = &gen->arena;
881
+
882
+ /* Push gen->frame around the interpreter activation. */
883
+ fp = cx->fp;
884
+ cx->fp = &gen->frame;
885
+ gen->frame.down = fp;
886
+ ok = js_Interpret(cx);
887
+ cx->fp = fp;
888
+ gen->frame.down = NULL;
889
+
890
+ /* Retract the stack pool and sanitize gen->arena. */
891
+ JS_ASSERT(!gen->arena.next);
892
+ JS_ASSERT(arena->next == &gen->arena);
893
+ JS_ASSERT(cx->stackPool.current == &gen->arena);
894
+ cx->stackPool.current = arena;
895
+ arena->next = NULL;
896
+
897
+ if (gen->frame.flags & JSFRAME_YIELDING) {
898
+ /* Yield cannot fail, throw or be called on closing. */
899
+ JS_ASSERT(ok);
900
+ JS_ASSERT(!cx->throwing);
901
+ JS_ASSERT(gen->state == JSGEN_RUNNING);
902
+ JS_ASSERT(op != JSGENOP_CLOSE);
903
+ gen->frame.flags &= ~JSFRAME_YIELDING;
904
+ gen->state = JSGEN_OPEN;
905
+ return JS_TRUE;
906
+ }
907
+
908
+ gen->frame.rval = JSVAL_VOID;
909
+ gen->state = JSGEN_CLOSED;
910
+ if (ok) {
911
+ /* Returned, explicitly or by falling off the end. */
912
+ if (op == JSGENOP_CLOSE)
913
+ return JS_TRUE;
914
+ return js_ThrowStopIteration(cx);
915
+ }
916
+
917
+ /*
918
+ * An error, silent termination by operation callback or an exception.
919
+ * Propagate the condition to the caller.
920
+ */
921
+ return JS_FALSE;
922
+ }
923
+
924
+ static JSBool
925
+ CloseGenerator(JSContext *cx, JSObject *obj)
926
+ {
927
+ JSGenerator *gen;
928
+
929
+ JS_ASSERT(STOBJ_GET_CLASS(obj) == &js_GeneratorClass);
930
+ gen = (JSGenerator *) JS_GetPrivate(cx, obj);
931
+ if (!gen) {
932
+ /* Generator prototype object. */
933
+ return JS_TRUE;
934
+ }
935
+
936
+ if (gen->state == JSGEN_CLOSED)
937
+ return JS_TRUE;
938
+
939
+ return SendToGenerator(cx, JSGENOP_CLOSE, obj, gen, JSVAL_VOID);
940
+ }
941
+
942
+ /*
943
+ * Common subroutine of generator_(next|send|throw|close) methods.
944
+ */
945
+ static JSBool
946
+ generator_op(JSContext *cx, JSGeneratorOp op, jsval *vp)
947
+ {
948
+ JSObject *obj;
949
+ JSGenerator *gen;
950
+ jsval arg;
951
+
952
+ obj = JS_THIS_OBJECT(cx, vp);
953
+ if (!JS_InstanceOf(cx, obj, &js_GeneratorClass, vp + 2))
954
+ return JS_FALSE;
955
+
956
+ gen = (JSGenerator *) JS_GetPrivate(cx, obj);
957
+ if (gen == NULL) {
958
+ /* This happens when obj is the generator prototype. See bug 352885. */
959
+ goto closed_generator;
960
+ }
961
+
962
+ if (gen->state == JSGEN_NEWBORN) {
963
+ switch (op) {
964
+ case JSGENOP_NEXT:
965
+ case JSGENOP_THROW:
966
+ break;
967
+
968
+ case JSGENOP_SEND:
969
+ if (!JSVAL_IS_VOID(vp[2])) {
970
+ js_ReportValueError(cx, JSMSG_BAD_GENERATOR_SEND,
971
+ JSDVG_SEARCH_STACK, vp[2], NULL);
972
+ return JS_FALSE;
973
+ }
974
+ break;
975
+
976
+ default:
977
+ JS_ASSERT(op == JSGENOP_CLOSE);
978
+ gen->state = JSGEN_CLOSED;
979
+ return JS_TRUE;
980
+ }
981
+ } else if (gen->state == JSGEN_CLOSED) {
982
+ closed_generator:
983
+ switch (op) {
984
+ case JSGENOP_NEXT:
985
+ case JSGENOP_SEND:
986
+ return js_ThrowStopIteration(cx);
987
+ case JSGENOP_THROW:
988
+ JS_SetPendingException(cx, vp[2]);
989
+ return JS_FALSE;
990
+ default:
991
+ JS_ASSERT(op == JSGENOP_CLOSE);
992
+ return JS_TRUE;
993
+ }
994
+ }
995
+
996
+ arg = (op == JSGENOP_SEND || op == JSGENOP_THROW)
997
+ ? vp[2]
998
+ : JSVAL_VOID;
999
+ if (!SendToGenerator(cx, op, obj, gen, arg))
1000
+ return JS_FALSE;
1001
+ *vp = gen->frame.rval;
1002
+ return JS_TRUE;
1003
+ }
1004
+
1005
+ static JSBool
1006
+ generator_send(JSContext *cx, uintN argc, jsval *vp)
1007
+ {
1008
+ return generator_op(cx, JSGENOP_SEND, vp);
1009
+ }
1010
+
1011
+ static JSBool
1012
+ generator_next(JSContext *cx, uintN argc, jsval *vp)
1013
+ {
1014
+ return generator_op(cx, JSGENOP_NEXT, vp);
1015
+ }
1016
+
1017
+ static JSBool
1018
+ generator_throw(JSContext *cx, uintN argc, jsval *vp)
1019
+ {
1020
+ return generator_op(cx, JSGENOP_THROW, vp);
1021
+ }
1022
+
1023
+ static JSBool
1024
+ generator_close(JSContext *cx, uintN argc, jsval *vp)
1025
+ {
1026
+ return generator_op(cx, JSGENOP_CLOSE, vp);
1027
+ }
1028
+
1029
+ static JSFunctionSpec generator_methods[] = {
1030
+ JS_FN(js_iterator_str, iterator_self, 0,0,JSPROP_ROPERM),
1031
+ JS_FN(js_next_str, generator_next, 0,0,JSPROP_ROPERM),
1032
+ JS_FN(js_send_str, generator_send, 1,1,JSPROP_ROPERM),
1033
+ JS_FN(js_throw_str, generator_throw, 1,1,JSPROP_ROPERM),
1034
+ JS_FN(js_close_str, generator_close, 0,0,JSPROP_ROPERM),
1035
+ JS_FS_END
1036
+ };
1037
+
1038
+ #endif /* JS_HAS_GENERATORS */
1039
+
1040
+ JSObject *
1041
+ js_InitIteratorClasses(JSContext *cx, JSObject *obj)
1042
+ {
1043
+ JSObject *proto, *stop;
1044
+
1045
+ /* Idempotency required: we initialize several things, possibly lazily. */
1046
+ if (!js_GetClassObject(cx, obj, JSProto_StopIteration, &stop))
1047
+ return NULL;
1048
+ if (stop)
1049
+ return stop;
1050
+
1051
+ proto = JS_InitClass(cx, obj, NULL, &js_IteratorClass, Iterator, 2,
1052
+ NULL, iterator_methods, NULL, NULL);
1053
+ if (!proto)
1054
+ return NULL;
1055
+ STOBJ_SET_SLOT(proto, JSSLOT_ITER_STATE, JSVAL_NULL);
1056
+
1057
+ #if JS_HAS_GENERATORS
1058
+ /* Initialize the generator internals if configured. */
1059
+ if (!JS_InitClass(cx, obj, NULL, &js_GeneratorClass, NULL, 0,
1060
+ NULL, generator_methods, NULL, NULL)) {
1061
+ return NULL;
1062
+ }
1063
+ #endif
1064
+
1065
+ return JS_InitClass(cx, obj, NULL, &js_StopIterationClass, NULL, 0,
1066
+ NULL, NULL, NULL, NULL);
1067
+ }