RhinoEngine.java
3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.mozilla.javascript.ClassShutter
* org.mozilla.javascript.Context
* org.mozilla.javascript.ContextFactory
* org.mozilla.javascript.EvaluatorException
* org.mozilla.javascript.Scriptable
* org.mozilla.javascript.ScriptableObject
*/
package com.adobe.xfa.scripthandler.rhino;
import org.mozilla.javascript.ClassShutter;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
public abstract class RhinoEngine {
protected static ThreadLocal<ScriptableObject> moThreadLocalScriptableObject = new ThreadLocal<ScriptableObject>(){
@Override
protected ScriptableObject initialValue() {
return null;
}
};
protected static final ThreadLocal<Boolean> moThreadLocalTopLevelScopeDirtied = new ThreadLocal<Boolean>(){
@Override
protected Boolean initialValue() {
return false;
}
};
public static Context getThreadLocalRuntimeContext() {
Context ctx = Context.getCurrentContext();
if (null == ctx) {
ctx = new ContextFactory().enterContext();
ctx.setClassShutter(new ClassShutter(){
public boolean visibleToScripts(String name) {
if (name.equals("org.mozilla.javascript.EvaluatorException")) {
return true;
}
return false;
}
});
ctx.setLanguageVersion(170);
if (null != moThreadLocalScriptableObject.get()) {
ScriptableObject initializedScriptableObject = (ScriptableObject)ctx.initStandardObjects(moThreadLocalScriptableObject.get());
moThreadLocalScriptableObject.set(initializedScriptableObject);
}
} else if (moThreadLocalTopLevelScopeDirtied.get().booleanValue()) {
assert (moThreadLocalScriptableObject.get() != null);
moThreadLocalTopLevelScopeDirtied.set(false);
ScriptableObject initializedScriptableObject = (ScriptableObject)ctx.initStandardObjects(moThreadLocalScriptableObject.get());
moThreadLocalScriptableObject.set(initializedScriptableObject);
}
return ctx;
}
public static void destroy() {
if (null != Context.getCurrentContext()) {
Context.exit();
}
}
public static Scriptable getTopLevelScope() {
RhinoEngine.getThreadLocalRuntimeContext();
assert (moThreadLocalScriptableObject != null && moThreadLocalScriptableObject.get() != null);
return (Scriptable)moThreadLocalScriptableObject.get();
}
public static void setTopLevelScope(ScriptableObject scriptableObject) {
moThreadLocalScriptableObject.set(scriptableObject);
moThreadLocalTopLevelScopeDirtied.set(true);
}
protected static void throwException(String sError) {
throw Context.throwAsScriptRuntimeEx((Throwable)new EvaluatorException(sError));
}
}