Skip to content

Instantly share code, notes, and snippets.

@IshanFx
Last active December 16, 2018 07:48
Show Gist options
  • Save IshanFx/2e7368382d405813c700acf247fc9b5b to your computer and use it in GitHub Desktop.
Save IshanFx/2e7368382d405813c700acf247fc9b5b to your computer and use it in GitHub Desktop.
Rhino JS executer for android
public static String runScript(Context androidContextObject) {
// Get the JavaScript in previous section
try {
Resources resources = androidContextObject.getResources();
InputStream rawResource = resources.openRawResource(R.raw.config);
Properties properties = new Properties();
properties.load(rawResource);
String source = properties.getProperty("jsExecute");
String functionName = "getRhinoHello";
Object[] functionParams = new Object[]{};
// Every Rhino VM begins with the enter()
// This Context is not Android's Context
org.mozilla.javascript.Context rhino = org.mozilla.javascript.Context.enter();
// Turn off optimization to make Rhino Android compatible
rhino.setOptimizationLevel(-1);
Scriptable scope = rhino.initStandardObjects();
// This line set the javaContext variable in JavaScript
//ScriptableObject.putProperty(scope, "javaContext", org.mozilla.javascript.Context.javaToJS(androidContextObject, scope));
// Note the forth argument is 1, which means the JavaScript source has
// been compressed to only one line using something like YUI
rhino.evaluateString(scope, source, "JavaScript", 1, null);
// We get the hello function defined in JavaScript
Object obj = scope.get(functionName, scope);
if (obj instanceof Function) {
Function function = (Function) obj;
// Call the hello function with params
Object result = function.call(rhino, scope, scope, functionParams);
// After the hello function is invoked, you will see logcat output
// Finally we want to print the result of hello function
String response = org.mozilla.javascript.Context.toString(result);
return response;
} else {
return null;
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// We must exit the Rhino VM
org.mozilla.javascript.Context.exit();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment