Search in sources :

Example 11 with ContextAction

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

the class ApplyOnPrimitiveNumberTest method testIt.

public void testIt() {
    final String script = "var fn = function() { return this; }\n" + "fn.apply(1)";
    final ContextAction action = new ContextAction() {

        public Object run(final Context _cx) {
            final ScriptableObject scope = _cx.initStandardObjects();
            final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
            assertEquals("object", ScriptRuntime.typeof(result));
            assertEquals("1", Context.toString(result));
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 12 with ContextAction

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

the class ArrayConcatTest method testArrayConcat.

public void testArrayConcat() {
    final String script = "var a = ['a0', 'a1'];\n" + "a[3] = 'a3';\n" + "var b = ['b1', 'b2'];\n" + "b.concat(a)";
    final ContextAction action = new ContextAction() {

        public Object run(final Context _cx) {
            final ScriptableObject scope = _cx.initStandardObjects();
            final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
            assertEquals("b1,b2,a0,a1,,a3", Context.toString(result));
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 13 with ContextAction

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

the class DecompileTest method newObject0Arg.

/**
	 * As of head of trunk on 30.09.09, decompile of "new Date()" returns "new Date" without parentheses.
	 * @see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=519692">Bug 519692</a> 
	 */
@Test
public void newObject0Arg() {
    final String source = "var x = new Date().getTime();";
    final ContextAction action = new ContextAction() {

        public Object run(final Context cx) {
            final Script script = cx.compileString(source, "my script", 0, null);
            Assert.assertEquals(source, cx.decompileScript(script, 4).trim());
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ContextAction(org.mozilla.javascript.ContextAction) Test(org.junit.Test)

Example 14 with ContextAction

use of org.mozilla.javascript.ContextAction in project jslint4java by happygiraffe.

the class JSLint method callReport.

/**
 * Construct a JSLint error report. This is in two parts: a list of errors, and an optional
 * function report.
 *
 * @param errorsOnly
 *            if the function report should be omitted.
 * @return the report, an HTML string.
 */
@NeedsContext
private String callReport(final boolean errorsOnly) {
    return (String) contextFactory.call(new ContextAction() {

        // TODO: This would probably benefit from injecting an API to manage JSLint.
        public Object run(Context cx) {
            Function fn = null;
            Object value = null;
            StringBuilder sb = new StringBuilder();
            // Look up JSLINT.data.
            value = lintFunc.get("data", lintFunc);
            if (value == UniqueTag.NOT_FOUND) {
                return "";
            }
            fn = (Function) value;
            // Call JSLINT.data().  This returns a JS data structure that we need below.
            Object data = fn.call(cx, lintFunc, null, Context.emptyArgs);
            // Look up JSLINT.error_report.
            value = lintFunc.get("error_report", lintFunc);
            // Shouldn't happen ordinarily, but some of my tests don't have it.
            if (value != UniqueTag.NOT_FOUND) {
                fn = (Function) value;
                // Call JSLint.report().
                sb.append(fn.call(cx, lintFunc, null, new Object[] { data }));
            }
            if (!errorsOnly) {
                // Look up JSLINT.report.
                value = lintFunc.get("report", lintFunc);
                // Shouldn't happen ordinarily, but some of my tests don't have it.
                if (value != UniqueTag.NOT_FOUND) {
                    fn = (Function) value;
                    // Call JSLint.report().
                    sb.append(fn.call(cx, lintFunc, null, new Object[] { data }));
                }
            }
            return sb.toString();
        }
    });
}
Also used : Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 15 with ContextAction

use of org.mozilla.javascript.ContextAction in project jslint4java by happygiraffe.

the class JSLint method buildResults.

/**
 * Assemble the {@link JSLintResult} object.
 */
@NeedsContext
private JSLintResult buildResults(final String systemId, final long startNanos, final long endNanos) {
    return (JSLintResult) contextFactory.call(new ContextAction() {

        public Object run(Context cx) {
            ResultBuilder b = new JSLintResult.ResultBuilder(systemId);
            b.duration(TimeUnit.NANOSECONDS.toMillis(endNanos - startNanos));
            for (Issue issue : readErrors(systemId)) {
                b.addIssue(issue);
            }
            // Collect a report on what we've just linted.
            b.report(callReport(false));
            // Extract JSLINT.data() output and set it on the result.
            Object o = lintFunc.get("data", lintFunc);
            // Real JSLINT will always have this, but some of my test stubs don't.
            if (o != UniqueTag.NOT_FOUND) {
                Function reportFunc = (Function) o;
                Scriptable data = (Scriptable) reportFunc.call(cx, lintFunc, null, Context.emptyArgs);
                for (String global : Util.listValueOfType("global", String.class, data)) {
                    b.addGlobal(global);
                }
                b.json(Util.booleanValue("json", data));
                for (JSFunction f : Util.listValue("functions", data, new JSFunctionConverter())) {
                    b.addFunction(f);
                }
            }
            // Extract the list of properties. Note that we don't expose the counts, as it
            // doesn't seem that useful.
            Object properties = lintFunc.get("property", lintFunc);
            if (properties != UniqueTag.NOT_FOUND) {
                for (Object id : ScriptableObject.getPropertyIds((Scriptable) properties)) {
                    b.addProperty(id.toString());
                }
            }
            return b.build();
        }
    });
}
Also used : Context(org.mozilla.javascript.Context) ResultBuilder(com.googlecode.jslint4java.JSLintResult.ResultBuilder) Function(org.mozilla.javascript.Function) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

Context (org.mozilla.javascript.Context)23 ContextAction (org.mozilla.javascript.ContextAction)23 Scriptable (org.mozilla.javascript.Scriptable)12 ScriptableObject (org.mozilla.javascript.ScriptableObject)11 Function (org.mozilla.javascript.Function)5 ContextFactory (org.mozilla.javascript.ContextFactory)4 Test (org.junit.Test)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Script (org.mozilla.javascript.Script)2 ImmutableList (com.google.common.collect.ImmutableList)1 ResultBuilder (com.googlecode.jslint4java.JSLintResult.ResultBuilder)1 ObjectColumnSelector (io.druid.segment.ObjectColumnSelector)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 BaseObjectColumnValueSelector (org.apache.druid.segment.BaseObjectColumnValueSelector)1 BaseFunction (org.mozilla.javascript.BaseFunction)1 JavaScriptException (org.mozilla.javascript.JavaScriptException)1 RhinoException (org.mozilla.javascript.RhinoException)1