use of org.mozilla.javascript.NativeJavaObject in project jmeter by apache.
the class BSFJavaScriptEngine method eval.
/**
* This is used by an application to evaluate a string containing
* some expression.
*/
@Override
public Object eval(String source, int lineNo, int columnNo, Object oscript) throws BSFException {
String scriptText = oscript.toString();
Object retval = null;
Context cx;
try {
cx = Context.enter();
cx.setOptimizationLevel(-1);
cx.setGeneratingDebug(false);
cx.setGeneratingSource(false);
cx.setOptimizationLevel(0);
cx.setDebugger(null, null);
retval = cx.evaluateString(global, scriptText, source, lineNo, null);
if (retval instanceof NativeJavaObject) {
retval = ((NativeJavaObject) retval).unwrap();
}
} catch (Throwable t) {
// NOSONAR We handle correctly Error case in function, includes JavaScriptException, rethrows Errors
handleError(t);
} finally {
Context.exit();
}
return retval;
}
use of org.mozilla.javascript.NativeJavaObject in project hackpad by dropbox.
the class JavaAcessibilityTest method testAccessingFields.
public void testAccessingFields() {
Object result = runScript(importClass + "PrivateAccessClass.staticPackagePrivateInt");
assertEquals(new Integer(0), result);
result = runScript(importClass + "PrivateAccessClass.staticPrivateInt");
assertEquals(new Integer(1), result);
result = runScript(importClass + "PrivateAccessClass.staticProtectedInt");
assertEquals(new Integer(2), result);
result = runScript(importClass + "new PrivateAccessClass().packagePrivateString");
assertEquals("package private", ((NativeJavaObject) result).unwrap());
result = runScript(importClass + "new PrivateAccessClass().privateString");
assertEquals("private", ((NativeJavaObject) result).unwrap());
result = runScript(importClass + "new PrivateAccessClass().protectedString");
assertEquals("protected", ((NativeJavaObject) result).unwrap());
result = runScript(importClass + "new PrivateAccessClass.PrivateNestedClass().packagePrivateInt");
assertEquals(new Integer(0), result);
result = runScript(importClass + "new PrivateAccessClass.PrivateNestedClass().privateInt");
assertEquals(new Integer(1), result);
result = runScript(importClass + "new PrivateAccessClass.PrivateNestedClass().protectedInt");
assertEquals(new Integer(2), result);
}
use of org.mozilla.javascript.NativeJavaObject in project kotlin by JetBrains.
the class RhinoFunctionNativeObjectResultChecker method assertResultValid.
@Override
protected void assertResultValid(Object result, Context context) {
if (result instanceof NativeJavaObject) {
NativeJavaObject nativeJavaObject = (NativeJavaObject) result;
Object unwrap = nativeJavaObject.unwrap();
super.assertResultValid(unwrap, context);
} else {
super.assertResultValid(result, context);
}
}
use of org.mozilla.javascript.NativeJavaObject 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.NativeJavaObject in project BPjs by bThink-BGU.
the class ContinuationProgramState method collectJsValue.
/**
* Take a Javascript value from Rhino, build a Java value for it.
* @param jsValue
* @return
*/
private Object collectJsValue(Object jsValue) {
if (jsValue == null) {
return null;
} else if (jsValue instanceof NativeFunction) {
return ((NativeFunction) jsValue).getEncodedSource();
} else if (jsValue instanceof NativeArray) {
NativeArray jsArr = (NativeArray) jsValue;
List<Object> retVal = new ArrayList<>((int) jsArr.getLength());
for (int idx = 0; idx < jsArr.getLength(); idx++) {
retVal.add(collectJsValue(jsArr.get(idx)));
}
return retVal;
} else if (jsValue instanceof ScriptableObject) {
ScriptableObject jsObj = (ScriptableObject) jsValue;
Map<Object, Object> retVal = new HashMap<>();
for (Object key : jsObj.getIds()) {
retVal.put(key, collectJsValue(jsObj.get(key)));
}
return retVal;
} else if (jsValue instanceof ConsString) {
return ((ConsString) jsValue).toString();
} else if (jsValue instanceof NativeJavaObject) {
NativeJavaObject jsJavaObj = (NativeJavaObject) jsValue;
Object obj = jsJavaObj.unwrap();
return obj;
} else {
String cn = jsValue.getClass().getCanonicalName();
if (!cn.startsWith("java.") && (!cn.startsWith("il.ac.bgu"))) {
System.out.println("collectJsValue: blind translation to java: " + jsValue + " (" + jsValue.getClass() + ")");
}
return jsValue;
}
}
Aggregations