Search in sources :

Example 66 with Scriptable

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

the class PrimitiveTypeScopeResolutionTest method testWithTwoScopes.

private void testWithTwoScopes(final String scriptScope1, final String scriptScope2) {
    final ContextAction action = new ContextAction() {

        public Object run(final Context cx) {
            final Scriptable scope1 = cx.initStandardObjects(new MySimpleScriptableObject("scope1"));
            final Scriptable scope2 = cx.initStandardObjects(new MySimpleScriptableObject("scope2"));
            cx.evaluateString(scope2, scriptScope2, "source2", 1, null);
            scope1.put("scope2", scope1, scope2);
            return cx.evaluateString(scope1, scriptScope1, "source1", 1, null);
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 67 with Scriptable

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

the class StackTraceTest method runWithExpectedStackTrace.

private void runWithExpectedStackTrace(final String _source, final String _expectedStackTrace) {
    final ContextAction action = new ContextAction() {

        public Object run(Context cx) {
            final Scriptable scope = cx.initStandardObjects();
            try {
                cx.evaluateString(scope, _source, "test.js", 0, null);
            } catch (final JavaScriptException e) {
                assertEquals(_expectedStackTrace, e.getScriptStackTrace());
                return null;
            }
            throw new RuntimeException("Exception expected!");
        }
    };
    Utils.runWithOptimizationLevel(action, -1);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable) JavaScriptException(org.mozilla.javascript.JavaScriptException)

Example 68 with Scriptable

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

the class Bug482203 method testJavaApi.

public void testJavaApi() throws Exception {
    Context cx = Context.enter();
    try {
        cx.setOptimizationLevel(-1);
        Script script = cx.compileReader(new InputStreamReader(Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null);
        Scriptable scope = cx.initStandardObjects();
        cx.executeScriptWithContinuations(script, scope);
        for (; ; ) {
            Object cont = ScriptableObject.getProperty(scope, "c");
            if (cont == null) {
                break;
            }
            cx.resumeContinuation(cont, scope, null);
        }
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) InputStreamReader(java.io.InputStreamReader) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 69 with Scriptable

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

the class ClassShutterExceptionTest method helper.

public void helper(String source) {
    Context cx = Context.enter();
    Context.ClassShutterSetter setter = cx.getClassShutterSetter();
    try {
        Scriptable globalScope = cx.initStandardObjects();
        if (setter == null) {
            setter = classShutterSetter;
        } else {
            classShutterSetter = setter;
        }
        setter.setClassShutter(new OpaqueShutter());
        cx.evaluateString(globalScope, source, "test source", 1, null);
    } finally {
        setter.setClassShutter(null);
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Scriptable(org.mozilla.javascript.Scriptable)

Example 70 with Scriptable

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

the class Main method processFiles.

static void processFiles(Context cx, String[] args) {
    // define "arguments" array in the top-level object:
    // need to allocate new array since newArray requires instances
    // of exactly Object[], not ObjectSubclass[]
    Object[] array = new Object[args.length];
    System.arraycopy(args, 0, array, 0, args.length);
    Scriptable argsObj = cx.newArray(global, array);
    global.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM);
    for (String file : fileList) {
        processSource(cx, file);
    }
}
Also used : 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