diff --git a/gojs/go.mod b/gojs/go.mod deleted file mode 100644 index 3996f68..0000000 --- a/gojs/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module apigo.cc/go/js/gojs - -go 1.25.0 diff --git a/gojs/gojs.go b/gojs/gojs.go deleted file mode 100644 index 8d7f36f..0000000 --- a/gojs/gojs.go +++ /dev/null @@ -1,37 +0,0 @@ -package gojs - -import "sync" - -var ( - modules = make(map[string]map[string]any) - mu sync.RWMutex -) - -// Register registers a Go module with its exported functions and properties. -// It is thread-safe and designed to be called during init() functions. -// If the same module name is registered multiple times, the exports are merged. -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. -// It is thread-safe and safe to iterate during runtime. -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 // exports map is treated as read-only after initialization - } - return res -} diff --git a/gojs/gojs_test.go b/gojs/gojs_test.go deleted file mode 100644 index 37c5467..0000000 --- a/gojs/gojs_test.go +++ /dev/null @@ -1,59 +0,0 @@ -package gojs_test - -import ( - "sync" - "testing" - - "apigo.cc/go/js/gojs" -) - -func TestRegisterAndGet(t *testing.T) { - gojs.Register("db", map[string]any{ - "query": func() string { return "ok" }, - }) - - // Test merging - gojs.Register("db", map[string]any{ - "exec": func() int { return 1 }, - }) - - mods := gojs.GetModules() - dbMod, ok := mods["db"] - if !ok { - t.Fatal("expected module 'db' to be registered") - } - - if _, ok := dbMod["query"]; !ok { - t.Error("expected 'query' in 'db' module") - } - if _, ok := dbMod["exec"]; !ok { - t.Error("expected 'exec' in 'db' module") - } -} - -func TestConcurrentRegister(t *testing.T) { - var wg sync.WaitGroup - for i := 0; i < 100; i++ { - wg.Add(1) - go func(i int) { - defer wg.Done() - gojs.Register("concurrent_mod", map[string]any{ - "func": i, - }) - }(i) - } - wg.Wait() - - mods := gojs.GetModules() - if _, ok := mods["concurrent_mod"]; !ok { - t.Fatal("expected 'concurrent_mod' to be registered") - } -} - -func BenchmarkGetModules(b *testing.B) { - gojs.Register("bench_mod", map[string]any{"a": 1, "b": 2}) - b.ResetTimer() - for i := 0; i < b.N; i++ { - _ = gojs.GetModules() - } -}