(Context cx, Script script)
| 81 | } |
| 82 | |
| 83 | static void runScripts(Context cx, Script script) { |
| 84 | // Initialize the standard objects (Object, Function, etc.) |
| 85 | // This must be done before scripts can be executed. The call |
| 86 | // returns a new scope that we will share. |
| 87 | TopLevel sharedScope = cx.initStandardObjects(null, true); |
| 88 | |
| 89 | // Now we can execute the precompiled script against the scope |
| 90 | // to define x variable and f function in the shared scope. |
| 91 | script.exec(cx, sharedScope, sharedScope.getGlobalThis()); |
| 92 | |
| 93 | // Now we spawn some threads that execute a script that calls the |
| 94 | // function 'f'. The scope chain looks like this: |
| 95 | // <pre> |
| 96 | // ------------------ ------------------ |
| 97 | // | per-thread scope | -prototype-> | shared scope | |
| 98 | // ------------------ ------------------ |
| 99 | // ^ |
| 100 | // | |
| 101 | // parentScope |
| 102 | // | |
| 103 | // ------------------ |
| 104 | // | f's activation | |
| 105 | // ------------------ |
| 106 | // </pre> |
| 107 | // Both the shared scope and the per-thread scope have variables 'x' |
| 108 | // defined in them. If 'f' is compiled with dynamic scope enabled, |
| 109 | // the 'x' from the per-thread scope will be used. Otherwise, the 'x' |
| 110 | // from the shared scope will be used. The 'x' defined in 'g' (which |
| 111 | // calls 'f') should not be seen by 'f'. |
| 112 | final int threadCount = 3; |
| 113 | Thread[] t = new Thread[threadCount]; |
| 114 | for (int i = 0; i < threadCount; i++) { |
| 115 | String source2 = |
| 116 | "" |
| 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, "thread" + i)); |
| 123 | } |
| 124 | for (int i = 0; i < threadCount; i++) t[i].start(); |
| 125 | // Don't return in this thread until all the spawned threads have |
| 126 | // completed. |
| 127 | for (int i = 0; i < threadCount; i++) { |
| 128 | try { |
| 129 | t[i].join(); |
| 130 | } catch (InterruptedException e) { |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static class PerThread implements Runnable { |
| 136 |
no test coverage detected