Search in sources :

Example 1 with Global

use of org.mozilla.javascript.tools.shell.Global in project cucumber-jvm by cucumber.

the class HTMLFormatterTest method writes_valid_report_js.

@Test
public void writes_valid_report_js() throws IOException {
    URL reportJs = new URL(outputDir, "report.js");
    Context cx = Context.enter();
    Global scope = new Global(cx);
    try {
        cx.evaluateReader(scope, new InputStreamReader(reportJs.openStream(), "UTF-8"), reportJs.getFile(), 1, null);
        fail("Should have failed");
    } catch (EcmaError expected) {
        assertTrue(expected.getMessage().startsWith("ReferenceError: \"document\" is not defined."));
    }
}
Also used : Context(org.mozilla.javascript.Context) EcmaError(org.mozilla.javascript.EcmaError) InputStreamReader(java.io.InputStreamReader) URL(java.net.URL) Global(org.mozilla.javascript.tools.shell.Global) Test(org.junit.Test)

Example 2 with Global

use of org.mozilla.javascript.tools.shell.Global in project hackpad by dropbox.

the class Main method main.

/**
     * Main entry point.  Creates a debugger attached to a Rhino
     * {@link org.mozilla.javascript.tools.shell.Main} shell session.
     */
public static void main(String[] args) {
    Main main = new Main("Rhino JavaScript Debugger");
    main.doBreak();
    main.setExitAction(new IProxy(IProxy.EXIT_ACTION));
    System.setIn(main.getIn());
    System.setOut(main.getOut());
    System.setErr(main.getErr());
    Global global = org.mozilla.javascript.tools.shell.Main.getGlobal();
    global.setIn(main.getIn());
    global.setOut(main.getOut());
    global.setErr(main.getErr());
    main.attachTo(org.mozilla.javascript.tools.shell.Main.shellContextFactory);
    main.setScope(global);
    main.pack();
    main.setSize(600, 460);
    main.setVisible(true);
    org.mozilla.javascript.tools.shell.Main.exec(args);
}
Also used : Global(org.mozilla.javascript.tools.shell.Global)

Example 3 with Global

use of org.mozilla.javascript.tools.shell.Global in project hackpad by dropbox.

the class ShellTest method run.

public static void run(final ShellContextFactory shellContextFactory, final File jsFile, final Parameters parameters, final Status status) throws Exception {
    final Global global = new Global();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final PrintStream p = new PrintStream(out);
    global.setOut(p);
    global.setErr(p);
    global.defineFunctionProperties(new String[] { "options" }, ShellTest.class, ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY);
    // test suite expects keywords to be disallowed as identifiers
    shellContextFactory.setAllowReservedKeywords(false);
    final TestState testState = new TestState();
    if (jsFile.getName().endsWith("-n.js")) {
        status.setNegative();
    }
    final Throwable[] thrown = { null };
    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                shellContextFactory.call(new ContextAction() {

                    public Object run(Context cx) {
                        status.running(jsFile);
                        testState.errors = new ErrorReporterWrapper(cx.getErrorReporter());
                        cx.setErrorReporter(testState.errors);
                        global.init(cx);
                        try {
                            runFileIfExists(cx, global, new File(jsFile.getParentFile().getParentFile().getParentFile(), "shell.js"));
                            runFileIfExists(cx, global, new File(jsFile.getParentFile().getParentFile(), "shell.js"));
                            runFileIfExists(cx, global, new File(jsFile.getParentFile(), "shell.js"));
                            runFileIfExists(cx, global, jsFile);
                            status.hadErrors(jsFile, testState.errors.errors.toArray(new Status.JsError[0]));
                        } catch (ThreadDeath e) {
                        } catch (Throwable t) {
                            status.threw(t);
                        }
                        return null;
                    }
                });
            } catch (Error t) {
                thrown[0] = t;
            } catch (RuntimeException t) {
                thrown[0] = t;
            } finally {
                synchronized (testState) {
                    testState.finished = true;
                }
            }
        }
    }, jsFile.getPath());
    t.setDaemon(true);
    t.start();
    t.join(parameters.getTimeoutMilliseconds());
    synchronized (testState) {
        if (!testState.finished) {
            callStop(t);
            status.timedOut();
        }
    }
    int expectedExitCode = 0;
    p.flush();
    status.outputWas(new String(out.toByteArray()));
    BufferedReader r = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(out.toByteArray())));
    String failures = "";
    for (; ; ) {
        String s = r.readLine();
        if (s == null) {
            break;
        }
        if (s.indexOf("FAILED!") != -1) {
            failures += s + '\n';
        }
        int expex = s.indexOf("EXPECT EXIT CODE ");
        if (expex != -1) {
            expectedExitCode = s.charAt(expex + "EXPECT EXIT CODE ".length()) - '0';
        }
    }
    if (thrown[0] != null) {
        status.threw(thrown[0]);
    }
    status.exitCodesWere(expectedExitCode, testState.exitCode);
    if (failures != "") {
        status.failed(failures);
    }
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Global(org.mozilla.javascript.tools.shell.Global) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 4 with Global

use of org.mozilla.javascript.tools.shell.Global in project hackpad by dropbox.

the class Main method mainEmbedded.

/**
     * Entry point for embedded applications.  This method attaches
     * to the global {@link ContextFactory} with a scope of a newly
     * created {@link Global} object.  No I/O redirection is performed
     * as with {@link #main(String[])}.
     */
public static Main mainEmbedded(String title) {
    ContextFactory factory = ContextFactory.getGlobal();
    Global global = new Global();
    global.init(factory);
    return mainEmbedded(factory, global, title);
}
Also used : Global(org.mozilla.javascript.tools.shell.Global)

Example 5 with Global

use of org.mozilla.javascript.tools.shell.Global in project hackpad by dropbox.

the class Main method mainEmbeddedImpl.

/**
     * Helper method for {@link #mainEmbedded(String)}, etc.
     */
private static Main mainEmbeddedImpl(ContextFactory factory, Object scopeProvider, String title) {
    if (title == null) {
        title = "Rhino JavaScript Debugger (embedded usage)";
    }
    Main main = new Main(title);
    main.doBreak();
    main.setExitAction(new IProxy(IProxy.EXIT_ACTION));
    main.attachTo(factory);
    if (scopeProvider instanceof ScopeProvider) {
        main.setScopeProvider((ScopeProvider) scopeProvider);
    } else {
        Scriptable scope = (Scriptable) scopeProvider;
        if (scope instanceof Global) {
            Global global = (Global) scope;
            global.setIn(main.getIn());
            global.setOut(main.getOut());
            global.setErr(main.getErr());
        }
        main.setScope(scope);
    }
    main.pack();
    main.setSize(600, 460);
    main.setVisible(true);
    return main;
}
Also used : Global(org.mozilla.javascript.tools.shell.Global)

Aggregations

Global (org.mozilla.javascript.tools.shell.Global)7 InputStreamReader (java.io.InputStreamReader)3 Context (org.mozilla.javascript.Context)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 URL (java.net.URL)1 Test (org.junit.Test)1 ContextFactory (org.mozilla.javascript.ContextFactory)1 EcmaError (org.mozilla.javascript.EcmaError)1