Search in sources :

Example 1 with NativeFunction

use of org.mozilla.javascript.NativeFunction in project Auto.js by hyb1996.

the class ProxyJavaObject method put.

@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);
    }
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) NativeFunction(org.mozilla.javascript.NativeFunction) NativeObject(org.mozilla.javascript.NativeObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 2 with NativeFunction

use of org.mozilla.javascript.NativeFunction in project LoboEvolution by LoboEvolution.

the class Codegen method createFunctionObject.

@Override
public Function createFunctionObject(Context cx, Scriptable scope, Object bytecode, Object staticSecurityDomain) {
    Class<?> cl = defineClass(bytecode, staticSecurityDomain);
    NativeFunction f;
    try {
        Constructor<?> ctor = cl.getConstructors()[0];
        Object[] initArgs = { scope, cx, Integer.valueOf(0) };
        f = (NativeFunction) ctor.newInstance(initArgs);
    } catch (Exception ex) {
        throw new RuntimeException("Unable to instantiate compiled class:" + ex.toString());
    }
    return f;
}
Also used : NativeFunction(org.mozilla.javascript.NativeFunction) RhinoException(org.mozilla.javascript.RhinoException)

Example 3 with NativeFunction

use of org.mozilla.javascript.NativeFunction in project servoy-client by Servoy.

the class ServoyFunctionPropertyType method toJSON.

public JSONWriter toJSON(JSONWriter writer, String key, Object object, PropertyDescription pd, DataConversion clientConversion, FlattenedSolution fs, FormElement fe, WebFormComponent formComponent) throws JSONException {
    Map<String, Object> map = new HashMap<>();
    if (object != null && fs != null) {
        // $NON-NLS-1$
        String[] components = object.toString().split("-");
        if (components.length == 5) {
            String scriptString = null;
            // this is a uuid
            ScriptMethod sm = fs.getScriptMethod(object.toString());
            if (sm != null) {
                ISupportChilds parent = sm.getParent();
                if (parent instanceof Solution) {
                    scriptString = "scopes." + sm.getScopeName() + "." + sm.getName();
                } else if (parent instanceof Form) {
                    if (formComponent != null) {
                        // use the real, runtime form
                        scriptString = formComponent.getDataAdapterList().getForm().getForm().getName() + "." + sm.getName();
                    } else {
                        scriptString = ((Form) parent).getName() + "." + sm.getName();
                    }
                } else if (parent instanceof TableNode && fe != null) {
                    scriptString = "entity." + fe.getForm().getName() + "." + sm.getName();
                }
                object = scriptString;
            } else
                Debug.log("can't find a scriptmethod for: " + object);
        }
    }
    try {
        if (object instanceof String) {
            addScriptToMap((String) object, map);
        } else if (object instanceof NativeFunction) {
            nativeFunctionToJSON((NativeFunction) object, map);
        } else if (object instanceof FunctionWrapper && ((FunctionWrapper) object).getWrappedFunction() instanceof NativeFunction) {
            nativeFunctionToJSON((NativeFunction) ((FunctionWrapper) object).getWrappedFunction(), map);
        } else if (object instanceof Map) {
            map = new HashMap<String, Object>((Map<String, Object>) object);
            if (map.get("script") instanceof String)
                addScriptToMap((String) map.get("script"), map);
        }
    } catch (Exception ex) {
        Debug.error(ex);
    }
    return JSONUtils.toBrowserJSONFullValue(writer, key, map.size() == 0 ? null : map, null, clientConversion, null);
}
Also used : FunctionWrapper(com.servoy.j2db.scripting.FunctionWrapper) ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) HashMap(java.util.HashMap) Form(com.servoy.j2db.persistence.Form) JSForm(com.servoy.j2db.scripting.solutionmodel.JSForm) NativeFunction(org.mozilla.javascript.NativeFunction) JSONException(org.json.JSONException) TableNode(com.servoy.j2db.persistence.TableNode) JSONObject(org.json.JSONObject) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) HashMap(java.util.HashMap) Map(java.util.Map) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Solution(com.servoy.j2db.persistence.Solution)

Example 4 with NativeFunction

use of org.mozilla.javascript.NativeFunction in project BPjs by bThink-BGU.

the class ContinuationProgramState method collectJsValue.

/**
 * Take a Javascript value from Rhino, build a Java value for it.
 * @param jsValue
 * @return
 */
private Object collectJsValue(Object jsValue) {
    if (jsValue == null) {
        return null;
    } else if (jsValue instanceof NativeFunction) {
        return ((NativeFunction) jsValue).getEncodedSource();
    } else if (jsValue instanceof NativeArray) {
        NativeArray jsArr = (NativeArray) jsValue;
        List<Object> retVal = new ArrayList<>((int) jsArr.getLength());
        for (int idx = 0; idx < jsArr.getLength(); idx++) {
            retVal.add(collectJsValue(jsArr.get(idx)));
        }
        return retVal;
    } else if (jsValue instanceof ScriptableObject) {
        ScriptableObject jsObj = (ScriptableObject) jsValue;
        Map<Object, Object> retVal = new HashMap<>();
        for (Object key : jsObj.getIds()) {
            retVal.put(key, collectJsValue(jsObj.get(key)));
        }
        return retVal;
    } else if (jsValue instanceof ConsString) {
        return ((ConsString) jsValue).toString();
    } else if (jsValue instanceof NativeJavaObject) {
        NativeJavaObject jsJavaObj = (NativeJavaObject) jsValue;
        Object obj = jsJavaObj.unwrap();
        return obj;
    } else {
        String cn = jsValue.getClass().getCanonicalName();
        if (!cn.startsWith("java.") && (!cn.startsWith("il.ac.bgu"))) {
            System.out.println("collectJsValue: blind translation to java: " + jsValue + " (" + jsValue.getClass() + ")");
        }
        return jsValue;
    }
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) ScriptableObject(org.mozilla.javascript.ScriptableObject) NativeFunction(org.mozilla.javascript.NativeFunction) ArrayList(java.util.ArrayList) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) ConsString(org.mozilla.javascript.ConsString) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) HashMap(java.util.HashMap) Map(java.util.Map) ConsString(org.mozilla.javascript.ConsString)

Example 5 with NativeFunction

use of org.mozilla.javascript.NativeFunction in project Auto.js by hyb1996.

the class ProxyObject method put.

@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);
    }
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) NativeFunction(org.mozilla.javascript.NativeFunction) NativeObject(org.mozilla.javascript.NativeObject)

Aggregations

NativeFunction (org.mozilla.javascript.NativeFunction)7 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Function (org.mozilla.javascript.Function)2 NativeObject (org.mozilla.javascript.NativeObject)2 ApplicationException (com.servoy.j2db.ApplicationException)1 ExitScriptException (com.servoy.j2db.ExitScriptException)1 FlattenedSolution (com.servoy.j2db.FlattenedSolution)1 DataException (com.servoy.j2db.dataprocessing.DataException)1 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)1 Form (com.servoy.j2db.persistence.Form)1 ISupportChilds (com.servoy.j2db.persistence.ISupportChilds)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ScriptCalculation (com.servoy.j2db.persistence.ScriptCalculation)1 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)1 Solution (com.servoy.j2db.persistence.Solution)1 TableNode (com.servoy.j2db.persistence.TableNode)1 FunctionWrapper (com.servoy.j2db.scripting.FunctionWrapper)1 JSForm (com.servoy.j2db.scripting.solutionmodel.JSForm)1