From 33e2f8617f901656019c0ded65ea089b7ce2acf8 Mon Sep 17 00:00:00 2001 From: mohanson Date: Wed, 15 Jan 2025 16:04:26 +0800 Subject: [PATCH] Add test and doc --- docs/syscalls.md | 7 ++++--- src/ckb_module.c | 10 +++++++--- tests/ckb_js_tests/Makefile | 6 ++++-- tests/ckb_js_tests/test_data/fs_module_mount/index.js | 2 ++ tests/ckb_js_tests/test_data/fs_module_mount/init.js | 3 ++- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/docs/syscalls.md b/docs/syscalls.md index cfe126a..b3f8df6 100644 --- a/docs/syscalls.md +++ b/docs/syscalls.md @@ -77,11 +77,12 @@ and `index`. Example: ```js -ckb.mount(index, source) +ckb.mount(index, source, mount_point) ``` -Arguments: source (the source of the cell to load), index (the index of the cell -to load within all cells with source `source`) +Arguments: source (the source of the cell to load), index (the index of the cellt +to load within all cells with source `source`), mount_point (the file system will +be mounted at this path, usually "/") Return value(s): None diff --git a/src/ckb_module.c b/src/ckb_module.c index f75540f..abe0ce6 100644 --- a/src/ckb_module.c +++ b/src/ckb_module.c @@ -606,14 +606,18 @@ static JSValue syscall_load_block_extension(JSContext *ctx, JSValueConst this_va } static JSValue mount(JSContext *ctx, JSValueConst this_value, int argc, JSValueConst *argv) { - JSValue buf = syscall_load_cell_data(ctx, this_value, argc-1, argv); + JSValue buf = syscall_load_cell_data(ctx, this_value, 2, argv); if (JS_IsException(buf)) { return JS_EXCEPTION; } - const char* prefix = JS_ToCString(ctx, argv[2]); + const char *prefix = JS_ToCString(ctx, argv[2]); + if (prefix[0] != '/') { + ThrowError(ctx, QJS_ERROR_MOUNT, "mount_point should starts with /"); + return JS_EXCEPTION; + } size_t psize = 0; uint8_t *addr = JS_GetArrayBuffer(ctx, &psize, buf); - int err = ckb_load_fs(prefix, addr, psize); + int err = ckb_load_fs(prefix + 1, addr, psize); if (err != 0) { ThrowError(ctx, QJS_ERROR_MOUNT, "ckb.mount failed"); return JS_EXCEPTION; diff --git a/tests/ckb_js_tests/Makefile b/tests/ckb_js_tests/Makefile index c7da09c..31782b4 100644 --- a/tests/ckb_js_tests/Makefile +++ b/tests/ckb_js_tests/Makefile @@ -6,7 +6,7 @@ FS_PACKER = node $(ROOT_DIR)/../../tools/fs-packer/dist/index.js BYTECODE_DIR = $(ROOT_DIR)/../../build/bytecode JS_SOURCE_DIR = $(ROOT_DIR)/test_data/fs_module_mount -BYTECODE_FILES = $(BYTECODE_DIR)/index.bc $(BYTECODE_DIR)/fib_module.bc +BYTECODE_FILES = index.bc fib_module.bc subdir/fib_module.bc define compile_js_to_bc $(CKB_DEBUGGER) --read-file $(1) --bin $(BIN_PATH) -- -c | \ @@ -25,6 +25,7 @@ all: out \ out: @mkdir -p $(ROOT_DIR)/../../build/bytecode + @mkdir -p $(ROOT_DIR)/../../build/bytecode/subdir cargo_test: cargo test @@ -32,7 +33,8 @@ cargo_test: fs_bytecode: $(call compile_js_to_bc,$(JS_SOURCE_DIR)/index.js,$(BYTECODE_DIR)/index.bc) $(call compile_js_to_bc,$(JS_SOURCE_DIR)/fib_module.js,$(BYTECODE_DIR)/fib_module.bc) - cd $(BYTECODE_DIR) && $(FS_PACKER) pack fs_modules_bc.fs $(notdir $(BYTECODE_FILES)) + cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/subdir/fib_module.bc + cd $(BYTECODE_DIR) && $(FS_PACKER) pack fs_modules_bc.fs $(BYTECODE_FILES) $(CKB_DEBUGGER) --max-cycles $(MAX_CYCLES) \ --read-file $(BYTECODE_DIR)/fs_modules_bc.fs \ --bin $(BIN_PATH) -- -f -r 2>&1 | \ diff --git a/tests/ckb_js_tests/test_data/fs_module_mount/index.js b/tests/ckb_js_tests/test_data/fs_module_mount/index.js index e63bfa3..a3e4dcf 100644 --- a/tests/ckb_js_tests/test_data/fs_module_mount/index.js +++ b/tests/ckb_js_tests/test_data/fs_module_mount/index.js @@ -1,4 +1,6 @@ /* example of JS module */ import * as module from './fib_module.js'; +import * as subdir_module from './subdir/fib_module.js'; console.log(`fib(10)=${module.fib(10)}!`); +console.log(`fib(10)=${subdir_module.fib(10)}!`); diff --git a/tests/ckb_js_tests/test_data/fs_module_mount/init.js b/tests/ckb_js_tests/test_data/fs_module_mount/init.js index 883bd88..8c3f7b9 100644 --- a/tests/ckb_js_tests/test_data/fs_module_mount/init.js +++ b/tests/ckb_js_tests/test_data/fs_module_mount/init.js @@ -1,4 +1,5 @@ import * as ckb from "@ckb-js-std/bindings"; -ckb.mount(2, ckb.SOURCE_CELL_DEP, "") +ckb.mount(2, ckb.SOURCE_CELL_DEP, "/") +ckb.mount(2, ckb.SOURCE_CELL_DEP, "/subdir/") console.log("init.js");