Skip to content

Commit

Permalink
fix: uncaught exception return success
Browse files Browse the repository at this point in the history
see
bellard/quickjs#232
fix bug in eval_buf:
bellard/quickjs#379
  • Loading branch information
XuJiandong committed Jan 8, 2025
1 parent 73ae885 commit 6d84f95
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
18 changes: 7 additions & 11 deletions src/qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
#include "qjs.h"

#define INIT_FILE_NAME "init.js"
#define INIT_FILE_NAME_BC "init.bc"
#define ENTRY_FILE_NAME "index.js"
#define ENTRY_FILE_NAME_BC "index.bc"

static void js_dump_obj(JSContext *ctx, JSValueConst val) {
const char *str;
Expand Down Expand Up @@ -167,8 +165,13 @@ static int eval_buf(JSContext *ctx, const void *buf, int buf_len, const char *fi
if (promise_state == JS_PROMISE_REJECTED) {
printf("JS_Eval return a rejected promise");
JSValue result = JS_PromiseResult(ctx, val);
printf("This promise returns JSValue with tag: %d", JS_VALUE_GET_TAG(result));
ret = 0;
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;
}
Expand All @@ -183,20 +186,13 @@ int run_from_file_system_buf(JSContext *ctx, char *buf, size_t buf_size) {

FSFile *init_file = NULL;
err = ckb_get_file(INIT_FILE_NAME, &init_file);
if (err != 0) {
ckb_get_file(INIT_FILE_NAME_BC, &init_file);
// skip error checking
}
if (init_file) {
err = eval_buf(ctx, init_file->content, init_file->size, INIT_FILE_NAME, 0);
CHECK(err);
}

FSFile *entry_file = NULL;
err = ckb_get_file(ENTRY_FILE_NAME, &entry_file);
if (err != 0) {
err = ckb_get_file(ENTRY_FILE_NAME_BC, &entry_file);
}
CHECK(err);
CHECK2(entry_file->size > 0, -1);
err = eval_buf(ctx, entry_file->content, entry_file->size, ENTRY_FILE_NAME, 0);
Expand Down
9 changes: 4 additions & 5 deletions tests/ckb_js_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 ../../../../tools/fs-packer/dist/index.js
all: \
cargo_test \
syscall \
Expand All @@ -16,9 +16,8 @@ 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:
Expand All @@ -33,4 +32,4 @@ install-lua:

clean:
cargo clean
rm -rf ../../build/testdata_fs_modules.bin
rm -rf ../../build/bytecode
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");
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 6d84f95

Please sign in to comment.