package lila.runtime;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import static java.lang.invoke.MethodType.methodType;

public class DynamicClassLoader extends ClassLoader {
	public Class<?> define(String name, byte[] classBytes) {
		return super.defineClass(name, classBytes, 0, classBytes.length);
	}

	public MethodHandle findMethod
		(Class<?> clazz, String name, Class<?> rtype, Class<?>...ptypes)
		throws Throwable
	{
		return MethodHandles.lookup()
			.findStatic(clazz, name, methodType(rtype, ptypes));
	}

	public LilaObject run(Class<?> clazz) throws Throwable {
		MethodHandle run = findMethod(clazz, "run", LilaObject.class);
		return (LilaObject)run.invokeExact();
	}
}

@@ -93,16 +93,53 @@
 andles;%0A
+import java.lang.invoke.MethodType;%0A%0A
 import s
@@ -460,16 +460,17 @@
 ss%3C?%3E...
+ 
 ptypes)%0A
@@ -491,16 +491,184 @@
 able%0A%09%7B%0A
+%09%09return findMethod(clazz, name, methodType(rtype, ptypes));%0A%09%7D%0A%0A%09public MethodHandle findMethod%0A%09%09(Class%3C?%3E clazz, String name, MethodType type)%0A%09%09throws Throwable%0A%09%7B%0A
 %09%09return
@@ -723,33 +723,12 @@
 me, 
-methodType(rtype, ptypes)
+type
 );%0A%09

generalize findMethod in class loader