Skip to content

Commit 10756d3

Browse files
committed
实现getKeys
1 parent fb10ff5 commit 10756d3

File tree

6 files changed

+168
-11
lines changed

6 files changed

+168
-11
lines changed

.idea/codeStyles/Project.xml

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/main/java/com/quickjs/android/example/MainActivity.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.quickjs.android.JSObject;
1414
import com.quickjs.android.QuickJS;
1515

16+
import java.util.Arrays;
17+
1618
public class MainActivity extends AppCompatActivity {
1719

1820
@Override
@@ -37,11 +39,14 @@ protected void onCreate(Bundle savedInstanceState) {
3739
void test() {
3840
QuickJS quickJS = QuickJS.createRuntime();
3941
JSContext jsContext = quickJS.createContext();
40-
jsContext.executeScript("function sayHello(param) {return param[0];}", "file.js");
41-
JSArray jsArray = new JSArray(jsContext);
42-
jsArray.push("Hello");
43-
String result = jsContext.executeStringFunction("sayHello", jsArray);
44-
Log.e("QuickJS", result);
42+
jsContext.set("name", "Wiki");
43+
jsContext.set("age", 18);
44+
jsContext.set("gender", 1);
45+
// jsContext.executeScript("function sayHello(param) {return param[0];}", "file.js");
46+
// JSArray jsArray = new JSArray(jsContext);
47+
// jsArray.push("Hello");
48+
// String result = jsContext.executeStringFunction("sayHello", jsArray);
49+
Log.e("QuickJS", Arrays.toString(jsContext.getKeys()));
4550
}
4651

4752
void testV8() {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,29 @@ Java_com_quickjs_android_QuickJS__1arrayAdd__JJLjava_lang_String_2(JNIEnv *env,
364364
const char *value_str = env->GetStringUTFChars(value, NULL);
365365
JSValue value_ = JS_NewString(ctx, value_str);
366366
JS_SetPropertyUint32(ctx, this_obj, getArrayLength(ctx, this_obj), value_);
367+
}extern "C"
368+
JNIEXPORT jboolean JNICALL
369+
Java_com_quickjs_android_QuickJS__1contains(JNIEnv *env, jclass clazz, jlong context_ptr,
370+
jlong object_handle, jstring key) {
371+
JSContext *ctx = reinterpret_cast<JSContext *>(context_ptr);
372+
JSValue this_obj = object_handle;
373+
const char *key_ = env->GetStringUTFChars(key, NULL);
374+
JSValue jsValue = JS_GetPropertyStr(ctx, this_obj, key_);
375+
return JS_IsNull(jsValue);
376+
}extern "C"
377+
JNIEXPORT jobjectArray JNICALL
378+
Java_com_quickjs_android_QuickJS__1getKeys(JNIEnv *env, jclass clazz, jlong context_ptr,
379+
jlong object_handle) {
380+
JSContext *ctx = reinterpret_cast<JSContext *>(context_ptr);
381+
JSValue this_obj = object_handle;
382+
JSPropertyEnum *tab;
383+
uint32_t len;
384+
JS_GetOwnPropertyNames(ctx, &tab, &len, this_obj, JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY);
385+
jclass strClass = env->FindClass("java/lang/String");
386+
jobjectArray stringArray = env->NewObjectArray(len, strClass, NULL);
387+
for (int i = 0; i < len; ++i) {
388+
jstring key = env->NewStringUTF(JS_AtomToCString(ctx, tab[i].atom));
389+
env->SetObjectArrayElement(stringArray, i, key);
390+
}
391+
return stringArray;
367392
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,10 @@ public Object executeJSFunction(String name, Object... parameters) {
134134
}
135135

136136
public boolean contains(String key) {
137-
// TODO
138-
return false;
137+
return QuickJS._contains(getContextPtr(), this.objectHandle, key);
139138
}
140139

141140
public String[] getKeys() {
142-
// TODO
143-
return null;
141+
return QuickJS._getKeys(getContextPtr(), this.objectHandle);
144142
}
145-
146-
147143
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public static QuickJS createRuntime() {
1111
return new QuickJS(_createRuntime());
1212
}
1313

14+
native static boolean _contains(long contextPtr, long objectHandle, String key);
15+
16+
native static String[] _getKeys(long contextPtr, long objectHandle);
17+
1418

1519
public JSContext createContext() {
1620
return new JSContext(_createContext(runtimePtr));

0 commit comments

Comments
 (0)