feat: init jsmod registry (by AI)

This commit is contained in:
AI Engineer 2026-05-30 14:11:56 +08:00
commit 056d8c6f6a
4 changed files with 59 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.geminiignore
.gemini
.ai/
env.json
env.yml
env.yaml
.log.meta.json

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module apigo.cc/go/jsmod
go 1.25.0

35
jsmod.go Normal file
View 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
View 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")
}
}