Search in sources :

Example 21 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject 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 22 with ScriptableObject

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

the class FlexibleCompletor method complete.

public int complete(String buffer, int cursor, List<String> candidates) {
    // Starting from "cursor" at the end of the buffer, look backward
    // and collect a list of identifiers separated by (possibly zero)
    // dots. Then look up each identifier in turn until getting to the
    // last, presumably incomplete fragment. Then enumerate all the
    // properties of the last object and find any that have the
    // fragment as a prefix and return those for autocompletion.
    int m = cursor - 1;
    while (m >= 0) {
        char c = buffer.charAt(m);
        if (!Character.isJavaIdentifierPart(c) && c != '.')
            break;
        m--;
    }
    String namesAndDots = buffer.substring(m + 1, cursor);
    String[] names = namesAndDots.split("\\.", -1);
    Scriptable obj = this.global;
    for (int i = 0; i < names.length - 1; i++) {
        Object val = obj.get(names[i], global);
        if (val instanceof Scriptable)
            obj = (Scriptable) val;
        else {
            // no matches
            return buffer.length();
        }
    }
    Object[] ids = (obj instanceof ScriptableObject) ? ((ScriptableObject) obj).getAllIds() : obj.getIds();
    String lastPart = names[names.length - 1];
    for (int i = 0; i < ids.length; i++) {
        if (!(ids[i] instanceof String))
            continue;
        String id = (String) ids[i];
        if (id.startsWith(lastPart)) {
            if (obj.get(id, obj) instanceof Function)
                id += "(";
            candidates.add(id);
        }
    }
    return buffer.length() - lastPart.length();
}
Also used : Function(org.mozilla.javascript.Function) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 23 with ScriptableObject

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

the class Require method executeModuleScript.

private Scriptable executeModuleScript(Context cx, String id, Scriptable exports, ModuleScript moduleScript, boolean isMain) {
    final ScriptableObject moduleObject = (ScriptableObject) cx.newObject(nativeScope);
    URI uri = moduleScript.getUri();
    URI base = moduleScript.getBase();
    defineReadOnlyProperty(moduleObject, "id", id);
    if (!sandboxed) {
        defineReadOnlyProperty(moduleObject, "uri", uri.toString());
    }
    final Scriptable executionScope = new ModuleScope(nativeScope, uri, base);
    // Set this so it can access the global JS environment objects. 
    // This means we're currently using the "MGN" approach (ModuleScript 
    // with Global Natives) as specified here: 
    // <http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension>
    executionScope.put("exports", executionScope, exports);
    executionScope.put("module", executionScope, moduleObject);
    moduleObject.put("exports", moduleObject, exports);
    install(executionScope);
    if (isMain) {
        defineReadOnlyProperty(this, "main", moduleObject);
    }
    executeOptionalScript(preExec, cx, executionScope);
    moduleScript.getScript().exec(cx, executionScope);
    executeOptionalScript(postExec, cx, executionScope);
    return ScriptRuntime.toObject(nativeScope, ScriptableObject.getProperty(moduleObject, "exports"));
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) URI(java.net.URI)

Example 24 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject 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 25 with ScriptableObject

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

the class DeletePropertyTest method testDeletePropInPrototype.

/**
	 * delete should not delete anything in the prototype chain.
	 */
@Test
public void testDeletePropInPrototype() throws Exception {
    final String script = "Array.prototype.foo = function() {};\n" + "Array.prototype[1] = function() {};\n" + "var t = [];\n" + "[].foo();\n" + "for (var i in t) delete t[i];\n" + "[].foo();\n" + "[][1]();\n";
    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);
            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) Test(org.junit.Test)

Aggregations

ScriptableObject (org.mozilla.javascript.ScriptableObject)44 Context (org.mozilla.javascript.Context)20 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)8 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)8 ContextAction (org.mozilla.javascript.ContextAction)6 ContextFactory (org.mozilla.javascript.ContextFactory)6 Scriptable (org.mozilla.javascript.Scriptable)6 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)5 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 IOException (java.io.IOException)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 ServletContext (javax.servlet.ServletContext)3 Function (org.mozilla.javascript.Function)3 Function (com.google.common.base.Function)2 PrintWriter (java.io.PrintWriter)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2