module.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package url
  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 = "url"
  8. var _ jsc.Module = (*Module)(nil)
  9. type Module struct {
  10. runtime *goja.Runtime
  11. classURL jsc.Class[*Module, *URL]
  12. classURLSearchParams jsc.Class[*Module, *URLSearchParams]
  13. classURLSearchParamsIterator jsc.Class[*Module, *jsc.Iterator[*Module, searchParam]]
  14. }
  15. func Require(runtime *goja.Runtime, module *goja.Object) {
  16. m := &Module{
  17. runtime: runtime,
  18. }
  19. m.classURL = createURL(m)
  20. m.classURLSearchParams = createURLSearchParams(m)
  21. m.classURLSearchParamsIterator = jsc.CreateIterator[*Module, searchParam](m)
  22. exports := module.Get("exports").(*goja.Object)
  23. exports.Set("URL", m.classURL.ToValue())
  24. exports.Set("URLSearchParams", m.classURLSearchParams.ToValue())
  25. }
  26. func Enable(runtime *goja.Runtime) {
  27. exports := require.Require(runtime, ModuleName).ToObject(runtime)
  28. runtime.Set("URL", exports.Get("URL"))
  29. runtime.Set("URLSearchParams", exports.Get("URLSearchParams"))
  30. }
  31. func (m *Module) Runtime() *goja.Runtime {
  32. return m.runtime
  33. }