private void importFunctions(@NonNull ScriptableObject scope) throws StethoJsException { // Define the functions for (Map.Entry<String, Function> entrySet : mFunctions.entrySet()) { String functionName = entrySet.getKey(); Function function = entrySet.getValue(); try { ScriptableObject.putProperty(scope, functionName, function); } catch (Exception e) { throw new StethoJsException(e, "Failed to setup function: %s", functionName); } } }
public void putObject(String name, Object object) { ScriptableObject.putProperty(scope, name, Context.javaToJS(object, scope)); }
private void importVariables(@NonNull ScriptableObject scope) throws StethoJsException { // Define the variables for (Map.Entry<String, Object> entrySet : mVariables.entrySet()) { String varName = entrySet.getKey(); Object varValue = entrySet.getValue(); try { Object jsValue; if (varValue instanceof Scriptable || varValue instanceof Undefined) { jsValue = varValue; } else { jsValue = Context.javaToJS(varValue, scope); } ScriptableObject.putProperty(scope, varName, jsValue); } catch (Exception e) { throw new StethoJsException(e, "Failed to setup variable: %s", varName); } } }
@Override public @Nullable Object evaluate(@NonNull String expression) throws Throwable { Object result; final Context jsContext = enterJsContext(); try { result = jsContext.evaluateString(mJsScope, expression, "chrome", 1, null); // Google chrome automatically saves the last expression to `$_`, we do the same Object jsValue = Context.javaToJS(result, mJsScope); ScriptableObject.putProperty(mJsScope, "$_", jsValue); } finally { Context.exit(); } return Context.jsToJava(result, Object.class); }
private @NonNull ScriptableObject initJsScope(@NonNull Context jsContext) { // Set the main Rhino goodies ImporterTopLevel importerTopLevel = new ImporterTopLevel(jsContext); ScriptableObject scope = jsContext.initStandardObjects(importerTopLevel, false); ScriptableObject.putProperty(scope, "context", Context.javaToJS(mContext, scope)); try { importClasses(jsContext, scope); importPackages(jsContext, scope); importConsole(scope); importVariables(scope); importFunctions(scope); } catch (StethoJsException e) { String message = String.format("%s\n%s", e.getMessage(), Log.getStackTraceString(e)); LogUtil.e(e, message); CLog.writeToConsole(Console.MessageLevel.ERROR, Console.MessageSource.JAVASCRIPT, message); } return scope; }
ScriptableObject.putProperty(scope, entry.getKey(), Context.javaToJS(entry.getValue(), scope));
for ( int i = 0; i < getAddClasses().length; i++ ) { Object jsOut = Context.javaToJS( getAddClasses()[i].getAddObject(), jsscope ); ScriptableObject.putProperty( jsscope, getAddClasses()[i].getJSName(), jsOut );
for ( int i = 0; i < input.getAddClasses().length; i++ ) { Object jsOut = Context.javaToJS( input.getAddClasses()[i].getAddObject(), jsscope ); ScriptableObject.putProperty( jsscope, input.getAddClasses()[i].getJSName(), jsOut );
for ( int i = 0; i < input.getAddClasses().length; i++ ) { Object jsOut = Context.javaToJS( input.getAddClasses()[i].getAddObject(), jsscope ); ScriptableObject.putProperty( jsscope, input.getAddClasses()[i].getJSName(), jsOut );
private Scriptable convertArray(final JSONArray array) throws JSONException { final Scriptable result = context.newArray(scope, array.length()); for (int i = 0, max = array.length(); i < max; i++) { ScriptableObject.putProperty(result, i, convert(array.get(i))); } return result; }
private Scriptable convertObject(final JSONObject obj) throws JSONException { if (obj == JSONObject.NULL) { return null; } final Scriptable result = context.newObject(scope); final Iterator<?> it = obj.keys(); while (it.hasNext()) { final String key = (String) it.next(); final Object value = obj.get(key); ScriptableObject.putProperty(result, key, convert(value)); } return result; }
public DocumentConverter(final Context context, final View view) throws IOException, JSONException { this.context = context; scope = context.initStandardObjects(); context.setLanguageVersion(Context.VERSION_1_8); // Allow custom document helper class. try { ScriptableObject.defineClass(scope, RhinoDocument.class); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } // Add a log object ScriptableObject.putProperty(scope, "log", new JSLog()); // Compile user-specified function try { viewFun = view.compileFunction(context, scope); } catch (final RhinoException e) { LOG.error("View code for " + view + " does not compile."); throw e; } }
for ( int i = 0; i < meta.getAddClasses().length; i++ ) { Object jsOut = Context.javaToJS( meta.getAddClasses()[ i ].getAddObject(), data.scope ); ScriptableObject.putProperty( data.scope, meta.getAddClasses()[ i ].getJSName(), jsOut );
/** * Binds this instance of require() into the specified scope under the * property name "require". * @param scope the scope where the require() function is to be installed. */ public void install(Scriptable scope) { ScriptableObject.putProperty(scope, "require", this); }
private static void setElem(Context cx, Scriptable target, long index, Object value) { if (index > Integer.MAX_VALUE) { String id = Long.toString(index); ScriptableObject.putProperty(target, id, value); } else { ScriptableObject.putProperty(target, (int)index, value); } }
static void init(Scriptable scope, boolean sealed) { NativeError obj = new NativeError(); ScriptableObject.putProperty(obj, "name", "Error"); ScriptableObject.putProperty(obj, "message", ""); ScriptableObject.putProperty(obj, "fileName", ""); ScriptableObject.putProperty(obj, "lineNumber", new Integer(0)); obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); }
public static Object setObjectProp(Scriptable obj, String property, Object value, Context cx) { if (obj instanceof XMLObject) { XMLObject xmlObject = (XMLObject)obj; xmlObject.ecmaPut(cx, property, value); } else { ScriptableObject.putProperty(obj, property, value); } return value; }
private static void defineReadOnlyProperty(ScriptableObject obj, String name, Object value) { ScriptableObject.putProperty(obj, name, value); obj.setAttributes(name, ScriptableObject.READONLY | ScriptableObject.PERMANENT); }
private static void defineReadOnlyProperty(ScriptableObject obj, String name, Object value) { ScriptableObject.putProperty(obj, name, value); obj.setAttributes(name, ScriptableObject.READONLY | ScriptableObject.PERMANENT); }
public static Object setObjectProp(Scriptable obj, String property, Object value, Context cx) { if (obj instanceof XMLObject) { XMLObject xmlObject = (XMLObject)obj; xmlObject.ecmaPut(cx, property, value); } else { ScriptableObject.putProperty(obj, property, value); } return value; }