/** * ECMA 11.4.3 says that typeof on host object is Implementation-dependent */ public void test0() throws Exception { final Function f = new BaseFunction() { @Override public Object call(Context _cx, Scriptable _scope, Scriptable _thisObj, Object[] _args) { return _args[0].getClass().getName(); } }; final ContextAction action = new ContextAction() { public Object run(final Context context) { final Scriptable scope = context.initStandardObjects(); scope.put("myObj", scope, f); return context.evaluateString(scope, "typeof myObj", "test script", 1, null); } }; doTest("function", action); }
protected boolean isFunctionVisible(BaseFunction function) { String name = function.getFunctionName(); String methodsString = function.toString(); Class<?>[] parameterTypes = parseMethodSignature(methodsString); if (parameterTypes == null) { throw new InternalException("Unable to parse method parameters for sandoboxing!"); } Method method = null; try { method = wrappedObjectType.getMethod(name, parameterTypes); } catch (Exception e) { throw new InternalException(e); } return isFunctionVisible(wrappedObject, method); }
private InspectorModulesProvider createWebkitModulesProvider() { return () -> new Stetho.DefaultInspectorModulesBuilder(context).runtimeRepl( new JsRuntimeReplFactoryBuilder(context) .addFunction("activity", new BaseFunction() { @Override public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { return activityProvider.getCurrentActivity(); } }).build() ).finish(); }
private static String toString(Object obj) { if (obj == null) { return "null"; } if (obj instanceof String) { return (String) obj; } if (obj instanceof NativeJavaArray) { Object array = ((NativeJavaArray) obj).unwrap(); int len = Array.getLength(array); StringBuilder sb = new StringBuilder("["); for (int i = 0; i < len; i++) { if (i != 0) { sb.append(","); } sb.append(toString(Array.get(array, i))); } return sb.append("]").toString(); } if (obj instanceof BaseFunction) { String funcName = ((BaseFunction) obj).getFunctionName(); if (StringUtils.isEmpty(funcName)) { return "function()"; } return "function " + funcName + "()"; } String str = ScriptRuntime.toString(obj); if (obj instanceof NativeArray) { return "[" + str + "]"; } return str; }
public static String getFunctionName(Function function) { if (function instanceof BaseFunction) { return "function " + ((BaseFunction)function).getFunctionName() + "()"; } else { return "function ()"; } }
private void setNewFieldValue(Class<?> fieldType, Object fieldValue, Exception exception, int recursion) { this.fieldType = fieldType; this.fieldValue = fieldValue; this.fieldTypeStr = (fieldType != null) ? fieldType.getSimpleName() : null; this.fieldTypeStr = (fieldType == null && fieldValue != null) ? fieldValue .getClass().getSimpleName() : this.fieldTypeStr; if (fieldValue instanceof Function) { if (fieldValue instanceof BaseFunction) { BaseFunction baseFunction = (BaseFunction) fieldValue; this.fieldValueStr = baseFunction.getFunctionName() + "()"; } else { this.fieldValueStr = "f()"; } } else if (fieldValue instanceof String) { this.fieldValueStr = "\"" + (String) fieldValue + "\""; } else if (fieldTypeStr != null) { try { this.fieldValueStr = (fieldValue != null) ? fieldValue .toString() : "null"; } catch (Exception e) { // e.printStackTrace(); this.fieldValueStr = "(exceptiono occured)"; } } constructObjectTree(this, fieldValue, recursion); }
private void setNewFieldValue(Class<?> fieldType, Object fieldValue, Exception exception, int recursion) { this.fieldType = fieldType; this.fieldValue = fieldValue; this.fieldTypeStr = (fieldType != null)? fieldType.getSimpleName() : null; this.fieldTypeStr = (fieldType == null && fieldValue != null)? fieldValue.getClass().getSimpleName() : this.fieldTypeStr; if (fieldValue instanceof Function) { if (fieldValue instanceof BaseFunction) { BaseFunction baseFunction = (BaseFunction)fieldValue; this.fieldValueStr = baseFunction.getFunctionName() + "()"; } else { this.fieldValueStr = "f()"; } } else if (fieldValue instanceof String) { this.fieldValueStr = "\"" + (String)fieldValue + "\""; } else if (fieldTypeStr != null) { try { this.fieldValueStr = (fieldValue != null)? fieldValue.toString() : "null"; } catch (Exception e) { //e.printStackTrace(); this.fieldValueStr = "(exceptiono occured)"; } } constructObjectTree(this, fieldValue, recursion); }
private void initFunctions( ) { Method[] tmpMethods = this.getClass( ).getDeclaredMethods( ); HashMap<String, Method> methods = new LinkedHashMap<String, Method>( ); for ( int i = 0; i < tmpMethods.length; i++ ) { Method tmpMethod = tmpMethods[i]; String methodName = tmpMethod.getName( ); // must handle special case with long parameter or polymiorphism if ( "getReportElementByID".equals( methodName ) //$NON-NLS-1$ || "setUserProperty".equals( methodName ) ) //$NON-NLS-1$ continue; if ( ( tmpMethod.getModifiers( ) & Modifier.PUBLIC ) != 0 ) methods.put( methodName, tmpMethod ); } Context.enter( ); try { for ( final Entry<String, Method> entry : methods.entrySet( ) ) { this.defineProperty( entry.getKey( ), new BaseFunction( ) { public Object call( Context cx, Scriptable scope, Scriptable thisObj, Object[] args ) { Object[] convertedArgs = JavascriptEvalUtil .convertToJavaObjects( args ); try { Method method = entry.getValue( ); return method.invoke( ReportDesign.this, convertedArgs ); } catch ( Exception e ) { throw new WrappedException( e ); } } }, DONTENUM ); } } finally { Context.exit( ); } this.defineProperty( "getReportElementByID", //$NON-NLS-1$ new Function_getReportElementByID( ), DONTENUM ); this.defineProperty( "setUserProperty", //$NON-NLS-1$ new Function_setUserProperty( ), DONTENUM ); }
/** * * {@inheritDoc} */ @Override public String getIdentifier(final Object entry) { // should be final but cannot be for sake of exception handling String identifier; if (entry instanceof Scriptable) { final Context cx = Context.enter(); try { final Object toString = ScriptableObject.getProperty((Scriptable) entry, "toString"); if (toString instanceof Function) { final Object toStringResult = ((Function) toString).call(cx, (Scriptable) entry, (Scriptable) entry, new Object[0]); identifier = ScriptRuntime.toString(toStringResult); } else if (toString != Scriptable.NOT_FOUND) { identifier = ScriptRuntime.toString(toString); } else if (entry instanceof BaseFunction) { final String functionName = ((BaseFunction) entry).getFunctionName(); identifier = functionName != null && functionName.length() != 0 ? functionName : entry.toString(); } else { identifier = entry.toString(); } } catch (final RhinoException ex) { LOGGER.debug("Exception determining entry identifier via Rhino - falling back to simple toString", ex); identifier = entry.toString(); } finally { Context.exit(); } } else if (entry != null) { identifier = entry.toString(); } else { identifier = null; } return identifier; }