Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for registering import module callback functions in quickjs module #146

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/jsbuiltin/src/00_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,25 @@ return module;
require.cache = {};
jssh.require = require;
jssh.requiremodule = requiremodule;

const importModuleCallbacks = [];

const registerImportModuleCallback = (callback) => {
if (typeof callback !== "function") {
throw new TypeError("callback must be a function");
}
importModuleCallbacks.push(callback);
};

const callImportModuleCallbacks = (name, dir) => {
for (const callback of importModuleCallbacks) {
const result = callback(name, dir);
if (result) {
return result;
}
}
};

jssh.registerImportModuleCallback = registerImportModuleCallback;
jssh.callImportModuleCallbacks = callImportModuleCallbacks;
}
11 changes: 10 additions & 1 deletion internal/jsexecutor/jsexecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type JSFunction = func(ctx *JSContext, this JSValue, args []JSValue) JSValue //

// NewJSRuntime 创建新的JSRuntime实例
func NewJSRuntime() JSRuntime {
return quickjs.NewRuntime()
runtime := quickjs.NewRuntime()
runtime.SetModuleLoader(moduleLoader)
return runtime
}

// IsGoFunction 判断是否为Go的函数类型
Expand Down Expand Up @@ -238,3 +240,10 @@ func anySliceToJSValue(ctx *quickjs.Context, v reflect.Value, vt reflect.Type) q
}
return arr
}

// RegisterModuleLoader 注册模块加载器
func RegisterModuleLoader(loader func(ctx *JSContext, moduleName string) (string, error)) {
moduleLoader = loader
}

var moduleLoader func(ctx *JSContext, moduleName string) (string, error)
24 changes: 22 additions & 2 deletions quickjs/quickjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import "C"

type Runtime struct {
ref *C.JSRuntime
ref *C.JSRuntime
moduleLoader func(ctx *Context, moduleName string) (string, error)
}

func NewRuntime() Runtime {
Expand All @@ -42,7 +43,26 @@
C.JS_AddIntrinsicOperators(ref)
C.JS_EnableBignumExt(ref, C.int(1))

return &Context{ref: ref}
ctx := &Context{ref: ref}

if r.moduleLoader != nil {
// typedef JSModuleDef *JSModuleLoaderFunc(JSContext *ctx, const char *module_name, void *opaque);
moduleLoaderFunc := func(_ *C.JSContext, cModuleName *C.char, opaque unsafe.Pointer) *C.JSModuleDef {
moduleName := C.GoString(cModuleName)
source, err := r.moduleLoader(ctx, moduleName)
if err != nil {
panic(err)
}
return C.JS_Eval(ctx.ref, C.CString(source), C.size_t(len(source)), cModuleName, C.JS_EVAL_TYPE_MODULE)

Check failure on line 56 in quickjs/quickjs.go

View workflow job for this annotation

GitHub Actions / Build on Linux other platforms

cannot use func() _Ctype_struct_JSValue {…}() (value of type _Ctype_struct_JSValue) as *_Ctype_struct_JSModuleDef value in return statement

Check failure on line 56 in quickjs/quickjs.go

View workflow job for this annotation

GitHub Actions / Build on macos-14

cannot use func() _Ctype_struct_JSValue {…}() (value of type _Ctype_struct_JSValue) as *_Ctype_struct_JSModuleDef value in return statement
}
C.JS_SetModuleLoaderFunc(r.ref, nil, *C.JSModuleLoaderFunc(moduleLoaderFunc), unsafe.Pointer(ctx))

Check failure on line 58 in quickjs/quickjs.go

View workflow job for this annotation

GitHub Actions / Build on Linux other platforms

cannot convert moduleLoaderFunc (variable of type func(_ *_Ctype_struct_JSContext, cModuleName *_Ctype_char, opaque unsafe.Pointer) *_Ctype_struct_JSModuleDef) to type _Ctype_JSModuleLoaderFunc

Check failure on line 58 in quickjs/quickjs.go

View workflow job for this annotation

GitHub Actions / Build on macos-14

cannot convert moduleLoaderFunc (variable of type func(_ *_Ctype_struct_JSContext, cModuleName *_Ctype_char, opaque unsafe.Pointer) *_Ctype_struct_JSModuleDef) to type _Ctype_JSModuleLoaderFunc
}

return ctx
}

func (r *Runtime) SetModuleLoader(loader func(ctx *Context, moduleName string) (string, error)) {
r.moduleLoader = loader
}

func (r Runtime) ExecutePendingJob() (Context, error) {
Expand Down
Loading