diff --git a/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/ClassBuilder.java b/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/ClassBuilder.java index d53d0ec..61c203a 100755 --- a/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/ClassBuilder.java +++ b/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/ClassBuilder.java @@ -1,5 +1,6 @@ package dev.hugeblank.bouquet.api.lib; +import dev.hugeblank.allium.loader.type.StaticBinder; import dev.hugeblank.allium.loader.type.coercion.TypeCoercions; import dev.hugeblank.allium.loader.type.annotation.LuaWrapped; import dev.hugeblank.allium.loader.type.property.PropertyResolver; @@ -208,7 +209,7 @@ public LuaValue build() { fields.apply(klass); - return JavaLib.importClass(EClass.fromJava(klass)); + return StaticBinder.bindClass(EClass.fromJava(klass)); } public String getName() { diff --git a/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/JavaLib.java b/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/JavaLib.java index 7457c18..f0091de 100755 --- a/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/JavaLib.java +++ b/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/JavaLib.java @@ -8,72 +8,15 @@ import dev.hugeblank.allium.loader.type.annotation.LuaWrapped; import dev.hugeblank.allium.loader.type.annotation.OptionalArg; import dev.hugeblank.allium.loader.type.coercion.TypeCoercions; -import dev.hugeblank.allium.util.AnnotationUtils; -import dev.hugeblank.allium.util.ArgumentUtils; import dev.hugeblank.allium.util.JavaHelpers; import me.basiqueevangelist.enhancedreflection.api.EClass; -import me.basiqueevangelist.enhancedreflection.api.EMethod; -import me.basiqueevangelist.enhancedreflection.api.typeuse.EClassUse; import org.squiddev.cobalt.*; -import java.lang.reflect.InaccessibleObjectException; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; import java.util.List; @LuaWrapped(name = "java") public class JavaLib implements WrappedLuaLibrary { - @LuaWrapped(name = "import") - public static LuaValue importClass(EClass clazz) { - return StaticBinder.bindClass(clazz); - } - - // TODO: Is this used anywhere? - private static Varargs invokeStatic(EClass clazz, String name, EMethod[] methods, LuaState state, Varargs args) throws LuaError { - List paramList = new ArrayList<>(); // String for displaying errors more smartly - StringBuilder error = new StringBuilder("Could not find parameter match for called function \"" + - name + "\" for \"" + clazz.name() + "\"" + - "\nThe following are correct argument types:\n" - ); - - for (var method : methods) { - var parameters = method.parameters(); - try { - var jargs = ArgumentUtils.toJavaArguments(state, args, 1, parameters); - if (jargs.length == parameters.size()) { // Found a match! - try { // Get the return type, invoke method, cast returned value, cry. - EClassUse ret = method.returnTypeUse().upperBound(); -// method.setAccessible(true); // throws InaccessibleObjectException | SecurityException - Object out = method.invoke(null, jargs); - return TypeCoercions.toLuaValue(out, ret); - } catch (InaccessibleObjectException | SecurityException | IllegalAccessException e) { - throw new LuaError(e); - } catch (InvocationTargetException e) { - if (e.getTargetException() instanceof LuaError err) - throw err; - - throw new LuaError(e); - } - } - } catch (InvalidArgumentException e) { - paramList.add(ArgumentUtils.paramsToPrettyString(parameters)); - } - } - - for (String headers : paramList) { - error.append(headers).append("\n"); - } - - throw new LuaError(error.toString()); - } - - // TODO: consider merging this with the Lua string library - @LuaWrapped - public static String[] split(String strToSplit, String delimiter) { - return strToSplit.split(delimiter); - } - @LuaWrapped public static String toYarn(String string) { return Allium.MAPPINGS.getYarn(string); @@ -84,50 +27,22 @@ public static String toYarn(String string) { return Allium.MAPPINGS.getIntermediary(string); } - // TODO: Is this used anywhere? - private static Varargs createInstance(LuaState state, EClass clazz, Varargs args) throws LuaError { - List paramList = new ArrayList<>(); - for (var constructor : clazz.constructors()) { - if (AnnotationUtils.isHiddenFromLua(constructor)) continue; - - var parameters = constructor.parameters(); - try { - var jargs = ArgumentUtils.toJavaArguments(state, args, 1, parameters); - - try { // Get the return type, invoke method, cast returned value, cry. - EClassUse ret = (EClassUse) constructor.receiverTypeUse(); - - if (ret == null) ret = clazz.asEmptyUse(); - - Object out = constructor.invoke(jargs); - return TypeCoercions.toLuaValue(out, ret); - } catch (IllegalAccessException | InvocationTargetException | InstantiationException e) { - throw new LuaError(e); - } - } catch (InvalidArgumentException e) { - paramList.add(ArgumentUtils.paramsToPrettyString(parameters)); - } - } - - StringBuilder error = new StringBuilder("Could not find parameter match for called constructor " + - clazz.name() + - "\nThe following are correct argument types:\n" - ); - - for (String headers : paramList) { - error.append(headers).append("\n"); + @LuaWrapped + public static LuaValue cast(@LuaStateArg LuaState state, LuaUserdata object, EClass klass) throws LuaError { + try { + return TypeCoercions.toLuaValue(TypeCoercions.toJava(state, object, klass), klass); + } catch (InvalidArgumentException e) { + throw new LuaError(e); } - - throw new LuaError(error.toString()); } @LuaWrapped - public static LuaValue cast(@LuaStateArg LuaState state, EClass klass, LuaUserdata object) throws LuaError { + public static boolean instanceOf(@LuaStateArg LuaState state, LuaUserdata object, EClass klass) { try { - return TypeCoercions.toLuaValue(TypeCoercions.toJava(state, object, klass), klass); - } catch (InvalidArgumentException e) { - e.printStackTrace(); - return Constants.NIL; + Object obj = TypeCoercions.toJava(state, object, Object.class); + return klass.isAssignableFrom(obj.getClass()); + } catch (LuaError | InvalidArgumentException e) { + return false; } } diff --git a/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/commands/ArgumentTypeLib.java b/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/commands/ArgumentTypeLib.java index 5d7edd4..df5fa45 100755 --- a/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/commands/ArgumentTypeLib.java +++ b/bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/commands/ArgumentTypeLib.java @@ -1,6 +1,7 @@ package dev.hugeblank.bouquet.api.lib.commands; import com.mojang.brigadier.arguments.ArgumentType; +import dev.hugeblank.allium.loader.type.StaticBinder; import dev.hugeblank.allium.loader.type.annotation.LuaIndex; import dev.hugeblank.allium.loader.type.annotation.LuaWrapped; import me.basiqueevangelist.enhancedreflection.api.EClass; @@ -32,7 +33,7 @@ public LuaValue index(String type) throws LuaError { } Class> clazz = types.get(toLoad); try { - return JavaLib.importClass(EClass.fromJava(clazz)); + return StaticBinder.bindClass(EClass.fromJava(clazz)); } catch (Exception e) { throw new LuaError(e); }