36 lines
755 B
Go
36 lines
755 B
Go
package jsmod
|
|
|
|
import "sync"
|
|
|
|
var (
|
|
modules = make(map[string]map[string]any)
|
|
mu sync.RWMutex
|
|
)
|
|
|
|
// Register registers a Go module with its exported functions and properties.
|
|
// These modules will be accessible within the JS environment.
|
|
func Register(name string, exports map[string]any) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
|
|
if modules[name] == nil {
|
|
modules[name] = make(map[string]any, len(exports))
|
|
}
|
|
|
|
for k, v := range exports {
|
|
modules[name][k] = v
|
|
}
|
|
}
|
|
|
|
// GetModules returns all registered modules for the JS engine to inject.
|
|
func GetModules() map[string]map[string]any {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
|
|
res := make(map[string]map[string]any, len(modules))
|
|
for name, exports := range modules {
|
|
res[name] = exports
|
|
}
|
|
return res
|
|
}
|