@@ -259,8 +259,8 @@ Java_com_quickjs_QuickJS__1releaseContext(JNIEnv *env, jclass clazz, jlong conte
259259extern " C"
260260JNIEXPORT jobject JNICALL
261261Java_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
304304extern " C"
305305JNIEXPORT void JNICALL
306306Java_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,
313313extern " C"
314314JNIEXPORT jobject JNICALL
315315Java_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,
326326extern " C"
327327JNIEXPORT jobject JNICALL
328328Java_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
339339extern " C"
340340JNIEXPORT jobject JNICALL
341341Java_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
352352extern " C"
353353JNIEXPORT jobject JNICALL
354354Java_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
362362extern " C"
363363JNIEXPORT jboolean JNICALL
364364Java_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"
374374JNIEXPORT jobjectArray JNICALL
375375Java_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
425424extern " C"
426425JNIEXPORT jobject JNICALL
427426Java_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
440439extern " C"
441440JNIEXPORT jobject JNICALL
442441Java_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) {
509507extern " C"
510508JNIEXPORT jobject JNICALL
511509Java_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}
520518extern " C"
521519JNIEXPORT jobject JNICALL
522520Java_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
535533extern " C"
536534JNIEXPORT jint JNICALL
537535Java_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}
543541extern " C"
544542JNIEXPORT void JNICALL
545543Java_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,
574571extern " C"
575572JNIEXPORT void JNICALL
576573Java_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}
604600extern " C"
605601JNIEXPORT jboolean JNICALL
606602Java_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"
611607JNIEXPORT jobject JNICALL
612608Java_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