RhinoEngine.java 3.13 KB
/*
 * 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));
    }

}