Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidModule module is invalid ErrInvalidModule = errors.New("invalid module") // ErrIllegalModuleName module name is illegal ErrIllegalModuleName = errors.New("illegal module name") // ErrNotFoundModule not found module ErrNotFoundModule = errors.New("cannot found module") )
Functions ¶
func Register ¶
Register registers a Module with the given name and implementation. If the module is not a Global module, the name will be prefixed with "ski/". The registered modules can later be imported in JavaScript code by name.
Example:
// Register a regular module that must be imported as "ski/mymodule"
modules.Register("mymodule", new(MyModule))
// Register a global module that can be lazily instantiated
modules.Register("sleep", modules.Global{
"sleep": modules.ModuleFunc(func(call sobek.FunctionCall, rt *sobek.Runtime) sobek.Value {
time.Sleep(time.Duration(call.Argument(0).ToInteger()))
return sobek.Undefined()
}),
})
Types ¶
type FileLoader ¶
FileLoader is a type alias for a function that returns the contents of the referenced file.
func DefaultFileLoader ¶
DefaultFileLoader the default file loader. Supports file and HTTP scheme loading.
type Global ¶
Global represents a collection of related modules grouped under a namespace. It maps module names to their Module implementations. Global modules are instantiated lazily when first accessed through the global object.
type Loader ¶
type Loader interface {
// CompileModule compile module from source string (cjs/esm).
CompileModule(name, source string) (sobek.CyclicModuleRecord, error)
// ResolveModule resolve the module returns the sobek.ModuleRecord.
ResolveModule(any, string) (sobek.ModuleRecord, error)
// EnableRequire enable the global function require to the sobek.Runtime.
EnableRequire(*sobek.Runtime) Loader
// EnableImportModuleDynamically sobek runtime SetImportModuleDynamically
EnableImportModuleDynamically(*sobek.Runtime) Loader
// EnableImportMeta sobek runtime SetFinalImportMeta
EnableImportMeta(*sobek.Runtime) Loader
// InitGlobal instantiates global objects for the runtime. It creates a proxy around the global object
// to lazily load global modules when they are first accessed.
//
// This allows for automatic loading of global modules like fetch, TextEncoder, etc.
// when they are referenced in code.
InitGlobal(*sobek.Runtime) Loader
// SetFileLoader set the FileLoader.
SetFileLoader(fl FileLoader)
}
Loader the js module loader.
type Module ¶
Module is the interface that must be implemented by JavaScript modules. It defines how a module is instantiated and made available to the JavaScript runtime.
Example implementation:
func init() {
// register a new module named "process"
modules.Register("process", new(Process))
}
type Process struct{}
func (Process) Instantiate(rt *sobek.Runtime) (sobek.Value, error) {
environ := os.Environ()
env := make(map[string]string, len(environ))
for _, kv := range environ {
k, v, _ := strings.Cut(kv, "=")
env[k] = v
}
ret := rt.NewObject()
_ = ret.Set("env", env)
return ret, nil
}
type ModuleFunc ¶
ModuleFunc type is an adapter to allow the use of ordinary functions as Module.
func (ModuleFunc) Instantiate ¶
type Option ¶
type Option func(*loader)
Option the new Loader options.
func WithFileLoader ¶
func WithFileLoader(fl FileLoader) Option
WithFileLoader the file loader of module loader.