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; }
Context.javaToJS(entry.getValue(), scope));
if ( getAddClasses() != null ) { for ( int i = 0; i < getAddClasses().length; i++ ) { Object jsOut = Context.javaToJS( getAddClasses()[i].getAddObject(), jsscope ); ScriptableObject.putProperty( jsscope, getAddClasses()[i].getJSName(), jsOut ); Context.javaToJS( ScriptValuesAddedFunctions.class, jsscope ); ( (ScriptableObject) jsscope ) .defineFunctionProperties(
if ( input.getAddClasses() != null ) { 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 ); Context.javaToJS( ScriptAddedFunctions.class, jsscope ); ( (ScriptableObject) jsscope ).defineFunctionProperties( jsFunctionList, ScriptAddedFunctions.class, ScriptableObject.DONTENUM );
if ( input.getAddClasses() != null ) { 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 ); Context.javaToJS( ScriptValuesAddedFunctions.class, jsscope ); ( (ScriptableObject) jsscope ).defineFunctionProperties( jsFunctionList, ScriptValuesAddedFunctions.class, ScriptableObject.DONTENUM );
if ( meta.getAddClasses() != null ) { 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 ); Context.javaToJS( ScriptValuesAddedFunctions.class, data.scope ); ( (ScriptableObject) data.scope ).defineFunctionProperties( ScriptValuesAddedFunctions.jsFunctionList, ScriptValuesAddedFunctions.class,
Object[] wrapArguments(Object[] args) { if (args == null) { return Context.emptyArgs; } Object[] res = new Object[args.length]; for (int i = 0; i < res.length; i++) { res[i] = Context.javaToJS(args[i], topLevel); } return res; }
Object[] wrapArguments(Object[] args) { if (args == null) { return Context.emptyArgs; } Object[] res = new Object[args.length]; for (int i = 0; i < res.length; i++) { res[i] = Context.javaToJS(args[i], topLevel); } return res; }
protected final Object toJSValue(Object value) { return Context.javaToJS(value, scope); } }
protected Object[] wrapArguments(Object[] args) { if (args == null) { return Context.emptyArgs; } Object[] res = new Object[args.length]; for (int i = 0; i < res.length; i++) { res[i] = Context.javaToJS(args[i], topLevel); } return res; }
Object[] wrapArguments(Object[] args) { if (args == null) { return Context.emptyArgs; } Object[] res = new Object[args.length]; for (int i = 0; i < res.length; i++) { res[i] = Context.javaToJS(args[i], topLevel); } return res; }
Object[] wrapArguments(Object[] args) { if (args == null) { return Context.emptyArgs; } Object[] res = new Object[args.length]; for (int i = 0; i < res.length; i++) { res[i] = Context.javaToJS(args[i], topLevel); } return res; }
/** * Access components. * * TODO: Do we want to restrict the access of sitemap components? (RP) * TODO: Do we want to raise an error or return null? (RP) */ public Object jsFunction_getComponent(String id) throws Exception { return org.mozilla.javascript.Context.javaToJS(currentCall.webAppContext.getBean(id), getParentScope()); }
@Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { if ( args.length != 1) { throw new RuntimeException("Only existsSync with 1 parameter supported"); } return Context.javaToJS(new File(Context.toString(args[0])).getAbsoluteFile().exists(), scope); } }
@JSFunction public static Object encodingExists(Context cx, Scriptable thisObj, Object[] args, Function funObj) { String encoding = stringArg(args, 0); return Context.javaToJS(Charset.isSupported(encoding), funObj); }
public Object jsFunction_getAttribute(String name) { return org.mozilla.javascript.Context.javaToJS( wk.getAttribute(name), getParentScope()); }
public Object jsFunction_getAttributeNames() { return org.mozilla.javascript.Context.javaToJS( wk.getAttributeNames(), getParentScope()); }