Search in sources :

Example 6 with NativeArray

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

the class JsonParserTest method shouldParseHeterogeneousJsonArray.

@Test
@SuppressWarnings("unchecked")
public void shouldParseHeterogeneousJsonArray() throws Exception {
    NativeArray actual = (NativeArray) parser.parseValue("[ \"hello\" , 3, null, [false] ]");
    assertEquals("hello", actual.get(0, actual));
    assertEquals(3, actual.get(1, actual));
    assertEquals(null, actual.get(2, actual));
    NativeArray innerArr = (NativeArray) actual.get(3, actual);
    assertEquals(false, innerArr.get(0, innerArr));
    assertEquals(4, actual.getLength());
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) Test(org.junit.Test)

Example 7 with NativeArray

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

the class UtilTest method testJavaToJS_JSArrays.

@Test
public void testJavaToJS_JSArrays() {
    String[] ary = new String[] { "a", "b" };
    Object result = Util.javaToJS(ary, scope);
    assertThat(result, instanceOf(NativeArray.class));
    // Woohoo—it got the correct class.  Now, let's check it works OK.
    NativeArray nAry = (NativeArray) result;
    assertThat(nAry.getLength(), is(2L));
    assertThat(nAry.get(0, nAry), is((Object) "a"));
    assertThat(nAry.get(1, nAry), is((Object) "b"));
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) ScriptableObject(org.mozilla.javascript.ScriptableObject) Test(org.junit.Test)

Example 8 with NativeArray

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

the class MapAdapter method get.

public Object get(Object key) {
    Object value;
    if (key instanceof Integer)
        value = ScriptableObject.getProperty(object, ((Integer) key).intValue());
    else if (key instanceof String)
        value = ScriptableObject.getProperty(object, (String) key);
    else
        value = null;
    if (value instanceof Wrapper)
        value = ((Wrapper) value).unwrap();
    else if (value == ScriptableObject.NOT_FOUND)
        value = null;
    else if (value instanceof NativeArray) {
        // Convert to a normal array
        // TODO: see if we need to convert the other way in put?
        NativeArray array = (NativeArray) value;
        int length = (int) array.getLength();
        Object[] list = new Object[length];
        for (int i = 0; i < length; i++) {
            Object obj = array.get(i, array);
            if (obj instanceof Wrapper)
                obj = ((Wrapper) obj).unwrap();
            list[i] = obj;
        }
        return list;
    }
    return value;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) Wrapper(org.mozilla.javascript.Wrapper) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 9 with NativeArray

use of org.mozilla.javascript.NativeArray in project kotlin by JetBrains.

the class RhinoQUnitResultChecker method assertResultValid.

protected void assertResultValid(Object result) {
    if (result instanceof NativeArray) {
        NativeArray array = (NativeArray) result;
        StringBuilder buffer = new StringBuilder();
        for (Object value : array) {
            String text = value.toString();
            System.out.println(text);
            if (!text.startsWith("PASS")) {
                if (buffer.length() > 0) {
                    buffer.append("\n");
                }
                buffer.append(text);
            }
        }
        if (buffer.length() > 0) {
            fail(buffer.toString());
        }
    } else {
        fail("Unknown QUnit result: " + result);
    }
}
Also used : NativeArray(org.mozilla.javascript.NativeArray)

Example 10 with NativeArray

use of org.mozilla.javascript.NativeArray in project sling by apache.

the class ScriptableNode method get.

/**
     * Gets the value of a (Javascript) property or child node. If there is a single single-value
     * JCR property of this node, return its string value. If there are multiple properties
     * of the same name or child nodes of the same name, return an array.
     */
@Override
public Object get(String name, Scriptable start) {
    // builtin javascript properties (jsFunction_ etc.) have priority
    final Object fromSuperclass = super.get(name, start);
    if (fromSuperclass != Scriptable.NOT_FOUND) {
        return fromSuperclass;
    }
    if (node == null) {
        return Undefined.instance;
    }
    final List<Scriptable> items = new ArrayList<Scriptable>();
    // Add all matching nodes to result
    try {
        NodeIterator it = node.getNodes(name);
        while (it.hasNext()) {
            items.add(ScriptRuntime.toObject(this, it.nextNode()));
        }
    } catch (RepositoryException e) {
        log.debug("RepositoryException while collecting Node children", e);
    }
    // Add all matching properties to result
    boolean isMulti = false;
    try {
        PropertyIterator it = node.getProperties(name);
        while (it.hasNext()) {
            Property prop = it.nextProperty();
            if (prop.getDefinition().isMultiple()) {
                isMulti = true;
                Value[] values = prop.getValues();
                for (int i = 0; i < values.length; i++) {
                    items.add(wrap(values[i]));
                }
            } else {
                items.add(wrap(prop.getValue()));
            }
        }
    } catch (RepositoryException e) {
        log.debug("RepositoryException while collecting Node properties", e);
    }
    if (items.size() == 0) {
        return getNative(name, start);
    } else if (items.size() == 1 && !isMulti) {
        return items.iterator().next();
    } else {
        NativeArray result = new NativeArray(items.toArray());
        ScriptRuntime.setObjectProtoAndParent(result, this);
        return result;
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) NativeArray(org.mozilla.javascript.NativeArray) ArrayList(java.util.ArrayList) PropertyIterator(javax.jcr.PropertyIterator) RepositoryException(javax.jcr.RepositoryException) Scriptable(org.mozilla.javascript.Scriptable) Value(javax.jcr.Value) Property(javax.jcr.Property)

Aggregations

NativeArray (org.mozilla.javascript.NativeArray)10 ScriptableObject (org.mozilla.javascript.ScriptableObject)4 Test (org.junit.Test)3 Function (org.mozilla.javascript.Function)2 NativeObject (org.mozilla.javascript.NativeObject)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 NodeIterator (javax.jcr.NodeIterator)1 Property (javax.jcr.Property)1 PropertyIterator (javax.jcr.PropertyIterator)1 RepositoryException (javax.jcr.RepositoryException)1 Value (javax.jcr.Value)1 StreamHostObject (org.jaggeryjs.hostobjects.stream.StreamHostObject)1