Skip to content
Draft
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
8 changes: 4 additions & 4 deletions compiler+runtime/src/cpp/jank/runtime/module/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,10 +791,6 @@ namespace jank::runtime::module
return find_result{ entry, module_type };
}
}
else if(entry.cpp.is_some())
{
return find_result{ entry, module_type::cpp };
}
else if(entry.jank.is_some())
{
return find_result{ entry, module_type::jank };
Expand All @@ -803,6 +799,10 @@ namespace jank::runtime::module
{
return find_result{ entry, module_type::cljc };
}
else if(entry.cpp.is_some())
{
return find_result{ entry, module_type::cpp };
}
}

return error::internal_runtime_failure(
Expand Down
91 changes: 91 additions & 0 deletions compiler+runtime/test/bash/module/compiled-modules/pass-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bb

^{:clj-kondo/ignore [:namespace-name-mismatch]}
(ns jank.test.module.compiled-module
(:require [babashka.fs :as fs]
[babashka.process :as proc]
[clojure.string :as string]
[clojure.test :as t :refer [deftest is testing use-fixtures]]))

(def this-nsym (ns-name *ns*))

(def module-a "jank-test.a")
(def module-a-path "./src/jank_test/a.cljc")
(defn compiled-a-path []
(first (fs/glob "./target" "**/a.o")))

(defn jank [command module-name & {:keys [extra-module-path]}]
(proc/sh ["jank" "--module-path"
(str "src" (when-not (empty? extra-module-path)
(str ":" extra-module-path)))
command module-name]))

(defn create-jar-with-a []
(let [jar-name "test.jar"
[compilation-dir rest-of-module-path] (->> (fs/glob "target" "**/a.o")
(first)
(fs/components)
(split-at 2)
(map (partial apply fs/path))
(map str))]
(proc/sh {:dir compilation-dir} "jar" "cf" jar-name rest-of-module-path)
(fs/path compilation-dir jar-name)))

(defmacro with-hidden-file
"Temporarily renames `path` to `path.bak`, evaluates body,
and restores the file in a `finally` block."
[path & body]
`(let [path# ~path
bak# (str path# ".bak")]
(fs/move path# bak#)
(try
~@body
(finally
(fs/move bak# path#)))))

(use-fixtures
:each
(fn [f]
(jank "compile-module" module-a)
(f)
(fs/delete-tree "./target")))

(deftest compiled-module-gets-loaded
(testing "loads a compiled module instead of the source"
(is (false? (-> (jank "run-main" module-a)
:out
(string/includes? ":only-during-read")))))

(testing "Source fresher than the compiled module, loads from source"
(proc/sh ["touch" module-a-path])
(-> (jank "run-module" module-a)
:out
println)
(is (true? (-> (jank "run-main" module-a)
:out
(string/includes? ":only-during-read"))))))

(deftest compiled-module-loadability
(testing "Doesn't load a module from archive"
(fs/with-temp-dir [tmp-dir]
(let [jar (-> (create-jar-with-a)
(fs/move tmp-dir))]
(with-hidden-file "target"
(is (true? (-> (jank "run-main" module-a :extra-module-path jar)
:out
(string/includes? ":only-during-read"))))))))

(testing "Doesn't load module if source not found"
(with-hidden-file module-a-path
(is (true? (-> (jank "run-main" module-a)
:out
(string/includes? "No sources for registered module")))))))

(defn -main []
(System/exit
(if (t/successful? (t/run-tests this-nsym))
0
1)))

(when (= *file* (System/getProperty "babashka.file"))
(apply -main *command-line-args*))
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns jank-test.a)

(defmacro with-prelog [& body]
(println :only-during-read)
`(do ~@body))

(with-prelog
(defn -main []
(println :success)))
4 changes: 4 additions & 0 deletions compiler+runtime/test/bash/module/cpp/pass-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail

jank --module-path src run-main jank-test.test-runner | grep '^:success$'
13 changes: 13 additions & 0 deletions compiler+runtime/test/bash/module/cpp/src/jank_test/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using jank_object_ref = void *;

extern "C" void jank_module_set_loaded(char const *module);
extern "C" jank_object_ref jank_eval(jank_object_ref s);
extern "C" jank_object_ref jank_string_create(char const *s);

extern "C" void jank_load_jank_test_a()
{
jank_eval(jank_string_create("(ns jank-test.a)"));
jank_module_set_loaded("jank-test.a");
jank_eval(jank_string_create("(defn -main [] \"a.cpp\")"));
}

2 changes: 2 additions & 0 deletions compiler+runtime/test/bash/module/cpp/src/jank_test/a.jank
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(ns jank-test.a)
(defn -main [] "a.jank")
2 changes: 2 additions & 0 deletions compiler+runtime/test/bash/module/cpp/src/jank_test/b.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(ns jank-test.b)
(defn -main [] "b.cljc")
12 changes: 12 additions & 0 deletions compiler+runtime/test/bash/module/cpp/src/jank_test/b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using jank_object_ref = void *;

extern "C" void jank_module_set_loaded(char const *module);
extern "C" jank_object_ref jank_eval(jank_object_ref s);
extern "C" jank_object_ref jank_string_create(char const *s);

extern "C" void jank_load_jank_test_b()
{
jank_eval(jank_string_create("(ns jank-test.b)"));
jank_module_set_loaded("jank-test.b");
jank_eval(jank_string_create("(defn -main [] \"b.cpp\")"));
}
2 changes: 2 additions & 0 deletions compiler+runtime/test/bash/module/cpp/src/jank_test/cljc.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(ns jank-test.cljc)
(defn -main [] #?(:clj :clj :jank :jank :default :default))
12 changes: 12 additions & 0 deletions compiler+runtime/test/bash/module/cpp/src/jank_test/cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using jank_object_ref = void *;

extern "C" void jank_module_set_loaded(char const *module);
extern "C" jank_object_ref jank_eval(jank_object_ref s);
extern "C" jank_object_ref jank_string_create(char const *s);

extern "C" void jank_load_jank_test_cpp()
{
jank_eval(jank_string_create("(ns jank-test.cpp)"));
jank_module_set_loaded("jank-test.cpp");
jank_eval(jank_string_create("(defn -main [] :cpp)"));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns jank-test.test-runner
(:require jank-test.a
jank-test.b
jank-test.cpp))

(defn -main []
(assert (= "a.jank" (jank-test.a/-main)) ".jank overrides .cpp")
(assert (= "b.cljc" (jank-test.b/-main)) ".cljc overrides .cpp")
(assert (= :cpp (jank-test.cpp/-main)) "cpp module should be loaded correctly")
;; exit code not reliable enough https://github.com/jank-lang/jank/issues/181
(println :success))
Loading