From ce2ecefaee50628e582d7e26bdb651267f053af1 Mon Sep 17 00:00:00 2001 From: Lyndon Date: Fri, 10 Jan 2025 11:12:08 +0800 Subject: [PATCH 1/2] refactor: improve exceptions print stack backtrace --- src/misc_module.c | 21 +++++++++++++++++++++ src/qjs.c | 11 +++-------- tests/basic/Makefile | 3 +++ tests/basic/test_exception.js | 21 +++++++++++++++++++++ 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 tests/basic/test_exception.js diff --git a/src/misc_module.c b/src/misc_module.c index 71c939c..31a0bf9 100644 --- a/src/misc_module.c +++ b/src/misc_module.c @@ -294,6 +294,21 @@ static const JSClassDef js_smt_class = { .finalizer = js_smt_finalizer, }; +// Add these new function definitions +static JSValue js_throw_exception(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { + const char *message = "Test exception"; + if (argc > 0) { + message = JS_ToCString(ctx, argv[0]); + if (!message) return JS_EXCEPTION; + } + JSValue error = JS_ThrowInternalError(ctx, "This is a test exception, %s", message); + if (argc > 0) JS_FreeCString(ctx, message); + return error; +} + +static const JSCFunctionListEntry js_test_funcs[] = { + JS_CFUNC_DEF("throw_exception", 1, js_throw_exception), +}; static int js_misc_init(JSContext *ctx, JSModuleDef *m) { JSValue proto, obj; @@ -324,6 +339,11 @@ static int js_misc_init(JSContext *ctx, JSModuleDef *m) { JS_SetPropertyFunctionList(ctx, base64, js_base64_funcs, countof(js_base64_funcs)); JS_SetModuleExport(ctx, m, "base64", base64); + // Create test object and add functions + JSValue test = JS_NewObject(ctx); + JS_SetPropertyFunctionList(ctx, test, js_test_funcs, countof(js_test_funcs)); + JS_SetModuleExport(ctx, m, "test", test); + return 0; } @@ -335,5 +355,6 @@ int js_init_module_misc(JSContext *ctx) { JS_AddModuleExport(ctx, m, "Smt"); JS_AddModuleExport(ctx, m, "hex"); JS_AddModuleExport(ctx, m, "base64"); + JS_AddModuleExport(ctx, m, "test"); return 0; } diff --git a/src/qjs.c b/src/qjs.c index 79841e4..3710aa5 100644 --- a/src/qjs.c +++ b/src/qjs.c @@ -168,14 +168,9 @@ static int eval_buf(JSContext *ctx, const void *buf, int buf_len, const char *fi if ((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_MODULE) { int promise_state = JS_PromiseState(ctx, val); if (promise_state == JS_PROMISE_REJECTED) { - printf("JS_Eval return a rejected promise"); - JSValue result = JS_PromiseResult(ctx, val); - const char *str = JS_ToCString(ctx, result); - if (str) { - printf("This promise returns: %s", str); - JS_FreeCString(ctx, str); - } - JS_FreeValue(ctx, result); + JSValue error = JS_PromiseResult(ctx, val); + js_std_dump_error1(ctx, error); + JS_FreeValue(ctx, error); ret = -2; } else { ret = 0; diff --git a/tests/basic/Makefile b/tests/basic/Makefile index 360101a..6f92061 100644 --- a/tests/basic/Makefile +++ b/tests/basic/Makefile @@ -22,6 +22,9 @@ qjs-tests: $(call run,test_bignum.js) $(call run,test_float.js) +exception: + $(call debug,test_exception.js) + log: $(CKB-DEBUGGER) --bin $(BIN_PATH) -- -e "console.log(scriptArgs[0], scriptArgs[1]);" hello world diff --git a/tests/basic/test_exception.js b/tests/basic/test_exception.js new file mode 100644 index 0000000..a3fd74f --- /dev/null +++ b/tests/basic/test_exception.js @@ -0,0 +1,21 @@ +import * as misc from 'misc'; + +function main_1() { + console.log("main_1() function"); + main_2(); +} +function main_2() { + console.log("main_2() function"); + main_3(); +} +function main_3() { + console.log("main_3() function"); + misc.test.throw_exception("Exception in main_3() function"); +} + +function main() { + console.log("main() function"); + main_1(); +} + +main(); From ef3a910dc366b27f8fd00a2368e76b674f371a3f Mon Sep 17 00:00:00 2001 From: Lyndon Date: Fri, 10 Jan 2025 11:24:39 +0800 Subject: [PATCH 2/2] docs: add comments --- src/ckb_module.c | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/ckb_module.c b/src/ckb_module.c index bda0933..0f169a4 100644 --- a/src/ckb_module.c +++ b/src/ckb_module.c @@ -52,7 +52,6 @@ static JSValue ThrowError(JSContext *ctx, int32_t error_code, const char *messag JSValue obj, ret; obj = JS_NewError(ctx); if (unlikely(JS_IsException(obj))) { - /* out of memory: throw JS_NULL to avoid recursing */ obj = JS_NULL; } else { JS_DefinePropertyValueStr(ctx, obj, "message", JS_NewString(ctx, message), @@ -60,10 +59,8 @@ static JSValue ThrowError(JSContext *ctx, int32_t error_code, const char *messag JS_DefinePropertyValueStr(ctx, obj, "errorCode", JS_NewInt32(ctx, error_code), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); } - // TODO - // if (add_backtrace) { - // build_backtrace(ctx, obj, NULL, 0, 0); - // } + // Syscall errors are expected to occur frequently during normal operation. + // We intentionally skip generating stack traces here to avoid performance overhead ret = JS_Throw(ctx, obj); return ret; } @@ -590,14 +587,6 @@ static JSValue mount(JSContext *ctx, JSValueConst this_value, int argc, JSValueC } } -/* -TODO: -// who allocated the memory indicated by aligned_addr? -int ckb_dlopen2(const uint8_t* dep_cell_hash, uint8_t hash_type, - uint8_t* aligned_addr, size_t aligned_size, void** handle, - size_t* consumed_size); -void* ckb_dlsym(void* handle, const char* symbol); -*/ static const JSCFunctionListEntry js_ckb_funcs[] = { JS_CFUNC_DEF("exit", 1, syscall_exit), JS_CFUNC_DEF("load_tx_hash", 1, syscall_load_tx_hash),