Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/ContinuationsApiTest.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.tests;
8
9 import java.io.*;
10
11 import junit.framework.TestCase;
12
13 import org.mozilla.javascript.ConsString;
14 import org.mozilla.javascript.Context;
15 import org.mozilla.javascript.Script;
16 import org.mozilla.javascript.Function;
17 import org.mozilla.javascript.ContinuationPending;
18 import org.mozilla.javascript.Scriptable;
19 import org.mozilla.javascript.WrappedException;
20 import org.mozilla.javascript.serialize.ScriptableInputStream;
21 import org.mozilla.javascript.serialize.ScriptableOutputStream;
22
23
24 /**
25 * Test of new API functions for running and resuming scripts containing
26 * continuations, and for suspending a continuation from a Java method
27 * called from JavaScript.
28 *
29 * @author Norris Boyd
30 */
31 public class ContinuationsApiTest extends TestCase {
32 Scriptable globalScope;
33
34 public static class MyClass implements Serializable {
35
36 private static final long serialVersionUID = 4189002778806232070L;
37
38 public int f(int a) {
39 Context cx = Context.enter();
40 try {
41 ContinuationPending pending = cx.captureContinuation();
42 pending.setApplicationState(a);
43 throw pending;
44 } finally {
45 Context.exit();
46 }
47 }
48
49 public int g(int a) {
50 Context cx = Context.enter();
51 try {
52 ContinuationPending pending = cx.captureContinuation();
53 pending.setApplicationState(2*a);
54 throw pending;
55 } finally {
56 Context.exit();
57 }
58 }
59 public String h() {
60 Context cx = Context.enter();
61 try {
62 ContinuationPending pending = cx.captureContinuation();
63 pending.setApplicationState("2*3");
64 throw pending;
65 } finally {
66 Context.exit();
67 }
68 }
69 }
70
71 @Override
72 public void setUp() {
73 Context cx = Context.enter();
74 try {
75 globalScope = cx.initStandardObjects();
76 cx.setOptimizationLevel(-1); // must use interpreter mode
77 globalScope.put("myObject", globalScope,
78 Context.javaToJS(new MyClass(), globalScope));
79 } finally {
80 Context.exit();
81 }
82 }
83
84 public void testScriptWithContinuations() {
85 Context cx = Context.enter();
86 try {
87 cx.setOptimizationLevel(-1); // must use interpreter mode
88 Script script = cx.compileString("myObject.f(3) + 1;",
89 "test source", 1, null);
90 cx.executeScriptWithContinuations(script, globalScope);
91 fail("Should throw ContinuationPending");
92 } catch (ContinuationPending pending) {
93
94 Object applicationState = pending.getApplicationState();
95 assertEquals(new Integer(3), applicationState);
96 int saved = (Integer) applicationState;
97 Object result = cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
98 assertEquals(5, ((Number)result).intValue());
99
100 } finally {
101 Context.exit();
102 }
103 }
104
105 public void testScriptWithMultipleContinuations() {
106 Context cx = Context.enter();
107 try {
108 cx.setOptimizationLevel(-1); // must use interpreter mode
109 Script script = cx.compileString(
110 "myObject.f(3) + myObject.g(3) + 2;",
111 "test source", 1, null);
112 cx.executeScriptWithContinuations(script, globalScope);
113 fail("Should throw ContinuationPending");
114 } catch (ContinuationPending pending) {
115 try {
116 Object applicationState = pending.getApplicationState();
117 assertEquals(new Integer(3), applicationState);
118 int saved = (Integer) applicationState;
119 cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
120 fail("Should throw another ContinuationPending");
121 } catch (ContinuationPending pending2) {
122 Object applicationState2 = pending2.getApplicationState();
123 assertEquals(new Integer(6), applicationState2);
124 int saved2 = (Integer) applicationState2;
125 Object result2 = cx.resumeContinuation(pending2.getContinuation(), globalScope, saved2 + 1);
126 assertEquals(13, ((Number)result2).intValue());
127 }
128 } finally {
129 Context.exit();
130 }
131 }
132
133 public void testScriptWithNestedContinuations() {
134 Context cx = Context.enter();
135 try {
136 cx.setOptimizationLevel(-1); // must use interpreter mode
137 Script script = cx.compileString("myObject.g( myObject.f(1) ) + 2;",
138 "test source", 1, null);
139 cx.executeScriptWithContinuations(script, globalScope);
140 fail("Should throw ContinuationPending");
141 } catch (ContinuationPending pending) {
142 try {
143 Object applicationState = pending.getApplicationState();
144 assertEquals(new Integer(1), applicationState);
145 int saved = (Integer) applicationState;
146 cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
147 fail("Should throw another ContinuationPending");
148 } catch (ContinuationPending pending2) {
149 Object applicationState2 = pending2.getApplicationState();
150 assertEquals(new Integer(4), applicationState2);
151 int saved2 = (Integer) applicationState2;
152 Object result2 = cx.resumeContinuation(pending2.getContinuation(), globalScope, saved2 + 2);
153 assertEquals(8, ((Number)result2).intValue());
154 }
155 } finally {
156 Context.exit();
157 }
158 }
159
160
161 public void testFunctionWithContinuations() {
162 Context cx = Context.enter();
163 try {
164 cx.setOptimizationLevel(-1); // must use interpreter mode
165 cx.evaluateString(globalScope,
166 "function f(a) { return myObject.f(a); }",
167 "function test source", 1, null);
168 Function f = (Function) globalScope.get("f", globalScope);
169 Object[] args = { 7 };
170 cx.callFunctionWithContinuations(f, globalScope, args);
171 fail("Should throw ContinuationPending");
172 } catch (ContinuationPending pending) {
173 Object applicationState = pending.getApplicationState();
174 assertEquals(7, ((Number)applicationState).intValue());
175 int saved = (Integer) applicationState;
176 Object result = cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
177 assertEquals(8, ((Number)result).intValue());
178 } finally {
179 Context.exit();
180 }
181 }
182
183 /**
184 * Since a continuation can only capture JavaScript frames and not Java
185 * frames, ensure that Rhino throws an exception when the JavaScript frames
186 * don't reach all the way to the code called by
187 * executeScriptWithContinuations or callFunctionWithContinuations.
188 */
189 public void testErrorOnEvalCall() {
190 Context cx = Context.enter();
191 try {
192 cx.setOptimizationLevel(-1); // must use interpreter mode
193 Script script = cx.compileString("eval('myObject.f(3);');",
194 "test source", 1, null);
195 cx.executeScriptWithContinuations(script, globalScope);
196 fail("Should throw IllegalStateException");
197 } catch (WrappedException we) {
198 Throwable t = we.getWrappedException();
199 assertTrue(t instanceof IllegalStateException);
200 assertTrue(t.getMessage().startsWith("Cannot capture continuation"));
201 } finally {
202 Context.exit();
203 }
204 }
205
206 public void testSerializationWithContinuations()
207 throws IOException, ClassNotFoundException
208 {
209 Context cx = Context.enter();
210 try {
211 cx.setOptimizationLevel(-1); // must use interpreter mode
212 cx.evaluateString(globalScope,
213 "function f(a) { var k = myObject.f(a); var t = []; return k; }",
214 "function test source", 1, null);
215 Function f = (Function) globalScope.get("f", globalScope);
216 Object[] args = { 7 };
217 cx.callFunctionWithContinuations(f, globalScope, args);
218 fail("Should throw ContinuationPending");
219 } catch (ContinuationPending pending) {
220 // serialize
221 ByteArrayOutputStream baos = new ByteArrayOutputStream();
222 ScriptableOutputStream sos = new ScriptableOutputStream(baos, globalScope);
223 sos.writeObject(globalScope);
224 sos.writeObject(pending.getContinuation());
225 sos.close();
226 baos.close();
227 byte[] serializedData = baos.toByteArray();
228
229 // deserialize
230 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
231 ScriptableInputStream sis = new ScriptableInputStream(bais, globalScope);
232 globalScope = (Scriptable) sis.readObject();
233 Object continuation = sis.readObject();
234 sis.close();
235 bais.close();
236
237 Object result = cx.resumeContinuation(continuation, globalScope, 8);
238 assertEquals(8, ((Number)result).intValue());
239 } finally {
240 Context.exit();
241 }
242 }
243
244 public void testContinuationsPrototypesAndSerialization() throws IOException, ClassNotFoundException {
245
246 byte[] serializedData = null;
247
248 {
249 Scriptable globalScope;
250 Context cx = Context.enter();
251 try {
252 globalScope = cx.initStandardObjects();
253 cx.setOptimizationLevel(-1); // must use interpreter mode
254 globalScope.put("myObject", globalScope, Context.javaToJS(new MyClass(), globalScope));
255 } finally {
256 Context.exit();
257 }
258
259 cx = Context.enter();
260 try {
261 cx.setOptimizationLevel(-1); // must use interpreter mode
262 cx.evaluateString(
263 globalScope,
264 "function f(a) { Number.prototype.blargh = function() {return 'foo';}; var k = myObject.f(a); var t = []; return new Number(8).blargh(); }",
265 "function test source", 1, null);
266 Function f = (Function) globalScope.get("f", globalScope);
267 Object[] args = { 7 };
268 cx.callFunctionWithContinuations(f, globalScope, args);
269 fail("Should throw ContinuationPending");
270 } catch (ContinuationPending pending) {
271 // serialize
272 ByteArrayOutputStream baos = new ByteArrayOutputStream();
273 ObjectOutputStream sos = new ObjectOutputStream(baos);
274 sos.writeObject(globalScope);
275 sos.writeObject(pending.getContinuation());
276 sos.close();
277 baos.close();
278 serializedData = baos.toByteArray();
279 } finally {
280 Context.exit();
281 }
282 }
283
284 {
285 try {
286 Context cx = Context.enter();
287
288 Scriptable globalScope;
289
290 // deserialize
291 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
292 ObjectInputStream sis = new ObjectInputStream(bais);
293 globalScope = (Scriptable) sis.readObject();
294 Object continuation = sis.readObject();
295 sis.close();
296 bais.close();
297
298 Object result = cx.resumeContinuation(continuation, globalScope, 8);
299 assertEquals("foo", result);
300 } finally {
301 Context.exit();
302 }
303 }
304
305 }
306
307 public void testContinuationsInlineFunctionsSerialization() throws IOException, ClassNotFoundException {
308
309 Scriptable globalScope;
310 Context cx = Context.enter();
311 try {
312 globalScope = cx.initStandardObjects();
313 cx.setOptimizationLevel(-1); // must use interpreter mode
314 globalScope.put("myObject", globalScope, Context.javaToJS(new MyClass(), globalScope));
315 } finally {
316 Context.exit();
317 }
318
319 cx = Context.enter();
320 try {
321 cx.setOptimizationLevel(-1); // must use interpreter mode
322 cx.evaluateString(globalScope, "function f(a) { var k = eval(myObject.h()); var t = []; return k; }",
323 "function test source", 1, null);
324 Function f = (Function) globalScope.get("f", globalScope);
325 Object[] args = { 7 };
326 cx.callFunctionWithContinuations(f, globalScope, args);
327 fail("Should throw ContinuationPending");
328 } catch (ContinuationPending pending) {
329 // serialize
330 ByteArrayOutputStream baos = new ByteArrayOutputStream();
331 ScriptableOutputStream sos = new ScriptableOutputStream(baos, globalScope);
332 sos.writeObject(globalScope);
333 sos.writeObject(pending.getContinuation());
334 sos.close();
335 baos.close();
336 byte[] serializedData = baos.toByteArray();
337
338 // deserialize
339 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
340 ScriptableInputStream sis = new ScriptableInputStream(bais, globalScope);
341 globalScope = (Scriptable) sis.readObject();
342 Object continuation = sis.readObject();
343 sis.close();
344 bais.close();
345
346 Object result = cx.resumeContinuation(continuation, globalScope, "2+3");
347 assertEquals(5, ((Number) result).intValue());
348 } finally {
349 Context.exit();
350 }
351 }
352
353 public void testConsStringSerialization() throws IOException, ClassNotFoundException {
354
355 ConsString r1 = new ConsString("foo", "bar");
356
357 ByteArrayOutputStream baos = new ByteArrayOutputStream();
358 ObjectOutputStream oos = new ObjectOutputStream(baos);
359
360 oos.writeObject(r1);
361
362 oos.flush();
363
364 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
365
366 ObjectInputStream ois = new ObjectInputStream(bais);
367
368 CharSequence r2 = (CharSequence) ois.readObject();
369
370 assertEquals("still the same at the other end", r1.toString(), r2.toString());
371
372 }
373
374 }