Welcome to mirror list, hosted at ThFree Co, Russian Federation.

VMRuntime.java « lang « java « classpath - github.com/mono/ikvm-fork.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b8fa583af3a590932ac0ed674721231e5f75be5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
  Copyright (C) 2003, 2004 Jeroen Frijters

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net
  
*/
package java.lang;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.nio.channels.Channels;
import gnu.java.nio.channels.FileChannelImpl;
import cli.System.Text.StringBuilder;
import cli.System.Diagnostics.ProcessStartInfo;

/**
 * VMRuntime represents the interface to the Virtual Machine.
 *
 * @author Jeroen Frijters
 */
final class VMRuntime
{
    /**
     * No instance is ever created.
     */
    private VMRuntime() 
    {
    }

    static void enableShutdownHooks()
    {
	cli.System.AppDomain.get_CurrentDomain().add_ProcessExit(new cli.System.EventHandler(new cli.System.EventHandler.Method() {
	    public void Invoke(Object sender, cli.System.EventArgs e) {
		Runtime.getRuntime().runShutdownHooks();
	    }
	}));
    }


    /**
     * Returns the number of available processors currently available to the
     * virtual machine. This number may change over time; so a multi-processor
     * program want to poll this to determine maximal resource usage.
     *
     * @return the number of processors available, at least 1
     */
    static int availableProcessors()
    {
	String s = cli.System.Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS");
	if(s != null)
	{
	    return Integer.parseInt(s);
	}
	return 1;
    }

    /**
     * Find out how much memory is still free for allocating Objects on the heap.
     *
     * @return the number of bytes of free memory for more Objects
     */
    static long freeMemory()
    {
	// TODO figure out if there is anything meaningful we can return here
	return 10 * 1024 * 1024;
    }

    /**
     * Find out how much memory total is available on the heap for allocating
     * Objects.
     *
     * @return the total number of bytes of memory for Objects
     */
    static long totalMemory()
    {
	// NOTE this really is a bogus number, but we have to return something
	return cli.System.GC.GetTotalMemory(false) + freeMemory();
    }

    /**
     * Returns the maximum amount of memory the virtual machine can attempt to
     * use. This may be <code>Long.MAX_VALUE</code> if there is no inherent
     * limit (or if you really do have a 8 exabyte memory!).
     *
     * @return the maximum number of bytes the virtual machine will attempt
     *         to allocate
     */
    static long maxMemory()
    {
	// spec says: If there is no inherent limit then the value Long.MAX_VALUE will be returned.
	return Long.MAX_VALUE;
    }

    /**
     * Run the garbage collector. This method is more of a suggestion than
     * anything. All this method guarantees is that the garbage collector will
     * have "done its best" by the time it returns. Notice that garbage
     * collection takes place even without calling this method.
     */
    static void gc()
    {
	cli.System.GC.Collect();
    }

    /**
     * Run finalization on all Objects that are waiting to be finalized. Again,
     * a suggestion, though a stronger one than {@link #gc()}. This calls the
     * <code>finalize</code> method of all objects waiting to be collected.
     *
     * @see #finalize()
     */
    static void runFinalization()
    {
        cli.System.GC.WaitForPendingFinalizers();
    }

    static void runFinalizationForExit()
    {
	// The CLR has its own finalization for exit strategy
    }

    /**
     * Tell the VM to trace every bytecode instruction that executes (print out
     * a trace of it).  No guarantees are made as to where it will be printed,
     * and the VM is allowed to ignore this request.
     *
     * @param on whether to turn instruction tracing on
     */
    static void traceInstructions(boolean on)
    {
	// not supported
    }

    /**
     * Tell the VM to trace every method call that executes (print out a trace
     * of it).  No guarantees are made as to where it will be printed, and the
     * VM is allowed to ignore this request.
     *
     * @param on whether to turn method tracing on
     */
    static void traceMethodCalls(boolean on)
    {
	// TODO integrate with method tracing
    }

    /**
     * Native method that actually sets the finalizer setting.
     *
     * @param value whether to run finalizers on exit
     */
    static void runFinalizersOnExit(boolean value)
    {
        runFinalizersOnExitFlag = value;
    }
    // the default is not the run finalizers on exit
    static volatile boolean runFinalizersOnExitFlag;

    /**
     * Native method that actually shuts down the virtual machine.
     *
     * @param status the status to end the process with
     */
    static void exit(int status)
    {
	cli.System.Environment.Exit(status);
    }

