Search in sources :

Example 11 with Function

use of org.mozilla.javascript.Function in project cxf by apache.

the class AbstractDOMProvider method publish.

public void publish() throws Exception {
    String addr = epAddress;
    String wsdlLoc = null;
    String svcNm = null;
    String portNm = null;
    String tgtNmspc = null;
    String binding = null;
    Object obj = webSvcProviderVar.get("wsdlLocation", webSvcProviderVar);
    if (obj == Scriptable.NOT_FOUND) {
        throw new JSDOMProviderException(NO_WSDL_LOCATION);
    }
    if (obj instanceof String) {
        wsdlLoc = (String) obj;
    }
    obj = webSvcProviderVar.get("serviceName", webSvcProviderVar);
    if (obj == Scriptable.NOT_FOUND) {
        throw new JSDOMProviderException(NO_SERVICE_NAME);
    }
    if (obj instanceof String) {
        svcNm = (String) obj;
    }
    obj = webSvcProviderVar.get("portName", webSvcProviderVar);
    if (obj == Scriptable.NOT_FOUND) {
        throw new JSDOMProviderException(NO_PORT_NAME);
    }
    if (obj instanceof String) {
        portNm = (String) obj;
    }
    obj = webSvcProviderVar.get("targetNamespace", webSvcProviderVar);
    if (obj == Scriptable.NOT_FOUND) {
        throw new JSDOMProviderException(NO_TGT_NAMESPACE);
    }
    if (obj instanceof String) {
        tgtNmspc = (String) obj;
    }
    if (addr == null) {
        obj = webSvcProviderVar.get("EndpointAddress", scriptScope);
        if (obj != Scriptable.NOT_FOUND && obj instanceof String) {
            addr = (String) obj;
            isBaseAddr = false;
        }
    }
    if (addr == null) {
        throw new JSDOMProviderException(NO_EP_ADDR);
    }
    if (isBaseAddr) {
        if (addr.endsWith("/")) {
            addr += portNm;
        } else {
            addr = addr + "/" + portNm;
        }
    }
    obj = webSvcProviderVar.get("BindingType", scriptScope);
    if (obj != Scriptable.NOT_FOUND && obj instanceof String) {
        binding = (String) obj;
    }
    obj = webSvcProviderVar.get("invoke", webSvcProviderVar);
    if (obj == Scriptable.NOT_FOUND) {
        throw new JSDOMProviderException(NO_INVOKE);
    }
    if (obj instanceof Function) {
        invokeFunc = (Function) obj;
    } else {
        throw new JSDOMProviderException(ILLEGAL_INVOKE_TYPE);
    }
    Bus bus = BusFactory.getThreadDefaultBus();
    JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
    factory.setWsdlLocation(wsdlLoc);
    factory.setBindingId(binding);
    factory.setServiceName(new QName(tgtNmspc, svcNm));
    factory.setEndpointName(new QName(tgtNmspc, portNm));
    ep = new EndpointImpl(bus, this, factory);
    ep.publish(addr);
}
Also used : Function(org.mozilla.javascript.Function) Bus(org.apache.cxf.Bus) QName(javax.xml.namespace.QName) EndpointImpl(org.apache.cxf.jaxws.EndpointImpl) XMLObject(org.mozilla.javascript.xml.XMLObject) JaxWsServerFactoryBean(org.apache.cxf.jaxws.JaxWsServerFactoryBean)

Example 12 with Function

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

the class ContinuationsApiTest method testSerializationWithContinuations.

