From 3d1c61db341ceb3fecd74c8bb7e0c05203855094 Mon Sep 17 00:00:00 2001 From: Ian Bull Date: Mon, 8 Sep 2014 12:06:55 -0700 Subject: [PATCH] Make V8Arrays extends V8Objecst In JS and in V8, Arrays are Objects. To make this usable in J2V8, the V8Array class now extends the V8Object class. Furthermore, Objects and Arrays are no longer stored separately in the native side registry. Instead they are kept in the same registry. Arrays elements can now be accessed using the get(key) method. This fixes #9. --- jni/com_eclipsesource_v8_V8Impl.cpp | 69 +++++++++---------- .../java/com/eclipsesource/v8/V8Array.java | 30 ++------ .../java/com/eclipsesource/v8/V8Object.java | 28 +++++--- .../eclipsesource/v8/tests/V8ArrayTest.java | 37 ++++++++++ 4 files changed, 93 insertions(+), 71 deletions(-) diff --git a/jni/com_eclipsesource_v8_V8Impl.cpp b/jni/com_eclipsesource_v8_V8Impl.cpp index 873ab6c16..185726517 100644 --- a/jni/com_eclipsesource_v8_V8Impl.cpp +++ b/jni/com_eclipsesource_v8_V8Impl.cpp @@ -15,7 +15,6 @@ class V8Runtime { Persistent globalObjectTemplate; Persistent context_; std::map * > objects; - std::map * > arrays; JNIEnv* env; jobject v8; }; @@ -137,8 +136,8 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1initNewV8Array v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); Local array = Array::New(isolate); - v8Isolates[v8RuntimeHandle]->arrays[arrayHandle] = new Persistent; - v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]->Reset(v8Isolates[v8RuntimeHandle]->isolate, array); + v8Isolates[v8RuntimeHandle]->objects[arrayHandle] = new Persistent; + v8Isolates[v8RuntimeHandle]->objects[arrayHandle]->Reset(v8Isolates[v8RuntimeHandle]->isolate, array); } JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1getObject @@ -182,7 +181,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1getArray throwResultUndefinedException(env, ""); return; } - v8Isolates[v8RuntimeHandle]->arrays[resultHandle]->Reset(v8Isolates[v8RuntimeHandle]->isolate, v8Value->ToObject()); + v8Isolates[v8RuntimeHandle]->objects[resultHandle]->Reset(v8Isolates[v8RuntimeHandle]->isolate, v8Value->ToObject()); env->ReleaseStringUTFChars(objectKey, utf_string); } @@ -205,9 +204,9 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1releaseArray } Isolate* isolate = getIsolate(env, v8RuntimeHandle); HandleScope handle_scope(isolate); - v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]->Reset(); - delete(v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); - v8Isolates[v8RuntimeHandle]->arrays.erase(arrayHandle); + v8Isolates[v8RuntimeHandle]->objects[arrayHandle]->Reset(); + delete(v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); + v8Isolates[v8RuntimeHandle]->objects.erase(arrayHandle); } JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1releaseRuntime @@ -456,7 +455,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1executeArrayScript throwResultUndefinedException(env, ""); return; } - v8Isolates[v8RuntimeHandle]->arrays[resultHandle]->Reset(v8Isolates[v8RuntimeHandle]->isolate, result->ToObject()); + v8Isolates[v8RuntimeHandle]->objects[resultHandle]->Reset(v8Isolates[v8RuntimeHandle]->isolate, result->ToObject()); env->ReleaseStringUTFChars(jjstring, js); return; } @@ -477,7 +476,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1executeArrayFunction int size = 0; Handle* args = NULL; if ( parameterHandle >= 0 ) { - Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[parameterHandle]); + Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[parameterHandle]); size = Array::Cast(*parameters)->Length(); args = new Handle [size]; for (int i = 0; i < size; i++) { @@ -493,7 +492,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1executeArrayFunction return; } - v8Isolates[v8RuntimeHandle]->arrays[resultHandle]->Reset(v8Isolates[v8RuntimeHandle]->isolate, result->ToObject()); + v8Isolates[v8RuntimeHandle]->objects[resultHandle]->Reset(v8Isolates[v8RuntimeHandle]->isolate, result->ToObject()); env->ReleaseStringUTFChars(jfunctionName, functionName); return; } @@ -514,7 +513,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1executeObjectFunction int size = 0; Handle* args = NULL; if ( parameterHandle >= 0 ) { - Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[parameterHandle]); + Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[parameterHandle]); size = Array::Cast(*parameters)->Length(); args = new Handle [size]; for (int i = 0; i < size; i++) { @@ -550,7 +549,7 @@ JNIEXPORT jint JNICALL Java_com_eclipsesource_v8_V8__1executeIntFunction int size = 0; Handle* args = NULL; if ( parameterHandle >= 0 ) { - Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[parameterHandle]); + Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[parameterHandle]); size = Array::Cast(*parameters)->Length(); args = new Handle [size]; for (int i = 0; i < size; i++) { @@ -584,7 +583,7 @@ JNIEXPORT jdouble JNICALL Java_com_eclipsesource_v8_V8__1executeDoubleFunction int size = 0; Handle* args = NULL; if ( parameterHandle >= 0 ) { - Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[parameterHandle]); + Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[parameterHandle]); size = Array::Cast(*parameters)->Length(); args = new Handle [size]; for (int i = 0; i < size; i++) { @@ -619,7 +618,7 @@ JNIEXPORT jboolean JNICALL Java_com_eclipsesource_v8_V8__1executeBooleanFunction int size = 0; Handle* args = NULL; if ( parameterHandle >= 0 ) { - Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[parameterHandle]); + Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[parameterHandle]); size = Array::Cast(*parameters)->Length(); args = new Handle [size]; for (int i = 0; i < size; i++) { @@ -654,7 +653,7 @@ JNIEXPORT jstring JNICALL Java_com_eclipsesource_v8_V8__1executeStringFunction int size = 0; Handle* args = NULL; if ( parameterHandle >= 0 ) { - Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[parameterHandle]); + Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[parameterHandle]); size = Array::Cast(*parameters)->Length(); args = new Handle [size]; for (int i = 0; i < size; i++) { @@ -689,7 +688,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1executeVoidFunction int size = 0; Handle* args = NULL; if ( parameterHandle >= 0 ) { - Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[parameterHandle]); + Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[parameterHandle]); size = Array::Cast(*parameters)->Length(); args = new Handle [size]; for (int i = 0; i < size; i++) { @@ -794,7 +793,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1addArray Handle global = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[objectHandle]); Local v8Key = String::NewFromUtf8(isolate, env -> GetStringUTFChars(key, NULL)); - Local v8Value = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[valueHandle]); + Local v8Value = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[valueHandle]); global->Set( v8Key, v8Value); } @@ -889,7 +888,7 @@ JNIEXPORT jint JNICALL Java_com_eclipsesource_v8_V8__1arrayGetSize HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); return Array::Cast(*array)->Length(); } @@ -902,7 +901,7 @@ JNIEXPORT jint JNICALL Java_com_eclipsesource_v8_V8__1arrayGetInteger HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); Handle v8Value = array->Get(index); if (v8Value.IsEmpty() || v8Value->IsUndefined() || !v8Value->IsInt32()) { @@ -921,7 +920,7 @@ JNIEXPORT jboolean JNICALL Java_com_eclipsesource_v8_V8__1arrayGetBoolean HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); Handle v8Value = array->Get(index); if (v8Value.IsEmpty() || v8Value->IsUndefined() || !v8Value->IsBoolean()) { @@ -940,7 +939,7 @@ JNIEXPORT jdouble JNICALL Java_com_eclipsesource_v8_V8__1arrayGetDouble HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); Handle v8Value = array->Get(index); if (v8Value.IsEmpty() || v8Value->IsUndefined() || !v8Value->IsNumber()) { @@ -959,7 +958,7 @@ JNIEXPORT jstring JNICALL Java_com_eclipsesource_v8_V8__1arrayGetString HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); Handle v8Value = array->Get(index); if (v8Value.IsEmpty() || v8Value->IsUndefined() || !v8Value->IsString()) { @@ -979,7 +978,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1arrayGetObject HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); Handle v8Value = array->Get(index); if (v8Value.IsEmpty() || v8Value->IsUndefined() || !v8Value->IsObject()) { @@ -999,14 +998,14 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1arrayGetArray HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); Handle v8Value = array->Get(index); if (v8Value.IsEmpty() || v8Value->IsUndefined() || !v8Value->IsArray()) { throwResultUndefinedException(env, ""); return; } - v8Isolates[v8RuntimeHandle]->arrays[resultHandle]->Reset(v8Isolates[v8RuntimeHandle]->isolate, v8Value->ToObject()); + v8Isolates[v8RuntimeHandle]->objects[resultHandle]->Reset(v8Isolates[v8RuntimeHandle]->isolate, v8Value->ToObject()); } JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1addArrayIntItem @@ -1018,7 +1017,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1addArrayIntItem HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); Local v8Value = v8::Int32::New(isolate, value); int index = Array::Cast(*array)->Length(); array->Set(index, v8Value); @@ -1033,7 +1032,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1addArrayDoubleItem HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); Local v8Value = v8::Number::New(isolate, value); int index = Array::Cast(*array)->Length(); array->Set(index, v8Value); @@ -1048,7 +1047,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1addArrayBooleanItem HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); Local v8Value = v8::Boolean::New(isolate, value); int index = Array::Cast(*array)->Length(); array->Set(index, v8Value); @@ -1063,7 +1062,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1addArrayStringItem HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); int index = Array::Cast(*array)->Length(); const char* utfString = env -> GetStringUTFChars(value, NULL); Local v8Value = String::NewFromUtf8(isolate, utfString); @@ -1081,9 +1080,9 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1addArrayArrayItem v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); int index = Array::Cast(*array)->Length(); - Local v8Value = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[valueHandle]); + Local v8Value = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[valueHandle]); array->Set(index, v8Value); } @@ -1097,7 +1096,7 @@ JNIEXPORT void JNICALL Java_com_eclipsesource_v8_V8__1addArrayObjectItem v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[arrayHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[arrayHandle]); int index = Array::Cast(*array)->Length(); Local v8Value = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[valueHandle]); array->Set(index, v8Value); @@ -1147,7 +1146,7 @@ JNIEXPORT jint JNICALL Java_com_eclipsesource_v8_V8__1getType__III HandleScope handle_scope(isolate); v8::Local context = v8::Local::New(isolate,v8Isolates[v8RuntimeHandle]->context_); Context::Scope context_scope(context); - Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[objectHandle]); + Handle array = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[objectHandle]); Handle v8Value = array->Get(index); if (v8Value.IsEmpty() || v8Value->IsUndefined()) { @@ -1214,7 +1213,7 @@ jobject createParameterArray(JNIEnv* env, int v8RuntimeHandle, jobject v8, int s jobject result = env->NewObject(cls, methodID, v8); jint parameterHandle = env->CallIntMethod(result, getHandle); - Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->arrays[parameterHandle]); + Handle parameters = Local::New(isolate, *v8Isolates[v8RuntimeHandle]->objects[parameterHandle]); for ( int i = 0; i < size; i++) { parameters->Set(i, args[i]); @@ -1446,7 +1445,7 @@ void v8ArrayCallback(const v8::FunctionCallbackInfo& args) { args.GetReturnValue().SetUndefined(); } else { int resultHandle = getArrayHandle(env, resultObject); - Handle result = Local::New(isolate, *v8Isolates[md->v8RuntimeHandle]->arrays[resultHandle]); + Handle result = Local::New(isolate, *v8Isolates[md->v8RuntimeHandle]->objects[resultHandle]); releaseArray(env, resultObject); args.GetReturnValue().Set(result); } diff --git a/src/main/java/com/eclipsesource/v8/V8Array.java b/src/main/java/com/eclipsesource/v8/V8Array.java index 48ef6d6cf..a0d4bc4b0 100644 --- a/src/main/java/com/eclipsesource/v8/V8Array.java +++ b/src/main/java/com/eclipsesource/v8/V8Array.java @@ -1,27 +1,15 @@ package com.eclipsesource.v8; -public class V8Array { - - private static int v8ArrayInstanceCounter = 1; - - private V8 v8; - private int arrayHandle; - private boolean released = false; +public class V8Array extends V8Object { public V8Array(final V8 v8) { - this.v8 = v8; + super(v8); V8.checkThread(); - arrayHandle = v8ArrayInstanceCounter++; - this.v8._initNewV8Array(v8.getV8RuntimeHandle(), arrayHandle); - this.v8.addObjRef(); - released = false; } - public void release() { - V8.checkThread(); - released = true; - v8._releaseArray(v8.getV8RuntimeHandle(), arrayHandle); - v8.releaseObjRef(); + @Override + protected void initialize(final int runtimeHandle, final int objectHandle) { + v8._initNewV8Array(runtimeHandle, objectHandle); } public int getSize() { @@ -108,12 +96,4 @@ public void add(final V8Array value) { v8._addArrayArrayItem(v8.getV8RuntimeHandle(), getHandle(), value.getHandle()); } - public int getHandle() { - return arrayHandle; - } - - public boolean isReleased() { - return released; - } - } \ No newline at end of file diff --git a/src/main/java/com/eclipsesource/v8/V8Object.java b/src/main/java/com/eclipsesource/v8/V8Object.java index 9a0427fe5..81ec0cae4 100644 --- a/src/main/java/com/eclipsesource/v8/V8Object.java +++ b/src/main/java/com/eclipsesource/v8/V8Object.java @@ -1,4 +1,3 @@ - package com.eclipsesource.v8; import java.lang.reflect.Method; @@ -13,10 +12,10 @@ public class V8Object { public static final int V8_ARRAY = 5; public static final int V8_OBJECT = 6; - private static int v8ObjectInstanceCounter = 1; + private static int v8ObjectInstanceCounter = 1; - private V8 v8; - private int objectHandle; + protected V8 v8; + private int objectHandle; private boolean released; protected V8Object() { @@ -29,11 +28,15 @@ public V8Object(final V8 v8) { this.v8 = v8; V8.checkThread(); objectHandle = v8ObjectInstanceCounter++; - this.v8._initNewV8Object(v8.getV8RuntimeHandle(), objectHandle); + initialize(v8.getV8RuntimeHandle(), objectHandle); this.v8.addObjRef(); released = false; } + protected void initialize(final int runtimeHandle, final int objectHandle) { + v8._initNewV8Object(runtimeHandle, objectHandle); + } + public int getHandle() { return objectHandle; } @@ -113,19 +116,22 @@ public V8Array createParameterList(final int size) { return null; } - public int executeIntFunction(final String name, final V8Array parameters) throws V8ExecutionException, V8ResultUndefined { + public int executeIntFunction(final String name, final V8Array parameters) throws V8ExecutionException, + V8ResultUndefined { V8.checkThread(); int parametersHandle = parameters == null ? -1 : parameters.getHandle(); return v8._executeIntFunction(v8.getV8RuntimeHandle(), getHandle(), name, parametersHandle); } - public double executeDoubleFunction(final String name, final V8Array parameters) throws V8ExecutionException, V8ResultUndefined { + public double executeDoubleFunction(final String name, final V8Array parameters) throws V8ExecutionException, + V8ResultUndefined { V8.checkThread(); int parametersHandle = parameters == null ? -1 : parameters.getHandle(); return v8._executeDoubleFunction(v8.getV8RuntimeHandle(), getHandle(), name, parametersHandle); } - public String executeStringFunction(final String name, final V8Array parameters) throws V8ExecutionException, V8ResultUndefined { + public String executeStringFunction(final String name, final V8Array parameters) throws V8ExecutionException, + V8ResultUndefined { V8.checkThread(); int parametersHandle = parameters == null ? -1 : parameters.getHandle(); return v8._executeStringFunction(v8.getV8RuntimeHandle(), getHandle(), name, parametersHandle); @@ -138,13 +144,13 @@ public boolean executeBooleanFunction(final String name, final V8Array parameter return v8._executeBooleanFunction(v8.getV8RuntimeHandle(), getHandle(), name, parametersHandle); } - public V8Array executeArrayFunction(final String name, final V8Array parameters) throws V8ExecutionException, V8ResultUndefined { + public V8Array executeArrayFunction(final String name, final V8Array parameters) throws V8ExecutionException, + V8ResultUndefined { V8.checkThread(); V8Array result = new V8Array(v8); try { int parametersHandle = parameters == null ? -1 : parameters.getHandle(); - v8._executeArrayFunction(v8.getV8RuntimeHandle(), objectHandle, name, parametersHandle, - result.getHandle()); + v8._executeArrayFunction(v8.getV8RuntimeHandle(), objectHandle, name, parametersHandle, result.getHandle()); } catch (Exception e) { result.release(); throw e; diff --git a/src/test/java/com/eclipsesource/v8/tests/V8ArrayTest.java b/src/test/java/com/eclipsesource/v8/tests/V8ArrayTest.java index c93cfd5e5..415a333e3 100644 --- a/src/test/java/com/eclipsesource/v8/tests/V8ArrayTest.java +++ b/src/test/java/com/eclipsesource/v8/tests/V8ArrayTest.java @@ -34,6 +34,43 @@ public void tearDown() { } } + @Test + public void testGetArrayElementFromProperties() { + V8Array v8Array = new V8Array(v8); + v8Array.add("1"); + v8Array.add(2); + v8Array.add(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.release(); + } + + @Test + public void testSetArrayElementsWithProperties() { + V8Array v8Array = new V8Array(v8); + v8Array.add("1"); + v8Array.add(2); + v8Array.add(3.3); + + v8Array.add("0", 1); + v8Array.add("10", 2); + v8Array.add("19", 3); + v8Array.add("bob", 4); + + assertEquals(20, v8Array.getSize()); + assertEquals(1, v8Array.getInteger(0)); + assertEquals(2, v8Array.getInteger(10)); + assertEquals(3, v8Array.getInteger(19)); + assertEquals(4, v8Array.getInteger("bob")); + v8Array.release(); + } + @Test public void testCreateAndReleaseArray() { for (int i = 0; i < 10000; i++) {