Java 类org.mozilla.javascript.NativeFunction 实例源码

项目:https-github.com-hyb1996-NoRootScriptDroid    文件:ProxyObject.java   
@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);
    }
}
项目:Auto.js    文件:ProxyObject.java   
@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);
    }
}
项目:Auto.js    文件:ProxyJavaObject.java   
@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);
    }
}
项目:spork-streaming    文件:JsScriptEngine.java   
@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;
}
项目:spork-streaming    文件:JSPig.java   
/**
 * 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);
}
项目:spork    文件:JsScriptEngine.java   
@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;
}
项目:spork    文件:JSPig.java   
/**
 * 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);
}
项目:PonIC    文件:JsScriptEngine.java   
@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;
}
项目:PonIC    文件:JSPig.java   
/**
 * 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);
}
项目:alfresco-enhanced-script-environment    文件:AbstractExecuteBatchFunction.java   
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));
            }
        }
    }
}
项目:sedge    文件:JsScriptEngine.java   
@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;
}
项目:sedge    文件:JSPig.java   
/**
 * 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);
}
项目:pizzascript    文件:PizzaImpl.java   
@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");
        }
    }
}
项目:android-js    文件:HelloAndroidActivityTest.java   
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();
    }

}
项目:pizzascript    文件:PizzaImpl.java   
@Override
public void waitFor(NativeFunction func) {
    waitFor(func, 100);
}
项目:minium    文件:MiniumBackend.java   
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);
}
项目:alfresco-enhanced-script-environment    文件:AbstractExecuteBatchFunction.java   
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();
    }
}
项目:alfresco-enhanced-script-environment    文件:AbstractExecuteBatchFunction.java   
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();
    }
}
项目:alfresco-enhanced-script-environment    文件:AbstractExecuteBatchFunction.java   
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();
    }
}
项目:pizzascript    文件:Pizza.java   
/**
 * Wait for the given function to return true
 *
 * @param func
 */
void waitFor(NativeFunction func);
项目:pizzascript    文件:Pizza.java   
/**
 * 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);
项目:android-js    文件:RhinoBroadcastReceiver.java   
/**
 *
 * @param onReceiveFunction
 */
public RhinoBroadcastReceiver(NativeFunction onReceiveFunction) {
    this.onReceiveFunction = onReceiveFunction;
}