Skip to content

Commit 97477f6

Browse files
author
Wiki
committed
JSValueTest
1 parent abf6986 commit 97477f6

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,16 @@ public void console() {
224224
context.set("console", console);
225225
context.executeVoidScript("console.log('Hello')", "file.js");
226226
}
227+
228+
@Test
229+
public void getType() {
230+
context.set("key1", "1");
231+
context.set("key2", 1);
232+
context.set("key3", true);
233+
context.set("key4", 1.1);
234+
assertEquals(JSValue.TYPE_STRING, context.getType("key1"));
235+
assertEquals(JSValue.TYPE_INTEGER, context.getType("key2"));
236+
assertEquals(JSValue.TYPE_BOOLEAN, context.getType("key3"));
237+
assertEquals(JSValue.TYPE_DOUBLE, context.getType("key4"));
238+
}
227239
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,16 @@ Java_com_quickjs_android_QuickJS__1get(JNIEnv *env, jclass clazz, jlong context_
310310
JSValue result = JS_GetPropertyStr(ctx, this_obj, key_);
311311
return To_JObject(env, context_ptr, expected_type, result);
312312
}
313-
313+
extern "C"
314+
JNIEXPORT jobject JNICALL
315+
Java_com_quickjs_android_QuickJS__1getValue(JNIEnv *env, jclass clazz, jlong context_ptr,
316+
jobject object_handle, jstring key) {
317+
const char *key_ = env->GetStringUTFChars(key, nullptr);
318+
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
319+
JSValue this_obj = TO_JS_VALUE(env, object_handle);
320+
JSValue result = JS_GetPropertyStr(ctx, this_obj, key_);
321+
return TO_JAVA_OBJECT(env, ctx, result);
322+
}
314323

315324

316325

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ public JSObject getObject(String key) {
101101

102102

103103
public int getType(String key) {
104-
// TODO
105-
// long ptr = QuickJS._getObject(this.getContextPtr(), this, key);
106-
return QuickJS._getObjectType(this.getContextPtr(), this);
104+
JSValue value = QuickJS._getValue(this.getContextPtr(), this, key);
105+
if (value == null) {
106+
return JSValue.TYPE_NULL;
107+
}
108+
return value.getJSType();
107109
}
108110

109111

@@ -190,4 +192,36 @@ public String[] getKeys() {
190192
Object executeFunction(int expectedType, String name, JSArray parameters) {
191193
return QuickJS._executeFunction(context.getContextPtr(), expectedType, this, name, parameters);
192194
}
195+
196+
static class Undefined extends JSObject {
197+
198+
Undefined(JSContext context, long tag, int u_int32, double u_float64, long u_ptr) {
199+
super(context, tag, u_int32, u_float64, u_ptr);
200+
}
201+
202+
@Override
203+
JSObject setObject(String key, Object value) {
204+
throw new UnsupportedOperationException();
205+
}
206+
207+
@Override
208+
Object get(int expectedType, String key) {
209+
throw new UnsupportedOperationException();
210+
}
211+
212+
@Override
213+
public JSObject registerJavaMethod(JavaCallback callback, String jsFunctionName) {
214+
throw new UnsupportedOperationException();
215+
}
216+
217+
@Override
218+
public JSObject registerJavaMethod(JavaVoidCallback callback, String jsFunctionName) {
219+
throw new UnsupportedOperationException();
220+
}
221+
222+
@Override
223+
Object executeFunction(int expectedType, String name, JSArray parameters) {
224+
throw new UnsupportedOperationException();
225+
}
226+
}
193227
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static JSValue createJSValue(long contextPtr, int type, long tag, int u_int32, d
7575
return new JSArray(context, tag, u_int32, u_float64, u_ptr);
7676
case JSValue.TYPE_JS_OBJECT:
7777
case JSValue.TYPE_UNDEFINED:
78-
return new JSObject(context, tag, u_int32, u_float64, u_ptr);
78+
return new JSObject.Undefined(context, tag, u_int32, u_float64, u_ptr);
7979
default:
8080
return new JSValue(context, tag, u_int32, u_float64, u_ptr);
8181
}
@@ -147,6 +147,8 @@ static Object executeJSFunction(JSContext context, JSValue objectHandle, String
147147

148148
native static JSValue _Undefined(long contextPtr);
149149

150+
native static JSValue _getValue(long contextPtr, JSObject jsObject, String key);
151+
150152

151153
static {
152154
System.loadLibrary("quickjs");

0 commit comments

Comments
 (0)