use of org.mozilla.javascript.Scriptable in project jslint4java by happygiraffe.
the class Util method listValue.
/**
* Convert a JavaScript array into a Java {@link List}. You must provide a
* converter which will be called on each value in order to convert it to a
* Java object.
*
* @param <T>
* The type of every array member.
* @param name
* The name of the array.
* @param scope
* The scope which contains the array.
* @param c
* A {@link Converter} instance to change the JavaScript object
* into a Java one.
* @return A {@link List} of Java objects.
*/
static <T> List<T> listValue(String name, Scriptable scope, Converter<T> c) {
Object val = scope.get(name, scope);
if (val == UniqueTag.NOT_FOUND || val instanceof Undefined) {
return new ArrayList<T>();
}
Scriptable ary = (Scriptable) val;
int count = intValue("length", ary);
List<T> list = new ArrayList<T>(count);
for (int i = 0; i < count; i++) {
list.add(c.convert(ary.get(i, ary)));
}
return list;
}
use of org.mozilla.javascript.Scriptable in project SmartZPN by andforce.
the class PacScriptParser method runScript.
private String runScript(String js, String functionName, Object[] functionParams) {
Context rhino = Context.enter();
rhino.setOptimizationLevel(-1);
try {
Scriptable scope = rhino.initStandardObjects();
rhino.evaluateString(scope, js, "JavaScript", js.split("\n").length, null);
Function function = (Function) scope.get(functionName, scope);
Object result = function.call(rhino, scope, scope, functionParams);
if (result instanceof String) {
return (String) result;
} else if (result instanceof NativeJavaObject) {
return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
} else if (result instanceof NativeObject) {
return (String) ((NativeObject) result).getDefaultValue(String.class);
}
return result.toString();
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Scriptable in project neo4j by neo4j.
the class JavascriptExecutor method createPrototype.
private Scriptable createPrototype(Context cx) {
Scriptable proto = cx.initStandardObjects();
Scriptable topLevel = new ImporterTopLevel(cx);
proto.setParentScope(topLevel);
return proto;
}
use of org.mozilla.javascript.Scriptable in project scriptographer by scriptographer.
the class ExtendedJavaClass method get.
public Object get(String name, Scriptable start) {
// We are completely redefining get here without relying on
// NativeJavaClass' implementation, in order to add more JS
// like behavior.
// When used as a constructor, ScriptRuntime.newObject() asks
// for our prototype to create an object of the correct type.
// We don't really care what the object is, since we're returning
// one constructed out of whole cloth, so we return null.
boolean isProto = name.equals("prototype");
if (!isProto) {
if (members.has(name, true)) {
return members.get(this, name, javaObject, true);
}
// to be more logical / java-like. TODO: Is this a Rhino bug?
if (staticFieldAndMethods != null) {
Object result = staticFieldAndMethods.get(name);
if (result != null)
return result;
}
}
if (properties != null && properties.containsKey(name)) {
// see whether this object defines the property.
return properties.get(name);
} else if (isProto) {
// getPrototype creates prototype Objects on the fly:
return getInstancePrototype();
} else {
Scriptable proto = getPrototype();
if (proto != null) {
Object result = proto.get(name, start);
if (result != Scriptable.NOT_FOUND)
return result;
}
}
// Experimental: look for nested classes by appending $name to
// current class' name.
Class<?> nestedClass = findNestedClass(getClassObject(), name);
if (nestedClass != null) {
ExtendedJavaClass nestedValue = new ExtendedJavaClass(ScriptableObject.getTopLevelScope(this), nestedClass, properties != null);
nestedValue.setParentScope(this);
return nestedValue;
}
return Scriptable.NOT_FOUND;
}
use of org.mozilla.javascript.Scriptable in project scriptographer by scriptographer.
the class ExtendedJavaObject method get.
public Object get(String name, Scriptable start) {
// defined in the underlying Java object
if (properties != null && properties.containsKey(name))
return properties.get(name);
if (changeReceiver != null)
fetchChangeReceiver();
Scriptable prototype = getPrototype();
Object result = prototype.get(name, this);
if (result != Scriptable.NOT_FOUND)
return result;
result = members.get(this, name, javaObject, false);
if (result != Scriptable.NOT_FOUND) {
if (javaObject instanceof ChangeReceiver)
handleChangeEmitter(result, name);
return result;
}
if (name.equals("prototype"))
return prototype;
return Scriptable.NOT_FOUND;
}
Aggregations