Skip to content

Commit af46d12

Browse files
authored
Merge pull request taoweiji#3 from taoweiji/develop
Develop
2 parents 075c9ba + b68558c commit af46d12

File tree

11 files changed

+182
-66
lines changed

11 files changed

+182
-66
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ quickjs-android 是 QuickJS JavaScript 引擎的 Andoroid 接口框架,实现
99
##### 引入依赖
1010

1111
```groovy
12-
implementation 'io.github.taoweiji.quickjs:quickjs-android:1.1.2'
12+
implementation 'io.github.taoweiji.quickjs:quickjs-android:1.1.3'
1313
```
1414

1515
##### 简单示例

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.quickjs.android.example;
22

33
import android.os.Bundle;
4+
import android.util.Log;
45

56
import androidx.appcompat.app.AppCompatActivity;
67

8+
import com.eclipsesource.v8.V8;
79
import com.quickjs.JSArray;
810
import com.quickjs.JSContext;
911
import com.quickjs.JSObject;
12+
import com.quickjs.JSValue;
1013
import com.quickjs.QuickJS;
1114

1215
public class MainActivity extends AppCompatActivity {
@@ -21,12 +24,18 @@ protected void onCreate(Bundle savedInstanceState) {
2124
jsContext = quickJS.createContext();
2225
jsContext = quickJS.createContext();
2326
test();
27+
testV8();
2428
}
2529

2630
void test() {
27-
jsContext.executeVoidScript("function test(data){ return data}", "file.js");
28-
new JSArray(jsContext);
29-
new JSObject(jsContext);
31+
JSValue result = jsContext.executeObjectScript2("a.a", "file.js");
32+
// JSValue.TYPE type = result.getType();
33+
Log.e("QuickJS", result.getClass().getName());
34+
}
35+
36+
void testV8() {
37+
// V8 v8 = V8.createV8Runtime();
38+
// v8.executeVoidScript("a.a");
3039
}
3140

3241
@Override

note.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- JS_SetProperty 必须增加JS_DupValue
2+
- new_XX,由于绑定了Java对象,所以不需要JS_DupValue
3+
- JS_GetProperty 绑定了java对象,所以也不需要JS_DupValue

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.quickjs;
22

3+
import android.util.Log;
4+
35
import com.quickjs.JSArray;
46
import com.quickjs.JSContext;
57
import com.quickjs.JSFunction;
@@ -10,6 +12,8 @@
1012
import org.junit.Before;
1113
import org.junit.Test;
1214

15+
import java.util.Arrays;
16+
1317
import static org.junit.Assert.*;
1418

1519
public class JSContextTest {
@@ -90,4 +94,24 @@ public void executeObjectScript() {
9094
Object result7 = context.executeScript("function test(data){return data};test", "file.js");
9195
assertEquals(JSFunction.class, result7.getClass());
9296
}
97+
98+
99+
@Test
100+
public void executeObjectScriptException() {
101+
try {
102+
context.executeVoidScript("function test1(params) {\n" +
103+
" params.say();\n" +
104+
"}\n" +
105+
"function test2(params) {\n" +
106+
" test1(params);\n" +
107+
"}\n" +
108+
"function test3(params) {\n" +
109+
" test2(params);\n" +
110+
"}\n" +
111+
"test3(\"\");", "file.js");
112+
throw new Exception();
113+
} catch (Exception e) {
114+
assertEquals(QuickJSException.class, e.getClass());
115+
}
116+
}
93117
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void testJavaCallback3() {
101101
context.set("boolFunction", new JSFunction(context, (JavaCallback) (receiver, args) -> true));
102102
context.set("stringFunction", new JSFunction(context, (JavaCallback) (receiver, args) -> "Hello"));
103103

104-
context.executeVoidFunction("intFunction", null);
104+
context.executeVoidFunction("intFunction", new JSArray(context).push(new JSArray(context)));
105105
JSFunction function = (JSFunction) context.getObject("intFunction");
106106
function.call(JSValue.TYPE.INTEGER, null, null);
107107

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

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ Java_com_quickjs_QuickJS__1releaseContext(JNIEnv *env, jclass clazz, jlong conte
259259
extern "C"
260260
JNIEXPORT jobject JNICALL
261261
Java_com_quickjs_QuickJS__1executeScript(JNIEnv *env, jclass clazz, jlong context_ptr,
262-
jint expected_type,
263-
jstring source, jstring file_name) {
262+
jint expected_type,
263+
jstring source, jstring file_name) {
264264
if (source == nullptr) {
265265
return nullptr;
266266
}
@@ -304,7 +304,7 @@ Java_com_quickjs_QuickJS__1initNewJSArray(JNIEnv *env, jclass clazz, jlong conte
304304
extern "C"
305305
JNIEXPORT void JNICALL
306306
Java_com_quickjs_QuickJS__1release(JNIEnv *env, jclass clazz, jlong context_ptr,
307-
jobject object_handle) {
307+
jobject object_handle) {
308308
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
309309
JSValue this_obj = TO_JS_VALUE(env, object_handle);
310310
JS_FreeValue(ctx, this_obj);
@@ -313,8 +313,8 @@ Java_com_quickjs_QuickJS__1release(JNIEnv *env, jclass clazz, jlong context_ptr,
313313
extern "C"
314314
JNIEXPORT jobject JNICALL
315315
Java_com_quickjs_QuickJS__1get(JNIEnv *env, jclass clazz, jlong context_ptr,
316-
int expected_type,
317-
jobject object_handle, jstring key) {
316+
int expected_type,
317+
jobject object_handle, jstring key) {
318318
const char *key_ = env->GetStringUTFChars(key, nullptr);
319319
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
320320
JSValue this_obj = TO_JS_VALUE(env, object_handle);
@@ -326,7 +326,7 @@ Java_com_quickjs_QuickJS__1get(JNIEnv *env, jclass clazz, jlong context_ptr,
326326
extern "C"
327327
JNIEXPORT jobject JNICALL
328328
Java_com_quickjs_QuickJS__1getValue(JNIEnv *env, jclass clazz, jlong context_ptr,
329-
jobject object_handle, jstring key) {
329+
jobject object_handle, jstring key) {
330330
const char *key_ = env->GetStringUTFChars(key, nullptr);
331331
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
332332
JSValue this_obj = TO_JS_VALUE(env, object_handle);
@@ -339,8 +339,8 @@ Java_com_quickjs_QuickJS__1getValue(JNIEnv *env, jclass clazz, jlong context_ptr
339339
extern "C"
340340
JNIEXPORT jobject JNICALL
341341
Java_com_quickjs_QuickJS__1arrayGet(JNIEnv *env, jclass clazz, jlong context_ptr,
342-
int expected_type,
343-
jobject object_handle, jint index) {
342+
int expected_type,
343+
jobject object_handle, jint index) {
344344
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
345345
JSValue this_obj = TO_JS_VALUE(env, object_handle);
346346
JSValue result = JS_GetPropertyUint32(ctx, this_obj, index);
@@ -352,7 +352,7 @@ Java_com_quickjs_QuickJS__1arrayGet(JNIEnv *env, jclass clazz, jlong context_ptr
352352
extern "C"
353353
JNIEXPORT jobject JNICALL
354354
Java_com_quickjs_QuickJS__1arrayGetValue(JNIEnv *env, jclass clazz, jlong context_ptr,
355-
jobject object_handle, jint index) {
355+
jobject object_handle, jint index) {
356356
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
357357
JSValue this_obj = TO_JS_VALUE(env, object_handle);
358358
JSValue result = JS_GetPropertyUint32(ctx, this_obj, index);
@@ -362,7 +362,7 @@ Java_com_quickjs_QuickJS__1arrayGetValue(JNIEnv *env, jclass clazz, jlong contex
362362
extern "C"
363363
JNIEXPORT jboolean JNICALL
364364
Java_com_quickjs_QuickJS__1contains(JNIEnv *env, jclass clazz, jlong context_ptr,
365-
jobject object_handle, jstring key) {
365+
jobject object_handle, jstring key) {
366366
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
367367
JSValue this_obj = TO_JS_VALUE(env, object_handle);
368368
const char *key_ = env->GetStringUTFChars(key, nullptr);
@@ -373,7 +373,7 @@ Java_com_quickjs_QuickJS__1contains(JNIEnv *env, jclass clazz, jlong context_ptr
373373
}extern "C"
374374
JNIEXPORT jobjectArray JNICALL
375375
Java_com_quickjs_QuickJS__1getKeys(JNIEnv *env, jclass clazz, jlong context_ptr,
376-
jobject object_handle) {
376+
jobject object_handle) {
377377
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
378378
JSValue this_obj = TO_JS_VALUE(env, object_handle);
379379
JSPropertyEnum *tab;
@@ -403,7 +403,6 @@ JSValue executeFunction(JNIEnv *env, jlong context_ptr, jobject object_handle, J
403403
argv[i] = JS_DupValue(ctx, JS_GetPropertyUint32(ctx, argArray, i));
404404
}
405405
}
406-
// JS_DupValue(ctx, this_obj);
407406
JSValue global = JS_GetGlobalObject(ctx);
408407

409408
if (JS_Equals(this_obj, global)) {
@@ -425,9 +424,9 @@ JSValue executeFunction(JNIEnv *env, jlong context_ptr, jobject object_handle, J
425424
extern "C"
426425
JNIEXPORT jobject JNICALL
427426
Java_com_quickjs_QuickJS__1executeFunction2(JNIEnv *env, jclass clazz, jlong context_ptr,
428-
jint expected_type, jobject object_handle,
429-
jobject functionHandle,
430-
jobject parameters_handle) {
427+
jint expected_type, jobject object_handle,
428+
jobject functionHandle,
429+
jobject parameters_handle) {
431430
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
432431
JSValue func_obj = TO_JS_VALUE(env, functionHandle);
433432
JS_DupValue(ctx, func_obj);
@@ -440,8 +439,8 @@ Java_com_quickjs_QuickJS__1executeFunction2(JNIEnv *env, jclass clazz, jlong con
440439
extern "C"
441440
JNIEXPORT jobject JNICALL
442441
Java_com_quickjs_QuickJS__1executeFunction(JNIEnv *env, jclass clazz, jlong context_ptr,
443-
jint expected_type, jobject object_handle,
444-
jstring name, jobject parameters_handle) {
442+
jint expected_type, jobject object_handle,
443+
jstring name, jobject parameters_handle) {
445444
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
446445
JSValue this_obj = TO_JS_VALUE(env, object_handle);
447446
JSValue func_obj = JS_GetPropertyStr(ctx, this_obj, env->GetStringUTFChars(name, nullptr));
@@ -473,7 +472,6 @@ callJavaCallback(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *
473472
JS_DupValue(ctx, this_val);
474473
}
475474
JS_FreeValue(ctx, global);
476-
// JS_DupValue(ctx,args);
477475
jobject result = env->CallStaticObjectMethod(quickJSCls, callJavaCallbackMethodID,
478476
context_ptr,
479477
callbackId,
@@ -509,20 +507,20 @@ JSValue newFunction(jlong context_ptr, jboolean void_method, int callbackId) {
509507
extern "C"
510508
JNIEXPORT jobject JNICALL
511509
Java_com_quickjs_QuickJS__1initNewJSFunction(JNIEnv *env,
512-
jclass clazz,
513-
jlong context_ptr,
514-
jint callbackId,
515-
jboolean void_method) {
510+
jclass clazz,
511+
jlong context_ptr,
512+
jint callbackId,
513+
jboolean void_method) {
516514
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
517515
JSValue func = newFunction(context_ptr, void_method, callbackId);
518516
return TO_JAVA_OBJECT(env, ctx, func);
519517
}
520518
extern "C"
521519
JNIEXPORT jobject JNICALL
522520
Java_com_quickjs_QuickJS__1registerJavaMethod(JNIEnv *env, jclass clazz, jlong context_ptr,
523-
jobject object_handle, jstring function_name,
524-
jint callbackId,
525-
jboolean void_method) {
521+
jobject object_handle, jstring function_name,
522+
jint callbackId,
523+
jboolean void_method) {
526524
const char *name_ = env->GetStringUTFChars(function_name, nullptr);
527525
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
528526
JSValue func = newFunction(context_ptr, void_method, callbackId);
@@ -535,15 +533,15 @@ Java_com_quickjs_QuickJS__1registerJavaMethod(JNIEnv *env, jclass clazz, jlong c
535533
extern "C"
536534
JNIEXPORT jint JNICALL
537535
Java_com_quickjs_QuickJS__1getObjectType(JNIEnv *env, jclass clazz, jlong context_ptr,
538-
jobject object_handle) {
536+
jobject object_handle) {
539537
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
540538
JSValue value = TO_JS_VALUE(env, object_handle);
541539
return GetObjectType(ctx, value);
542540
}
543541
extern "C"
544542
JNIEXPORT void JNICALL
545543
Java_com_quickjs_QuickJS__1set(JNIEnv *env, jclass clazz, jlong context_ptr,
546-
jobject object_handle, jstring key, jobject value) {
544+
jobject object_handle, jstring key, jobject value) {
547545
const char *key_ = env->GetStringUTFChars(key, nullptr);
548546
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
549547
JSValue this_obj = TO_JS_VALUE(env, object_handle);
@@ -565,7 +563,6 @@ Java_com_quickjs_QuickJS__1set(JNIEnv *env, jclass clazz, jlong context_ptr,
565563
const char *value_ = env->GetStringUTFChars((jstring) value, nullptr);
566564
JS_SetPropertyStr(ctx, this_obj, key_, JS_NewString(ctx, value_));
567565
} else if (env->IsInstanceOf(value, jsValueCls)) {
568-
// TODO 导致内存泄漏
569566
JSValue tmp = JS_DupValue(ctx, TO_JS_VALUE(env, value));
570567
JS_SetPropertyStr(ctx, this_obj, key_, tmp);
571568
}
@@ -574,7 +571,7 @@ Java_com_quickjs_QuickJS__1set(JNIEnv *env, jclass clazz, jlong context_ptr,
574571
extern "C"
575572
JNIEXPORT void JNICALL
576573
Java_com_quickjs_QuickJS__1arrayAdd(JNIEnv *env, jclass clazz, jlong context_ptr,
577-
jobject object_handle, jobject value) {
574+
jobject object_handle, jobject value) {
578575
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
579576
JSValue this_obj = TO_JS_VALUE(env, object_handle);
580577
int len = GetArrayLength(ctx, this_obj);
@@ -596,20 +593,52 @@ Java_com_quickjs_QuickJS__1arrayAdd(JNIEnv *env, jclass clazz, jlong context_ptr
596593
const char *value_ = env->GetStringUTFChars((jstring) value, nullptr);
597594
JS_SetPropertyUint32(ctx, this_obj, len, JS_NewString(ctx, value_));
598595
} else if (env->IsInstanceOf(value, jsValueCls)) {
599-
// TODO 导致内存泄漏
600596
JSValue tmp = JS_DupValue(ctx, TO_JS_VALUE(env, value));
601597
JS_SetPropertyUint32(ctx, this_obj, len, tmp);
602598
}
603599
}
604600
extern "C"
605601
JNIEXPORT jboolean JNICALL
606602
Java_com_quickjs_QuickJS__1isUndefined(JNIEnv *env, jclass clazz, jlong context_ptr,
607-
jobject js_value) {
603+
jobject js_value) {
608604
JSValue value = TO_JS_VALUE(env, js_value);
609605
return JS_IsUndefined(value);
610606
}extern "C"
611607
JNIEXPORT jobject JNICALL
612608
Java_com_quickjs_QuickJS__1Undefined(JNIEnv *env, jclass clazz, jlong context_ptr) {
613609
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
614610
return TO_JAVA_OBJECT(env, ctx, JS_UNDEFINED);
611+
}
612+
613+
extern "C"
614+
JNIEXPORT jobjectArray JNICALL
615+
Java_com_quickjs_QuickJS__1getException(JNIEnv *env, jclass clazz, jlong context_ptr) {
616+
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
617+
JSValue exc = JS_GetException(ctx);
618+
if (!JS_IsError(ctx, exc)) {
619+
return nullptr;
620+
}
621+
622+
JSValue func = JS_GetPropertyStr(ctx, exc, "toString");
623+
JSValue nameValue = JS_GetPropertyStr(ctx, exc, "name");
624+
JSValue stackValue = JS_GetPropertyStr(ctx, exc, "stack");
625+
JSValue titleValue = JS_Call(ctx, func, exc, 0, nullptr);
626+
JS_FreeValue(ctx, func);
627+
628+
std::vector<const char *> messages;
629+
messages.push_back(JS_ToCString(ctx, nameValue));
630+
messages.push_back(JS_ToCString(ctx, titleValue));
631+
while (!JS_IsUndefined(stackValue)) {
632+
messages.push_back(JS_ToCString(ctx, stackValue));
633+
JS_FreeValue(ctx, stackValue);
634+
stackValue = JS_GetPropertyStr(ctx, stackValue, "stack");
635+
}
636+
JS_FreeValue(ctx, exc);
637+
638+
jobjectArray stringArray = env->NewObjectArray(messages.size(), stringCls, nullptr);
639+
for (int i = 0; i < messages.size(); ++i) {
640+
jstring str = env->NewStringUTF(messages[i]);
641+
env->SetObjectArrayElement(stringArray, i, str);
642+
}
643+
return stringArray;
615644
}

0 commit comments

Comments
 (0)