Search in sources :

Example 96 with Context

use of org.mozilla.javascript.Context in project hackpad by dropbox.

the class RequireTest method testRelativeId.

public void testRelativeId() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testRelativeId.js"), "testRelativeId.js", 1, null);
}
Also used : Context(org.mozilla.javascript.Context) Require(org.mozilla.javascript.commonjs.module.Require) Scriptable(org.mozilla.javascript.Scriptable)

Example 97 with Context

use of org.mozilla.javascript.Context in project hackpad by dropbox.

the class RequireTest method testSetMainForAlreadyLoadedModule.

public void testSetMainForAlreadyLoadedModule() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    require.install(scope);
    cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"), "testSetMainForAlreadyLoadedModule.js", 1, null);
    try {
        require.requireMain(cx, "assert");
        fail();
    } catch (IllegalStateException e) {
        assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
    }
}
Also used : Context(org.mozilla.javascript.Context) Require(org.mozilla.javascript.commonjs.module.Require) Scriptable(org.mozilla.javascript.Scriptable)

Example 98 with Context

use of org.mozilla.javascript.Context in project druid by druid-io.

the class JavaScriptPostAggregator method compile.

private static Function compile(String function) {
    final ContextFactory contextFactory = ContextFactory.getGlobal();
    final Context context = contextFactory.enterContext();
    context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL);
    final ScriptableObject scope = context.initStandardObjects();
    final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null);
    Context.exit();
    return new Function() {

        public double apply(Object[] args) {
            // ideally we need a close() function to discard the context once it is not used anymore
            Context cx = Context.getCurrentContext();
            if (cx == null) {
                cx = contextFactory.enterContext();
            }
            return Context.toNumber(fn.call(cx, scope, scope, args));
        }
    };
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 99 with Context

use of org.mozilla.javascript.Context in project druid by druid-io.

the class JavaScriptExtractionFn method compile.

private static Function<Object, String> compile(String function) {
    final ContextFactory contextFactory = ContextFactory.getGlobal();
    final Context context = contextFactory.enterContext();
    context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL);
    final ScriptableObject scope = context.initStandardObjects();
    final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null);
    Context.exit();
    return new Function<Object, String>() {

        public String apply(Object input) {
            // ideally we need a close() function to discard the context once it is not used anymore
            Context cx = Context.getCurrentContext();
            if (cx == null) {
                cx = contextFactory.enterContext();
            }
            final Object res = fn.call(cx, scope, scope, new Object[] { input });
            return res != null ? Context.toString(res) : null;
        }
    };
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) Function(com.google.common.base.Function) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 100 with Context

use of org.mozilla.javascript.Context 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)

Aggregations

Context (org.mozilla.javascript.Context)152 Scriptable (org.mozilla.javascript.Scriptable)68 ScriptableObject (org.mozilla.javascript.ScriptableObject)62 ContextAction (org.mozilla.javascript.ContextAction)23 ContextFactory (org.mozilla.javascript.ContextFactory)22 Script (org.mozilla.javascript.Script)17 IOException (java.io.IOException)14 Function (org.mozilla.javascript.Function)13 ScriptContext (javax.script.ScriptContext)10 Test (org.junit.Test)8 RhinoException (org.mozilla.javascript.RhinoException)8 ArrayList (java.util.ArrayList)7 ContinuationPending (org.mozilla.javascript.ContinuationPending)7 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 InputStreamReader (java.io.InputStreamReader)6 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)6 JavaScriptException (org.mozilla.javascript.JavaScriptException)6 Date (java.util.Date)5 HashSet (java.util.HashSet)5 List (java.util.List)5