public void testSerializationWithContinuations() throws IOException, ClassNotFoundException {
    Context cx = Context.enter();
    try {
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        cx.evaluateString(globalScope, "function f(a) { var k = myObject.f(a); var t = []; return k; }", "function test source", 1, null);
        Function f = (Function) globalScope.get("f", globalScope);
        Object[] args = { 7 };
        cx.callFunctionWithContinuations(f, globalScope, args);
        fail("Should throw ContinuationPending");
    } catch (ContinuationPending pending) {
        // serialize
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ScriptableOutputStream sos = new ScriptableOutputStream(baos, globalScope);
        sos.writeObject(globalScope);
        sos.writeObject(pending.getContinuation());
        sos.close();
        baos.close();
        byte[] serializedData = baos.toByteArray();
        // deserialize
        ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
        ScriptableInputStream sis = new ScriptableInputStream(bais, globalScope);
        globalScope = (Scriptable) sis.readObject();
        Object continuation = sis.readObject();
        sis.close();
        bais.close();
        Object result = cx.resumeContinuation(continuation, globalScope, 8);
        assertEquals(8, ((Number) result).intValue());
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) ContinuationPending(org.mozilla.javascript.ContinuationPending) ScriptableInputStream(org.mozilla.javascript.serialize.ScriptableInputStream) ScriptableOutputStream(org.mozilla.javascript.serialize.ScriptableOutputStream) Scriptable(org.mozilla.javascript.Scriptable)

Example 13 with Function

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

the class ContinuationsApiTest method testFunctionWithContinuations.

public void testFunctionWithContinuations() {
    Context cx = Context.enter();
    try {
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        cx.evaluateString(globalScope, "function f(a) { return myObject.f(a); }", "function test source", 1, null);
        Function f = (Function) globalScope.get("f", globalScope);
        Object[] args = { 7 };
        cx.callFunctionWithContinuations(f, globalScope, args);
        fail("Should throw ContinuationPending");
    } catch (ContinuationPending pending) {
        Object applicationState = pending.getApplicationState();
        assertEquals(7, ((Number) applicationState).intValue());
        int saved = (Integer) applicationState;
        Object result = cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
        assertEquals(8, ((Number) result).intValue());
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) ContinuationPending(org.mozilla.javascript.ContinuationPending)

Example 14 with Function

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

the class Main method processSource.

/**
     * Evaluate JavaScript source.
     *
     * @param cx the current context
     * @param filename the name of the file to compile, or null
     *                 for interactive mode.
     */
public static void processSource(Context cx, String filename) {
    if (filename == null || filename.equals("-")) {
        PrintStream ps = global.getErr();
        if (filename == null) {
            // print implementation version
            ps.println(cx.getImplementationVersion());
        }
        String charEnc = shellContextFactory.getCharacterEncoding();
        if (charEnc == null) {
            charEnc = System.getProperty("file.encoding");
        }
        BufferedReader in;
        try {
            in = new BufferedReader(new InputStreamReader(global.getIn(), charEnc));
        } catch (UnsupportedEncodingException e) {
            throw new UndeclaredThrowableException(e);
        }
        int lineno = 1;
        boolean hitEOF = false;
        while (!hitEOF) {
            String[] prompts = global.getPrompts(cx);
            if (filename == null)
                ps.print(prompts[0]);
            ps.flush();
            String source = "";
            // Collect lines of source to compile.
            while (true) {
                String newline;
                try {
                    newline = in.readLine();
                } catch (IOException ioe) {
                    ps.println(ioe.toString());
                    break;
                }
                if (newline == null) {
                    hitEOF = true;
                    break;
                }
                source = source + newline + "\n";
                lineno++;
                if (cx.stringIsCompilableUnit(source))
                    break;
                ps.print(prompts[1]);
            }
            Script script = loadScriptFromSource(cx, source, "<stdin>", lineno, null);
            if (script != null) {
                Object result = evaluateScript(script, cx, global);
                // Avoid printing out undefined or function definitions.
                if (result != Context.getUndefinedValue() && !(result instanceof Function && source.trim().startsWith("function"))) {
                    try {
                        ps.println(Context.toString(result));
                    } catch (RhinoException rex) {
                        ToolErrorReporter.reportException(cx.getErrorReporter(), rex);
                    }
                }
                NativeArray h = global.history;
                h.put((int) h.getLength(), h, source);
            }
        }
        ps.println();
    } else if (filename.equals(mainModule)) {
        try {
            require.requireMain(cx, filename);
        } catch (RhinoException rex) {
            ToolErrorReporter.reportException(cx.getErrorReporter(), rex);
            exitCode = EXITCODE_RUNTIME_ERROR;
        } catch (VirtualMachineError ex) {
            // Treat StackOverflow and OutOfMemory as runtime errors
            ex.printStackTrace();
            String msg = ToolErrorReporter.getMessage("msg.uncaughtJSException", ex.toString());
            exitCode = EXITCODE_RUNTIME_ERROR;
            Context.reportError(msg);
        }
    } else {
        processFile(cx, global, filename);
    }
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) PrintStream(java.io.PrintStream) Script(org.mozilla.javascript.Script) InputStreamReader(java.io.InputStreamReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Function(org.mozilla.javascript.Function) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) BufferedReader(java.io.BufferedReader) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException)

Example 15 with Function

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

the class FlexibleCompletor method complete.

public int complete(String buffer, int cursor, List<String> candidates) {
    // Starting from "cursor" at the end of the buffer, look backward
    // and collect a list of identifiers separated by (possibly zero)
    // dots. Then look up each identifier in turn until getting to the
    // last, presumably incomplete fragment. Then enumerate all the
    // properties of the last object and find any that have the
    // fragment as a prefix and return those for autocompletion.
    int m = cursor - 1;
    while (m >= 0) {
        char c = buffer.charAt(m);
        if (!Character.isJavaIdentifierPart(c) && c != '.')
            break;
        m--;
    }
    String namesAndDots = buffer.substring(m + 1, cursor);
    String[] names = namesAndDots.split("\\.", -1);
    Scriptable obj = this.global;
    for (int i = 0; i < names.length - 1; i++) {
        Object val = obj.get(names[i], global);
        if (val instanceof Scriptable)
            obj = (Scriptable) val;
        else {
            // no matches
            return buffer.length();
        }
    }
    Object[] ids = (obj instanceof ScriptableObject) ? ((ScriptableObject) obj).getAllIds() : obj.getIds();
    String lastPart = names[names.length - 1];
    for (int i = 0; i < ids.length; i++) {
        if (!(ids[i] instanceof String))
            continue;
        String id = (String) ids[i];
        if (id.startsWith(lastPart)) {
            if (obj.get(id, obj) instanceof Function)
                id += "(";
            candidates.add(id);
        }
    }
    return buffer.length() - lastPart.length();
}
Also used : Function(org.mozilla.javascript.Function) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

Function (org.mozilla.javascript.Function)21 ScriptableObject (org.mozilla.javascript.ScriptableObject)12 Context (org.mozilla.javascript.Context)10 Scriptable (org.mozilla.javascript.Scriptable)6 ContextAction (org.mozilla.javascript.ContextAction)4 Map (java.util.Map)3 SightlyException (org.apache.sling.scripting.sightly.SightlyException)3 BaseFunction (org.mozilla.javascript.BaseFunction)3 HashMap (java.util.HashMap)2 TimingFunction (org.apache.sling.scripting.sightly.js.impl.async.TimingFunction)2 HybridObject (org.apache.sling.scripting.sightly.js.impl.rhino.HybridObject)2 ContinuationPending (org.mozilla.javascript.ContinuationPending)2 NativeArray (org.mozilla.javascript.NativeArray)2 NativeObject (org.mozilla.javascript.NativeObject)2 RhinoException (org.mozilla.javascript.RhinoException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ResultBuilder (com.googlecode.jslint4java.JSLintResult.ResultBuilder)1 ArgumentReader (com.scratchdisk.script.ArgumentReader)1 StringArgumentReader (com.scratchdisk.script.StringArgumentReader)1 ExtendedJavaClass (com.scratchdisk.script.rhino.ExtendedJavaClass)1