Skip to content

Commit

Permalink
refactor: code_hash handling in syscalls and improve error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Jan 15, 2025
1 parent 9a81a71 commit 15386f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions src/ckb_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ static JSValue syscall_exec_cell(JSContext *ctx, JSValueConst this_value, int ar
const char *passed_argv[256] = {0};
uint8_t code_hash[32];

JSValue buffer = JS_GetTypedArrayBuffer(ctx, argv[0], NULL, NULL, NULL);
CHECK2(!JS_IsException(buffer), ERROR_TEMP);
uint8_t *p = JS_GetArrayBuffer(ctx, &code_hash_len, buffer);
CHECK2(code_hash_len == 32 && p != NULL, ERROR_TEMP);
uint8_t *p = JS_GetArrayBuffer(ctx, &code_hash_len, argv[0]);
if (code_hash_len != 32 || p == NULL) {
return JS_ThrowTypeError(ctx, "invalid code_hash format");
}
memcpy(code_hash, p, 32);

CHECK2(!qjs_bad_int_arg(ctx, argv[1], 1), ERROR_TEMP);
Expand All @@ -374,7 +374,6 @@ static JSValue syscall_exec_cell(JSContext *ctx, JSValueConst this_value, int ar
CHECK(err);
// never reach here if succeeded
exit:
JS_FreeValue(ctx, buffer);
if (err != 0) {
return JS_EXCEPTION;
} else {
Expand Down Expand Up @@ -409,13 +408,13 @@ static JSValue syscall_spawn_cell(JSContext *ctx, JSValueConst this_value, int a
uint32_t offset = 0;
uint32_t length = 0;
uint32_t spgs_argc = 0;
const char *spgs_argv[32] = {};
const char *spgs_argv[32] = {0};
uint64_t spgs_pid = 0;
uint64_t spgs_fds[32] = {0};
JSValue buffer = JS_GetTypedArrayBuffer(ctx, argv[0], NULL, NULL, NULL);
CHECK2(!JS_IsException(buffer), ERROR_TEMP);
uint8_t *p = JS_GetArrayBuffer(ctx, &code_hash_len, buffer);
CHECK2(code_hash_len == 32 && p != NULL, ERROR_TEMP);
uint8_t *p = JS_GetArrayBuffer(ctx, &code_hash_len, argv[0]);
if (code_hash_len != 32 || p == NULL) {
return JS_ThrowTypeError(ctx, "invalid code_hash format");
}
memcpy(code_hash, p, 32);

CHECK2(!qjs_bad_int_arg(ctx, argv[1], 1), ERROR_TEMP);
Expand Down Expand Up @@ -469,6 +468,9 @@ static JSValue syscall_spawn_cell(JSContext *ctx, JSValueConst this_value, int a
err = ckb_spawn_cell(code_hash, hash_type, offset, length, &spgs);
CHECK(err);
exit:
for (size_t i = 0; i < spgs_argc; i++) {
JS_FreeCString(ctx, spgs_argv[i]);
}
if (err != 0) {
return JS_EXCEPTION;
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/ckb_js_tests/test_data/syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function test_spawn() {
argv: ['-e', js_code],
inherited_fds: [fds[1]],
};
let pid = ckb.spawnCell(code_hash, ckb.SCRIPT_HASH_TYPE_TYPE, 0, 0, spawn_args);
let pid = ckb.spawnCell(code_hash.buffer, ckb.SCRIPT_HASH_TYPE_TYPE, 0, 0, spawn_args);
let txt = new Uint8Array(ckb.read(fds[0], 4));
console.assert(txt[0] == 0);
console.assert(txt[1] == 1);
Expand Down

0 comments on commit 15386f3

Please sign in to comment.