-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (107 loc) · 3.18 KB
/
Program.cs
File metadata and controls
118 lines (107 loc) · 3.18 KB
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
using System;
using System.Diagnostics;
using System.Threading;
using Mono.Options;
using Java.Interop;
namespace Hello
{
class App
{
const int N = 1000000;
public static void Main (string[] args)
{
string? jvmPath = global::Java.InteropTests.TestJVM.GetJvmLibraryPath ();
bool createMultipleVMs = false;
bool reportTiming = false;
bool showHelp = false;
var options = new OptionSet () {
"Using the JVM from C#!",
"",
"Options:",
{ "jvm=",
$"{{PATH}} to JVM to use. Default is:\n {jvmPath}",
v => jvmPath = v },
{ "m",
"Create multiple Java VMs. This will likely creash.",
v => createMultipleVMs = v != null },
{ "t",
$"Timing; invoke Object.hashCode() {N} times, print average.",
v => reportTiming = v != null },
{ "h|help",
"Show this message and exit.",
v => showHelp = v != null },
};
options.Parse (args);
if (showHelp) {
options.WriteOptionDescriptions (Console.Out);
return;
}
var builder = new JreRuntimeOptions () {
JniAddNativeMethodRegistrationAttributePresent = true,
JvmLibraryPath = jvmPath,
};
builder.AddOption ("-Xcheck:jni");
var jvm = builder.CreateJreVM ();
if (reportTiming) {
ReportTiming ();
return;
}
if (createMultipleVMs) {
CreateAnotherJVM ();
return;
}
CreateJLO ();
}
static void CreateJLO ()
{
var jlo = new Java.Lang.Object ();
Console.WriteLine ($"binding? {jlo.ToString ()}");
}
static void ReportTiming ()
{
var jlo = new Java.Lang.Object ();
var t = Stopwatch.StartNew ();
for (int i = 0; i < N; ++i) {
jlo.GetHashCode ();
}
t.Stop ();
Console.WriteLine ($"Object.hashCode: {N} invocations. Total={t.Elapsed}; Average={t.Elapsed.TotalMilliseconds / (double) N}ms");
}
static unsafe void CreateAnotherJVM ()
{
Console.WriteLine ("Part 2!");
using (var vm = new JreRuntimeOptions ().CreateJreVM ()) {
Console.WriteLine ("# JniEnvironment.EnvironmentPointer={0}", JniEnvironment.EnvironmentPointer);
Console.WriteLine ("vm.SafeHandle={0}", vm.InvocationPointer);
var t = new JniType ("java/lang/Object");
var c = t.GetConstructor ("()V");
var o = t.NewObject (c, null);
var m = t.GetInstanceMethod ("hashCode", "()I");
int i = JniEnvironment.InstanceMethods.CallIntMethod (o, m);
Console.WriteLine ("java.lang.Object={0}", o);
Console.WriteLine ("hashcode={0}", i);
JniObjectReference.Dispose (ref o);
t.Dispose ();
// var o = JniTypes.FindClass ("java/lang/Object");
/*
var waitForCreation = new CountdownEvent (1);
var exitThread = new CountdownEvent (1);
var t = new Thread (() => {
var vm2 = new JavaVMBuilder ().CreateJavaVM ();
waitForCreation.Signal ();
exitThread.Wait ();
});
t.Start ();
waitForCreation.Wait ();
*/
foreach (var h in vm.GetAvailableInvocationPointers ()) {
Console.WriteLine ("WITHIN: GetCreatedJavaVMs: {0}", h);
}
// exitThread.Signal ();
}
foreach (var h in JniRuntime.GetRegisteredRuntimes ()) {
Console.WriteLine ("POST: GetCreatedJavaVMs: {0}", h);
}
}
}
}