package squidev.ccstudio.core.asm;

import jdk.internal.org.objectweb.asm.Type;
import org.luaj.vm2.lib.LibFunction;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

/**
 * A class writer that resolves classes to LibFunction if it can't guess their super class
 */
public class LuaClassWriter extends ClassWriter {
	private static final String LIBFUNCTION = Type.getInternalName(LibFunction.class);

	/**
	 * Constructs a new {@link org.objectweb.asm.ClassWriter} object and enables optimizations for
	 * "mostly add" bytecode transformations. These optimizations are the
	 * following:
	 * <p>
	 * <ul>
	 * <li>The constant pool from the original class is copied as is in the new
	 * class, which saves time. New constant pool entries will be added at the
	 * end if necessary, but unused constant pool entries <i>won't be
	 * removed</i>.</li>
	 * <li>Methods that are not transformed are copied as is in the new class,
	 * directly from the original class bytecode (i.e. without emitting visit
	 * events for all the method instructions), which saves a <i>lot</i> of
	 * time. Untransformed methods are detected by the fact that the
	 * {@link org.objectweb.asm.ClassReader} receives {@link org.objectweb.asm.MethodVisitor} objects that come from
	 * a {@link org.objectweb.asm.ClassWriter} (and not from any other {@link org.objectweb.asm.ClassVisitor}
	 * instance).</li>
	 * </ul>
	 *
	 * @param classReader the {@link org.objectweb.asm.ClassReader} used to read the original class. It
	 *                    will be used to copy the entire constant pool from the
	 *                    original class and also to copy other fragments of original
	 *                    bytecode where applicable.
	 * @param flags       option flags that can be used to modify the default behavior
	 *                    of this class. <i>These option flags do not affect methods
	 *                    that are copied as is in the new class. This means that the
	 *                    maximum stack size nor the stack frames will be computed for
	 *                    these methods</i>. See {@link #COMPUTE_MAXS},
	 *                    {@link #COMPUTE_FRAMES}.
	 */
	public LuaClassWriter(ClassReader classReader, int flags) {
		super(classReader, flags);
	}

	/**
	 * Constructs a new {@link org.objectweb.asm.ClassWriter} object.
	 *
	 * @param flags option flags that can be used to modify the default behavior
	 *              of this class. See {@link #COMPUTE_MAXS},
	 *              {@link #COMPUTE_FRAMES}.
	 */
	public LuaClassWriter(int flags) {
		super(flags);
	}

	/**
	 * Returns the common super type of the two given types. The default
	 * implementation of this method <i>loads</i> the two given classes and uses
	 * the java.lang.Class methods to find the common super class. It can be
	 * overridden to compute this common super type in other ways, in particular
	 * without actually loading any class, or to take into account the class
	 * that is currently being generated by this ClassWriter, which can of
	 * course not be loaded since it is under construction.
	 *
	 * @param type1 the internal name of a class.
	 * @param type2 the internal name of another class.
	 * @return the internal name of the common super class of the two given
	 * classes.
	 */
	@Override
	protected String getCommonSuperClass(String type1, String type2) {
		try {
			return super.getCommonSuperClass(type1, type2);
		} catch (RuntimeException e) {
			if (e.getMessage().contains("NotFound") && type1.contains("_LuaCompiled") && type2.contains("_LuaCompiled")) {
				return LIBFUNCTION;
			}
			return "java/lang/Object";
		}
	}
}

@@ -40,118 +40,36 @@
 ort 
-jdk.internal.org.objectweb.asm.Type;%0Aimport org.luaj.vm2.lib.LibFunction;%0Aimport org.objectweb.asm.ClassReader
+org.luaj.vm2.lib.LibFunction
 ;%0Aim
@@ -103,16 +103,47 @@
 sWriter;
+%0Aimport org.objectweb.asm.Type;
 %0A%0A/**%0A *
@@ -285,16 +285,16 @@
 riter %7B%0A
+
 %09private
@@ -374,1860 +374,8 @@
 );%0A%0A
-%09/**%0A%09 * Constructs a new %7B@link org.objectweb.asm.ClassWriter%7D object and enables optimizations for%0A%09 * %22mostly add%22 bytecode transformations. These optimizations are the%0A%09 * following:%0A%09 * %3Cp%3E%0A%09 * %3Cul%3E%0A%09 * %3Cli%3EThe constant pool from the original class is copied as is in the new%0A%09 * class, which saves time. New constant pool entries will be added at the%0A%09 * end if necessary, but unused constant pool entries %3Ci%3Ewon't be%0A%09 * removed%3C/i%3E.%3C/li%3E%0A%09 * %3Cli%3EMethods that are not transformed are copied as is in the new class,%0A%09 * directly from the original class bytecode (i.e. without emitting visit%0A%09 * events for all the method instructions), which saves a %3Ci%3Elot%3C/i%3E of%0A%09 * time. Untransformed methods are detected by the fact that the%0A%09 * %7B@link org.objectweb.asm.ClassReader%7D receives %7B@link org.objectweb.asm.MethodVisitor%7D objects that come from%0A%09 * a %7B@link org.objectweb.asm.ClassWriter%7D (and not from any other %7B@link org.objectweb.asm.ClassVisitor%7D%0A%09 * instance).%3C/li%3E%0A%09 * %3C/ul%3E%0A%09 *%0A%09 * @param classReader the %7B@link org.objectweb.asm.ClassReader%7D used to read the original class. It%0A%09 *                    will be used to copy the entire constant pool from the%0A%09 *                    original class and also to copy other fragments of original%0A%09 *                    bytecode where applicable.%0A%09 * @param flags       option flags that can be used to modify the default behavior%0A%09 *                    of this class. %3Ci%3EThese option flags do not affect methods%0A%09 *                    that are copied as is in the new class. This means that the%0A%09 *                    maximum stack size nor the stack frames will be computed for%0A%09 *                    these methods%3C/i%3E. See %7B@link #COMPUTE_MAXS%7D,%0A%09 *                    %7B@link #COMPUTE_FRAMES%7D.%0A%09 */%0A%09public LuaClassWriter(ClassReader classReader, int flags) %7B%0A%09%09super(classReader, flags);%0A%09%7D%0A%0A
 %09/**

Fix ASM import