Search in sources :

Example 61 with Scriptable

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

the class JsonParser method readObject.

private Object readObject() throws ParseException {
    Scriptable object = cx.newObject(scope);
    String id;
    Object value;
    boolean needsComma = false;
    consumeWhitespace();
    while (pos < length) {
        char c = src.charAt(pos++);
        switch(c) {
            case '}':
                return object;
            case ',':
                if (!needsComma) {
                    throw new ParseException("Unexpected comma in object literal");
                }
                needsComma = false;
                break;
            case '"':
                if (needsComma) {
                    throw new ParseException("Missing comma in object literal");
                }
                id = readString();
                consume(':');
                value = readValue();
                long index = ScriptRuntime.indexFromString(id);
                if (index < 0) {
                    object.put(id, object, value);
                } else {
                    object.put((int) index, object, value);
                }
                needsComma = true;
                break;
            default:
                throw new ParseException("Unexpected token in object literal");
        }
        consumeWhitespace();
    }
    throw new ParseException("Unterminated object literal");
}
Also used : Scriptable(org.mozilla.javascript.Scriptable)

Example 62 with Scriptable

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

the class RECharSet method executeRegExp.

/*
     * indexp is assumed to be an array of length 1
     */
Object executeRegExp(Context cx, Scriptable scope, RegExpImpl res, String str, int[] indexp, int matchType) {
    REGlobalData gData = new REGlobalData();
    int start = indexp[0];
    int end = str.length();
    if (start > end)
        start = end;
    //
    // Call the recursive matcher to do the real work.
    //
    boolean matches = matchRegExp(gData, re, str, start, end, res.multiline);
    if (!matches) {
        if (matchType != PREFIX)
            return null;
        return Undefined.instance;
    }
    int index = gData.cp;
    int ep = indexp[0] = index;
    int matchlen = ep - (start + gData.skipped);
    index -= matchlen;
    Object result;
    Scriptable obj;
    if (matchType == TEST) {
        /*
             * Testing for a match and updating cx.regExpImpl: don't allocate
             * an array object, do return true.
             */
        result = Boolean.TRUE;
        obj = null;
    } else {
        /*
             * The array returned on match has element 0 bound to the matched
             * string, elements 1 through re.parenCount bound to the paren
             * matches, an index property telling the length of the left context,
             * and an input property referring to the input string.
             */
        result = cx.newArray(scope, 0);
        obj = (Scriptable) result;
        String matchstr = str.substring(index, index + matchlen);
        obj.put(0, obj, matchstr);
    }
    if (re.parenCount == 0) {
        res.parens = null;
        res.lastParen = SubString.emptySubString;
    } else {
        SubString parsub = null;
        int num;
        res.parens = new SubString[re.parenCount];
        for (num = 0; num < re.parenCount; num++) {
            int cap_index = gData.parens_index(num);
            String parstr;
            if (cap_index != -1) {
                int cap_length = gData.parens_length(num);
                parsub = new SubString(str, cap_index, cap_length);
                res.parens[num] = parsub;
                if (matchType == TEST)
                    continue;
                parstr = parsub.toString();
                obj.put(num + 1, obj, parstr);
            } else {
                if (matchType != TEST)
                    obj.put(num + 1, obj, Undefined.instance);
            }
        }
        res.lastParen = parsub;
    }
    if (!(matchType == TEST)) {
        /*
             * Define the index and input properties last for better for/in loop
             * order (so they come after the elements).
             */
        obj.put("index", obj, Integer.valueOf(start + gData.skipped));
        obj.put("input", obj, str);
    }
    if (res.lastMatch == null) {
        res.lastMatch = new SubString();
        res.leftContext = new SubString();
        res.rightContext = new SubString();
    }
    res.lastMatch.str = str;
    res.lastMatch.index = index;
    res.lastMatch.length = matchlen;
    res.leftContext.str = str;
    if (cx.getLanguageVersion() == Context.VERSION_1_2) {
        /*
             * JS1.2 emulated Perl4.0.1.8 (patch level 36) for global regexps used
             * in scalar contexts, and unintentionally for the string.match "list"
             * psuedo-context.  On "hi there bye", the following would result:
             *
             * Language     while(/ /g){print("$`");}   s/ /$`/g
             * perl4.036    "hi", "there"               "hihitherehi therebye"
             * perl5        "hi", "hi there"            "hihitherehi therebye"
             * js1.2        "hi", "there"               "hihitheretherebye"
             *
             * Insofar as JS1.2 always defined $` as "left context from the last
             * match" for global regexps, it was more consistent than perl4.
             */
        res.leftContext.index = start;
        res.leftContext.length = gData.skipped;
    } else {
        /*
             * For JS1.3 and ECMAv2, emulate Perl5 exactly:
             *
             * js1.3        "hi", "hi there"            "hihitherehi therebye"
             */
        res.leftContext.index = 0;
        res.leftContext.length = start + gData.skipped;
    }
    res.rightContext.str = str;
    res.rightContext.index = ep;
    res.rightContext.length = end - ep;
    return result;
}
Also used : IdFunctionObject(org.mozilla.javascript.IdFunctionObject) IdScriptableObject(org.mozilla.javascript.IdScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 63 with Scriptable

use of org.mozilla.javascript.Scriptable 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 64 with Scriptable

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

the class FunctionTest method assertEvaluates.

private void assertEvaluates(final Object expected, final String source) {
    final ContextAction action = new ContextAction() {

        public Object run(Context cx) {
            final Scriptable scope = cx.initStandardObjects();
            final Object rep = cx.evaluateString(scope, source, "test.js", 0, null);
            assertEquals(expected, rep);
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 65 with Scriptable

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

the class GeneratedMethodNameTest method doTest.

public void doTest(final String scriptCode) throws Exception {
    final Context cx = ContextFactory.getGlobal().enterContext();
    try {
        Scriptable topScope = cx.initStandardObjects();
        topScope.put("javaNameGetter", topScope, new JavaNameGetter());
        Script script = cx.compileString(scriptCode, "myScript", 1, null);
        script.exec(cx, topScope);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

Scriptable (org.mozilla.javascript.Scriptable)106 Context (org.mozilla.javascript.Context)49 ScriptableObject (org.mozilla.javascript.ScriptableObject)30 ContextAction (org.mozilla.javascript.ContextAction)12 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 NativeObject (org.mozilla.javascript.NativeObject)7 Function (org.mozilla.javascript.Function)6 Map (java.util.Map)5 Notifier (org.apache.cxf.javascript.JavascriptTestUtilities.Notifier)5 Test (org.junit.Test)5 InputStreamReader (java.io.InputStreamReader)4 RhinoException (org.mozilla.javascript.RhinoException)4 Wrapper (org.mozilla.javascript.Wrapper)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IdentityHashMap (java.util.IdentityHashMap)3 ScriptContext (javax.script.ScriptContext)3 Callable (org.mozilla.javascript.Callable)3 ContextFactory (org.mozilla.javascript.ContextFactory)3 NativeJavaClass (org.mozilla.javascript.NativeJavaClass)3