@Override public void put(String name, Scriptable start, Object value) { if (name.equals("__proxy__")) { NativeObject proxy = (NativeObject) value; Object getter = proxy.get("get", start); if (getter instanceof NativeFunction) { mGetter = (NativeFunction) getter; } Object setter = proxy.get("set", start); if (setter instanceof NativeFunction) { mSetter = (NativeFunction) setter; } } else if (mSetter != null) { mSetter.call(Context.getCurrentContext(), start, start, new Object[]{name, value}); } else { super.put(name, start, value); } }
@Override protected Map<String, Object> getParamsFromVariables() throws IOException { Map<String, Object> params = new HashMap<String, Object>(); Object[] ids = scope.getIds(); for (Object id : ids) { if (id instanceof String) { String name = (String) id; Object value = scope.get(name, scope); if (!(value instanceof NativeFunction)) { LOG.debug("Registering parameter "+name+" => "+scope.get(name, scope)); params.put(name, eval(name).toString()); } } } return params; }
/** * javascript helper for binding parameters. * See: {@link Pig#bind(Map)} * @param o a javascript object to be converted into a Map * @return the bound script * @throws IOException if {@link Pig#bind(Map)} throws an IOException */ public BoundScript bind(Object o) throws IOException { NativeObject vars = (NativeObject)o; Map<String, Object> params = new HashMap<String, Object>(); Object[] ids = vars.getIds(); for (Object id : ids) { if (id instanceof String) { String name = (String) id; Object value = vars.get(name, vars); if (!(value instanceof NativeFunction) && value != null) { params.put(name, value.toString()); } } } return pig.bind(params); }
protected void adaptFunctionScope(final Pair<Scriptable, Function> callback, final Scriptable globalScope, final boolean restore) { if (callback != null) { final Function fn = callback.getSecond(); if (fn instanceof NativeFunction) { if (restore) { final Scriptable parentScope = fn.getParentScope(); if (parentScope instanceof ThreadLocalParentScope) { fn.setParentScope(((ThreadLocalParentScope) parentScope).getRealParentScope()); } } else { fn.setParentScope(new ThreadLocalParentScope(fn.getParentScope(), globalScope, this.facadeFactory)); } } } }
@Override public void waitFor(NativeFunction func, long waitIterationMilliseconds) { while (!RhinoUtils.toBoolean(func.call(javaScriptEngine.getContext(), javaScriptEngine.getScope(), javaScriptEngine.getScope(), new Object[0]))) { try { Thread.sleep(waitIterationMilliseconds); } catch (InterruptedException e) { throw new ScriptException(ErrorType.Timeout, "waitFor() interrupted"); } } }
public void testFunction() throws Exception { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); cx.setLanguageVersion(Context.VERSION_1_7); cx.setApplicationClassLoader(getClass().getClassLoader()); final Scriptable scope = cx.initStandardObjects(); System.out.println("START TEST OF RHINO"); Object result = cx.evaluateString(scope, new StringBuffer() .append("function( a, b ) { return a + b ; }") .toString(), "testFunction", 1, null ); Assert.assertNotNull(result); Assert.assertTrue( result instanceof NativeFunction ); result = ((NativeFunction)result).call( cx, scope, null /*this*/, new Object[] { 10, 5 }); Assert.assertNotNull(result); Assert.assertTrue(result instanceof Number); Assert.assertEquals(((java.lang.Number) result).intValue(), 15); System.out.printf("END TEST OF RHINO [%s]\n", result.getClass()); } finally { Context.exit(); } }
@Override public void waitFor(NativeFunction func) { waitFor(func, 100); }
public void addStepDefinition(Global jsStepDefinition, NativeRegExp regexp, NativeFunction bodyFunc, NativeFunction argumentsFromFunc) throws Throwable { StackTraceElement stepDefLocation = jsLocation(); RhinoStepDefinition stepDefinition = new RhinoStepDefinition(cx, scope, jsStepDefinition, regexp, bodyFunc, stepDefLocation, argumentsFromFunc); glue.addStepDefinition(stepDefinition); }
protected Scriptable doBeforeProcess(final Context parentContext, final Scriptable parentScope, final Scriptable thisObj, final Pair<Scriptable, Function> beforeProcessCallback) { final Context cx = Context.enter(); try { this.scriptProcessor.inheritCallChain(parentContext); final Scriptable processScope = cx.newObject(parentScope); processScope.setPrototype(null); processScope.setParentScope(null); // check for registerChildScope function on logger and register process scope if function is available final Object loggerValue = ScriptableObject.getProperty(parentScope, RhinoLogFunction.LOGGER_OBJ_NAME); if (loggerValue instanceof Scriptable) { final Scriptable loggerObj = (Scriptable) loggerValue; final Object registerChildScopeFuncValue = ScriptableObject.getProperty(loggerObj, RhinoLogFunction.REGISTER_CHILD_SCOPE_FUNC_NAME); if (registerChildScopeFuncValue instanceof Function) { final Function registerChildScopeFunc = (Function) registerChildScopeFuncValue; registerChildScopeFunc.call(cx, parentScope, thisObj, new Object[] { processScope }); } } if (beforeProcessCallback.getSecond() != null) { final Scriptable beforeProcessOriginalCallScope = beforeProcessCallback.getFirst(); final Scriptable beforeProcessCallScope = this.facadeFactory.toFacadedObject(beforeProcessOriginalCallScope, parentScope); final Function beforeProcessFn = beforeProcessCallback.getSecond(); if (beforeProcessFn instanceof NativeFunction) { // native function has parent scope based on location in source code // per batch function contract we need to execute it in our process scope final NativeFunction nativeFn = (NativeFunction) beforeProcessFn; final ThreadLocalParentScope threadLocalParentScope = (ThreadLocalParentScope) nativeFn.getParentScope(); threadLocalParentScope.setEffectiveParentScope(processScope); try { // execute with thread local parent scope nativeFn.call(cx, processScope, beforeProcessCallScope, new Object[0]); } finally { threadLocalParentScope.removeEffectiveParentScope(); } } else { // not a native function, so has no associated scope - calling as-is beforeProcessFn.call(cx, processScope, beforeProcessCallScope, new Object[0]); } } return processScope; } catch (final WrappedException ex) { final Throwable wrappedException = ex.getWrappedException(); if (wrappedException instanceof RuntimeException) { throw (RuntimeException) wrappedException; } throw ex; } finally { Context.exit(); } }
protected void doProcess(final Context parentContext, final Scriptable parentScope, final Scriptable processScope, final Scriptable thisObj, final Pair<Scriptable, Function> processCallback, final Object element) { final Context cx = Context.enter(); try { this.scriptProcessor.inheritCallChain(parentContext); final Scriptable processOriginalCallScope = processCallback.getFirst(); final Scriptable processCallScope = this.facadeFactory.toFacadedObject(processOriginalCallScope, parentScope); final Function processFn = processCallback.getSecond(); if (processFn instanceof NativeFunction) { // native function has parent scope based on location in source code // per batch function contract we need to execute it in our process scope final NativeFunction nativeFn = (NativeFunction) processFn; final ThreadLocalParentScope threadLocalParentScope = (ThreadLocalParentScope) nativeFn.getParentScope(); threadLocalParentScope.setEffectiveParentScope(processScope); try { // execute with thread local parent scope nativeFn.call(cx, processScope, processCallScope, new Object[] { element }); } finally { threadLocalParentScope.removeEffectiveParentScope(); } } else { // not a native function, so has not associated scope - calling as-is processFn.call(cx, processScope, processCallScope, new Object[] { element }); } } catch (final WrappedException ex) { final Throwable wrappedException = ex.getWrappedException(); if (wrappedException instanceof RuntimeException) { throw (RuntimeException) wrappedException; } throw ex; } finally { Context.exit(); } }
protected void doAfterProcess(final Context parentContext, final Scriptable parentScope, final Scriptable processScope, final Scriptable thisObj, final Pair<Scriptable, Function> afterProcessCallback) { final Context cx = Context.enter(); try { this.scriptProcessor.inheritCallChain(parentContext); if (afterProcessCallback.getSecond() != null) { final Scriptable afterProcessOriginalCallScope = afterProcessCallback.getFirst(); final Scriptable afterProcessCallScope = this.facadeFactory.toFacadedObject(afterProcessOriginalCallScope, parentScope); final Function afterProcessFn = afterProcessCallback.getSecond(); if (afterProcessFn instanceof NativeFunction) { // native function has parent scope based on location in source code // per batch function contract we need to execute it in our process scope final NativeFunction nativeFn = (NativeFunction) afterProcessFn; final ThreadLocalParentScope threadLocalParentScope = (ThreadLocalParentScope) nativeFn.getParentScope(); threadLocalParentScope.setEffectiveParentScope(processScope); try { // execute with thread local parent scope nativeFn.call(cx, processScope, afterProcessCallScope, new Object[0]); } finally { threadLocalParentScope.removeEffectiveParentScope(); } } else { // not a native function, so has not associated scope - calling as-is afterProcessFn.call(cx, processScope, afterProcessCallScope, new Object[0]); } } } catch (final WrappedException ex) { final Throwable wrappedException = ex.getWrappedException(); if (wrappedException instanceof RuntimeException) { throw (RuntimeException) wrappedException; } throw ex; } finally { Context.exit(); // clear thread-local facade mapping this.facadeFactory.clearThread(); } }
/** * Wait for the given function to return true * * @param func */ void waitFor(NativeFunction func);
/** * Wait for the given function to return true * * @param func * @param waitIterationMilliseconds the milliseconds to wait before calling func again */ void waitFor(NativeFunction func, long waitIterationMilliseconds);
/** * * @param onReceiveFunction */ public RhinoBroadcastReceiver(NativeFunction onReceiveFunction) { this.onReceiveFunction = onReceiveFunction; }