Skip to content

Commit

Permalink
Add support for registering import module callback functions in quick…
Browse files Browse the repository at this point in the history
…js module
  • Loading branch information
leizongmin committed Jul 11, 2024
1 parent e0fb354 commit b9f8de0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
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)
17 changes: 14 additions & 3 deletions quickjs/quickjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
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,17 @@ func (r Runtime) NewContext() *Context {
C.JS_AddIntrinsicOperators(ref)
C.JS_EnableBignumExt(ref, C.int(1))

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

if r.moduleLoader != nil {
C.JS_SetModuleLoaderFunc(r.ref, nil, (*C.JSModuleLoaderFunc)(C.InvokeModuleLoader), unsafe.Pointer(ctx))

Check failure on line 49 in quickjs/quickjs.go

View workflow job for this annotation

GitHub Actions / Build on Linux other platforms

could not determine kind of name for C.InvokeModuleLoader

Check failure on line 49 in quickjs/quickjs.go

View workflow job for this annotation

GitHub Actions / Build on ubuntu-20.04

could not determine kind of name for C.InvokeModuleLoader

Check failure on line 49 in quickjs/quickjs.go

View workflow job for this annotation

GitHub Actions / Build on macos-14

could not determine kind of name for C.InvokeModuleLoader
}

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 Expand Up @@ -400,7 +411,7 @@ func (v Value) SetByInt64(idx int64, val Value) {
}

func (v Value) SetByUint32(idx uint32, val Value) {
C.JS_SetPropertyUint32(v.ctx.ref, v.ref, C.uint32_t(idx), val.ref)
C.JS_SetPropertyUint32(v.ctx.ref, v.ref, C.uint32_t(idx))
}

func (v Value) Len() int64 { return v.Get("length").Int64() }
Expand Down

0 comments on commit b9f8de0

Please sign in to comment.