Skip to content

Commit

Permalink
merge: branch 'hugeblank/bouquet/javalib-refactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
hugeblank committed Sep 17, 2024
2 parents 825cc14 + 1262f5e commit a4bae2c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
107 changes: 11 additions & 96 deletions bouquet/src/main/java/dev/hugeblank/bouquet/api/lib/JavaLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
Expand All @@ -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<String> 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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,7 +33,7 @@ public LuaValue index(String type) throws LuaError {
}
Class<? extends ArgumentType<?>> clazz = types.get(toLoad);
try {
return JavaLib.importClass(EClass.fromJava(clazz));
return StaticBinder.bindClass(EClass.fromJava(clazz));
} catch (Exception e) {
throw new LuaError(e);
}
Expand Down

0 comments on commit a4bae2c

Please sign in to comment.