Skip to content

Commit

Permalink
Gotta use runtime handle.
Browse files Browse the repository at this point in the history
Add Assembly to Launcher.Run. We populate this with the entry point type and use it to ensure the assembly is on the classpath. Fixes #564
  • Loading branch information
wasabii committed Jul 23, 2024
1 parent 2f99de4 commit 45b713f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/IKVM.Runtime/Java/Externs/ikvm/runtime/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ public static int run(Type main, string[] args, string jvmArgPrefix, global::jav
foreach (global::java.util.Map.Entry entry in (IEnumerable)properties.entrySet())
p.Add((string)entry.getKey(), (string)entry.getValue());

return IKVM.Runtime.Launcher.Run(main != null ? ((global::java.lang.Class)main).getName() : null, false, args, jvmArgPrefix, p);
return IKVM.Runtime.Launcher.Run(
main?.Assembly,
((global::java.lang.Class)main)?.getName(),
jar: false,
args,
jvmArgPrefix,
p);
#endif
}

Expand Down
10 changes: 8 additions & 2 deletions src/IKVM.Runtime/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,17 @@ static bool ArgEquals(ReadOnlySpan<char> a, string b)
}

/// <summary>
/// Launches a Java application.
/// Services as the managed entry point jump for a Java executable.
/// </summary>
/// <param name="assembly"></param>
/// <param name="main"></param>
/// <param name="jar"></param>
/// <param name="args"></param>
/// <param name="rarg"></param>
/// <param name="properties"></param>
/// <returns></returns>
[HideFromJava(HideFromJavaFlags.StackTrace)]
public static int Run(string main, bool jar, string[] args, string rarg, IDictionary<string, string> properties)
public static int Run(Assembly assembly, string main, bool jar, string[] args, string rarg, IDictionary<string, string> properties)
{
if (args is null)
throw new ArgumentNullException(nameof(args));
Expand All @@ -275,6 +276,11 @@ public static int Run(string main, bool jar, string[] args, string rarg, IDictio
#else
HandleDebugTrace();

// ensure the entry assembly is added to the classpath
if (assembly != null)
AddBootClassPathAssembly(assembly);

// initialize attribute parsing
var initialize = properties != null ? new Dictionary<string, string>(properties) : new Dictionary<string, string>();
var showversion = false;
var exit = false;
Expand Down
3 changes: 1 addition & 2 deletions src/IKVM.Runtime/RuntimeAssemblyClassLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,10 +1129,9 @@ internal override bool InternalsVisibleToImpl(RuntimeJavaType wrapper, RuntimeJa
internal void AddDelegate(RuntimeAssemblyClassLoader acl)
{
LazyInitExports();

lock (this)
{
delegates = ArrayUtil.Concat(delegates, acl);
}
}

#if !IMPORTER && !EXPORTER
Expand Down
17 changes: 11 additions & 6 deletions src/IKVM.Tools.Importer/CompilerClassLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,19 +514,24 @@ void SetMain(RuntimeJavaType type, PEFileKinds target, IDictionary<string, strin

var ilgen = Context.CodeEmitterFactory.Create(mainMethodProxy);

// first argument to Launch (type name)
// first argument to Launch (assembly)
ilgen.Emit(OpCodes.Ldtoken, type.TypeAsTBD);
ilgen.Emit(OpCodes.Call, Context.CompilerFactory.GetTypeFromHandleMethod);
ilgen.Emit(OpCodes.Callvirt, Context.Types.Type.GetProperty(nameof(System.Type.Assembly)).GetGetMethod());

// second argument to Launch (type name)
ilgen.Emit(OpCodes.Ldstr, type.Name);

// second argument: is this a jar
// third argument: is this a jar
ilgen.Emit(OpCodes.Ldc_I4_0);

// third argument: args
// fourth argument: args
ilgen.Emit(OpCodes.Ldarg_0);

// fourth argument, runtime prefix
// fifth argument, runtime prefix
ilgen.Emit(OpCodes.Ldstr, DEFAULT_RUNTIME_ARGS_PREFIX);

// fifth argument, property set to initialize JVM
// sixth argument, property set to initialize JVM
if (properties.Count > 0)
{
var environmentType = Context.Resolver.ResolveCoreType(typeof(Environment).FullName);
Expand Down Expand Up @@ -557,7 +562,7 @@ void SetMain(RuntimeJavaType type, PEFileKinds target, IDictionary<string, strin
}

// invoke the launcher main method
var launchMethod = Context.Resolver.ResolveRuntimeType("IKVM.Runtime.Launcher").GetMethod("Run");
var launchMethod = Context.Resolver.ResolveRuntimeType(typeof(IKVM.Runtime.Launcher).FullName).GetMethod(nameof(IKVM.Runtime.Launcher.Run));
ilgen.Emit(OpCodes.Call, launchMethod);
ilgen.Emit(OpCodes.Ret);

Expand Down
2 changes: 1 addition & 1 deletion src/java/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class Program
{

[HideFromJava]
public static int Main(string[] args) => Launcher.Run(null, false, args, "", null);
public static int Main(string[] args) => Launcher.Run(null, null, false, args, "", null);

}

Expand Down

0 comments on commit 45b713f

Please sign in to comment.