Skip to content

Commit 641b0ec

Browse files
author
Wiki
committed
JSArrayTest
1 parent 97477f6 commit 641b0ec

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

quickjs-android/src/androidTest/java/com/quickjs/android/JSArrayTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,18 @@ public void getArray() {
9393
assertEquals("Wiki", array.getArray(0).getString(0));
9494
user.close();
9595
}
96+
97+
@Test
98+
public void getType() {
99+
JSArray array = new JSArray(context);
100+
array.push("1");
101+
array.push(1);
102+
array.push(true);
103+
array.push(1.1);
104+
assertEquals(JSValue.TYPE_STRING, array.getType(0));
105+
assertEquals(JSValue.TYPE_INTEGER, array.getType(1));
106+
assertEquals(JSValue.TYPE_BOOLEAN, array.getType(2));
107+
assertEquals(JSValue.TYPE_DOUBLE, array.getType(3));
108+
array.close();
109+
}
96110
}

quickjs-android/src/main/cpp/quickjs-jni.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,16 @@ Java_com_quickjs_android_QuickJS__1arrayGet(JNIEnv *env, jclass clazz, jlong con
334334
return To_JObject(env, context_ptr, expected_type, result);
335335
}
336336

337+
extern "C"
338+
JNIEXPORT jobject JNICALL
339+
Java_com_quickjs_android_QuickJS__1arrayGetValue(JNIEnv *env, jclass clazz, jlong context_ptr,
340+
jobject object_handle, jint index) {
341+
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
342+
JSValue this_obj = TO_JS_VALUE(env, object_handle);
343+
JSValue result = JS_GetPropertyUint32(ctx, this_obj, index);
344+
return TO_JAVA_OBJECT(env, ctx, result);
345+
}
346+
337347
extern "C"
338348
JNIEXPORT jboolean JNICALL
339349
Java_com_quickjs_android_QuickJS__1contains(JNIEnv *env, jclass clazz, jlong context_ptr,

quickjs-android/src/main/java/com/quickjs/android/JSArray.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ public JSArray getArray(int index) {
6969
return null;
7070
}
7171

72+
public int getType(int index) {
73+
JSValue value = QuickJS._arrayGetValue(this.getContextPtr(), this, index);
74+
if (value == null) {
75+
return JSValue.TYPE_NULL;
76+
}
77+
return value.getJSType();
78+
}
79+
7280
public JSArray push(int value) {
7381
return pushObject(value);
7482
}

quickjs-android/src/main/java/com/quickjs/android/QuickJS.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ static Object executeJSFunction(JSContext context, JSValue objectHandle, String
149149

150150
native static JSValue _getValue(long contextPtr, JSObject jsObject, String key);
151151

152+
native static JSValue _arrayGetValue(long contextPtr, JSArray jsArray, int index);
153+
152154

153155
static {
154156
System.loadLibrary("quickjs");

0 commit comments

Comments
 (0)