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;
}
};
}
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();
}
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"));
}
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();
}
}
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);
}
Aggregations