Skip to content

Commit

Permalink
Fix uncaught exception returning success (#29)
Browse files Browse the repository at this point in the history
* fix uncaught exception return success, see bellard/quickjs#232
* fix bug in eval_buf: bellard/quickjs#379
* cleanup test cases
  • Loading branch information
XuJiandong authored Jan 8, 2025
1 parent b81d44e commit fdc0233
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 133 deletions.
19 changes: 18 additions & 1 deletion src/qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,24 @@ static int eval_buf(JSContext *ctx, const void *buf, int buf_len, const char *fi
js_std_dump_error(ctx);
ret = -1;
} else {
ret = 0;
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);
ret = -2;
} else {
ret = 0;
}
} else {
ret = 0;
}
}
JS_FreeValue(ctx, val);
return ret;
Expand Down
54 changes: 27 additions & 27 deletions tests/ckb_js_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,58 @@ CKB_DEBUGGER ?= ckb-debugger
MAX_CYCLES ?= 2000000000
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
BIN_PATH = $(ROOT_DIR)/../../build/ckb-js-vm
FS_PACKER = node $(ROOT_DIR)/../../tools/fs-packer/dist/index.js

all: \
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

define compile_js_to_bc
$(CKB_DEBUGGER) --read-file $(1) --bin $(BIN_PATH) -- -c | \
awk -f $(ROOT_DIR)/../../tools/compile.awk | \
xxd -r -p > $(2)
endef

all: out \
cargo_test \
file_system \
syscall \
fs_bytecode \
simple_udt \
fs_mount \
module
fs_bytecode \
cell_target

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

cargo_test:
cargo test

spawn_caller:
clang-18 --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs \
-I ../../deps/ckb-c-stdlib/libc -I ../../deps/ckb-c-stdlib \
-nostdinc -nostdlib -o ../../build/bytecode/spawn_caller test_data/spawn_caller.c

module: spawn_caller
cargo run --bin module | ${CKB_DEBUGGER} --tx-file=- -s lock

build/bytecode/fs_modules.fs: test_data/fs_module/index.js test_data/fs_module/fib_module.js
cd test_data/fs_module && node ../../../../tools/fs-packer/dist/index.js pack ../../../../$@ index.js fib_module.js

fs_bytecode:
$(CKB_DEBUGGER) --read-file test_data/fs_module/index.js --bin $(BIN_PATH) -- -c | awk -f $(ROOT_DIR)/../../tools/compile.awk | xxd -r -p > ../../build/bytecode/index.bc
$(CKB_DEBUGGER) --read-file test_data/fs_module/fib_module.js --bin $(BIN_PATH) -- -c | awk -f $(ROOT_DIR)/../../tools/compile.awk | xxd -r -p > ../../build/bytecode/fib_module.bc
cd ../../build/bytecode && node ../../tools/fs-packer/dist/index.js pack ../../build/bytecode/fs_modules_bc.fs index.bc fib_module.bc
$(CKB_DEBUGGER) --max-cycles $(MAX_CYCLES) --read-file ../../build/bytecode/fs_modules_bc.fs --bin $(BIN_PATH) -- -f -r 2>&1 | fgrep 'Run result: 0'

file_system: build/bytecode/fs_modules.fs
cargo run --bin default_by_cell | $(CKB_DEBUGGER) -s lock --tx-file=- --read-file ../../$^ -- -f -r 2>&1 | fgrep 'Run result: 0'
$(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))
$(CKB_DEBUGGER) --max-cycles $(MAX_CYCLES) \
--read-file $(BYTECODE_DIR)/fs_modules_bc.fs \
--bin $(BIN_PATH) -- -f -r 2>&1 | \
fgrep 'Run result: 0'

syscall:
cargo run --bin syscall | $(CKB_DEBUGGER) --tx-file=- -s lock

fs_mount:
cd test_data/fs_module_mount && node ../../../../tools/fs-packer/dist/index.js pack ../../../../build/bytecode/fib_module.fs fib_module.js
cd test_data/fs_module_mount && node ../../../../tools/fs-packer/dist/index.js pack ../../../../build/bytecode/fib_module_mount.fs index.js init.js

cd test_data/fs_module_mount && $(FS_PACKER) pack ../../../../build/bytecode/fib_module.fs fib_module.js
cd test_data/fs_module_mount && $(FS_PACKER) pack ../../../../build/bytecode/fib_module_mount.fs index.js init.js
cargo run --bin module_mount | ${CKB_DEBUGGER} --tx-file=- -s lock

simple_udt:
cargo run --bin simple_udt | $(CKB_DEBUGGER) --tx-file=- --script-group-type type --cell-type output --cell-index 0

