Skip to content

Commit 73b8b03

Browse files
author
Wiki
committed
入参改成object
1 parent 0924302 commit 73b8b03

File tree

5 files changed

+149
-261
lines changed

5 files changed

+149
-261
lines changed

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

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,19 @@ public class MainActivity extends AppCompatActivity {
2222
@Override
2323
protected void onCreate(Bundle savedInstanceState) {
2424
super.onCreate(savedInstanceState);
25-
// QuickJS quickJS = QuickJS.createV8Runtime();
26-
// JSContext jsContext = quickJS.createContext();
27-
// int resultInt = jsContext.executeIntegerScript("var a = 2+10;\n a;", "file.js");
28-
// double resultDouble = jsContext.executeDoubleScript("var a = 2.0;\n a;", "file.js");
29-
// boolean resultBool = jsContext.executeBooleanScript("var a = 1 > 0;\n a;", "file.js");
30-
// String resultString = jsContext.executeStringScript("var a = 'Hello World';\n a;", "file.js");
31-
// Log.e("quickjs", String.valueOf(resultInt));
32-
// Log.e("quickjs", String.valueOf(resultDouble));
33-
// Log.e("quickjs", String.valueOf(resultBool));
34-
// Log.e("quickjs", String.valueOf(resultString));
35-
// jsContext.add("", 1);
36-
// jsContext.close();
37-
// quickJS.close();
38-
3925
test();
40-
// Integer integer = new Integer(3);
4126
}
4227

4328
void test() {
4429
QuickJS quickJS = QuickJS.createRuntime();
4530
JSContext jsContext = quickJS.createContext();
46-
// int result = jsContext.executeIntegerScript("int a=1228;\na;", "file.js");
47-
//
48-
jsContext.set("name", "Wiki");
49-
Log.e("QuickJS", String.valueOf(jsContext.getString("name")));
50-
// jsContext.set("age", 18);
51-
// jsContext.set("gender", 1);
52-
// jsContext.executeScript("function sayHello(param) {return param[3];}", "file.js");
53-
// JSArray jsArray = new JSArray(jsContext);
54-
// jsArray.length();
55-
// jsArray.push("Hello");
56-
// jsArray.push(true);
57-
// jsArray.push(1);
58-
// jsArray.push(3.14);
59-
// Log.e("QuickJS", String.valueOf(jsContext.executeDoubleFunction("sayHello", jsArray)));
31+
32+
JSObject object = new JSObject(jsContext);
33+
long start = System.currentTimeMillis();
34+
for (int i = 0; i < 10000; i++) {
35+
object.set(String.valueOf(i), i);
36+
}
37+
Log.e("test1", String.valueOf(System.currentTimeMillis() - start));
6038
}
6139

6240
void testV8() {

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

Lines changed: 23 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ jobject TO_JAVA_OBJECT(JNIEnv *env, JSContext *ctx, JSValue value) {
9494
createJSValueMethodID,
9595
(jlong) ctx,
9696
type,
97-
value,
97+
(jlong) value,
9898
0,
99-
0,
100-
0);
99+
0.0,
100+
(jlong) 0);
101101
#else
102102
return env->CallStaticObjectMethod(quickJSCls,
103103
createJSValueMethodID,
@@ -152,11 +152,10 @@ jobject To_JObject(JNIEnv *env, jlong context_ptr, int expected_type, JSValue re
152152
return env->NewObject(booleanCls, booleanInitMethodID, JS_VALUE_GET_BOOL(result));
153153
case TYPE_STRING:
154154
return env->NewStringUTF(JS_ToCString(ctx, result));
155-
// TODO
156-
// case TYPE_JS_ARRAY:
157-
// case TYPE_JS_OBJECT:
158-
// case TYPE_JS_FUNCTION:
159-
// return TO_JAVA_OBJECT(env, ctx, result);
155+
case TYPE_JS_ARRAY:
156+
case TYPE_JS_OBJECT:
157+
case TYPE_JS_FUNCTION:
158+
return TO_JAVA_OBJECT(env, ctx, result);
160159
}
161160
return nullptr;
162161
}
@@ -224,13 +223,13 @@ Java_com_quickjs_android_QuickJS__1createRuntime(JNIEnv *env, jclass clazz) {
224223
extern "C"
225224
JNIEXPORT jlong JNICALL
226225
Java_com_quickjs_android_QuickJS__1createContext(JNIEnv *env, jclass clazz, jlong runtime_ptr) {
227-
JSRuntime *runtime = reinterpret_cast<JSRuntime *>(runtime_ptr);
226+
auto *runtime = reinterpret_cast<JSRuntime *>(runtime_ptr);
228227
auto *ctx = JS_NewContext(runtime);
229228
return reinterpret_cast<jlong>(ctx);
230229
}extern "C"
231230
JNIEXPORT void JNICALL
232231
Java_com_quickjs_android_QuickJS__1releaseRuntime(JNIEnv *env, jclass clazz, jlong runtime_ptr) {
233-
JSRuntime *runtime = reinterpret_cast<JSRuntime *>(runtime_ptr);
232+
auto *runtime = reinterpret_cast<JSRuntime *>(runtime_ptr);
234233
JS_FreeRuntime(runtime);
235234
}extern "C"
236235
JNIEXPORT void JNICALL
@@ -284,157 +283,35 @@ Java_com_quickjs_android_QuickJS__1release(JNIEnv *env, jclass clazz, jlong cont
284283
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
285284
JSValue this_obj = TO_JS_VALUE(env, object_handle);
286285
JS_FreeValue(ctx, this_obj);
287-
}extern "C"
288-
JNIEXPORT jint JNICALL
289-
Java_com_quickjs_android_QuickJS__1getInteger(JNIEnv *env, jclass clazz, jlong context_ptr,
290-
jobject object_handle, jstring key) {
291-
const char *key_ = env->GetStringUTFChars(key, nullptr);
292-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
293-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
294-
JSValue jsValue = JS_GetPropertyStr(ctx, this_obj, key_);
295-
return JS_VALUE_GET_INT(jsValue);
296-
}extern "C"
297-
JNIEXPORT jboolean JNICALL
298-
Java_com_quickjs_android_QuickJS__1getBoolean(JNIEnv *env, jclass clazz, jlong context_ptr,
299-
jobject object_handle, jstring key) {
300-
const char *key_ = env->GetStringUTFChars(key, nullptr);
301-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
302-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
303-
JSValue jsValue = JS_GetPropertyStr(ctx, this_obj, key_);
304-
return JS_VALUE_GET_BOOL(jsValue);
305-
306286
}
287+
307288
extern "C"
308289
JNIEXPORT jobject JNICALL
309-
Java_com_quickjs_android_QuickJS__1getObject(JNIEnv *env, jclass clazz, jlong context_ptr,
310-
jobject object_handle, jstring key) {
311-
const char *key_ = env->GetStringUTFChars(key, nullptr);
312-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
313-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
314-
JSValue jsValue = JS_GetPropertyStr(ctx, this_obj, key_);
315-
return TO_JAVA_OBJECT(env, ctx, jsValue);
316-
}
317-
extern "C"
318-
JNIEXPORT jdouble JNICALL
319-
Java_com_quickjs_android_QuickJS__1getDouble(JNIEnv *env, jclass clazz, jlong context_ptr,
320-
jobject object_handle, jstring key) {
290+
Java_com_quickjs_android_QuickJS__1get(JNIEnv *env, jclass clazz, jlong context_ptr,
291+
int expected_type,
292+
jobject object_handle, jstring key) {
321293
const char *key_ = env->GetStringUTFChars(key, nullptr);
322294
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
323295
JSValue this_obj = TO_JS_VALUE(env, object_handle);
324-
JSValue jsValue = JS_GetPropertyStr(ctx, this_obj, key_);
325-
double pres;
326-
JS_ToFloat64(ctx, &pres, jsValue);
327-
return pres;
328-
}extern "C"
329-
JNIEXPORT jstring JNICALL
330-
Java_com_quickjs_android_QuickJS__1getString(JNIEnv *env, jclass clazz, jlong context_ptr,
331-
jobject object_handle, jstring key) {
332-
const char *key_ = env->GetStringUTFChars(key, nullptr);
333-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
334-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
335-
JSValue jsValue = JS_GetPropertyStr(ctx, this_obj, key_);
336-
const char *str = JS_ToCString(ctx, jsValue);
337-
jstring j_str = env->NewStringUTF(str);
338-
return j_str;
296+
JSValue result = JS_GetPropertyStr(ctx, this_obj, key_);
297+
return To_JObject(env, context_ptr, expected_type, result);
339298
}
340299

341-
extern "C"
342-
JNIEXPORT jstring JNICALL
343-
Java_com_quickjs_android_QuickJS__1arrayGetString(JNIEnv *env, jclass clazz, jlong context_ptr,
344-
jobject object_handle, jint index) {
345-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
346-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
347-
JSValue jsValue = JS_GetPropertyUint32(ctx, this_obj, index);
348-
const char *str = JS_ToCString(ctx, jsValue);
349-
jstring j_str = env->NewStringUTF(str);
350-
return j_str;
351-
}
352-
extern "C"
353-
JNIEXPORT jdouble JNICALL
354-
Java_com_quickjs_android_QuickJS__1arrayGetDouble(JNIEnv *env, jclass clazz, jlong context_ptr,
355-
jobject object_handle, jint index) {
356-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
357-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
358-
JSValue jsValue = JS_GetPropertyUint32(ctx, this_obj, index);
359-
double pres;
360-
JS_ToFloat64(ctx, &pres, jsValue);
361-
return pres;
362-
}extern "C"
363-
JNIEXPORT jboolean JNICALL
364-
Java_com_quickjs_android_QuickJS__1arrayGetBoolean(JNIEnv *env, jclass clazz, jlong context_ptr,
365-
jobject object_handle, jint index) {
366-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
367-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
368-
JSValue jsValue = JS_GetPropertyUint32(ctx, this_obj, index);
369-
return JS_VALUE_GET_BOOL(jsValue);
370-
}
371300

372-
extern "C"
373-
JNIEXPORT jint JNICALL
374-
Java_com_quickjs_android_QuickJS__1arrayGetInteger(JNIEnv *env, jclass clazz, jlong context_ptr,
375-
jobject object_handle, jint index) {
376-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
377-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
378-
JSValue jsValue = JS_GetPropertyUint32(ctx, this_obj, index);
379-
return JS_VALUE_GET_INT(jsValue);
380-
}
301+
302+
381303
extern "C"
382304
JNIEXPORT jobject JNICALL
383-
Java_com_quickjs_android_QuickJS__1arrayGetObject(JNIEnv *env, jclass clazz, jlong context_ptr,
384-
jobject object_handle, jint index) {
305+
Java_com_quickjs_android_QuickJS__1arrayGet(JNIEnv *env, jclass clazz, jlong context_ptr,
306+
int expected_type,
307+
jobject object_handle, jint index) {
385308
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
386309
JSValue this_obj = TO_JS_VALUE(env, object_handle);
387-
JSValue jsValue = JS_GetPropertyUint32(ctx, this_obj, index);
388-
if (JS_IsNull(jsValue)) return 0;
389-
if (JS_IsObject(jsValue)) return TO_JAVA_OBJECT(env, ctx, jsValue);
390-
return nullptr;
310+
JSValue result = JS_GetPropertyUint32(ctx, this_obj, index);
311+
return To_JObject(env, context_ptr, expected_type, result);
391312
}
392313

393314
extern "C"
394-
JNIEXPORT void JNICALL
395-
Java_com_quickjs_android_QuickJS__1arrayAdd__JJI(JNIEnv *env, jclass clazz, jlong context_ptr,
396-
jobject object_handle, jint value) {
397-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
398-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
399-
JSValue value_ = JS_NewInt32(ctx, value);
400-
JS_SetPropertyUint32(ctx, this_obj, getArrayLength(ctx, this_obj), value_);
401-
}extern "C"
402-
JNIEXPORT void JNICALL
403-
Java_com_quickjs_android_QuickJS__1arrayAdd__JJD(JNIEnv *env, jclass clazz, jlong context_ptr,
404-
jobject object_handle, jdouble value) {
405-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
406-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
407-
JSValue value_ = JS_NewFloat64(ctx, value);
408-
JS_SetPropertyUint32(ctx, this_obj, getArrayLength(ctx, this_obj), value_);
409-
}extern "C"
410-
JNIEXPORT void JNICALL
411-
Java_com_quickjs_android_QuickJS__1arrayAdd__JJZ(JNIEnv *env, jclass clazz, jlong context_ptr,
412-
jobject object_handle, jboolean value) {
413-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
414-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
415-
JSValue value_ = JS_NewBool(ctx, value);
416-
JS_SetPropertyUint32(ctx, this_obj, getArrayLength(ctx, this_obj), value_);
417-
}extern "C"
418-
JNIEXPORT void JNICALL
419-
Java_com_quickjs_android_QuickJS__1arrayAddObject(JNIEnv *env, jclass clazz, jlong context_ptr,
420-
jobject object_handle, jobject value) {
421-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
422-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
423-
JSValue value_ = TO_JS_VALUE(env, value);
424-
JS_SetPropertyUint32(ctx, this_obj, getArrayLength(ctx, this_obj), value_);
425-
}
426-
extern "C"
427-
JNIEXPORT void JNICALL
428-
Java_com_quickjs_android_QuickJS__1arrayAdd__JJLjava_lang_String_2(JNIEnv *env, jclass clazz,
429-
jlong context_ptr,
430-
jobject object_handle,
431-
jstring value) {
432-
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
433-
JSValue this_obj = TO_JS_VALUE(env, object_handle);
434-
const char *value_str = env->GetStringUTFChars(value, nullptr);
435-
JSValue value_ = JS_NewString(ctx, value_str);
436-
JS_SetPropertyUint32(ctx, this_obj, getArrayLength(ctx, this_obj), value_);
437-
}extern "C"
438315
JNIEXPORT jboolean JNICALL
439316
Java_com_quickjs_android_QuickJS__1contains(JNIEnv *env, jclass clazz, jlong context_ptr,
440317
jobject object_handle, jstring key) {
@@ -671,4 +548,4 @@ Java_com_quickjs_android_QuickJS__1arrayAdd(JNIEnv *env, jclass clazz, jlong con
671548
} else if (env->IsInstanceOf(value, jsValueCls)) {
672549
JS_SetPropertyUint32(ctx, this_obj, len, TO_JS_VALUE(env, value));
673550
}
674-
}
551+
}

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

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,82 @@ public JSArray(JSContext context) {
1111
super(context, tag, u_int32, u_float64, u_ptr);
1212
}
1313

14+
public Object get(int expectedType, int index) {
15+
return QuickJS._arrayGet(this.getContextPtr(), expectedType, this, index);
16+
}
17+
18+
public JSArray pushObject(Object value) {
19+
QuickJS._arrayAdd(getContextPtr(), this, value);
20+
return this;
21+
}
22+
23+
1424
public int getInteger(int index) {
15-
return QuickJS._arrayGetInteger(this.getContextPtr(), this, index);
25+
Object result = get(JSValue.INTEGER, index);
26+
if (result instanceof Integer) {
27+
return (int) result;
28+
}
29+
return 0;
1630
}
1731

1832
public boolean getBoolean(int index) {
19-
return QuickJS._arrayGetBoolean(this.getContextPtr(), this, index);
33+
Object result = get(JSValue.BOOLEAN, index);
34+
if (result instanceof Boolean) {
35+
return (boolean) result;
36+
}
37+
return false;
2038
}
2139

2240
public double getDouble(int index) {
23-
return QuickJS._arrayGetDouble(this.getContextPtr(), this, index);
41+
Object result = get(JSValue.DOUBLE, index);
42+
if (result instanceof Double) {
43+
return (double) result;
44+
}
45+
return 0;
2446
}
2547

2648
public String getString(int index) {
27-
return QuickJS._arrayGetString(this.getContextPtr(), this, index);
49+
Object result = get(JSValue.STRING, index);
50+
if (result instanceof String) {
51+
return (String) result;
52+
}
53+
return null;
2854
}
2955

3056
public JSObject getObject(int index) {
31-
return QuickJS._arrayGetObject(this.getContextPtr(), this, index);
57+
Object result = get(JSValue.JS_OBJECT, index);
58+
if (result instanceof JSObject) {
59+
return (JSObject) result;
60+
}
61+
return null;
3262
}
3363

3464
public JSArray getArray(int index) {
35-
JSObject object = getObject(index);
36-
if (object instanceof JSArray) {
37-
return (JSArray) object;
65+
Object result = get(JSValue.JS_ARRAY, index);
66+
if (result instanceof JSArray) {
67+
return (JSArray) result;
3868
}
3969
return null;
4070
}
4171

4272
public JSArray push(int value) {
43-
QuickJS._arrayAdd(getContextPtr(), this, value);
44-
return this;
73+
return pushObject(value);
4574
}
4675

4776
public JSArray push(double value) {
48-
QuickJS._arrayAdd(getContextPtr(), this, value);
49-
return this;
77+
return pushObject(value);
5078
}
5179

5280
public JSArray push(String value) {
53-
QuickJS._arrayAdd(getContextPtr(), this, value);
54-
return this;
81+
return pushObject(value);
5582
}
5683

5784
public JSArray push(boolean value) {
58-
QuickJS._arrayAdd(getContextPtr(), this, value);
59-
return this;
85+
return pushObject(value);
6086
}
6187

6288
public JSArray push(JSValue value) {
63-
QuickJS._arrayAdd(getContextPtr(), this, value);
64-
return this;
89+
return pushObject(value);
6590
}
6691

6792
public int length() {

0 commit comments

Comments
 (0)