WIP trmnl BYOS
1package cache
2
3import (
4 "github.com/dop251/goja"
5 "github.com/dop251/goja_nodejs/require"
6)
7
8const ModuleName = "cache"
9
10type KVCache struct {
11 data map[string]goja.Value
12}
13
14func (c *KVCache) get(fc goja.FunctionCall) goja.Value {
15 key := fc.Argument(0).String()
16
17 if val, ok := c.data[key]; ok {
18 return val
19 }
20
21 return goja.Undefined()
22}
23
24func (c *KVCache) set(fc goja.FunctionCall) goja.Value {
25 key := fc.Argument(0).String()
26 val := fc.Argument(1)
27
28 c.data[key] = val
29
30 return goja.Undefined()
31}
32
33func Require(rt *goja.Runtime, module *goja.Object) {
34 cache := &KVCache{
35 data: map[string]goja.Value{},
36 }
37
38 o := module.Get("exports").(*goja.Object)
39 o.Set("get", cache.get)
40 o.Set("set", cache.set)
41}
42
43func Enable(rt *goja.Runtime) {
44 rt.Set("cache", require.Require(rt, ModuleName))
45}
46
47func init() {
48 require.RegisterCoreModule(ModuleName, Require)
49}