Search in sources :

Example 6 with NativeFunction

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

the class ScriptEngine method compileScriptProvider.

/**
 * @param sp
 * @param scope
 * @param cx
 * @return
 */
@SuppressWarnings("nls")
protected Function compileScriptProvider(IScriptProvider sp, Scriptable scope, Context cx, String sourceName) {
    String declaration = sp.getDeclaration();
    // dont return the calc function itself but still the value.
    if (sp instanceof ScriptCalculation) {
        declaration = extractFunction(declaration, "function $1_");
    } else {
        declaration = extractFunction(declaration, "function $1");
    }
    // f below always seems to be NativeFunction instance as both Codegen.createFunctionObject and Interpreter.createFunctionObject return
    // a NativeFunction instance; and that is what cx.compileFunction(...) ends up calling
    Function f = cx.compileFunction(scope, declaration, sourceName, sp.getLineNumberOffset(), null);
    if (!(sp instanceof ScriptCalculation)) {
        if (sp.getScopeName() != null) {
            // $NON-NLS-1$
            f.put("_scopename_", f, sp.getScopeName());
        }
        // $NON-NLS-1$
        f.put("_methodname_", f, sp.getDataProviderID());
        f.put("_AllowToRunInFind_", f, Boolean.valueOf(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
        sp.getDeclaration().indexOf("@AllowToRunInFind") != -1 || declaration.indexOf(".search") != -1 || declaration.indexOf("controller.loadAllRecords") != -1));
    }
    String methodName = sp.getName();
    PerformanceData performanceData = application instanceof IPerformanceDataProvider ? ((IPerformanceDataProvider) application).getPerformanceData() : null;
    if (performanceData != null) {
        String scopeName = scope.getClassName();
        if (scope instanceof LazyCompilationScope) {
            scopeName = ((LazyCompilationScope) scope).getScopeName();
            scopeName = lookForSuperCalls(sp, scopeName);
        } else if (scope.getParentScope() instanceof LazyCompilationScope) {
            scopeName = ((LazyCompilationScope) scope.getParentScope()).getScopeName();
            scopeName = lookForSuperCalls(sp, scopeName);
        } else if (scope instanceof FoundSet) {
            Scriptable parentScope = ((FoundSet) scope).getPrototype();
            if (parentScope instanceof LazyCompilationScope) {
                scopeName = ((LazyCompilationScope) parentScope).getScopeName();
            }
        }
        methodName = scopeName + "." + methodName;
        return new FunctionWrapper(f, methodName, performanceData, application.getClientID());
    }
    return f;
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) NativeFunction(org.mozilla.javascript.NativeFunction) Function(org.mozilla.javascript.Function) IPerformanceDataProvider(com.servoy.j2db.server.shared.IPerformanceDataProvider) PerformanceData(com.servoy.j2db.server.shared.PerformanceData) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) Scriptable(org.mozilla.javascript.Scriptable)

Example 7 with NativeFunction

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

the class ScriptEngine method eval.

public Object eval(Scriptable scope, String eval_string) {
    String userUidBefore = null;
    if (Context.getCurrentContext() == null) {
        userUidBefore = application.getClientInfo().getUserUid();
    }
    Context cx = Context.enter();
    try {
        Object o = null;
        // $NON-NLS-1$ //$NON-NLS-2$
        Function compileFunction = cx.compileFunction(scope, "function evalFunction(){}", "evalFunction", 0, null);
        if (compileFunction instanceof FunctionWrapper)
            compileFunction = ((FunctionWrapper) compileFunction).getWrappedFunction();
        if (compileFunction instanceof NativeFunction) {
            o = // $NON-NLS-1$
            cx.evaluateString(// $NON-NLS-1$
            ScriptRuntime.createFunctionActivation((NativeFunction) compileFunction, scope, null), // $NON-NLS-1$
            eval_string, // $NON-NLS-1$
            "internal_anon", // $NON-NLS-1$
            1, null);
        } else {
            // $NON-NLS-1$
            o = cx.evaluateString(scope, eval_string, "internal_anon", 1, null);
        }
        if (o instanceof Wrapper) {
            o = ((Wrapper) o).unwrap();
        }
        if (o == Scriptable.NOT_FOUND || o == Undefined.instance) {
            o = null;
        }
        return o;
    } catch (Exception ex) {
        if (ex instanceof ExitScriptException) {
            throw (ExitScriptException) ex;
        } else if (ex.getCause() instanceof ExitScriptException) {
            throw (ExitScriptException) ex.getCause();
        } else if (ex instanceof JavaScriptException && ((JavaScriptException) ex).getValue() instanceof ExitScriptException) {
            throw (ExitScriptException) ((JavaScriptException) ex).getValue();
        } else if (application.getSolution() != null) {
            // $NON-NLS-1$//$NON-NLS-2$
            application.reportError(application.getI18NMessage("servoy.formPanel.error.evalString") + eval_string + "'", ex);
        } else {
            // $NON-NLS-1$
            Debug.trace("Solution already closed, ignoring exception", ex);
        }
    } finally {
        Context.exit();
        testClientUidChange(scope, userUidBefore);
    }
    return null;
}
Also used : Context(org.mozilla.javascript.Context) NativeFunction(org.mozilla.javascript.NativeFunction) Function(org.mozilla.javascript.Function) RenderableWrapper(com.servoy.j2db.ui.RenderableWrapper) Wrapper(org.mozilla.javascript.Wrapper) DataRendererOnRenderWrapper(com.servoy.j2db.ui.DataRendererOnRenderWrapper) NativeFunction(org.mozilla.javascript.NativeFunction) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ApplicationException(com.servoy.j2db.ApplicationException) ExitScriptException(com.servoy.j2db.ExitScriptException) DataException(com.servoy.j2db.dataprocessing.DataException) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ExitScriptException(com.servoy.j2db.ExitScriptException) JavaScriptException(org.mozilla.javascript.JavaScriptException)

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