Skip to content

Commit 393167e

Browse files
committed
Register pre-compiled namespaces with runtime
Signed-off-by: James Hamlin <jfhamlin@gmail.com>
1 parent 940d621 commit 393167e

File tree

30 files changed

+239
-132
lines changed

30 files changed

+239
-132
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
bin
22
doc/repl/glj.wasm
3+
.direnv
4+
5+
# useful to symlink in for context
6+
clojure

pkg/codegen/codegen.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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")

pkg/codegen/testdata/codegen/test/const_keyword/load.go.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ package const_DASH_keyword
55
import (
66
fmt "fmt"
77
lang "github.com/glojurelang/glojure/pkg/lang"
8+
runtime "github.com/glojurelang/glojure/pkg/runtime"
89
)
910

11+
func init() {
12+
runtime.RegisterNSLoader("codegen.test.const-keyword", LoadNS)
13+
}
14+
1015
func checkDerefVar(v *lang.Var) any {
1116
if v.IsMacro() {
1217
panic(lang.NewIllegalArgumentError(fmt.Sprintf("can't take value of macro: %v", v)))
1318
}
1419
return v.Get()
1520
}
21+
1622
func checkArity(args []any, expected int) {
1723
if len(args) != expected {
1824
panic(lang.NewIllegalArgumentError("wrong number of arguments (" + fmt.Sprint(len(args)) + ")"))

pkg/codegen/testdata/codegen/test/const_number/load.go.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ package const_DASH_number
55
import (
66
fmt "fmt"
77
lang "github.com/glojurelang/glojure/pkg/lang"
8+
runtime "github.com/glojurelang/glojure/pkg/runtime"
89
)
910

11+
func init() {
12+
runtime.RegisterNSLoader("codegen.test.const-number", LoadNS)
13+
}
14+
1015
func checkDerefVar(v *lang.Var) any {
1116
if v.IsMacro() {
1217
panic(lang.NewIllegalArgumentError(fmt.Sprintf("can't take value of macro: %v", v)))
1318
}
1419
return v.Get()
1520
}
21+
1622
func checkArity(args []any, expected int) {
1723
if len(args) != expected {
1824
panic(lang.NewIllegalArgumentError("wrong number of arguments (" + fmt.Sprint(len(args)) + ")"))

pkg/codegen/testdata/codegen/test/const_string/load.go.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ package const_DASH_string
55
import (
66
fmt "fmt"
77
lang "github.com/glojurelang/glojure/pkg/lang"
8+
runtime "github.com/glojurelang/glojure/pkg/runtime"
89
)
910

11+
func init() {
12+
runtime.RegisterNSLoader("codegen.test.const-string", LoadNS)
13+
}
14+
1015
func checkDerefVar(v *lang.Var) any {
1116
if v.IsMacro() {
1217
panic(lang.NewIllegalArgumentError(fmt.Sprintf("can't take value of macro: %v", v)))
1318
}
1419
return v.Get()
1520
}
21+
1622
func checkArity(args []any, expected int) {
1723
if len(args) != expected {
1824
panic(lang.NewIllegalArgumentError("wrong number of arguments (" + fmt.Sprint(len(args)) + ")"))

0 commit comments

Comments
 (0)