Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve exceptions #32

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions src/ckb_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,15 @@ 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),
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
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;
}
Expand Down Expand Up @@ -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),
Expand Down
21 changes: 21 additions & 0 deletions src/misc_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
11 changes: 3 additions & 8 deletions src/qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions tests/basic/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions tests/basic/test_exception.js
Original file line number Diff line number Diff line change
@@ -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();
Loading