package cache import ( "github.com/dop251/goja" "github.com/dop251/goja_nodejs/require" ) const ModuleName = "cache" type KVCache struct { data map[string]goja.Value } func (c *KVCache) get(fc goja.FunctionCall) goja.Value { key := fc.Argument(0).String() if val, ok := c.data[key]; ok { return val } return goja.Undefined() } func (c *KVCache) set(fc goja.FunctionCall) goja.Value { key := fc.Argument(0).String() val := fc.Argument(1) c.data[key] = val return goja.Undefined() } func Require(rt *goja.Runtime, module *goja.Object) { cache := &KVCache{ data: map[string]goja.Value{}, } o := module.Get("exports").(*goja.Object) o.Set("get", cache.get) o.Set("set", cache.set) } func Enable(rt *goja.Runtime) { rt.Set("cache", require.Require(rt, ModuleName)) } func init() { require.RegisterCoreModule(ModuleName, Require) }