Search in sources :

Example 11 with ContextFactory

use of org.mozilla.javascript.ContextFactory 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() {

        @Override
        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 12 with ContextFactory

use of org.mozilla.javascript.ContextFactory in project cxf by apache.

the class JavascriptTestUtilities method initializeRhino.

public void initializeRhino() {
    rhinoContextFactory = new ContextFactory();
    if (System.getProperty("cxf.jsdebug") != null && !rhinoDebuggerUp) {
        try {
            Class<?> debuggerMain = ClassLoaderUtils.loadClass("org.mozilla.javascript.tools.debugger.Main", getClass());
            if (debuggerMain != null) {
                Method mainMethod = debuggerMain.getMethod("mainEmbedded", ContextFactory.class, Scriptable.class, String.class);
                mainMethod.invoke(null, rhinoContextFactory, rhinoScope, "Debug embedded JavaScript.");
                rhinoDebuggerUp = true;
            }
        } catch (Exception e) {
            LOG.log(Level.WARNING, "Failed to launch Rhino debugger", e);
        }
    }
    rhinoContext = rhinoContextFactory.enterContext();
    rhinoScope = rhinoContext.initStandardObjects();
    try {
        ScriptableObject.defineClass(rhinoScope, JsAssert.class);
        ScriptableObject.defineClass(rhinoScope, Trace.class);
        ScriptableObject.defineClass(rhinoScope, Notifier.class);
        ScriptableObject.defineClass(rhinoScope, CountDownNotifier.class);
        // so that the stock test for IE can gracefully fail.
        rhinoContext.evaluateString(rhinoScope, "var window = new Object();", "<internal>", 0, null);
    } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
        throw new RuntimeException(e);
    } finally {
        Context.exit();
    }
    JsSimpleDomNode.register(rhinoScope);
    JsSimpleDomParser.register(rhinoScope);
    JsNamedNodeMap.register(rhinoScope);
    JsXMLHttpRequest.register(rhinoScope);
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Method(java.lang.reflect.Method) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) RhinoException(org.mozilla.javascript.RhinoException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with ContextFactory

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

the class CustomSetterAcceptNullScriptableTest method testSetNullForScriptableSetter.

public void testSetNullForScriptableSetter() throws Exception {
    final String scriptCode = "foo.myProp = new Foo2();\n" + "foo.myProp = null;";
    final ContextFactory factory = new ContextFactory();
    final Context cx = factory.enterContext();
    try {
        final ScriptableObject topScope = cx.initStandardObjects();
        final Foo foo = new Foo();
        // define custom setter method
        final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
        foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);
        topScope.put("foo", topScope, foo);
        ScriptableObject.defineClass(topScope, Foo2.class);
        cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) Method(java.lang.reflect.Method)

Example 14 with ContextFactory

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

the class DoctestsTest method runDoctest.

@Test
public void runDoctest() throws Exception {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(optimizationLevel);
        Global global = new Global(cx);
        // global.runDoctest throws an exception on any failure
        int testsPassed = global.runDoctest(cx, global, source, name, 1);
        System.out.println(name + "(" + optimizationLevel + "): " + testsPassed + " passed.");
        assertTrue(testsPassed > 0);
    } catch (Exception ex) {
        System.out.println(name + "(" + optimizationLevel + "): FAILED due to " + ex);
        throw ex;
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) Global(org.mozilla.javascript.tools.shell.Global) IOException(java.io.IOException)

Example 15 with ContextFactory

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

Aggregations

ContextFactory (org.mozilla.javascript.ContextFactory)25 Context (org.mozilla.javascript.Context)22 ScriptableObject (org.mozilla.javascript.ScriptableObject)15 Function (com.google.common.base.Function)4 ContextAction (org.mozilla.javascript.ContextAction)4 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 Callable (java.util.concurrent.Callable)3 ExecutorService (java.util.concurrent.ExecutorService)3 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)3 Scriptable (org.mozilla.javascript.Scriptable)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 SlingContextFactory (org.apache.sling.scripting.javascript.helper.SlingContextFactory)2 StreamHostObject (org.jaggeryjs.hostobjects.stream.StreamHostObject)2 Function (org.mozilla.javascript.Function)2 NativeObject (org.mozilla.javascript.NativeObject)2 RhinoException (org.mozilla.javascript.RhinoException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ObjectColumnSelector (io.druid.segment.ObjectColumnSelector)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1