1
0

module.go 859 B

1234567891011121314151617181920212223242526272829303132333435
  1. package boxctx
  2. import (
  3. "github.com/sagernet/sing-box/script/jsc"
  4. "github.com/sagernet/sing-box/script/modules/require"
  5. "github.com/dop251/goja"
  6. )
  7. const ModuleName = "context"
  8. type Module struct {
  9. runtime *goja.Runtime
  10. classContext jsc.Class[*Module, *Context]
  11. }
  12. func Require(runtime *goja.Runtime, module *goja.Object) {
  13. m := &Module{
  14. runtime: runtime,
  15. }
  16. m.classContext = createContext(m)
  17. exports := module.Get("exports").(*goja.Object)
  18. exports.Set("Context", m.classContext.ToValue())
  19. }
  20. func Enable(runtime *goja.Runtime, context *Context) {
  21. exports := require.Require(runtime, ModuleName).ToObject(runtime)
  22. classContext := jsc.GetClass[*Module, *Context](runtime, exports, "Context")
  23. context.class = classContext
  24. runtime.Set("context", classContext.New(context))
  25. }
  26. func (m *Module) Runtime() *goja.Runtime {
  27. return m.runtime
  28. }