    /**
     * Load a file. If it has already been loaded, do nothing. The name has
     * already been mapped to a true filename.
     *
     * @param filename the file to load
     * @return 0 on failure, nonzero on success
     */
    static native int nativeLoad(String filename, Object classLoader);

    /**
     * Map a system-independent "short name" to the full file name.
     *
     * @param libname the short version of the library name
     * @return the full filename
     */
    static String mapLibraryName(String libname)
    {
	if(cli.System.Environment.get_OSVersion().ToString().indexOf("Unix") >= 0)
	{
	    return "lib" + libname + ".so";
	}
	else
	{
	    return libname + ".dll";
	}
    }

    /**
     * Execute a process. The command line has already been tokenized, and
     * the environment should contain name=value mappings. If directory is null,
     * use the current working directory; otherwise start the process in that
     * directory.  If env is null, then the new process should inherit
     * the environment of this process.
     *
     * @param cmd the non-null command tokens
     * @param env the environment setup
     * @param dir the directory to use, may be null
     * @return the newly created process
     * @throws NullPointerException if cmd or env have null elements
     */
    static Process exec(String[] cmd, String[] env, File dir) throws java.io.IOException
    {
	StringBuilder sb = new StringBuilder();
	for(int i = 1; i < cmd.length; i++)
	{
	    if(i > 1)
	    {
		sb.Append(' ');
	    }
	    // HACK if the arg contains a space, we surround it with quotes
	    // this isn't nearly good enough, but for now it'll have to do
	    if(cmd[i].indexOf(' ') >= 0 && cmd[i].charAt(0) != '"')
	    {
		sb.Append('"').Append(cmd[i]).Append('"');
	    }
	    else
	    {
		sb.Append(cmd[i]);
	    }
	}
	ProcessStartInfo si = new ProcessStartInfo(cmd[0], sb.ToString());
	si.set_UseShellExecute(false);
	si.set_RedirectStandardError(true);
	si.set_RedirectStandardOutput(true);
	si.set_RedirectStandardInput(true);
	si.set_CreateNoWindow(true);
	if(dir != null)
	{
	    si.set_WorkingDirectory(dir.toString());
	}
	if(env != null)
	{
	    for(int i = 0; i < env.length; i++)
	    {
		int pos = env[i].indexOf('=');
		si.get_EnvironmentVariables().set_Item(env[i].substring(0, pos), env[i].substring(pos + 1));
	    }
	}
	try
	{
	    if(false) throw new cli.System.ComponentModel.Win32Exception();
	    if(false) throw new cli.System.InvalidOperationException();
	    return new DotNetProcess(cli.System.Diagnostics.Process.Start(si));
	}
	catch(cli.System.ComponentModel.Win32Exception x1)
	{
	    throw new java.io.IOException(x1.getMessage());
	}
	catch(cli.System.InvalidOperationException x2)
	{
	    throw new java.io.IOException(x2.getMessage());
	}
    }

    private static class DotNetProcess extends Process
    {
	private cli.System.Diagnostics.Process proc;
	private OutputStream stdin;
	private InputStream stdout;
	private InputStream stderr;

	private DotNetProcess(cli.System.Diagnostics.Process proc)
	{
	    this.proc = proc;
	    stdin = Channels.newOutputStream(new FileChannelImpl(proc.get_StandardInput().get_BaseStream()));
	    stdout = Channels.newInputStream(new FileChannelImpl(proc.get_StandardOutput().get_BaseStream()));
	    stderr = Channels.newInputStream(new FileChannelImpl(proc.get_StandardError().get_BaseStream()));
	}

	public OutputStream getOutputStream()
	{
	    return stdin;
	}

	public InputStream getInputStream()
	{
	    return stdout;
	}

	public InputStream getErrorStream()
	{
	    return stderr;
	}

	public int waitFor() throws InterruptedException
	{
            // to be interruptable we have to use polling
            for(;;)
            {
                if(Thread.interrupted())
                {
                    throw new InterruptedException();
                }
	        if(proc.WaitForExit(100))
                {
	            return proc.get_ExitCode();
                }
            }
	}

	public int exitValue()
	{
	    if(proc.get_HasExited())
	    {
		return proc.get_ExitCode();
	    }
	    throw new IllegalThreadStateException();
	}

	public void destroy()
	{
	    try
	    {
		if(false) throw new cli.System.InvalidOperationException();
		proc.Kill();
	    }
	    catch(cli.System.InvalidOperationException x)
	    {
	    }
	}
    }
} // class VMRuntime