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 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 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;
}
Aggregations