本文整理了Java中com.eclipsesource.v8.V8Array
类的一些代码示例,展示了V8Array
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。V8Array
类的具体详情如下:
包路径:com.eclipsesource.v8.V8Array
类名称:V8Array
[英]A V8Value that represents a JavaScript array. JavaScript Arrays contain elements by index, but can also contain elements by 'key' which is why V8Array extends V8Object.
[中]表示JavaScript数组的v8值。JavaScript数组按索引包含元素,但也可以按“键”包含元素,这就是V8Array扩展V8Object的原因。
代码示例来源:origin: eclipsesource/J2V8
private Object getArrayItem(final V8Array array, final int index) {
try {
int type = array.getType(index);
switch (type) {
case INTEGER:
return array.getInteger(index);
case DOUBLE:
return array.getDouble(index);
case BOOLEAN:
return array.getBoolean(index);
case STRING:
return array.getString(index);
case V8_ARRAY:
case V8_TYPED_ARRAY:
return array.getArray(index);
case V8_OBJECT:
return array.getObject(index);
case V8_FUNCTION:
return array.getObject(index);
case V8_ARRAY_BUFFER:
return array.get(index);
case UNDEFINED:
return V8.getUndefined();
}
} catch (V8ResultUndefined e) {
// do nothing
}
return null;
}
代码示例来源:origin: eclipsesource/J2V8
@Override
public Object invoke(final V8Object receiver, final V8Array parameters) {
V8Array requireParams = new V8Array(v8);
try {
requireParams.push(file.getAbsolutePath());
return require.call(null, requireParams);
} finally {
requireParams.close();
}
}
});
代码示例来源:origin: eclipsesource/J2V8
/**
* Returns all the property names for the given object.
*
* @param kind Indicate whether named, indexed or both kinds of properties are requested.
* @param limit Limit the number of properties returned to the specified value
* @return All the property names for a given object
*/
public String[] getPropertyNames(final PropertyKind kind, final int limit) {
V8Array parameters = new V8Array(v8Object.getRuntime());
parameters.push(kind.index);
parameters.push(limit);
V8Array propertyNames = null;
try {
propertyNames = v8Object.executeArrayFunction(PROPERTY_NAMES, parameters);
String[] result = new String[propertyNames.length()];
for (int i = 0; i < result.length; i++) {
result[i] = propertyNames.getString(i);
}
return result;
} finally {
parameters.close();
if (propertyNames != null) {
propertyNames.close();
}
}
}
代码示例来源:origin: eclipsesource/J2V8
/**
* Creates a Java array from a V8Array. The type of the Array must be specified.
* Currently, only INTEGER, DOUBLE, BOOLEAN and STRING are supported.
* The V8Array must only contain elements of type 'arrayType'.
*
* This method will use J2V8's bulk array copy making it faster than iterating over
* all the elements in the array.
*
* @param array The V8Array to convert to a Java Array.
* @param arrayType The type of the V8Array to convert.
*
* @return A Java array representing a V8Array.
*/
public static Object getTypedArray(final V8Array array, final int arrayType) {
int length = array.length();
if (arrayType == V8Value.INTEGER) {
return array.getIntegers(0, length);
} else if (arrayType == V8Value.DOUBLE) {
return array.getDoubles(0, length);
} else if (arrayType == V8Value.BOOLEAN) {
return array.getBooleans(0, length);
} else if (arrayType == V8Value.STRING) {
return array.getStrings(0, length);
}
throw new RuntimeException("Unsupported bulk load type: " + arrayType);
}
代码示例来源:origin: eclipsesource/J2V8
@Test
public void testAddBoolean() {
V8Array array = new V8Array(v8);
array.push(true);
array.push(false);
assertEquals(2, array.length());
assertTrue(array.getBoolean(0));
assertFalse(array.getBoolean(1));
array.close();
}
代码示例来源:origin: eclipsesource/J2V8
@Test
public void testGenericPushString() {
V8Array array = new V8Array(v8);
array.push((Object) "foo");
assertEquals("foo", array.get(0));
array.close();
}
代码示例来源:origin: eclipsesource/J2V8
@Test
public void testGetArrayElementFromProperties() {
V8Array v8Array = new V8Array(v8);
v8Array.push("1").push(2).push(3.3);
String result1 = v8Array.getString("0");
int result2 = v8Array.getInteger("1");
double result3 = v8Array.getDouble("2");
assertEquals("1", result1);
assertEquals(2, result2);
assertEquals(3.3, result3, 0.000001);
v8Array.close();
}
代码示例来源:origin: eclipsesource/J2V8
@Test
public void testAddNull() {
V8Array v8Array = new V8Array(v8);
v8Array.pushNull();
assertEquals(1, v8Array.length());
assertEquals(NULL, v8Array.getType(0));
assertNull(v8Array.getObject(0));
v8Array.close();
}
代码示例来源:origin: eclipsesource/J2V8
@Test
public void testAddNullAsObject() {
V8Array array = new V8Array(v8);
array.push((V8Object) null);
assertNull(array.getObject(0));
array.close();
}
代码示例来源:origin: eclipsesource/J2V8
@Test
public void testArrayFunctionCall() {
v8.executeVoidScript("function add(a,b,c,d) {return [a,b,c,d];}");
V8Array parameters = new V8Array(v8);
parameters.push(true);
parameters.push(false);
parameters.push(7);
parameters.push("foo");
V8Array result = v8.executeArrayFunction("add", parameters);
assertTrue(result.getBoolean(0));
assertFalse(result.getBoolean(1));
assertEquals(7, result.getInteger(2));
assertEquals("foo", result.getString(3));
parameters.close();
result.close();
}
代码示例来源:origin: eclipsesource/J2V8
/**** Mixed Array ****/
@Test
public void testMixedArray() {
V8Array array = v8.executeArrayScript("['a', 3, 3.1, true];");
assertEquals(4, array.length());
assertEquals("a", array.getString(0));
assertEquals(3, array.getInteger(1));
assertEquals(3.1, array.getDouble(2), 0.00001);
assertTrue(array.getBoolean(3));
array.close();
}
代码示例来源:origin: eclipsesource/J2V8
@SuppressWarnings("unchecked")
private static List<? super Object> toList(final V8Array array, final V8Map<Object> cache, final TypeAdapter adapter) {
if (array == null) {
return Collections.emptyList();
}
if (cache.containsKey(array)) {
return (List<? super Object>) cache.get(array);
}
List<? super Object> result = new ArrayList<Object>();
cache.put(array, result);
for (int i = 0; i < array.length(); i++) {
Object object = null;
int type = V8Value.UNDEFINED;
try {
object = array.get(i);
type = array.getType(i);
Object value = getValue(object, type, cache, adapter);
if (value != IGNORE) {
result.add(value);
}
} finally {
if (object instanceof Releasable) {
((Releasable) object).release();
}
}
}
return result;
}
代码示例来源:origin: eclipsesource/J2V8
/*** Get Array ***/
@Test
public void testArrayGetArray() {
V8Array array = v8.executeArrayScript("[[1,2,3],['first','second'],[true]];");
V8Array array1 = array.getArray(0);
V8Array array2 = array.getArray(1);
V8Array array3 = array.getArray(2);
assertEquals(3, array1.length());
assertEquals(2, array2.length());
assertEquals(1, array3.length());
array.close();
array1.close();
array2.close();
array3.close();
}
代码示例来源:origin: jooby-project/jooby
private static JavaVoidCallback console(V8 v8, final Consumer<String> log) {
return (self, args) -> {
StringBuilder buff = new StringBuilder();
for (int i = 0; i < args.length(); i++) {
buff.append(args.get(i));
}
log.accept(buff.toString());
};
}
代码示例来源:origin: eclipsesource/J2V8
@Test
public void testAddNullAsArray() {
V8Array array = new V8Array(v8);
array.push((V8Array) null);
assertNull(array.getArray(0));
array.close();
}
代码示例来源:origin: jooby-project/jooby
private static void b64(final V8 v8) {
v8.registerJavaMethod((JavaCallback) (receiver, args) -> {
byte[] bytes = args.get(0).toString().getBytes(StandardCharsets.UTF_8);
return BaseEncoding.base64().encode(bytes);
}, "btoa");
v8.registerJavaMethod((JavaCallback) (receiver, args) -> {
byte[] atob = BaseEncoding.base64().decode(args.get(0).toString());
return new String(atob, StandardCharsets.UTF_8);
}, "atob");
}
代码示例来源:origin: eclipsesource/J2V8
/**
* Get all the BreakPoint IDs as an array.
*
* @return A list of BreakPoint IDs.
*/
public int[] getScriptBreakPointIDs() {
V8Array breakPoints = debugObject.executeArrayFunction(SCRIPT_BREAK_POINTS, null);
try {
int[] result = new int[breakPoints.length()];
for (int i = 0; i < breakPoints.length(); i++) {
V8Object breakPoint = breakPoints.getObject(i);
try {
result[i] = breakPoint.executeIntegerFunction(NUMBER, null);
} finally {
breakPoint.close();
}
}
return result;
} finally {
breakPoints.close();
}
}
代码示例来源:origin: eclipsesource/J2V8
@Test
public void testPushList() {
V8Array array = new V8Array(v8);
List<String> list = new ArrayList<String>();
list.add("one");
V8ObjectUtils.pushValue(v8, array, list);
V8Array result = array.getArray(0);
assertEquals("one", result.getString(0));
result.close();
array.close();
}
代码示例来源:origin: eclipsesource/J2V8
int type = V8Value.UNDEFINED;
try {
object = array.get(index);
type = array.getType(index);
Object result = getValue(object, type, cache, DEFAULT_TYPE_ADAPTER);
if ((result == object) && (result instanceof V8Value)) {
代码示例来源:origin: eclipsesource/J2V8
@Test(expected = V8ResultUndefined.class)
public void testGetBooleansWithoutBooleans() {
V8Array a = v8.executeArrayScript("[true, 'a', false, false, true];");
try {
a.getBooleans(0, 5);
} finally {
a.close();
}
}
内容来源于网络,如有侵权,请联系作者删除!