~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/runtime/vm.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Jeroen Frijters
 
3
 
 
4
  This software is provided 'as-is', without any express or implied
 
5
  warranty.  In no event will the authors be held liable for any damages
 
6
  arising from the use of this software.
 
7
 
 
8
  Permission is granted to anyone to use this software for any purpose,
 
9
  including commercial applications, and to alter it and redistribute it
 
10
  freely, subject to the following restrictions:
 
11
 
 
12
  1. The origin of this software must not be misrepresented; you must not
 
13
     claim that you wrote the original software. If you use this software
 
14
     in a product, an acknowledgment in the product documentation would be
 
15
     appreciated but is not required.
 
16
  2. Altered source versions must be plainly marked as such, and must not be
 
17
     misrepresented as being the original software.
 
18
  3. This notice may not be removed or altered from any source distribution.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
using System;
 
25
using System.Threading;
 
26
#if STATIC_COMPILER || STUB_GENERATOR
 
27
using IKVM.Reflection;
 
28
using Type = IKVM.Reflection.Type;
 
29
#else
 
30
using System.Reflection;
 
31
#endif
 
32
using System.IO;
 
33
using System.Diagnostics;
 
34
using System.Text;
 
35
using System.Security;
 
36
using System.Security.Permissions;
 
37
using IKVM.Internal;
 
38
 
 
39
#if !STATIC_COMPILER && !STUB_GENERATOR
 
40
namespace IKVM.Internal
 
41
{
 
42
        public static class Starter
 
43
        {
 
44
                public static void PrepareForSaveDebugImage()
 
45
                {
 
46
                        JVM.IsSaveDebugImage  = true;
 
47
                }
 
48
        
 
49
                public static void SaveDebugImage()
 
50
                {
 
51
                        DynamicClassLoader.SaveDebugImages();
 
52
                }
 
53
 
 
54
#if !FIRST_PASS
 
55
                public static java.lang.reflect.Method FindMainMethod(java.lang.Class clazz)
 
56
                {
 
57
                        // This method exists because we don't use Class.getDeclaredMethods(),
 
58
                        // since that could cause us to run into NoClassDefFoundError if any of the
 
59
                        // method signatures references a missing class.
 
60
                        TypeWrapper tw = TypeWrapper.FromClass(clazz);
 
61
                        MethodWrapper mw = tw.GetMethodWrapper("main", "([Ljava.lang.String;)V", true);
 
62
                        if (mw != null && mw.IsStatic)
 
63
                        {
 
64
                                return (java.lang.reflect.Method)mw.ToMethodOrConstructor(true);
 
65
                        }
 
66
                        return null;
 
67
                }
 
68
#endif
 
69
 
 
70
                public static bool ClassUnloading
 
71
                {
 
72
#if CLASSGC
 
73
                        get { return JVM.classUnloading; }
 
74
                        set { JVM.classUnloading = value; }
 
75
#else
 
76
                        get { return false; }
 
77
                        set { }
 
78
#endif
 
79
                }
 
80
 
 
81
                public static bool RelaxedVerification
 
82
                {
 
83
                        get { return JVM.relaxedVerification; }
 
84
                        set { JVM.relaxedVerification = value; }
 
85
                }
 
86
        }
 
87
}
 
88
#endif // !STATIC_COMPILER && !STUB_GENERATOR
 
89
 
 
90
namespace IKVM.Internal
 
