github.com/dop251/goja_nodejs@v0.0.0-20240418154818-2aae10d4cbcf/url/module.go (about)

     1  package url
     2  
     3  import (
     4  	"github.com/dop251/goja"
     5  	"github.com/dop251/goja_nodejs/require"
     6  )
     7  
     8  const ModuleName = "url"
     9  
    10  type urlModule struct {
    11  	r *goja.Runtime
    12  
    13  	URLSearchParamsPrototype         *goja.Object
    14  	URLSearchParamsIteratorPrototype *goja.Object
    15  }
    16  
    17  func Require(runtime *goja.Runtime, module *goja.Object) {
    18  	exports := module.Get("exports").(*goja.Object)
    19  	m := &urlModule{
    20  		r: runtime,
    21  	}
    22  	exports.Set("URL", m.createURLConstructor())
    23  	exports.Set("URLSearchParams", m.createURLSearchParamsConstructor())
    24  	exports.Set("domainToASCII", m.domainToASCII)
    25  	exports.Set("domainToUnicode", m.domainToUnicode)
    26  }
    27  
    28  func Enable(runtime *goja.Runtime) {
    29  	m := require.Require(runtime, ModuleName).ToObject(runtime)
    30  	runtime.Set("URL", m.Get("URL"))
    31  	runtime.Set("URLSearchParams", m.Get("URLSearchParams"))
    32  }
    33  
    34  func init() {
    35  	require.RegisterCoreModule(ModuleName, Require)
    36  }