Skip to content

Commit

Permalink
Add test and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Jan 23, 2025
1 parent 0b48e11 commit 33e2f86
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
7 changes: 4 additions & 3 deletions docs/syscalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions src/ckb_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions tests/ckb_js_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 | \
Expand All @@ -25,14 +25,16 @@ all: out \

out:
@mkdir -p $(ROOT_DIR)/../../build/bytecode
@mkdir -p $(ROOT_DIR)/../../build/bytecode/subdir

cargo_test:
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 | \
Expand Down
2 changes: 2 additions & 0 deletions tests/ckb_js_tests/test_data/fs_module_mount/index.js
Original file line number Diff line number Diff line change
@@ -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)}!`);
3 changes: 2 additions & 1 deletion tests/ckb_js_tests/test_data/fs_module_mount/init.js
Original file line number Diff line number Diff line change
@@ -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");

0 comments on commit 33e2f86

Please sign in to comment.