Skip to content

Commit

Permalink
feat: functional enough
Browse files Browse the repository at this point in the history
  • Loading branch information
hugeblank committed Sep 27, 2024
1 parent 486dc14 commit ed58ba0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.squiddev.cobalt.LuaValue;
import org.squiddev.cobalt.ValueFactory;
import org.squiddev.cobalt.Varargs;
import org.squiddev.cobalt.function.Dispatch;
import org.squiddev.cobalt.function.LuaFunction;

import java.util.HashMap;
Expand Down Expand Up @@ -110,10 +111,9 @@ private static BiFunction<LuaState, LuaFunction, Object> generateProxyClass(ECla
if (!isVoid) m.visitInsn(DUP);
m.visitVarInsn(ALOAD, 0);
m.visitFieldInsn(GETFIELD, className, FUNCTION_FIELD_NAME, Type.getDescriptor(LuaFunction.class));
m.visitInsn(SWAP);
m.visitVarInsn(ALOAD, varPrefix);
m.visitMethodInsn(INVOKESTATIC, Type.getInternalName(ValueFactory.class), "varargsOf", "([Lorg/squiddev/cobalt/LuaValue;)Lorg/squiddev/cobalt/Varargs;", false);
m.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(LuaFunction.class), "invoke", "(Lorg/squiddev/cobalt/LuaState;Lorg/squiddev/cobalt/Varargs;)Lorg/squiddev/cobalt/Varargs;", false);
m.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Dispatch.class), "invoke", "(Lorg/squiddev/cobalt/LuaState;Lorg/squiddev/cobalt/LuaValue;Lorg/squiddev/cobalt/Varargs;)Lorg/squiddev/cobalt/Varargs;", false);

if (!isVoid) {
m.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(Varargs.class), "first", "()Lorg/squiddev/cobalt/LuaValue;", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import me.basiqueevangelist.enhancedreflection.api.typeuse.EClassUse;
import net.minecraft.util.Identifier;
import org.squiddev.cobalt.*;
import org.squiddev.cobalt.function.LuaFunction;

import java.lang.reflect.Array;
import java.util.*;
Expand Down Expand Up @@ -55,9 +56,65 @@ public static Object toJava(LuaState state, LuaValue value, EClass<?> clatz) thr
return null;

if (value instanceof AlliumUserdata<?> userdata)
return userdata.toUserdata(clatz.wrapPrimitive());

return null;
try {
return userdata.toUserdata(clatz.wrapPrimitive());
} catch (ClassCastException e) {
throw new InvalidArgumentException(e);
}

clatz = clatz.unwrapPrimitive();

var deserializerFactory = FROM_LUA.get(clatz.raw());
if (deserializerFactory != null) {
var deserializer = deserializerFactory.apply(clatz);

if (deserializer != null) {
Object result = deserializer.fromLua(state, value);

if (result != null) return result;
}
}

if (clatz.type() == ClassType.ARRAY) {
try {
LuaTable table = value.checkTable();
int length = table.length();
Object arr = Array.newInstance(clatz.arrayComponent().raw(), table.length());
for (int i = 0; i < length; i++) {
Array.set(arr, i, toJava(state, table.rawget(i + 1), clatz.arrayComponent()));
}
return clatz.cast(arr);
} catch (Exception e) {
throw new LuaError(
"Expected table of "
+ clatz.arrayComponent()
+ "s, got "
+ value.typeName()
);
}
}

if (value instanceof LuaFunction func && clatz.type() == ClassType.INTERFACE) { // Callbacks
EMethod ifaceMethod = null;

int unimplemented = 0;
for (var meth : clatz.methods()) {
if (meth.isAbstract()) {
unimplemented++;
ifaceMethod = meth;

if (unimplemented > 1) {
break;
}
}
}

if (unimplemented == 1) {
return ProxyGenerator.getProxyFactory(clatz, ifaceMethod).apply(state, func);
} // TODO: Weird code was removed here. Did that break anything?
}

throw new InvalidArgumentException("Couldn't convert " + value + " to java! Target type is " + clatz);
}

public static LuaValue toLuaValue(Object out) {
Expand Down

0 comments on commit ed58ba0

Please sign in to comment.