Search in sources :

Example 6 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 7 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 8 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)

Aggregations

NativeJavaObject (org.mozilla.javascript.NativeJavaObject)8 Context (org.mozilla.javascript.Context)5 Scriptable (org.mozilla.javascript.Scriptable)5 Test (org.junit.Test)2 ScriptableObject (org.mozilla.javascript.ScriptableObject)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Function (org.mozilla.javascript.Function)1 NativeObject (org.mozilla.javascript.NativeObject)1 RhinoException (org.mozilla.javascript.RhinoException)1 Undefined (org.mozilla.javascript.Undefined)1 Wrapper (org.mozilla.javascript.Wrapper)1 EvaluationException (org.neo4j.server.rest.domain.EvaluationException)1