Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ generate:
go generate ./...

aot: gocmd $(STDLIB-TARGETS)
GLOJURE_USE_AOT=false \
GLOJURE_STDLIB_PATH=./pkg/stdlib \
GLJ_USE_AOT=false \
GLJPATH=./pkg/stdlib \
$(GO-CMD) run -tags glj_no_aot_stdlib ./cmd/glj \
<<<"(map compile '[$(AOT-NAMESPACES)])"

Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_darwin_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3999,6 +3999,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/runtime.*Fn", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Fn)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/runtime.*Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.GetLoadPath", github_com_glojurelang_glojure_pkg_runtime.GetLoadPath)
_register("github.com/glojurelang/glojure/pkg/runtime.GetNSLoader", github_com_glojurelang_glojure_pkg_runtime.GetNSLoader)
_register("github.com/glojurelang/glojure/pkg/runtime.GetUseAOT", github_com_glojurelang_glojure_pkg_runtime.GetUseAOT)
_register("github.com/glojurelang/glojure/pkg/runtime.NewEnvironment", github_com_glojurelang_glojure_pkg_runtime.NewEnvironment)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_darwin_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3999,6 +3999,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/runtime.*Fn", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Fn)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/runtime.*Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.GetLoadPath", github_com_glojurelang_glojure_pkg_runtime.GetLoadPath)
_register("github.com/glojurelang/glojure/pkg/runtime.GetNSLoader", github_com_glojurelang_glojure_pkg_runtime.GetNSLoader)
_register("github.com/glojurelang/glojure/pkg/runtime.GetUseAOT", github_com_glojurelang_glojure_pkg_runtime.GetUseAOT)
_register("github.com/glojurelang/glojure/pkg/runtime.NewEnvironment", github_com_glojurelang_glojure_pkg_runtime.NewEnvironment)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_js_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3999,6 +3999,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/runtime.*Fn", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Fn)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/runtime.*Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.GetLoadPath", github_com_glojurelang_glojure_pkg_runtime.GetLoadPath)
_register("github.com/glojurelang/glojure/pkg/runtime.GetNSLoader", github_com_glojurelang_glojure_pkg_runtime.GetNSLoader)
_register("github.com/glojurelang/glojure/pkg/runtime.GetUseAOT", github_com_glojurelang_glojure_pkg_runtime.GetUseAOT)
_register("github.com/glojurelang/glojure/pkg/runtime.NewEnvironment", github_com_glojurelang_glojure_pkg_runtime.NewEnvironment)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_linux_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3999,6 +3999,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/runtime.*Fn", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Fn)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/runtime.*Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.GetLoadPath", github_com_glojurelang_glojure_pkg_runtime.GetLoadPath)
_register("github.com/glojurelang/glojure/pkg/runtime.GetNSLoader", github_com_glojurelang_glojure_pkg_runtime.GetNSLoader)
_register("github.com/glojurelang/glojure/pkg/runtime.GetUseAOT", github_com_glojurelang_glojure_pkg_runtime.GetUseAOT)
_register("github.com/glojurelang/glojure/pkg/runtime.NewEnvironment", github_com_glojurelang_glojure_pkg_runtime.NewEnvironment)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_linux_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3999,6 +3999,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/runtime.*Fn", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Fn)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/runtime.*Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.GetLoadPath", github_com_glojurelang_glojure_pkg_runtime.GetLoadPath)
_register("github.com/glojurelang/glojure/pkg/runtime.GetNSLoader", github_com_glojurelang_glojure_pkg_runtime.GetNSLoader)
_register("github.com/glojurelang/glojure/pkg/runtime.GetUseAOT", github_com_glojurelang_glojure_pkg_runtime.GetUseAOT)
_register("github.com/glojurelang/glojure/pkg/runtime.NewEnvironment", github_com_glojurelang_glojure_pkg_runtime.NewEnvironment)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_windows_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3999,6 +3999,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/runtime.*Fn", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Fn)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/runtime.*Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.GetLoadPath", github_com_glojurelang_glojure_pkg_runtime.GetLoadPath)
_register("github.com/glojurelang/glojure/pkg/runtime.GetNSLoader", github_com_glojurelang_glojure_pkg_runtime.GetNSLoader)
_register("github.com/glojurelang/glojure/pkg/runtime.GetUseAOT", github_com_glojurelang_glojure_pkg_runtime.GetUseAOT)
_register("github.com/glojurelang/glojure/pkg/runtime.NewEnvironment", github_com_glojurelang_glojure_pkg_runtime.NewEnvironment)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_windows_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3999,6 +3999,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/runtime.*Fn", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Fn)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/runtime.*Generator", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_runtime.Generator)(nil)))
_register("github.com/glojurelang/glojure/pkg/runtime.GetLoadPath", github_com_glojurelang_glojure_pkg_runtime.GetLoadPath)
_register("github.com/glojurelang/glojure/pkg/runtime.GetNSLoader", github_com_glojurelang_glojure_pkg_runtime.GetNSLoader)
_register("github.com/glojurelang/glojure/pkg/runtime.GetUseAOT", github_com_glojurelang_glojure_pkg_runtime.GetUseAOT)
_register("github.com/glojurelang/glojure/pkg/runtime.NewEnvironment", github_com_glojurelang_glojure_pkg_runtime.NewEnvironment)
Expand Down
2 changes: 0 additions & 2 deletions pkg/gljmain/gljmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ For more information, visit: https://github.com/glojurelang/glojure
}

