Search in sources :

Example 56 with Function

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

the class JSForm method js_setOnRecordEditStartMethod.

/**
 * @deprecated As of release 4.1, replaced by setOnRecordEditStart(JSMethod).
 */
@Deprecated
@ServoyClientSupport(ng = false, wc = true, sc = true)
public void js_setOnRecordEditStartMethod(Object functionOrInteger) {
    checkModification();
    if (functionOrInteger instanceof Function) {
        Function function = (Function) functionOrInteger;
        ScriptMethod scriptMethod = getScriptMethod(function, application.getFlattenedSolution());
        if (scriptMethod != null) {
            getForm().setOnRecordEditStartMethodID(scriptMethod.getID());
        } else {
            getForm().setOnRecordEditStartMethodID(0);
        }
    } else if (functionOrInteger instanceof Number) {
        getForm().setOnRecordEditStartMethodID(((Number) functionOrInteger).intValue());
    }
}
Also used : JSFunction(org.mozilla.javascript.annotations.JSFunction) Function(org.mozilla.javascript.Function) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ServoyClientSupport(com.servoy.base.scripting.annotations.ServoyClientSupport)

Example 57 with Function

use of org.mozilla.javascript.Function in project kolmafia by kolmafia.

the class JavascriptRuntime method executeRun.

private Value executeRun(final String functionName, final Object[] arguments, final boolean executeTopLevel) {
    Context cx = Context.getCurrentContext();
    Scriptable scope = currentTopScope;
    Object[] argumentsNonNull = arguments != null ? arguments : new Object[] {};
    Object[] runArguments = Arrays.stream(argumentsNonNull).map(o -> o instanceof MonsterData ? EnumeratedWrapper.wrap(scope, MonsterProxy.class, DataTypes.makeMonsterValue((MonsterData) o)) : o).toArray();
    return executeFunction(scope, () -> {
        Scriptable exports = null;
        if (executeTopLevel) {
            Require require = new SafeRequire(cx, scope, currentStdLib);
            if (scriptFile != null) {
                exports = require.requireMain(cx, scriptFile.toURI().toString());
            } else {
                require.install(scope);
                return cx.evaluateString(scope, scriptString, "command line", 1, null);
            }
        }
        if (functionName != null) {
            Object mainFunction = ScriptableObject.getProperty(exports != null ? exports : scope, functionName);
            if (mainFunction instanceof Function) {
                return ((Function) mainFunction).call(cx, scope, cx.newObject(currentTopScope), runArguments);
            }
        }
        return null;
    });
}
Also used : Context(org.mozilla.javascript.Context) EcmaError(org.mozilla.javascript.EcmaError) Arrays(java.util.Arrays) KoLConstants(net.sourceforge.kolmafia.KoLConstants) Supplier(java.util.function.Supplier) MonsterProxy(net.sourceforge.kolmafia.textui.parsetree.ProxyRecordValue.MonsterProxy) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Symbol(net.sourceforge.kolmafia.textui.parsetree.Symbol) ScriptException(net.sourceforge.kolmafia.textui.ScriptException) Matcher(java.util.regex.Matcher) WrappedException(org.mozilla.javascript.WrappedException) KoLmafia(net.sourceforge.kolmafia.KoLmafia) Type(net.sourceforge.kolmafia.textui.parsetree.Type) Preferences(net.sourceforge.kolmafia.preferences.Preferences) Function(org.mozilla.javascript.Function) Require(org.mozilla.javascript.commonjs.module.Require) StaticEntity(net.sourceforge.kolmafia.StaticEntity) ContextFactory(org.mozilla.javascript.ContextFactory) RuntimeLibrary(net.sourceforge.kolmafia.textui.RuntimeLibrary) Context(org.mozilla.javascript.Context) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DataTypes(net.sourceforge.kolmafia.textui.DataTypes) Value(net.sourceforge.kolmafia.textui.parsetree.Value) VariableReference(net.sourceforge.kolmafia.textui.parsetree.VariableReference) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) ProxyRecordValue(net.sourceforge.kolmafia.textui.parsetree.ProxyRecordValue) Scriptable(org.mozilla.javascript.Scriptable) ScriptableObject(org.mozilla.javascript.ScriptableObject) AbstractRuntime(net.sourceforge.kolmafia.textui.AbstractRuntime) MonsterData(net.sourceforge.kolmafia.MonsterData) Pattern(java.util.regex.Pattern) Function(org.mozilla.javascript.Function) Require(org.mozilla.javascript.commonjs.module.Require) MonsterProxy(net.sourceforge.kolmafia.textui.parsetree.ProxyRecordValue.MonsterProxy) MonsterData(net.sourceforge.kolmafia.MonsterData) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 58 with Function

use of org.mozilla.javascript.Function in project kolmafia by kolmafia.

the class EnumeratedWrapperPrototype method initToScope.

