Search in sources :

Example 21 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project jmeter by apache.

the class BSFJavaScriptEngine method eval.

/**
 * This is used by an application to evaluate a string containing
 * some expression.
 */
@Override
public Object eval(String source, int lineNo, int columnNo, Object oscript) throws BSFException {
    String scriptText = oscript.toString();
    Object retval = null;
    Context cx;
    try {
        cx = Context.enter();
        cx.setOptimizationLevel(-1);
        cx.setGeneratingDebug(false);
        cx.setGeneratingSource(false);
        cx.setOptimizationLevel(0);
        cx.setDebugger(null, null);
        retval = cx.evaluateString(global, scriptText, source, lineNo, null);
        if (retval instanceof NativeJavaObject) {
            retval = ((NativeJavaObject) retval).unwrap();
        }
    } catch (Throwable t) {
        // NOSONAR We handle correctly Error case in function, includes JavaScriptException, rethrows Errors
        handleError(t);
    } finally {
        Context.exit();
    }
    return retval;
}
Also used : Context(org.mozilla.javascript.Context) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 22 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project hackpad by dropbox.

the class JavaAcessibilityTest method testAccessingFields.

public void testAccessingFields() {
    Object result = runScript(importClass + "PrivateAccessClass.staticPackagePrivateInt");
    assertEquals(new Integer(0), result);
    result = runScript(importClass + "PrivateAccessClass.staticPrivateInt");
    assertEquals(new Integer(1), result);
    result = runScript(importClass + "PrivateAccessClass.staticProtectedInt");
    assertEquals(new Integer(2), result);
    result = runScript(importClass + "new PrivateAccessClass().packagePrivateString");
    assertEquals("package private", ((NativeJavaObject) result).unwrap());
    result = runScript(importClass + "new PrivateAccessClass().privateString");
    assertEquals("private", ((NativeJavaObject) result).unwrap());
    result = runScript(importClass + "new PrivateAccessClass().protectedString");
    assertEquals("protected", ((NativeJavaObject) result).unwrap());
    result = runScript(importClass + "new PrivateAccessClass.PrivateNestedClass().packagePrivateInt");
    assertEquals(new Integer(0), result);
    result = runScript(importClass + "new PrivateAccessClass.PrivateNestedClass().privateInt");
    assertEquals(new Integer(1), result);
    result = runScript(importClass + "new PrivateAccessClass.PrivateNestedClass().protectedInt");
    assertEquals(new Integer(2), result);
}
Also used : NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 23 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project kotlin by JetBrains.

the class RhinoFunctionNativeObjectResultChecker method assertResultValid.

@Override
protected void assertResultValid(Object result, Context context) {
    if (result instanceof NativeJavaObject) {
        NativeJavaObject nativeJavaObject = (NativeJavaObject) result;
        Object unwrap = nativeJavaObject.unwrap();
        super.assertResultValid(unwrap, context);
    } else {
        super.assertResultValid(result, context);
    }
}
Also used : NativeJavaObject(org.mozilla.javascript.NativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 24 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project SmartZPN by andforce.

the class PacScriptParser method runScript.

private String runScript(String js, String functionName, Object[] functionParams) {
    Context rhino = Context.enter();
    rhino.setOptimizationLevel(-1);
    try {
        Scriptable scope = rhino.initStandardObjects();
        rhino.evaluateString(scope, js, "JavaScript", js.split("\n").length, null);
        Function function = (Function) scope.get(functionName, scope);
        Object result = function.call(rhino, scope, scope, functionParams);
        if (result instanceof String) {
            return (String) result;
        } else if (result instanceof NativeJavaObject) {
            return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
        } else if (result instanceof NativeObject) {
            return (String) ((NativeObject) result).getDefaultValue(String.class);
        }
        return result.toString();
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) NativeObject(org.mozilla.javascript.NativeObject) Function(org.mozilla.javascript.Function) NativeObject(org.mozilla.javascript.NativeObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 25 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject 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)

Aggregations

NativeJavaObject (org.mozilla.javascript.NativeJavaObject)45 Scriptable (org.mozilla.javascript.Scriptable)16 Context (org.mozilla.javascript.Context)11 NativeArray (org.mozilla.javascript.NativeArray)9 ScriptableObject (org.mozilla.javascript.ScriptableObject)9 NativeJavaArray (org.mozilla.javascript.NativeJavaArray)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 IOException (java.io.IOException)5 Test (org.junit.Test)4 NativeObject (org.mozilla.javascript.NativeObject)4 Undefined (org.mozilla.javascript.Undefined)4 NodeList (org.w3c.dom.NodeList)4 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)3 SolutionScope (com.servoy.j2db.scripting.SolutionScope)3 EngineException (com.twinsoft.convertigo.engine.EngineException)3 Collection (java.util.Collection)3 Vector (java.util.Vector)3 FunctionObject (org.mozilla.javascript.FunctionObject)3 Node (org.w3c.dom.Node)3