| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import org.mozilla.javascript.*; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
public class DynamicScopes { |
| 13 |
|
| 14 |
static boolean useDynamicScope; |
| 15 |
|
| 16 |
static class MyFactory extends ContextFactory |
| 17 |
{ |
| 18 |
@Override |
| 19 |
protected boolean hasFeature(Context cx, int featureIndex) |
| 20 |
{ |
| 21 |
if (featureIndex == Context.FEATURE_DYNAMIC_SCOPE) { |
| 22 |
return useDynamicScope; |
| 23 |
} |
| 24 |
return super.hasFeature(cx, featureIndex); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
static { |
| 29 |
ContextFactory.initGlobal(new MyFactory()); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static void main(String[] args) |
| 59 |
{ |
| 60 |
Context cx = Context.enter(); |
| 61 |
try { |
| 62 |
|
| 63 |
String source = "" |
| 64 |
+"var x = 'sharedScope';\n" |
| 65 |
+"function f() { return x; }\n" |
| 66 |
|
| 67 |
+"function initClosure(prefix) {\n" |
| 68 |
+" return function test() { return prefix+x; }\n" |
| 69 |
+"}\n" |
| 70 |
+"var closure = initClosure('nested:');\n" |
| 71 |
+""; |
| 72 |
Script script = cx.compileString(source, "sharedScript", 1, null); |
| 73 |
|
| 74 |
useDynamicScope = false; |
| 75 |
runScripts(cx, script); |
| 76 |
useDynamicScope = true; |
| 77 |
runScripts(cx, script); |
| 78 |
} finally { |
| 79 |
Context.exit(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
static void runScripts(Context cx, Script script) |
| 84 |
{ |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
ScriptableObject sharedScope = cx.initStandardObjects(null, true); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
script.exec(cx, sharedScope); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
final int threadCount = 3; |
| 114 |
Thread[] t = new Thread[threadCount]; |
| 115 |
for (int i=0; i < threadCount; i++) { |
| 116 |
String source2 = "" |
| 117 |
+"function g() { var x = 'local'; return f(); }\n" |
| 118 |
+"java.lang.System.out.println(g());\n" |
| 119 |
+"function g2() { var x = 'local'; return closure(); }\n" |
| 120 |
+"java.lang.System.out.println(g2());\n" |
| 121 |
+""; |
| 122 |
t[i] = new Thread(new PerThread(sharedScope, source2, |
| 123 |
"thread" + i)); |
| 124 |
} |
| 125 |
for (int i=0; i < threadCount; i++) |
| 126 |
t[i].start(); |
| 127 |
|
| 128 |
|
| 129 |
for (int i=0; i < threadCount; i++) { |
| 130 |
try { |
| 131 |
t[i].join(); |
| 132 |
} catch (InterruptedException e) { |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static class PerThread implements Runnable { |
| 138 |
|
| 139 |
PerThread(Scriptable sharedScope, String source, String x) { |
| 140 |
this.sharedScope = sharedScope; |
| 141 |
this.source = source; |
| 142 |
this.x = x; |
| 143 |
} |
| 144 |
|
| 145 |
public void run() { |
| 146 |
|
| 147 |
Context cx = Context.enter(); |
| 148 |
try { |
| 149 |
|
| 150 |
Scriptable threadScope = cx.newObject(sharedScope); |
| 151 |
threadScope.setPrototype(sharedScope); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
threadScope.setParentScope(null); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
threadScope.put("x", threadScope, x); |
| 162 |
cx.evaluateString(threadScope, source, "threadScript", 1, null); |
| 163 |
} finally { |
| 164 |
Context.exit(); |
| 165 |
} |
| 166 |
} |
| 167 |
private Scriptable sharedScope; |
| 168 |
private String source; |
| 169 |
private String x; |
| 170 |
} |
| 171 |
|
| 172 |
} |
| 173 |
|