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());
}
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"));
}
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;
}
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);
}
}
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;
}
}
Aggregations