/** * @see org.mozilla.javascript.Scriptable#get(java.lang.String, org.mozilla.javascript.Scriptable) */ public Object get(String name, Scriptable start) { // get the property from the underlying QName map if ("length".equals(name)) { return this.size(); } else if ("hasOwnProperty".equals(name)) { return new Callable() { @Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { return (args.length > 0 ? hasOwnProperty(args[0]) : null); } }; } else { return get(name); } }
public void testJsApi() throws Exception { Context cx = Context.enter(); cx.setOptimizationLevel(-1); Script script = cx.compileReader(new InputStreamReader( Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null); Scriptable scope = cx.initStandardObjects(); script.exec(cx, scope); for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } ((Callable)cont).call(cx, scope, scope, new Object[] { null }); } }
public void testJsApi() throws Exception { Context cx = RhinoAndroidHelper.prepareContext(); try { cx.setOptimizationLevel(-1); Script script = cx.compileString(TestUtils.readAsset("Bug482203.js"), "", 1, null); Scriptable scope = cx.initStandardObjects(); script.exec(cx, scope); int counter = 0; for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } counter++; ((Callable)cont).call(cx, scope, scope, new Object[] { null }); } assertEquals(counter, 5); assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); } finally { Context.exit(); } }
/** * Calls {@link Callable#call(Context, Scriptable, Scriptable, Object[])} of * <code>callable</code> under restricted security domain where an action is * allowed only if it is allowed according to the Java stack on the * moment of the <code>callWithDomain</code> call and * <code>securityDomain</code>. Any call to * {@link #getDynamicSecurityDomain(Object)} during execution of * {@link Callable#call(Context, Scriptable, Scriptable, Object[])} * should return a domain incorporate restrictions imposed by * <code>securityDomain</code>. */ public Object callWithDomain(Object securityDomain, final Context cx, final Callable callable, final Scriptable scope, final Scriptable thisObj, final Object[] args) { AccessControlContext acc; if (securityDomain instanceof AccessControlContext) acc = (AccessControlContext)securityDomain; else { RhinoClassLoader loader = (RhinoClassLoader)securityDomain; acc = loader.rhinoAccessControlContext; } PrivilegedExceptionAction execAction = new PrivilegedExceptionAction() { public Object run() { return callable.call(cx, scope, thisObj, args); } }; try { return AccessController.doPrivileged(execAction, acc); } catch (Exception e) { throw new WrappedException(e); } }
public void testJsApi() throws Exception { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); Script script = cx.compileReader(new InputStreamReader( Bug482203Test.class.getResourceAsStream("Bug482203.js")), "", 1, null); Scriptable scope = cx.initStandardObjects(); script.exec(cx, scope); int counter = 0; for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } counter++; ((Callable)cont).call(cx, scope, scope, new Object[] { null }); } assertEquals(counter, 5); assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); } finally { Context.exit(); } }
protected void initializeFunctions() { functions = new HashMap<>(); functions.put("contains", new Callable() { @Override public Object call(Context context, Scriptable scope, Scriptable thisObject, Object[] args) { if (args==null || args.length!=2 || args[0]==null) { return false; } if (args[0] instanceof String) { return ((String)args[0]).contains((CharSequence) args[1]); } if (args[0] instanceof Collection) { return ((Collection)args[0]).contains(args[1]); } return false; } }); }
@Override public Object callWithDomain(Object securityDomain, final Context ctx, final Callable callable, final Scriptable scope, final Scriptable thisObj, final Object[] args) { Object obj = null; try { if (securityDomain == null) { obj = callable.call(ctx, scope, thisObj, args); } else { PrivilegedAction<Object> action = () -> callable.call(ctx, scope, thisObj, args); AccessControlContext acctx = new AccessControlContext( new ProtectionDomain[] { (ProtectionDomain) securityDomain }); return AccessController.doPrivileged(action, acctx); } } catch (MissingResourceException err) { logger.error("Missing Resource"); } return obj; }
/** * Execute top call to script or function. When the runtime is about to * execute a script or function that will create the first stack frame * with scriptable code, it calls this method to perform the real call. * In this way execution of any script happens inside this function. */ @Override protected Object doTopCall(final Callable callable, final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) { AccessControlContext accCtxt = null; Scriptable global = ScriptableObject.getTopLevelScope(scope); Scriptable globalProto = global.getPrototype(); if (globalProto instanceof RhinoTopLevel) { accCtxt = ((RhinoTopLevel) globalProto).getAccessContext(); } if (accCtxt != null) { return AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { return superDoTopCall(callable, cx, scope, thisObj, args); } }, accCtxt); } else { return superDoTopCall(callable, cx, scope, thisObj, args); } }
@Override protected Object doTopCall(Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { if (Looper.myLooper() == Looper.getMainLooper()) { try { return super.doTopCall(callable, cx, scope, thisObj, args); } catch (Exception e) { mUiThreadExceptionHandler.uncaughtException(Thread.currentThread(), e); return null; } } return super.doTopCall(callable, cx, scope, thisObj, args); }
@Override protected Object doTopCall(Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { MyContext mcx = (MyContext)cx; mcx.quota = 2000; return super.doTopCall(callable, cx, scope, thisObj, args); }
public Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) { if(jsonObject == null) return Undefined.instance; Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), new Callable() { @Override public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) { return objects[1]; } }); return param; }
public Object makeParamArray(JSONArray jsonArray, Context rhino, Scriptable scope) { //Object param = NativeJSON.parse(rhino, scope, "{myarray: " + jsonArray.toString() + " }", new Callable() { Object param = NativeJSON.parse(rhino, scope, jsonArray.toString(), new Callable() { @Override public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) { return objects[1]; } }); return param; }
public Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) { Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), new Callable() { @Override public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) { return objects[1]; } }); return param; }
public Object get( String name, Scriptable scope ) { ParameterAttribute parameter = getParameterAttribute( name ); if ( FIELD_VALUE.equals( name ) ) { return parameter.getValue( ); } else if ( FIELD_DISPLAY_TEXT.equals( name ) ) { return parameter.getDisplayText( ); } Object value = parameter.getValue( ); Scriptable jsValue = Context.toObject( value, scope ); Scriptable prototype = jsValue.getPrototype( ); if( prototype != null ) { Object property = jsValue.getPrototype( ).get( name, jsValue ); if ( property instanceof Callable ) { Callable callable = (Callable) property; return new JsValueCallable( callable, jsValue ); } return jsValue.get( name, jsValue ); } else { return jsValue.get( name, jsValue ); } }
@Override protected Object doTopCall(Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { TimeBoxedContext mcx = (TimeBoxedContext) cx; mcx.startTime = System.currentTimeMillis(); return super.doTopCall(callable, cx, scope, thisObj, args); }
public JavaScriptCallableThisDecorator(Callable decorated, Scriptable thisTarget) { this.decorated = decorated; this.thisTarget = thisTarget; }
@Override protected Object doTopCall(Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { SecureScriptContext mcx = (SecureScriptContext) cx; mcx.setStartTime(System.currentTimeMillis()); return super.doTopCall(callable, cx, scope, thisObj, args); }
public boolean renderFunction(StringBuilder builder, FunctionBinding binding) throws RenderException { // Attempt fast call of platform function implementation if(JSPlatformIntegration.platformFunctionRenderer != null) { if(JSPlatformIntegration.platformFunctionRenderer.renderFunction(this.template.getOwner(), builder, binding)) { return true; } } // Otherwise call a JS function to find the implementation Object function = null; Context jsContext = Context.getCurrentContext(); Scriptable rootScope = this.template.getParentScope(); // A single function is cached to catch the common case where one JS function // is used over and over again in a template. String functionName = binding.getFunctionName(); if(this.cachedFunctionName != null && this.cachedFunctionName.equals(functionName)) { function = this.cachedFunction; } if(function == null) { Object functionFinder = ScriptableObject.getProperty(rootScope, FUNCTION_FINDER_NAME); if(functionFinder instanceof Callable) { function = ((Callable)functionFinder).call(jsContext, rootScope, rootScope, new Object[] {functionName}); } } // Call the JS function to render something into the template if((function != null) && !(function instanceof Undefined)) { if(function instanceof Callable) { // Cache this function in case it's called again this.cachedFunction = (Callable)function; this.cachedFunctionName = functionName; // Get a "this" object for the template function call, which may have been cached // from the previous function execution to avoid allocating new objects. JSFunctionThis fnThisObject = this.cachedFnThisObject; this.cachedFnThisObject = null; if(fnThisObject == null) { fnThisObject = (JSFunctionThis)jsContext.newObject(rootScope, "$HaploTemplateFnThis"); } fnThisObject.setForTemplateFnCall(builder, binding); Object result = null; try { // Call JS template function with this object and arguments from the template result = ((Callable)function).call(jsContext, rootScope, fnThisObject, binding.allValueArguments()); } finally { fnThisObject.resetAfterTemplateFnCall(); // Store the fnThisObject for reuse in a future function call this.cachedFnThisObject = fnThisObject; } if(result instanceof CharSequence) { Escape.escape(result.toString(), builder, binding.getContext()); } return true; } else { throw new RenderException(binding.getDriver(), "JavaScript template function "+functionName+"() must be implemented by a function"); } } return false; }
@Override public void setGetterOrSetter(String arg0, int arg1, Callable arg2, boolean arg3) { updated(name); super.setGetterOrSetter(arg0, arg1, arg2, arg3); }
public JsValueCallable( Callable callable) { this.impl = callable; }
public synchronized VMValue evaluate( String expression ) { int currentState = debugger.currentState( ); if ( currentState == VM_TERMINATED ) { return null; } JsValue result = null; Debugger oldDebugger = cx.getDebugger( ); Object oldContextData = cx.getDebuggerContextData( ); int oldLevel = cx.getOptimizationLevel( ); cx.setDebugger( null, null ); cx.setOptimizationLevel( -1 ); cx.setGeneratingDebug( false ); try { Callable script = (Callable) cx.compileString( expression, EVALUATOR_LITERAL, 0, null ); Object val = script.call( cx, scope, thisObj, ScriptRuntime.emptyArgs ); if ( val == Undefined.instance ) { result = new JsValue( UNDEFINED_LITERAL, UNDEFINED_TYPE ); } else { result = new JsValue( val ); } } catch ( Exception ex ) { result = new JsValue( ex.getMessage( ), EXCEPTION_TYPE ); } finally { cx.setGeneratingDebug( true ); cx.setOptimizationLevel( oldLevel ); cx.setDebugger( oldDebugger, oldContextData ); } return result; }
public JsValueCallable( Callable callable, Scriptable value) { this.impl = callable; this.value = value; }
private Object superDoTopCall(Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { return super.doTopCall(callable, cx, scope, thisObj, args); }
protected Object doTopCall(Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { TimedContext mcx = (TimedContext)cx; mcx.startTime = System.currentTimeMillis(); return super.doTopCall(callable, cx, scope, thisObj, args); }