From 6d84f95b406a532df02ba45f7ed744d432608a6f Mon Sep 17 00:00:00 2001 From: Lyndon Date: Wed, 8 Jan 2025 11:17:08 +0800 Subject: [PATCH] fix: uncaught exception return success see https://github.com/bellard/quickjs/issues/232 fix bug in eval_buf: https://github.com/bellard/quickjs/issues/379 --- src/qjs.c | 18 +++++++----------- tests/ckb_js_tests/Makefile | 9 ++++----- .../test_data/fs_module_mount/init.js | 2 ++ tools/fs-packer/dist/index.js | 8 ++++++-- tools/fs-packer/src/index.ts | 6 ++++-- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/qjs.c b/src/qjs.c index 24b8360..5e8bad5 100644 --- a/src/qjs.c +++ b/src/qjs.c @@ -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; @@ -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; } @@ -183,10 +186,6 @@ 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); @@ -194,9 +193,6 @@ int run_from_file_system_buf(JSContext *ctx, char *buf, size_t buf_size) { 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); diff --git a/tests/ckb_js_tests/Makefile b/tests/ckb_js_tests/Makefile index edc8c19..fb0bd57 100644 --- a/tests/ckb_js_tests/Makefile +++ b/tests/ckb_js_tests/Makefile @@ -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 \ @@ -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: @@ -33,4 +32,4 @@ install-lua: clean: cargo clean - rm -rf ../../build/testdata_fs_modules.bin + rm -rf ../../build/bytecode 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 4028f5c..eb56cd6 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,2 +1,4 @@ import * as ckb from "ckb"; ckb.mount(2, ckb.SOURCE_CELL_DEP) + +console.log("init.js"); \ No newline at end of file diff --git a/tools/fs-packer/dist/index.js b/tools/fs-packer/dist/index.js index 7fc0fc2..9dab6b2 100644 --- a/tools/fs-packer/dist/index.js +++ b/tools/fs-packer/dist/index.js @@ -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*/]; } @@ -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(); @@ -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++; @@ -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) { diff --git a/tools/fs-packer/src/index.ts b/tools/fs-packer/src/index.ts index 2250b90..8b1d9ae 100644 --- a/tools/fs-packer/src/index.ts +++ b/tools/fs-packer/src/index.ts @@ -39,6 +39,7 @@ async function appendFileToStream( ): Promise { const content = await fs.readFile(filePath); await writeToFile(content, stream); + await writeToFile(Buffer.from([0]), stream); } async function appendStringNullToStream( @@ -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 @@ -105,7 +108,6 @@ async function unpack(directory: string, fileContent: Buffer): Promise { const value = fileContent .toString("utf8", position, position + length) .replace(/\0$/, ""); - position += length; return value; }