ScriptObject.java 3.23 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.mozilla.javascript.Context
 *  org.mozilla.javascript.Script
 *  org.mozilla.javascript.Scriptable
 *  org.mozilla.javascript.ScriptableObject
 *  org.mozilla.javascript.debug.DebuggableScript
 */
package com.adobe.xfa.scripthandler.rhino;

import com.adobe.xfa.scripthandler.rhino.RhinoEngine;
import java.util.ArrayList;
import java.util.List;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.debug.DebuggableScript;

class ScriptObject {
    private ScriptableObject moScriptableObj;
    private final List<String> mMethods;
    private final List<String> mVars;

    public ScriptObject(Script oScript) {
        assert (oScript != null);
        Context context = RhinoEngine.getThreadLocalRuntimeContext();
        DebuggableScript view = Context.getDebuggableView((Script)oScript);
        this.mVars = new ArrayList<String>();
        this.mMethods = new ArrayList<String>();
        if (view != null) {
            int nVars = view.getParamAndVarCount();
            for (int i = 0; i < nVars; ++i) {
                String name = view.getParamOrVarName(i);
                this.mVars.add(name);
            }
            int nFuncs = view.getFunctionCount();
            for (int i2 = 0; i2 < nFuncs; ++i2) {
                DebuggableScript func = view.getFunction(i2);
                String name = func.getFunctionName();
                this.mMethods.add(name);
            }
        }
        this.compile(context.decompileScript(oScript, 0));
    }

    private void compile(String sSource) {
        boolean bSuccess;
        Context context = RhinoEngine.getThreadLocalRuntimeContext();
        Scriptable scope = RhinoEngine.getTopLevelScope();
        this.moScriptableObj = (ScriptableObject)context.newObject(scope);
        boolean bl = bSuccess = this.moScriptableObj != null;
        if (!bSuccess) {
            return;
        }
        StringBuilder sBuf = new StringBuilder(sSource);
        sBuf.append('\n');
        String sThisDot = "this.";
        String sEquals = " = ";
        String sSemiColon = ";\n";
        for (String sName2 : this.mMethods) {
            sBuf.append("this.");
            sBuf.append(sName2);
            sBuf.append(" = ");
            sBuf.append(sName2);
            sBuf.append(";\n");
        }
        for (String sName2 : this.mVars) {
            sBuf.append("this.");
            sBuf.append(sName2);
            sBuf.append(" = ");
            sBuf.append(sName2);
            sBuf.append(";\n");
        }
        context.evaluateString((Scriptable)this.moScriptableObj, sBuf.toString(), null, 0, (Object)null);
    }

    public Object get(String name, Scriptable start) {
        if (this.mVars.contains(name)) {
            return ScriptableObject.getProperty((Scriptable)this.moScriptableObj, (String)name);
        }
        return Scriptable.NOT_FOUND;
    }

    public void put(String name, Scriptable start, Object value) {
        if (this.mVars.contains(name)) {
            ScriptableObject.putProperty((Scriptable)this.moScriptableObj, (String)name, (Object)value);
        }
    }
}