1 jls.loader.provide('jls.lang.Runtime');
  2 
  3 jls.loader.require('jls.lang.Process');
  4 jls.loader.require('jls.lang.ProcessBuilder');
  5 
  6 /**
  7  * @class The Runtime class provides interaction with the underlying OS.
  8  */
  9 jls.lang.Runtime = jls.lang.Class.create({});
 10 
 11 Object.extend(jls.lang.Runtime, /** @lends jls.lang.Runtime */
 12 {
 13     /**
 14      * Executes the specified command and arguments in a separate process with the specified environment and working directory.
 15      * 
 16      * @param {Array} cmdArray Array of strings specifying the command-line arguments. The first argument is the name of the executable file.
 17      * @param {Array} envp Array of key=values specifying the environment strings. If undefined, the new process inherits the environment of the parent process.
 18      * @param {jls.io.File} dir The working directory of the subprocess, or undefined if the subprocess should inherit the working directory of the current process.
 19      * @returns {jls.lang.Process} A new Process for managing the subprocess.
 20      */
 21     exec : function(cmdArray, envp, dir) {
 22         var pb = new jls.lang.ProcessBuilder(cmdArray, envp);
 23         if (dir) {
 24             pb.setCurrentDirectory(dir);
 25         }
 26         return pb.start();
 27     },
 28     /**
 29      * Terminates the program and returns a value to the OS.
 30      * 
 31      * @param {Number} status The exit status to return to the OS.
 32      */
 33     exit : function(status) {
 34         _native.core.exit(status);
 35     },
 36     /**
 37      * Forcibly terminates the program and returns a value to the OS.
 38      * 
 39      * @param {Number} status The exit status to return to the OS.
 40      */
 41     halt : function(status) {
 42         _native.core.halt(status);
 43     },
 44     /**
 45      * Runs the garbage collector.
 46      */
 47     gc : function() {
 48         _native.core.gc();
 49     },
 50     /**
 51      * Loads a native library compliant with the JLS native engine.
 52      * 
 53      * @param {String} name The name of the library to load.
 54      */
 55     loadLibrary : function(libname) {
 56         jls.loader.requireLibrary(libname);
 57     },
 58     _shutdownHooks : [],
 59     _shutdownHook : function() {
 60         for (var i = 0; i < jls.lang.Runtime._shutdownHooks.length; i++) {
 61             try {
 62                 jls.lang.Runtime._shutdownHooks[i]();
 63             } catch (e) {
 64                 jls.logger.warn('uncaught exception while calling shutdown hook \"' + e + '\"');
 65             }
 66         }
 67     },
 68     /**
 69      * Registers a new virtual-machine shutdown hook.
 70      * 
 71      * @param {Function} hook The hook function.
 72      */
 73     addShutdownHook : function(hook) {
 74         if (! ('shutdownHook' in _native.core)) {
 75             _native.core.shutdownHook = jls.lang.Runtime._shutdownHook;
 76         }
 77         jls.lang.Runtime._shutdownHooks.push(hook);
 78     },
 79     /**
 80      * Gets the current runtime instance.
 81      * 
 82      * @returns {jls.lang.Runtime} The runtime instance.
 83      */
 84     getRuntime : function() {
 85         return jls.lang.Runtime;
 86     }
 87 });
 88 
 89