Skip to content

Commit

Permalink
feat: enhance evalScript and loadScript to support module flag
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Jan 13, 2025
1 parent 5c3966d commit 1654fa9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
31 changes: 23 additions & 8 deletions src/ckb_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,10 @@ static JSValue mount(JSContext *ctx, JSValueConst this_value, int argc, JSValueC
}
}

JSValue eval_script(JSContext *ctx, const char *str, int len) {
JSValue val = JS_Eval(ctx, str, len, "<evalScript>", JS_EVAL_TYPE_MODULE);
JSValue eval_script(JSContext *ctx, const char *str, int len, bool enable_module) {
int eval_flags = enable_module ? JS_EVAL_TYPE_MODULE : (JS_EVAL_FLAG_ASYNC | JS_EVAL_TYPE_GLOBAL);

JSValue val = JS_Eval(ctx, str, len, "<evalScript>", eval_flags);

if (JS_IsException(val)) {
JS_Throw(ctx, val);
Expand Down Expand Up @@ -614,17 +616,22 @@ JSValue eval_script(JSContext *ctx, const char *str, int len) {
static JSValue js_eval_script(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
const char *str;
size_t len;
JSValue val;
JSValue ret;
bool enable_module = false;

if (argc < 1) {
return JS_ThrowTypeError(ctx, "evalScript requires 1 argument");
return JS_ThrowTypeError(ctx, "evalScript requires at least 1 argument");
}

// Get enable_module flag if provided
if (argc > 1) {
enable_module = JS_ToBool(ctx, argv[1]);
}

str = JS_ToCStringLen(ctx, &len, argv[0]);
if (!str) return JS_EXCEPTION;

ret = eval_script(ctx, str, len);
ret = eval_script(ctx, str, len, enable_module);
JS_FreeCString(ctx, str);
return ret;
}
Expand Down Expand Up @@ -676,12 +683,20 @@ static JSValue js_load_file(JSContext *ctx, JSValueConst _this_val, int argc, JS
}

static JSValue js_load_script(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
bool enable_module = false;

// Get enable_module flag if provided
if (argc > 1) {
enable_module = JS_ToBool(ctx, argv[1]);
}

JSValue ret = js_load_file(ctx, this_val, argc, argv);
if (JS_IsException(ret)) return ret;

size_t len = 0;
const char *str = JS_ToCStringLen(ctx, &len, ret);
JS_FreeValue(ctx, ret);
ret = eval_script(ctx, str, len);
ret = eval_script(ctx, str, len, enable_module);
JS_FreeCString(ctx, str);
return ret;
}
Expand Down Expand Up @@ -726,8 +741,8 @@ static const JSCFunctionListEntry js_ckb_funcs[] = {
JS_CFUNC_DEF("process_id", 0, syscall_process_id),
JS_CFUNC_DEF("load_block_extension", 3, syscall_load_block_extension),
JS_CFUNC_DEF("mount", 2, mount),
JS_CFUNC_DEF("evalScript", 1, js_eval_script),
JS_CFUNC_DEF("loadScript", 1, js_load_script),
JS_CFUNC_DEF("evalScript", 2, js_eval_script),
JS_CFUNC_DEF("loadScript", 2, js_load_script),
JS_CFUNC_DEF("loadFile", 1, js_load_file),
JS_CFUNC_DEF("parseExtJSON", 1, js_parse_ext_json),

Expand Down
2 changes: 1 addition & 1 deletion src/ckb_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ int read_local_file(char *buf, int size);
int load_cell_code_info_explicit(size_t *buf_size, size_t *index, const uint8_t *code_hash, uint8_t hash_type);
int load_cell_code_info(size_t *buf_size, size_t *index, bool *use_filesystem);
int load_cell_code(size_t buf_size, size_t index, uint8_t *buf);
JSValue eval_script(JSContext *ctx, const char *str, int len);
JSValue eval_script(JSContext *ctx, const char *str, int len, bool enable_module);

#endif // _CKB_MODULE_H_
37 changes: 33 additions & 4 deletions tests/module/test_ckb.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,55 @@ function test_eval_script() {
import * as ckb from 'ckb';
globalThis.test_eval_script = 100;
`;
ckb.evalScript(script);
console.assert(globalThis.test_eval_script === 100, "evalScript failed");
ckb.evalScript(script, true);
console.assert(globalThis.test_eval_script === 100, "test_eval_script failed");
}

function test_eval_script_no_module() {
const script = `
let a = 100;
let b = 200;
a + b
`;
let result = ckb.evalScript(script);
console.assert(result === 300, "test_eval_script_no_module failed");
}

function test_eval_script_no_module_with_exception() {
const script = `
let a = 100;
let b = 200;
abcdefg.abc = 100;
a + b
`;
let success = false;
try {
ckb.evalScript(script);
} catch (e) {
success = true;
}
console.assert(success, "test_eval_script_no_module failed");
}


function test_eval_script_with_exception() {
const script = `
import * as ckb from 'ckb';
abcdef.init();
`;
let success = false;
try {
ckb.evalScript(script);
ckb.evalScript(script, true);
} catch (e) {
success = true;
}
console.assert(success, "evalScript with exception failed");
console.assert(success, "test_eval_script_with_exception with exception failed");
}

console.log("test_ckb.js...");
test_eval_script();
test_eval_script_with_exception();
test_eval_script_no_module();
test_eval_script_no_module_with_exception();
console.log("test_ckb.js ok");

0 comments on commit 1654fa9

Please sign in to comment.