Search in sources :

Example 76 with Scriptable

use of org.mozilla.javascript.Scriptable in project gradle by gradle.

the class JsHintWorker method process.

@Override
public JsHintResult process(JsHintSpec spec) {
    Scriptable jsHintScope = RhinoWorkerUtils.parse(spec.getJsHint(), "UTF-8");
    String encoding = spec.getEncoding();
    Map<File, Map<String, Object>> results = new LinkedHashMap<File, Map<String, Object>>();
    for (File target : spec.getSource()) {
        LOGGER.info("Reading file: {}", target.getAbsolutePath());
        String source = readFile(target, encoding);
        Map<String, Object> result = jsHint(jsHintScope, source, target.getName());
        results.put(target, result);
    }
    return new JsHintResult(results);
}
Also used : Scriptable(org.mozilla.javascript.Scriptable) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 77 with Scriptable

use of org.mozilla.javascript.Scriptable in project gradle by gradle.

the class CoffeeScriptCompilerWorker method process.

public void process(SerializableCoffeeScriptCompileSpec spec) {
    Scriptable coffeeScriptScope = parse(spec.getCoffeeScriptJs(), "UTF-8", new Action<Context>() {

        public void execute(Context context) {
            context.setOptimizationLevel(-1);
        }
    });
    String encoding = spec.getOptions().getEncoding();
    CoffeeScriptCompileDestinationCalculator destinationCalculator = new CoffeeScriptCompileDestinationCalculator(spec.getDestinationDir());
    for (RelativeFile target : spec.getSource()) {
        String source = readFile(target.getFile(), encoding);
        String output = compile(coffeeScriptScope, source, target.getRelativePath().getPathString());
        writeFile(output, destinationCalculator.transform(target.getRelativePath()), encoding);
    }
}
Also used : Context(org.mozilla.javascript.Context) CoffeeScriptCompileDestinationCalculator(org.gradle.plugins.javascript.coffeescript.compile.internal.CoffeeScriptCompileDestinationCalculator) RelativeFile(org.gradle.api.internal.file.RelativeFile) Scriptable(org.mozilla.javascript.Scriptable)

Example 78 with Scriptable

use of org.mozilla.javascript.Scriptable in project gradle by gradle.

the class RhinoWorkerUtils method childScope.

public static <R> R childScope(Scriptable parentScope, ScopeOperation<R> operation) {
    Context context = Context.enter();
    try {
        operation.initContext(context);
        Scriptable childScope = context.newObject(parentScope);
        childScope.setParentScope(parentScope);
        return operation.action(childScope, context);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Scriptable(org.mozilla.javascript.Scriptable)

Example 79 with Scriptable

use of org.mozilla.javascript.Scriptable in project SmartZPN by andforce.

the class PacScriptParser method runScript.

private String runScript(String js, String functionName, Object[] functionParams) {
    Context rhino = Context.enter();
    rhino.setOptimizationLevel(-1);
    try {
        Scriptable scope = rhino.initStandardObjects();
        rhino.evaluateString(scope, js, "JavaScript", js.split("\n").length, null);
        Function function = (Function) scope.get(functionName, scope);
        Object result = function.call(rhino, scope, scope, functionParams);
        if (result instanceof String) {
            return (String) result;
        } else if (result instanceof NativeJavaObject) {
            return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
        } else if (result instanceof NativeObject) {
            return (String) ((NativeObject) result).getDefaultValue(String.class);
        }
        return result.toString();
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) NativeObject(org.mozilla.javascript.NativeObject) Function(org.mozilla.javascript.Function) NativeObject(org.mozilla.javascript.NativeObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 80 with Scriptable

use of org.mozilla.javascript.Scriptable in project scriptographer by scriptographer.

the class ExtendedJavaClass method get.

public Object get(String name, Scriptable start) {
    // We are completely redefining get here without relying on
    // NativeJavaClass' implementation, in order to add more JS
    // like behavior.
    // When used as a constructor, ScriptRuntime.newObject() asks
    // for our prototype to create an object of the correct type.
    // We don't really care what the object is, since we're returning
    // one constructed out of whole cloth, so we return null.
    boolean isProto = name.equals("prototype");
    if (!isProto) {
        if (members.has(name, true)) {
            return members.get(this, name, javaObject, true);
        }
        // to be more logical / java-like. TODO: Is this a Rhino bug?
        if (staticFieldAndMethods != null) {
            Object result = staticFieldAndMethods.get(name);
            if (result != null)
                return result;
        }
    }
    if (properties != null && properties.containsKey(name)) {
        // see whether this object defines the property.
        return properties.get(name);
    } else if (isProto) {
        // getPrototype creates prototype Objects on the fly:
        return getInstancePrototype();
    } else {
        Scriptable proto = getPrototype();
        if (proto != null) {
            Object result = proto.get(name, start);
            if (result != Scriptable.NOT_FOUND)
                return result;
        }
    }
    // Experimental: look for nested classes by appending $name to
    // current class' name.
    Class<?> nestedClass = findNestedClass(getClassObject(), name);
    if (nestedClass != null) {
        ExtendedJavaClass nestedValue = new ExtendedJavaClass(ScriptableObject.getTopLevelScope(this), nestedClass, properties != null);
        nestedValue.setParentScope(this);
        return nestedValue;
    }
    return Scriptable.NOT_FOUND;
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

Scriptable (org.mozilla.javascript.Scriptable)106 Context (org.mozilla.javascript.Context)49 ScriptableObject (org.mozilla.javascript.ScriptableObject)30 ContextAction (org.mozilla.javascript.ContextAction)12 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 NativeObject (org.mozilla.javascript.NativeObject)7 Function (org.mozilla.javascript.Function)6 Map (java.util.Map)5 Notifier (org.apache.cxf.javascript.JavascriptTestUtilities.Notifier)5 Test (org.junit.Test)5 InputStreamReader (java.io.InputStreamReader)4 RhinoException (org.mozilla.javascript.RhinoException)4 Wrapper (org.mozilla.javascript.Wrapper)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IdentityHashMap (java.util.IdentityHashMap)3 ScriptContext (javax.script.ScriptContext)3 Callable (org.mozilla.javascript.Callable)3 ContextFactory (org.mozilla.javascript.ContextFactory)3 NativeJavaClass (org.mozilla.javascript.NativeJavaClass)3