// =========================================================================== // java_host.cpp -- the Java execution host (JVM hand-off) for `sleela run`. // =========================================================================== // Runs `.java` sources, `.jar` archives and `--class-path/-cp/--class` main // classes on an external JVM discovered by runtime/java_runtime_bridge. It is a // LanguageHandler used only for its runHost hook and registers itself, so the // compiler driver contains no JVM launch code: it just offers the `run` // arguments to lang::runHost. It is a separate object from java_handler.cpp // because it depends on the runtime bridge, which the compile-only Nordshrift // binary does not link. See impl/frontend/COMPILER-JAVA-REVIEW.md (Stage 2). // =========================================================================== #include "../language_handler.h" #include #include #include #include #include #include #include #include #include #include #if defined(_WIN32) # include # include #else # include # include #endif extern "C" { #include "../../../runtime/java_runtime_bridge.h" } namespace fs=std::filesystem; namespace sleela { namespace javahost { static bool readFile(const std::string&path,std::string&out){std::ifstream f(path,std::ios::binary);if(!f)return false;std::ostringstream ss;ss<=ext.size()&&path.compare(path.size()-ext.size(),ext.size(),ext)==0;} // Launch a child process with an argv vector, without routing Java arguments // through a shell or the interactive PTY. This preserves whitespace/metacharacters // in application arguments and returns the child's actual exit status. static int runExternalProcess(const std::vector& args){ if(args.empty()||args[0].empty()){ std::cerr<<"sleela: cannot launch an empty process command.\n"; return 2; } #if defined(_WIN32) std::vector wide; wide.reserve(args.size()); for(const auto& arg:args){ int count=MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,arg.c_str(),-1,nullptr,0); if(count<=0){ std::cerr<<"sleela: process argument is not valid UTF-8.\n"; return 2; } std::wstring value((size_t)count,L'\0'); if(MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,arg.c_str(),-1,&value[0],count)<=0){ std::cerr<<"sleela: unable to convert process argument to UTF-16.\n"; return 2; } value.resize((size_t)count-1); wide.push_back(std::move(value)); } std::vector argv; argv.reserve(wide.size()+1); for(const auto& arg:wide)argv.push_back(arg.c_str()); argv.push_back(nullptr); intptr_t status=_wspawnvp(_P_WAIT,wide[0].c_str(),argv.data()); if(status==-1){ std::cerr<<"sleela: unable to start Java process (errno "< argv; argv.reserve(args.size()+1); for(const auto& arg:args)argv.push_back(const_cast(arg.c_str())); argv.push_back(nullptr); pid_t child=fork(); if(child<0){ std::cerr<<"sleela: fork failed while launching Java: "<&programArgs={}){ std::string src; if(!readFile(path,src)){ std::cerr<<"sleelvac: cannot open Java source '"< args={probe.java_executable}; if(probe.kind==SLEELA_JAVA_FX){ const char* modulePath=std::getenv("SLEELA_JAVAFX_MODULE_PATH"); const char* configuredModules=std::getenv("SLEELA_JAVAFX_MODULES"); if(modulePath&&*modulePath){ args.push_back("--module-path"); args.emplace_back(modulePath); args.push_back("--add-modules"); args.emplace_back((configuredModules&&*configuredModules) ? configuredModules : "javafx.controls,javafx.graphics"); }else{ std::cerr<<"[javafx] no module path configured; launch will work only if " "JavaFX is already available to this Java runtime. Set " "SLEELA_JAVAFX_MODULE_PATH and optionally SLEELA_JAVAFX_MODULES.\n"; } #if !defined(_WIN32) && !defined(__APPLE__) const char* display=std::getenv("DISPLAY"); const char* wayland=std::getenv("WAYLAND_DISPLAY"); if((!display||!*display)&&(!wayland||!*wayland)) std::cerr<<"[javafx] no DISPLAY or WAYLAND_DISPLAY detected; graphical startup may fail on this headless host.\n"; #endif } /* Optional JVM options passthrough for single-file source launches, e.g. SLEELA_JAVA_OPTS="--enable-preview --source 21 -ea". These are JVM flags and must precede the source-file operand. Tokens are split on unquoted whitespace; there is no shell, so each token becomes one argv element. */ if(const char* jopts=std::getenv("SLEELA_JAVA_OPTS")){ std::string tok; std::string s=jopts; for(size_t i=0;i<=s.size();++i){ char c=(i "< "<&javaArgs,const std::string&description){ const char* javaMode=std::getenv("SLEELA_JAVA_MODE"); const char* javaEnabled=std::getenv("SLEELA_JAVA_ENABLED"); if((javaMode&&std::string(javaMode)=="disabled") || (javaEnabled&&std::string(javaEnabled)=="0")){ std::cerr<<"[java] Java execution is disabled by SLEELA_JAVA_MODE or SLEELA_JAVA_ENABLED.\n"; return 2; } SleelaJavaRuntimeProbeResult probe{}; if(sleela_java_runtime_probe_local_vm(&probe)!=1 || !probe.java_vm_available || !probe.java_executable[0]){ std::cerr<<"[java] A Java runtime is required for "< args={probe.java_executable}; args.insert(args.end(),javaArgs.begin(),javaArgs.end()); std::cout<<"[java] external JVM: "< "<&programArgs){ std::error_code ec; if(!fs::is_regular_file(fs::path(jar),ec)){ std::cerr<<"sleela: Java JAR not found or not a regular file: "< args={"-jar",jar}; args.insert(args.end(),programArgs.begin(),programArgs.end()); return runJavaApplication(args,"JAR "+jar); } static int runJavaClass(const std::string&classpath,const std::string&mainClass, const std::vector&programArgs){ if(!validJavaMainClass(mainClass)){ std::cerr<<"sleela: invalid Java main class name '"< args={"-cp",classpath.empty()?".":classpath,mainClass}; args.insert(args.end(),programArgs.begin(),programArgs.end()); return runJavaApplication(args,"class "+mainClass+" (classpath "+(classpath.empty()?".":classpath)+")"); } class JavaHost : public LanguageHandler { public: const char* name() const override { return "java-host"; } // args[0] is the file or option after `run`; the rest are program arguments. bool runHost(const std::vector& a,int& rc) const override { if(a.empty()) return false; const std::string& first=a[0]; auto rest=[&](size_t from){ return std::vector(a.size()>from?a.begin()+from:a.end(),a.end()); }; if(hasExt(first,".jar")){ rc=runJavaJar(first,rest(1)); return true; } if(first=="--class-path"||first=="--classpath"||first=="-cp"){ if(a.size()<3){ std::cerr<<"sleela run: expected --class-path [arguments...]\n"; rc=2; return true; } rc=runJavaClass(a[1],a[2],rest(3)); return true; } if(first=="--class"){ if(a.size()<2){ std::cerr<<"sleela run: expected --class [arguments...]\n"; rc=2; return true; } rc=runJavaClass(".",a[1],rest(2)); return true; } if(hasExt(first,".java")){ std::string src; if(!readFile(first,src)) return false; // not readable: the core ingests it SleelaJavaRuntimeProbeResult probe{}; SleelaJavaProgramRequest request{}; request.classpath="."; request.source_file=first.c_str(); SleelaJavaProgramPlan plan{}; int dispatch=sleela_java_runtime_dispatch_source(src.data(),src.size(),first.c_str(),&request,&probe,&plan); if(dispatch<0){ rc=1; return true; } rc=runJavaFrameworkSource(first,rest(1)); return true; } return false; } }; static JavaHost g_host; static lang::Registrar g_registrar(&g_host); } }