91
{
 
92
        static class JVM
 
93
        {
 
94
#if STATIC_COMPILER
 
95
                internal const bool FinishingForDebugSave = false;
 
96
                internal const bool IsSaveDebugImage = false;
 
97
#elif !STUB_GENERATOR
 
98
                private static bool finishingForDebugSave;
 
99
                private static int emitSymbols;
 
100
                internal static bool IsSaveDebugImage;
 
101
#if CLASSGC
 
102
                internal static bool classUnloading = true;
 
103
#endif
 
104
#endif // STATIC_COMPILER
 
105
                private static Assembly coreAssembly;
 
106
                internal static bool relaxedVerification = true;
 
107
 
 
108
                internal static Version SafeGetAssemblyVersion(System.Reflection.Assembly asm)
 
109
                {
 
110
                        // Assembly.GetName().Version requires FileIOPermission,
 
111
                        // so we parse the FullName manually :-(
 
112
                        string name = asm.FullName;
 
113
                        int start = name.IndexOf(", Version=");
 
114
                        if(start >= 0)
 
115
                        {
 
116
                                start += 10;
 
117
                                int end = name.IndexOf(',', start);
 
118
                                if(end >= 0)
 
119
                                {
 
120
                                        return new Version(name.Substring(start, end - start));
 
121
                                }
 
122
                        }
 
123
                        return new Version();
 
124
                }
 
125
 
 
126
                internal static string SafeGetEnvironmentVariable(string name)
 
127
                {
 
128
                        try
 
129
                        {
 
130
                                return Environment.GetEnvironmentVariable(name);
 
131
                        }
 
132
                        catch(SecurityException)
 
133
                        {
 
134
                                return null;
 
135
                        }
 
136
                }
 
137
 
 
138
                internal static Assembly CoreAssembly
 
139
                {
 
140
                        get
 
141
                        {
 
142
#if !STATIC_COMPILER && !STUB_GENERATOR
 
143
                                if(coreAssembly == null)
 
144
                                {
 
145
#if FIRST_PASS
 
146
                                        throw new InvalidOperationException("This version of IKVM.Runtime.dll was compiled with FIRST_PASS defined.");
 
147
#else
 
148
                                        coreAssembly = typeof(java.lang.Object).Assembly;
 
149
#endif
 
150
                                }
 
151
#endif // !STATIC_COMPILER
 
152
                                return coreAssembly;
 
153
                        }
 
154
                        set
 
155
                        {
 
156
                                coreAssembly = value;
 
157
                        }
 
158
                }
 
159
 
 
160
#if !STATIC_COMPILER && !STUB_GENERATOR
 
161
                internal static bool FinishingForDebugSave
 
162
                {
 
163
                        get
 
164
                        {
 
165
                                return finishingForDebugSave;
 
166
                        }
 
167
                        set
 
168
                        {
 
169
                                finishingForDebugSave = value;
 
170
                        }
 
171
                }
 
172
 
 
173
                internal static bool EmitSymbols
 
174
                {
 
175
                        get
 
176
                        {
 
177
                                if (emitSymbols == 0)
 
178
                                {
 
179
                                        int state;
 
180
                                        string debug = System.Configuration.ConfigurationManager.AppSettings["ikvm-emit-symbols"];
 
181
                                        if (debug == null)
 
182
                                        {
 
183
                                                state = Debugger.IsAttached ? 1 : 2;
 
184
                                        }
 
185
                                        else
 
186
                                        {
 
187
                                                state = debug.Equals("True", StringComparison.OrdinalIgnoreCase) ? 1 : 2;
 
188
                                        }
 
189
                                        // make sure we only set the value once, because it isn't allowed to changed as that could cause
 
190
                                        // the compiler to try emitting symbols into a ModuleBuilder that doesn't accept them (and would
 
191
                                        // throw an InvalidOperationException)
 
192
                                        Interlocked.CompareExchange(ref emitSymbols, state, 0);
 
193
                                }
 
194
                                return emitSymbols == 1;
 
195
                        }
 
196
                }
 
197
#endif // !STATIC_COMPILER && !STUB_GENERATOR
 
198
 
 
199
                internal static bool IsUnix
 
200
                {
 
201
                        get
 
202
                        {
 
203
                                return Environment.OSVersion.Platform == PlatformID.Unix;
 
204
                        }
 
205
                }
 
206
        
 
207
                internal static string MangleResourceName(string name)
 
208
                {
 
209
                        // FXBUG there really shouldn't be any need to mangle the resource names,
 
210
                        // but in order for ILDASM/ILASM round tripping to work reliably, we have
 
211
                        // to make sure that we don't produce resource names that'll cause ILDASM
 
212
                        // to generate invalid filenames.
 
213
                        StringBuilder sb = new StringBuilder("ikvm__", name.Length + 6);
 
214
                        foreach(char c in name)
 
215
                        {
 
216
                                if("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-+.()$#@~=&{}[]0123456789`".IndexOf(c) != -1)
 
217
                                {
 
218
                                        sb.Append(c);
 
219
                                }
 
220
                                else if(c == '/')
 
221
                                {
 
222
                                        sb.Append('!');
 
223
                                }
 
224
                                else
 
225
                                {
 
226
                                        sb.Append('%');
 
227
                                        sb.Append(string.Format("{0:X4}", (int)c));
 
228
                                }
 
229
                        }
 
230
                        return sb.ToString();
 
231
                }
 
232
 
 
233
                // based on Bret Mulvey's C# port of Jenkins32
 
234
                // note that this algorithm cannot be changed, because we persist these hashcodes in the metadata of shared class loader assemblies
 
235
                internal static int PersistableHash(string str)
 
236
                {
 
237
                        uint key = 1;
 
238
                        foreach (char c in str)
 
239
                        {
 
240
                                key += c;
 
241
                                key += (key << 12);
 
242
                                key ^= (key >> 22);
 
243
                                key += (key << 4);
 
244
                                key ^= (key >> 9);
 
245
                                key += (key << 10);
 
246
                                key ^= (key >> 2);
 
247
                                key += (key << 7);
 
248
                                key ^= (key >> 12);
 
249
                        }
 
250
                        return (int)key;
 
251
                }
 
252
 
 
253
#if !STATIC_COMPILER
 
254
                internal static void CriticalFailure(string message, Exception x)
 
255
                {
 
256
                        try
 
257
                        {
 
258
                                Tracer.Error(Tracer.Runtime, "CRITICAL FAILURE: {0}", message);
 
259
                                System.Type messageBox = null;
 
260
#if !STUB_GENERATOR
 
261
                                // NOTE we use reflection to invoke MessageBox.Show, to make sure we run in environments where WinForms isn't available
 
262
                                Assembly winForms = IsUnix ? null : Assembly.Load("System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
 
263
                                if(winForms != null)
 
264
                                {
 
265
                                        messageBox = winForms.GetType("System.Windows.Forms.MessageBox");
 
266
                                }
 
267
#endif
 
268
                                message = String.Format("****** Critical Failure: {1} ******{0}{0}" +
 
269
                                        "PLEASE FILE A BUG REPORT FOR IKVM.NET WHEN YOU SEE THIS MESSAGE{0}{0}" +
 
270
                                        (messageBox != null ? "(on Windows you can use Ctrl+C to copy the contents of this message to the clipboard){0}{0}" : "") +
 
271
                                        "{2}{0}" + 
 
272
                                        "{3}{0}" +
 
273
                                        "{4}",
 
274
                                        Environment.NewLine,
 
275
                                        message,
 
276
                                        x,
 
277
                                        x != null ? new StackTrace(x, true).ToString() : "",
 
278
                                        new StackTrace(true));
 
279
                                if(messageBox != null)
 
280
                                {
 
281
                                        try
 
282
                                        {
 
283
                                                Version ver = SafeGetAssemblyVersion(typeof(JVM).Assembly);
 
284
                                                messageBox.InvokeMember("Show", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public, null, null, new object[] { message, "IKVM.NET " + ver + " Critical Failure" });
 
285
                                        }
 
286
                                        catch
 
287
                                        {
 
288
                                                Console.Error.WriteLine(message);
 
289
                                        }
 
290
                                }
 
291
                                else
 
292
                                {
 
293
                                        Console.Error.WriteLine(message);
 
294
                                }
 
295
                        }
 
296
                        catch(Exception ex)
 
297
                        {
 
298
                                Console.Error.WriteLine(ex);
 
299
                        }
 
300
                        finally
 
301
                        {
 
302
                                Environment.Exit(666);
 
303
                        }
 
304
                }
 
305
#endif // !STATIC_COMPILER
 
306
 
 
307
#if STATIC_COMPILER || STUB_GENERATOR
 
308
                internal static Type LoadType(System.Type type)
 
309
                {
 
310
                        return StaticCompiler.GetRuntimeType(type.FullName);
 
311
                }
 
312
#endif
 
313
 
 
314
                // this method resolves types in IKVM.Runtime.dll
 
315
                // (the version of IKVM.Runtime.dll that we're running
 
316
                // with can be different from the one we're compiling against.)
 
317
                internal static Type LoadType(Type type)
 
318
                {
 
319
#if STATIC_COMPILER || STUB_GENERATOR
 
320
                        return StaticCompiler.GetRuntimeType(type.FullName);
 
321
#else
 
322
                        return type;
 
323
#endif
 
324
                }
 
325
 
 
326
                internal static object Box(object val)
 
327
                {
 
328
#if STATIC_COMPILER || FIRST_PASS || STUB_GENERATOR
 
329
                        return null;
 
330
#else
 
331
                        if(val is byte)
 
332
                        {
 
333
                                return java.lang.Byte.valueOf((byte)val);
 
334
                        }
 
335
                        else if(val is bool)
 
336
                        {
 
337
                                return java.lang.Boolean.valueOf((bool)val);
 
338
                        }
 
339
                        else if(val is short)
 
340
                        {
 
341
                                return java.lang.Short.valueOf((short)val);
 
342
                        }
 
343
                        else if(val is char)
 
344
                        {
 
345
                                return java.lang.Character.valueOf((char)val);
 
346
                        }
 
347
                        else if(val is int)
 
348
                        {
 
349
                                return java.lang.Integer.valueOf((int)val);
 
350
                        }
 
351
                        else if(val is float)
 
352
                        {
 
353
                                return java.lang.Float.valueOf((float)val);
 
354
                        }
 
355
                        else if(val is long)
 
356
                        {
 
357
                                return java.lang.Long.valueOf((long)val);
 
358
                        }
 
359
                        else if(val is double)
 
360
                        {
 
361
                                return java.lang.Double.valueOf((double)val);
 
362
                        }
 
363
                        else
 
364
                        {
 
365
                                throw new java.lang.IllegalArgumentException();
 
366
                        }
 
367
#endif
 
368
                }
 
369
 
 
370
                internal static object Unbox(object val)
 
371
                {
 
372
#if STATIC_COMPILER || FIRST_PASS || STUB_GENERATOR
 
373
                        return null;
 
374
#else
 
375
                        java.lang.Byte b = val as java.lang.Byte;
 
376
                        if(b != null)
 
377
                        {
 
378
                                return b.byteValue();
 
379
                        }
 
380
                        java.lang.Boolean b1 = val as java.lang.Boolean;
 
381
                        if(b1 != null)
 
382
                        {
 
383
                                return b1.booleanValue();
 
384
                        }
 
385
                        java.lang.Short s = val as java.lang.Short;
 
386
                        if(s != null)
 
387
                        {
 
388
                                return s.shortValue();
 
389
                        }
 
390
                        java.lang.Character c = val as java.lang.Character;
 
391
                        if(c != null)
 
392
                        {
 
393
                                return c.charValue();
 
394
                        }
 
395
                        java.lang.Integer i = val as java.lang.Integer;
 
396
                        if(i != null)
 
397
                        {
 
398
                                return i.intValue();
 
399
                        }
 
400
                        java.lang.Float f = val as java.lang.Float;
 
401
                        if(f != null)
 
402
                        {
 
403
                                return f.floatValue();
 
404
                        }
 
405
                        java.lang.Long l = val as java.lang.Long;
 
406
                        if(l != null)
 
407
                        {
 
408
                                return l.longValue();
 
409
                        }
 
410
                        java.lang.Double d = val as java.lang.Double;
 
411
                        if(d != null)
 
412
                        {
 
413
                                return d.doubleValue();
 
414
                        }
 
415
                        else
 
416
                        {
 
417
                                throw new java.lang.IllegalArgumentException();
 
418
                        }
 
419
#endif
 
420
                }
 
421
 
 
422
#if !STATIC_COMPILER && !STUB_GENERATOR
 
423
                internal static object NewAnnotation(object classLoader, object definition)
 
424
                {
 
425
#if FIRST_PASS
 
426
                        return null;
 
427
#else
 
428
                        return ikvm.@internal.AnnotationAttributeBase.newAnnotation((java.lang.ClassLoader)classLoader, definition);
 
429
#endif
 
430
                }
 
431
#endif
 
432
 
 
433
#if !STATIC_COMPILER && !STUB_GENERATOR
 
434
                internal static object NewAnnotationElementValue(object classLoader, object expectedClass, object definition)
 
435
                {
 
436
#if FIRST_PASS
 
437
                        return null;
 
438
#else
 
439
                        try
 
440
                        {
 
441
                                return ikvm.@internal.AnnotationAttributeBase.decodeElementValue(definition, (java.lang.Class)expectedClass, (java.lang.ClassLoader)classLoader);
 
442
                        }
 
443
                        catch(java.lang.IllegalAccessException)
 
444
                        {
 
445
                                // TODO this shouldn't be here
 
446
                                return null;
 
447
                        }
 
448
#endif
 
449
                }
 
450
#endif
 
451
 
 
452
#if !STATIC_COMPILER && !STUB_GENERATOR
 
453
                // helper for JNI (which doesn't have access to core library internals)
 
454
                internal static object NewDirectByteBuffer(long address, int capacity)
 
455
                {
 
456
#if FIRST_PASS
 
457
                        return null;
 
458
#else
 
459
                        return java.nio.DirectByteBuffer.__new(address, capacity);
 
460
#endif
 
461
                }
 
462
#endif
 
463
 
 
464
                internal static Type Import(System.Type type)
 
465
                {
 
466
#if STATIC_COMPILER || STUB_GENERATOR
 
467
                        return StaticCompiler.Universe.Import(type);
 
468
#else
 
469
                        return type;
 
470
#endif
 
471
                }
 
472
        }
 
473
}