feat: init jsmod registry (by AI)
This commit is contained in:
commit
056d8c6f6a
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
.geminiignore
|
||||
.gemini
|
||||
.ai/
|
||||
env.json
|
||||
env.yml
|
||||
env.yaml
|
||||
.log.meta.json
|
||||
35
jsmod.go
Normal file
35
jsmod.go
Normal file
@ -0,0 +1,35 @@
|
||||
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
|
||||
}
|
||||
14
jsmod_test.go
Normal file
14
jsmod_test.go
Normal file
@ -0,0 +1,14 @@
|
||||
package jsmod_test
|
||||
|
||||
import (
|
||||
"apigo.cc/go/jsmod"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRegister(t *testing.T) {
|
||||
jsmod.Register("test", map[string]any{"foo": "bar"})
|
||||
mods := jsmod.GetModules()
|
||||
if mods["test"]["foo"] != "bar" {
|
||||
t.Error("Register failed")
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user