Skip to content

Commit

Permalink
Follow xjd's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Jan 23, 2025
1 parent 2874526 commit 7eb0e8c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
40 changes: 19 additions & 21 deletions libc/src/ckb_cell_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ int get_file(const CellFileSystem *fs, const char *filename, FSFile **f) {
CellFileSystem *cfs = (CellFileSystem *)fs;
CellFileSystemNode *node = cfs->current;
while (node != NULL) {
size_t filename_len = strlen(filename);
size_t prefix_len = strlen(node->prefix) - 1;
if (prefix_len > filename_len) {
if (strncmp(node->prefix + 1, filename, strlen(node->prefix) - 1) != 0) {
if (cfs->next == NULL) {
break;
}
cfs = cfs->next;
node = cfs->current;
continue;
}
const char *filename_without_prefix = filename + prefix_len;
const char *basename = filename + strlen(node->prefix) - 1;
for (uint32_t i = 0; i < node->count; i++) {
FSEntry entry = node->files[i];
if (strcmp(filename_without_prefix, node->start + entry.filename.offset) == 0) {
if (strcmp(basename, node->start + entry.filename.offset) == 0) {
// TODO: check the memory addresses are legal
file->filename = filename;
file->size = entry.content.length;
Expand Down Expand Up @@ -64,22 +62,22 @@ prefix_normalize("foo/bar") == "/foo/bar/"
prefix_normalize("foo/bar/") == "/foo/bar/"
*/
char *prefix_normalize(const char *prefix) {
int len = strlen(prefix);
if (len == 0) {
return "/";
}
char *buf = malloc(len + 3);
buf[0] = '/';
memcpy(buf+1, prefix, len);
buf[len+1] = '/';
buf[len+2] = 0;
if (prefix[len - 1] == '/') {
buf[len+1] = 0;
}
if (prefix[0] == '/') {
buf++;
}
return buf;
int len = strlen(prefix);
if (len == 0) {
return "/";
}
char *buf = malloc(len + 3);
buf[0] = '/';
memcpy(buf + 1, prefix, len);
buf[len + 1] = '/';
buf[len + 2] = 0;
if (prefix[len - 1] == '/') {
buf[len + 1] = 0;
}
if (prefix[0] == '/') {
buf++;
}
return buf;
}

int load_fs(CellFileSystem **fs, const char *prefix, void *buf, uint64_t buflen) {
Expand Down
12 changes: 9 additions & 3 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 = index.bc fib_module.bc subdir/fib_module.bc
BYTECODE_FILES = index.bc fib_module.bc a/fib_module.bc b/fib_module.bc c/fib_module.bc d/fib_module.bc

define compile_js_to_bc
$(CKB_DEBUGGER) --read-file $(1) --bin $(BIN_PATH) -- -c | \
Expand All @@ -25,15 +25,21 @@ all: out \

out:
@mkdir -p $(ROOT_DIR)/../../build/bytecode
@mkdir -p $(ROOT_DIR)/../../build/bytecode/subdir
@mkdir -p $(ROOT_DIR)/../../build/bytecode/a
@mkdir -p $(ROOT_DIR)/../../build/bytecode/b
@mkdir -p $(ROOT_DIR)/../../build/bytecode/c
@mkdir -p $(ROOT_DIR)/../../build/bytecode/d

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)
cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/subdir/fib_module.bc
cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/a/fib_module.bc
cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/b/fib_module.bc
cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/c/fib_module.bc
cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/d/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 \
Expand Down
10 changes: 8 additions & 2 deletions tests/ckb_js_tests/test_data/fs_module_mount/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/* example of JS module */
import * as module from './fib_module.js';
import * as subdir_module from './subdir/fib_module.js';
import * as module_a from './a/fib_module.js';
import * as module_b from './b/fib_module.js';
import * as module_c from './c/fib_module.js';
import * as module_d from './d/fib_module.js';

console.log(`fib(10)=${module.fib(10)}!`);
console.log(`fib(10)=${subdir_module.fib(10)}!`);
console.log(`fib(10)=${module_a.fib(10)}!`);
console.log(`fib(10)=${module_b.fib(10)}!`);
console.log(`fib(10)=${module_c.fib(10)}!`);
console.log(`fib(10)=${module_d.fib(10)}!`);
6 changes: 5 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,5 +1,9 @@
import * as ckb from "@ckb-js-std/bindings";

ckb.mount(2, ckb.SOURCE_CELL_DEP, "/")
ckb.mount(2, ckb.SOURCE_CELL_DEP, "/subdir/")
ckb.mount(2, ckb.SOURCE_CELL_DEP, "a")
ckb.mount(2, ckb.SOURCE_CELL_DEP, "/b")
ckb.mount(2, ckb.SOURCE_CELL_DEP, "c/")
ckb.mount(2, ckb.SOURCE_CELL_DEP, "/d/")

console.log("init.js");

0 comments on commit 7eb0e8c

Please sign in to comment.