@@ -102,6 +102,7 @@ func (g *Generator) Generate(ns *lang.Namespace) error {
102102
103103 // add lang import
104104 g .addImport ("github.com/glojurelang/glojure/pkg/lang" )
105+ g .addImport ("github.com/glojurelang/glojure/pkg/runtime" )
105106 g .addImport ("fmt" ) // for error formatting
106107 g .writef ("// reference fmt to avoid unused import error\n " )
107108 g .writef ("_ = fmt.Printf\n " )
@@ -183,18 +184,31 @@ func (g *Generator) Generate(ns *lang.Namespace) error {
183184
184185 // Now construct the complete init function
185186 var initBuf bytes.Buffer
187+ {
188+ // Reproduce the behavior of root-resource function
189+ rootResourceName := "/" + ns .Name ().String ()
190+ rootResourceName = strings .ReplaceAll (rootResourceName , "-" , "_" )
191+ rootResourceName = strings .ReplaceAll (rootResourceName , "." , "/" )
192+ initBuf .WriteString (`func init() {
193+ runtime.RegisterNSLoader(` + fmt .Sprintf ("%q" , rootResourceName ) + `, LoadNS)
194+ }
195+
196+ ` )
197+ }
186198 initBuf .WriteString (`func checkDerefVar (v *lang.Var) any {
187199 if v.IsMacro() {
188200 panic(lang.NewIllegalArgumentError(fmt.Sprintf("can't take value of macro: %v", v)))
189201 }
190202 return v.Get()
191203}
204+
192205` )
193206 initBuf .WriteString (`func checkArity(args []any, expected int) {
194207 if len(args) != expected {
195208 panic(lang.NewIllegalArgumentError("wrong number of arguments (" + fmt.Sprint(len(args)) + ")"))
196209 }
197210}
211+
198212` )
199213 initBuf .WriteString (fmt .Sprintf ("// LoadNS initializes the namespace %q\n " , ns .Name ().String ()))
200214 initBuf .WriteString ("func LoadNS() {\n " )
0 commit comments