cell_target:
echo "blake2b(../../basic/test_loop.js) == 91bc1d1fc8c19289e72d15004dd506b76246bfead13391a5b35179cf7c8f37ef"
cargo run --bin default_by_cell | $(CKB_DEBUGGER) -s lock --tx-file=- -- -t 91bc1d1fc8c19289e72d15004dd506b76246bfead13391a5b35179cf7c8f37ef01

install-lua:
sudo apt install lua5.4

clean:
cargo clean
rm -rf ../../build/testdata_fs_modules.bin
rm -rf ../../build/bytecode
9 changes: 0 additions & 9 deletions tests/ckb_js_tests/src/bin/module.rs

This file was deleted.

75 changes: 0 additions & 75 deletions tests/ckb_js_tests/templates/module.json

This file was deleted.

10 changes: 0 additions & 10 deletions tests/ckb_js_tests/test_data/fs_module/fib_module.js

This file was deleted.

6 changes: 0 additions & 6 deletions tests/ckb_js_tests/test_data/fs_module/index.js

This file was deleted.

3 changes: 2 additions & 1 deletion tests/ckb_js_tests/test_data/fs_module_mount/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* example of JS module */
import * as module from './fib_module.js';
console.log(`fib(10)=${module.fib(10)}`);

console.log(`fib(10)=${module.fib(10)}!`);
2 changes: 2 additions & 0 deletions tests/ckb_js_tests/test_data/fs_module_mount/init.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import * as ckb from "ckb";
ckb.mount(2, ckb.SOURCE_CELL_DEP)

console.log("init.js");
1 change: 1 addition & 0 deletions tests/ckb_js_tests/test_data/syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function test_misc() {
function test_spawn() {
console.log('test_spawn ...');
const js_code = `
import * as ckb from 'ckb';
let fds = ckb.inherited_fds();
ckb.write(fds[0], new Uint8Array([0, 1, 2, 3]));
ckb.close(fds[0]);
Expand Down
8 changes: 6 additions & 2 deletions tools/fs-packer/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ function appendFileToStream(filePath, stream) {
content = _a.sent();
return [4 /*yield*/, writeToFile(content, stream)];
case 2:
_a.sent();
return [4 /*yield*/, writeToFile(Buffer.from([0]), stream)];
case 3:
_a.sent();
return [2 /*return*/];
}
Expand Down Expand Up @@ -145,11 +148,12 @@ function pack(files, outputStream) {
return [4 /*yield*/, appendIntegerToStream(offset, outputStream)];
case 3:
_f.sent();
length = Buffer.byteLength(name_1) + 1;
length = Buffer.byteLength(name_1);
return [4 /*yield*/, appendIntegerToStream(length, outputStream)];
case 4:
_f.sent();
offset += length;
offset += 1; // add trailing zero
return [4 /*yield*/, appendIntegerToStream(offset, outputStream)];
case 5:
_f.sent();
Expand All @@ -160,6 +164,7 @@ function pack(files, outputStream) {
case 7:
_f.sent();
offset += length;
offset += 1; // add trailing zero
_f.label = 8;
case 8:
_i++;
Expand Down Expand Up @@ -208,7 +213,6 @@ function unpack(directory, fileContent) {
var value = fileContent
.toString("utf8", position, position + length)
.replace(/\0$/, "");
position += length;
return value;
}
function copyToFile(directory, filename, offset, length) {
Expand Down
6 changes: 4 additions & 2 deletions tools/fs-packer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async function appendFileToStream(
): Promise<void> {
const content = await fs.readFile(filePath);
await writeToFile(content, stream);
await writeToFile(Buffer.from([0]), stream);
}

async function appendStringNullToStream(
Expand Down Expand Up @@ -72,13 +73,15 @@ async function pack(
for (const [name, filePath] of Object.entries(files)) {
console.log(`packing file ${filePath} to ${name}`);
await appendIntegerToStream(offset, outputStream);
length = Buffer.byteLength(name) + 1;
length = Buffer.byteLength(name);
await appendIntegerToStream(length, outputStream);
offset += length;
offset += 1; // add trailing zero
await appendIntegerToStream(offset, outputStream);
length = await getFileSize(filePath);
await appendIntegerToStream(length, outputStream);
offset += length;
offset += 1; // add trailing zero
}

// Write actual file data
Expand All @@ -105,7 +108,6 @@ async function unpack(directory: string, fileContent: Buffer): Promise<void> {
const value = fileContent
.toString("utf8", position, position + length)
.replace(/\0$/, "");
position += length;
return value;
}

Expand Down

0 comments on commit fdc0233

Please sign in to comment.