func Main(args []string) {
runtime.AddLoadPath(os.DirFS("."))

if len(args) == 0 {
repl.Start()
} else if args[0] == "--version" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

func TestCodegen(t *testing.T) {
if runtime.GetUseAOT() {
t.Skip("Skipping codegen tests with AOT enabled; run with GLOJURE_USE_AOT=0")
t.Skip("Skipping codegen tests with AOT enabled; run with GLJ_USE_AOT=0")
}

var testFiles []string
Expand Down
11 changes: 11 additions & 0 deletions pkg/runtime/envinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ func NewEnvironment(opts ...EvalOption) lang.Environment {
versionVar.BindRoot(parseVersion(Version))
}

// Set the load path
loadPathVar := core.FindInternedVar(lang.NewSymbol("*load-path*"))
if loadPathVar != nil {
pathStrings := GetLoadPath()
pathValues := make([]any, len(pathStrings))
for i, path := range pathStrings {
pathValues[i] = path
}
loadPathVar.BindRoot(lang.NewVector(pathValues...))
}

lang.InternVar(core, lang.NewSymbol("load-file"), func(filename string) any {
buf, err := os.ReadFile(filename)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func newEnvironment(ctx context.Context, stdout, stderr io.Writer) *environment
"print-dup",
"read-eval",
"glojure-version",
"load-path",
} {
coreNS.InternWithValue(lang.NewSymbol("*"+dyn+"*"), nil, true).SetDynamic()
}
Expand Down
69 changes: 69 additions & 0 deletions pkg/runtime/loadpath_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package runtime_test

import (
"os"
"strings"
"testing"

"github.com/glojurelang/glojure/pkg/runtime"
)

func TestGetLoadPath(t *testing.T) {
// Test that GetLoadPath returns a valid slice of strings
paths := runtime.GetLoadPath()
if len(paths) == 0 {
t.Fatal("GetLoadPath returned empty slice")
}

// Should contain at least the current directory and stdlib
foundCurrent := false
foundStdlib := false
for _, path := range paths {
if path == "." {
foundCurrent = true
}
if path == "<StdLib>" || strings.Contains(path, "stdlib") {
foundStdlib = true
}
}

if !foundCurrent {
t.Error("Load path should contain current directory '.'")
}
if !foundStdlib {
t.Error("Load path should contain stdlib")
}
}

func TestGLJPATHEnvironmentVariable(t *testing.T) {
// This test would need to be run in a subprocess to avoid affecting
// the global loadPath, but for now we'll just verify the structure
paths := runtime.GetLoadPath()

// Verify all paths are strings
for i, path := range paths {
if path == "" {
t.Errorf("Path at index %d is empty", i)
}
}

// The expected order is:
// [...GLJPATH dirs, current dir, <StdLib>]
if len(paths) > 0 {
// Last element should always be <StdLib>
last := paths[len(paths)-1]
if last != "<StdLib>" {
t.Errorf("Last path should be '<StdLib>', got: %s", last)
}

// If no GLJPATH, order should be [current dir, <StdLib>]
if os.Getenv("GLJPATH") == "" {
if len(paths) >= 2 {
secondToLast := paths[len(paths)-2]
if secondToLast != "." {
t.Errorf("Second to last path should be '.', got: %s", secondToLast)
}
}
}
}
}
53 changes: 47 additions & 6 deletions pkg/runtime/rtcompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,32 @@ var (

useAot = func() bool {
// default to true
gua := strings.ToLower(os.Getenv("GLOJURE_USE_AOT"))
gua := strings.ToLower(os.Getenv("GLJ_USE_AOT"))
return !(gua == "0" || gua == "false" || gua == "no" || gua == "off")
}()
)

func init() {
stdlibPath := os.Getenv("GLOJURE_STDLIB_PATH")
if stdlibPath != "" {
AddLoadPath(os.DirFS(stdlibPath))
} else {
AddLoadPath(stdlib.StdLib)
// Start with current directory
AddLoadPath(os.DirFS("."))

// Add GLJPATH directories if set
gljPath := os.Getenv("GLJPATH")
if gljPath != "" {
paths := strings.Split(gljPath, ":")
for i := len(paths) - 1; i >= 0; i-- {
path := paths[i]
if path != "" {
// Prepend each path so they appear in the correct order
loadPathLock.Lock()
loadPath = append([]fs.FS{os.DirFS(path)}, loadPath...)
loadPathLock.Unlock()
}
}
}

// Always add <StdLib> at the end
AddLoadPath(stdlib.StdLib)
}

func GetUseAOT() bool {
Expand All @@ -55,6 +69,28 @@ func AddLoadPath(fs fs.FS) {
loadPath = append(loadPath, fs)
}

// GetLoadPath returns the current load path as a slice of strings.
// This is used to expose the load path to Clojure code via the *load-path* dynamic variable.
func GetLoadPath() []string {
loadPathLock.Lock()
defer loadPathLock.Unlock()

result := make([]string, len(loadPath))
for i, fs := range loadPath {
// Convert fs.FS to string representation
// os.DirFS returns a string type under the hood
if reflect.TypeOf(fs).Kind() == reflect.String {
result[i] = fmt.Sprintf("%s", fs)
} else if fs == stdlib.StdLib {
// For embedded filesystem (stdlib), use a special marker
result[i] = "<StdLib>"
} else {
result[i] = fmt.Sprintf("<%T>", fs)
}
}
return result
}

// RT is a struct with methods that map to Clojure's RT class' static
// methods. This approach is used to make translation of core.clj to
// Glojure easier.
Expand Down Expand Up @@ -172,6 +208,7 @@ func (rt *RTMethods) Load(scriptBase string) {
loadPathLock.Lock()
lp := loadPath
loadPathLock.Unlock()
found := false
for _, fs := range lp {
for _, ext := range []string{".glj", ".cljc"} {
testFilename := scriptBase + ext
Expand All @@ -180,9 +217,13 @@ func (rt *RTMethods) Load(scriptBase string) {
buf = b
foundFS = fs
filename = testFilename
found = true
break
}
}
if found {
break
}
}
if filename == "" {
panic(fmt.Errorf("failed to load %s: not found in load path", scriptBase))
Expand Down
11 changes: 11 additions & 0 deletions pkg/stdlib/clojure/core/loader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.