| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package org.mozilla.javascript.tests; |
| 9 |
|
| 10 |
import junit.framework.TestCase; |
| 11 |
|
| 12 |
import org.mozilla.javascript.Context; |
| 13 |
import org.mozilla.javascript.ContextFactory; |
| 14 |
import org.mozilla.javascript.Scriptable; |
| 15 |
import org.mozilla.javascript.Callable; |
| 16 |
import org.mozilla.javascript.drivers.TestUtils; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public class ObserveInstructionCountTest extends TestCase { |
| 22 |
|
| 23 |
static class MyContext extends Context { |
| 24 |
MyContext(ContextFactory factory) { |
| 25 |
super(factory); |
| 26 |
} |
| 27 |
int quota; |
| 28 |
} |
| 29 |
|
| 30 |
static class QuotaExceeded extends RuntimeException { |
| 31 |
private static final long serialVersionUID = -8018441873635071899L; |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
protected void setUp() { |
| 36 |
TestUtils.setGlobalContextFactory(new MyFactory()); |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
protected void tearDown() { |
| 41 |
TestUtils.setGlobalContextFactory(null); |
| 42 |
} |
| 43 |
|
| 44 |
static class MyFactory extends ContextFactory { |
| 45 |
|
| 46 |
@Override |
| 47 |
protected Context makeContext() |
| 48 |
{ |
| 49 |
MyContext cx = new MyContext(this); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
cx.setInstructionObserverThreshold(500); |
| 54 |
return cx; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
protected void observeInstructionCount(Context cx, int instructionCount) |
| 59 |
{ |
| 60 |
MyContext mcx = (MyContext)cx; |
| 61 |
mcx.quota -= instructionCount; |
| 62 |
if (mcx.quota <= 0) { |
| 63 |
throw new QuotaExceeded(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
protected Object doTopCall(Callable callable, |
| 69 |
Context cx, Scriptable scope, |
| 70 |
Scriptable thisObj, Object[] args) |
| 71 |
{ |
| 72 |
MyContext mcx = (MyContext)cx; |
| 73 |
mcx.quota = 2000; |
| 74 |
return super.doTopCall(callable, cx, scope, thisObj, args); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
private void baseCase(int optimizationLevel, String source) { |
| 79 |
ContextFactory factory = new MyFactory(); |
| 80 |
Context cx = factory.enterContext(); |
| 81 |
cx.setOptimizationLevel(optimizationLevel); |
| 82 |
assertTrue(cx instanceof MyContext); |
| 83 |
try { |
| 84 |
Scriptable globalScope = cx.initStandardObjects(); |
| 85 |
cx.evaluateString(globalScope, |
| 86 |
source, |
| 87 |
"test source", 1, null); |
| 88 |
fail(); |
| 89 |
} catch (QuotaExceeded e) { |
| 90 |
|
| 91 |
} catch (RuntimeException e) { |
| 92 |
fail(e.toString()); |
| 93 |
} finally { |
| 94 |
Context.exit(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public void testWhileTrueInGlobal() { |
| 99 |
String source = "var i=0; while (true) i++;"; |
| 100 |
baseCase(-1, source); |
| 101 |
baseCase(1, source); |
| 102 |
} |
| 103 |
|
| 104 |
public void testWhileTrueNoCounterInGlobal() { |
| 105 |
String source = "while (true);"; |
| 106 |
baseCase(-1, source); |
| 107 |
baseCase(1, source); |
| 108 |
} |
| 109 |
|
| 110 |
public void testWhileTrueInFunction() { |
| 111 |
String source = "var i=0; function f() { while (true) i++; } f();"; |
| 112 |
baseCase(-1, source); |
| 113 |
baseCase(1, source); |
| 114 |
} |
| 115 |
|
| 116 |
public void testForever() { |
| 117 |
String source = "for(;;);"; |
| 118 |
baseCase(-1, source); |
| 119 |
baseCase(1, source); |
| 120 |
} |
| 121 |
|
| 122 |
} |