Well, it is really strange:
diff --git a/src-java/babashka/impl/Graal.java b/src-java/babashka/impl/Graal.java
index 9754185..604b5f6 100644
--- a/src-java/babashka/impl/Graal.java
+++ b/src-java/babashka/impl/Graal.java
@@ -18,23 +18,22 @@ public class Graal {
@Override
public List<String> getHeaderFiles() {
- return Collections.singletonList("<stdlib.h>");
+ return Collections.singletonList("<stdio.h>");
}
}
@CFunction
- private static native int setenv(CCharPointer name, CCharPointer value, int overwrite);
+ private static native int puts(CCharPointer name);
// API
public static int setEnv(String name, String value) {
- int ret = 0;
+ int ret = -1;
System.out.println("setenv" + " " + name + " " + value);
try (CCharPointerHolder nameHolder = CTypeConversion.toCString(name);
CCharPointerHolder valueHolder = CTypeConversion.toCString(value)) {
- ret = setenv(nameHolder.get(), valueHolder.get(), 1);
- System.out.println(System.getenv(name));
+ ret = puts(nameHolder.get());
+ System.out.println(Integer.toString(ret));
}
- System.out.println(System.getenv(name));
return ret;
}
user=> (babashka.utils/set-env "this is the text that should be printed" "")
:setting "this is the text that should be printed" ""
setenv this is the text that should be printed
this is the text that should be printed
40
40
nil
Definitively seems to be working, but the setenv
does seems to do its job but it doesn't actually set the variable :thinking_face:So, changing environment variables in Java isn't supported. But this now works in an experimental branch (called set-env
) 👿
$ ./bb -e "(require '[babashka.os :as os])" -e '(prn (os/set-env "FOO" "BAR")) (prn (os/get-env "FOO"))'
true
"BAR"
Yet, it doesn't compose well with System/getenv, since the Java implementation caches the environment variables on the first call to such methodsThe talk I did back in February at the Graal 2021 workshop at CGO is finally available: Babashka: a native Clojure interpreter for scripting https://youtu.be/Yjeh57eE9rg