public void initToScope(Context cx, Scriptable scope, Scriptable runtimeLibrary) {
    setPrototype(ScriptableObject.getObjectPrototype(scope));
    if (recordValueClass != null) {
        for (Method method : recordValueClass.getDeclaredMethods()) {
            if (method.getName().startsWith("get_")) {
                ProxyRecordMethodWrapper methodWrapper = new ProxyRecordMethodWrapper(scope, ScriptableObject.getFunctionPrototype(scope), method);
                String methodShortName = JavascriptRuntime.toCamelCase(method.getName().replace("get_", ""));
                setGetterOrSetter(methodShortName, 0, methodWrapper, false);
            }
        }
    }
    try {
        Method constructorMethod = EnumeratedWrapper.class.getDeclaredMethod("constructDefaultValue");
        FunctionObject constructor = new FunctionObject(getClassName(), constructorMethod, scope);
        constructor.addAsConstructor(scope, this);
        if (runtimeLibrary != null) {
            ScriptableObject.defineProperty(runtimeLibrary, getClassName(), constructor, DONTENUM | READONLY | PERMANENT);
        }
        Method getMethod = EnumeratedWrapper.class.getDeclaredMethod("genericGet", Context.class, Scriptable.class, Object[].class, Function.class);
        Function getFunction = new FunctionObject("get", getMethod, scope);
        ScriptableObject.defineProperty(getFunction, "typeName", getClassName(), DONTENUM | READONLY | PERMANENT);
        constructor.defineProperty("get", getFunction, DONTENUM | READONLY | PERMANENT);
        Method allMethod = EnumeratedWrapper.class.getDeclaredMethod("all", Context.class, Scriptable.class, Object[].class, Function.class);
        Function allFunction = new FunctionObject("all", allMethod, scope);
        ScriptableObject.defineProperty(allFunction, "typeName", getClassName(), DONTENUM | READONLY | PERMANENT);
        constructor.defineProperty("all", allFunction, DONTENUM | READONLY | PERMANENT);
        constructor.sealObject();
        for (String methodName : new String[] { "toString" }) {
            Method method = EnumeratedWrapper.class.getDeclaredMethod(methodName);
            FunctionObject functionObject = new FunctionObject(methodName, method, scope);
            defineProperty(methodName, functionObject, DONTENUM | READONLY | PERMANENT);
            functionObject.sealObject();
        }
    } catch (NoSuchMethodException e) {
        KoLmafia.updateDisplay(KoLConstants.MafiaState.ERROR, "NoSuchMethodException: " + e.getMessage());
    }
    sealObject();
}
Also used : Function(org.mozilla.javascript.Function) ScriptableObject(org.mozilla.javascript.ScriptableObject) FunctionObject(org.mozilla.javascript.FunctionObject) Method(java.lang.reflect.Method) FunctionObject(org.mozilla.javascript.FunctionObject)

Example 59 with Function

use of org.mozilla.javascript.Function in project jslint4java by happygiraffe.

the class JSLintBuilder method fromReader.

/**
 * Initialize the scope with an arbitrary jslint.
 *
 * @param reader
 *            an input source providing jslint.js.
 * @param name
 *            the name of the resource backed by the reader
 * @return a configured {@link JSLint}
 * @throws IOException
 *             if there are any problems reading from {@code reader} .
 */
@NeedsContext
public JSLint fromReader(Reader reader, String name) throws IOException {
    try {
        Context cx = contextFactory.enterContext();
        ScriptableObject scope = cx.initStandardObjects();
        cx.evaluateReader(scope, reader, name, 1, null);
        Function lintFunc = (Function) scope.get("JSLINT", scope);
        return new JSLint(contextFactory, lintFunc);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 60 with Function

use of org.mozilla.javascript.Function in project scriptographer by scriptographer.

the class TopLevel method mapJavaClass.

/**
 * Maps a Java class to a JavaScript prototype, so this can be used
 * instead for wrapping of returned java types. So far this is only
 * used for java.io.File in Scriptographer.
 */
public static void mapJavaClass(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    if (args.length == 2) {
        for (int i = 0; i < args.length; i++) args[i] = Context.jsToJava(args[i], Object.class);
        if (args[0] instanceof Class && args[1] instanceof Function) {
            Class cls = (Class) args[0];
            Function proto = (Function) args[1];
            RhinoWrapFactory factory = (RhinoWrapFactory) cx.getWrapFactory();
            factory.mapJavaClass(cls, proto);
        }
    }
}
Also used : Function(org.mozilla.javascript.Function) ExtendedJavaClass(com.scratchdisk.script.rhino.ExtendedJavaClass)

Aggregations

Function (org.mozilla.javascript.Function)126 Scriptable (org.mozilla.javascript.Scriptable)35 Context (org.mozilla.javascript.Context)33 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)27 ScriptableObject (org.mozilla.javascript.ScriptableObject)27 JSFunction (org.mozilla.javascript.annotations.JSFunction)25 MouseEventImpl (org.loboevolution.html.js.events.MouseEventImpl)11 BaseFunction (org.mozilla.javascript.BaseFunction)10 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)10 NativeObject (org.mozilla.javascript.NativeObject)10 RepositoryException (com.servoy.j2db.persistence.RepositoryException)8 GlobalScope (com.servoy.j2db.scripting.GlobalScope)7 ServoyException (com.servoy.j2db.util.ServoyException)7 IOException (java.io.IOException)7 HtmlRendererContext (org.loboevolution.http.HtmlRendererContext)7 JSONObject (org.json.JSONObject)6 ModelNode (org.loboevolution.html.dom.nodeimpl.ModelNode)6 RhinoException (org.mozilla.javascript.RhinoException)6 PropertyDescription (org.sablo.specification.PropertyDescription)6 ArrayList (java.util.ArrayList)5