-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLuaBinder.java
More file actions
349 lines (310 loc) · 12.3 KB
/
LuaBinder.java
File metadata and controls
349 lines (310 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package dev.amble.lib.script.lua;
import dev.amble.lib.client.gui.AmbleElement;
import dev.amble.lib.client.gui.lua.LuaElement;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import org.joml.Vector3f;
import org.luaj.vm2.*;
import org.luaj.vm2.lib.*;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
import org.luaj.vm2.lib.jse.CoerceLuaToJava;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Binds Java objects to Lua, exposing methods annotated with @LuaExpose.
* Uses MethodHandles for improved performance over reflection.
*/
public final class LuaBinder {
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final Map<Class<?>, LuaTable> CACHE = new HashMap<>();
public static LuaValue bind(Object target) {
LuaTable table = CACHE.computeIfAbsent(
target.getClass(),
LuaBinder::buildMetatable
);
LuaUserdata userdata = new LuaUserdata(target);
userdata.setmetatable(table);
return userdata;
}
// ===== Separate coerceResult methods for each type =====
/**
* Coerces a null value to Lua NIL.
*/
public static LuaValue coerceNull() {
return LuaValue.NIL;
}
/**
* Coerces a String to a Lua string.
*/
public static LuaValue coerceString(String value) {
return LuaString.valueOf(value);
}
/**
* Coerces an integer to a Lua number.
*/
public static LuaValue coerceInt(int value) {
return LuaInteger.valueOf(value);
}
/**
* Coerces a long to a Lua number.
*/
public static LuaValue coerceLong(long value) {
return LuaInteger.valueOf(value);
}
/**
* Coerces a float to a Lua number.
*/
public static LuaValue coerceFloat(float value) {
return LuaDouble.valueOf(value);
}
/**
* Coerces a double to a Lua number.
*/
public static LuaValue coerceDouble(double value) {
return LuaDouble.valueOf(value);
}
/**
* Coerces a boolean to a Lua boolean.
*/
public static LuaValue coerceBoolean(boolean value) {
return LuaBoolean.valueOf(value);
}
/**
* Coerces a List to a Lua table (1-indexed array).
*/
public static LuaValue coerceList(List<?> list) {
LuaTable table = new LuaTable();
for (int i = 0; i < list.size(); i++) {
table.set(i + 1, coerceResult(list.get(i)));
}
return table;
}
/**
* Coerces a Vector3f to a Lua table with x, y, z fields.
*/
public static LuaValue coerceVector3f(Vector3f vec3) {
LuaTable table = new LuaTable();
table.set("x", vec3.x());
table.set("y", vec3.y());
table.set("z", vec3.z());
table.set("toString", new ZeroArgFunction() {
@Override
public LuaValue call() {
return LuaString.valueOf("(" + vec3.x() + ", " + vec3.y() + ", " + vec3.z() + ")");
}
});
return table;
}
/**
* Coerces a Vec3d to a Lua table with x, y, z fields.
*/
public static LuaValue coerceVec3d(Vec3d vec3) {
LuaTable table = new LuaTable();
table.set("x", vec3.x);
table.set("y", vec3.y);
table.set("z", vec3.z);
table.set("toString", new ZeroArgFunction() {
@Override
public LuaValue call() {
return LuaString.valueOf("(" + vec3.x + ", " + vec3.y + ", " + vec3.z + ")");
}
});
return table;
}
/**
* Coerces a BlockPos to a Lua table with x, y, z fields.
*/
public static LuaValue coerceBlockPos(BlockPos pos) {
LuaTable table = new LuaTable();
table.set("x", pos.getX());
table.set("y", pos.getY());
table.set("z", pos.getZ());
table.set("toString", new ZeroArgFunction() {
@Override
public LuaValue call() {
return LuaString.valueOf("(" + pos.getX() + ", " + pos.getY() + ", " + pos.getZ() + ")");
}
});
return table;
}
/**
* Coerces an ItemStack to a bound LuaItemStack.
*/
public static LuaValue coerceItemStack(ItemStack stack) {
return bind(new LuaItemStack(stack));
}
/**
* Coerces an Entity to a bound MinecraftEntity.
*/
public static LuaValue coerceEntity(Entity entity) {
return bind(new MinecraftEntity(entity));
}
/**
* Coerces an Identifier to a Lua string.
*/
public static LuaValue coerceIdentifier(Identifier identifier) {
return LuaString.valueOf(identifier.toString());
}
/**
* Coerces an AmbleElement to a bound LuaElement.
*/
public static LuaValue coerceAmbleElement(AmbleElement element) {
if (element instanceof LuaElement luaElement) {
return bind(luaElement);
}
return bind(new LuaElement(element));
}
/**
* Coerces an NbtCompound to a Lua table.
*/
public static LuaValue coerceNbtCompound(NbtCompound nbt) {
LuaTable table = new LuaTable();
for (String key : nbt.getKeys()) {
NbtElement element = nbt.get(key);
table.set(key, coerceNbtElement(element));
}
return table;
}
/**
* Coerces any NbtElement to the appropriate Lua type.
*/
public static LuaValue coerceNbtElement(NbtElement element) {
if (element == null) {
return LuaValue.NIL;
}
return switch (element.getType()) {
case NbtElement.BYTE_TYPE -> LuaInteger.valueOf(((net.minecraft.nbt.NbtByte) element).byteValue());
case NbtElement.SHORT_TYPE -> LuaInteger.valueOf(((net.minecraft.nbt.NbtShort) element).shortValue());
case NbtElement.INT_TYPE -> LuaInteger.valueOf(((net.minecraft.nbt.NbtInt) element).intValue());
case NbtElement.LONG_TYPE -> LuaInteger.valueOf(((net.minecraft.nbt.NbtLong) element).longValue());
case NbtElement.FLOAT_TYPE -> LuaDouble.valueOf(((net.minecraft.nbt.NbtFloat) element).floatValue());
case NbtElement.DOUBLE_TYPE -> LuaDouble.valueOf(((net.minecraft.nbt.NbtDouble) element).doubleValue());
case NbtElement.STRING_TYPE -> LuaString.valueOf(element.asString());
case NbtElement.BYTE_ARRAY_TYPE -> {
byte[] bytes = ((net.minecraft.nbt.NbtByteArray) element).getByteArray();
LuaTable table = new LuaTable();
for (int i = 0; i < bytes.length; i++) {
table.set(i + 1, LuaInteger.valueOf(bytes[i]));
}
yield table;
}
case NbtElement.INT_ARRAY_TYPE -> {
int[] ints = ((net.minecraft.nbt.NbtIntArray) element).getIntArray();
LuaTable table = new LuaTable();
for (int i = 0; i < ints.length; i++) {
table.set(i + 1, LuaInteger.valueOf(ints[i]));
}
yield table;
}
case NbtElement.LONG_ARRAY_TYPE -> {
long[] longs = ((net.minecraft.nbt.NbtLongArray) element).getLongArray();
LuaTable table = new LuaTable();
for (int i = 0; i < longs.length; i++) {
table.set(i + 1, LuaInteger.valueOf(longs[i]));
}
yield table;
}
case NbtElement.LIST_TYPE -> {
NbtList list = (NbtList) element;
LuaTable table = new LuaTable();
for (int i = 0; i < list.size(); i++) {
table.set(i + 1, coerceNbtElement(list.get(i)));
}
yield table;
}
case NbtElement.COMPOUND_TYPE -> coerceNbtCompound((NbtCompound) element);
default -> LuaString.valueOf(element.asString());
};
}
// ===== Main coerceResult dispatcher =====
/**
* Coerces any Java object to an appropriate Lua value.
* Dispatches to type-specific coercion methods.
*/
public static LuaValue coerceResult(Object obj) {
if (obj == null) return coerceNull();
if (obj instanceof LuaValue lv) return lv;
if (obj instanceof String s) return coerceString(s);
if (obj instanceof Integer i) return coerceInt(i);
if (obj instanceof Long l) return coerceLong(l);
if (obj instanceof Float f) return coerceFloat(f);
if (obj instanceof Double d) return coerceDouble(d);
if (obj instanceof Boolean b) return coerceBoolean(b);
if (obj instanceof Number n) return CoerceJavaToLua.coerce(n);
if (obj instanceof List<?> list) return coerceList(list);
if (obj instanceof Vector3f vec3) return coerceVector3f(vec3);
if (obj instanceof Vec3d vec3) return coerceVec3d(vec3);
if (obj instanceof BlockPos pos) return coerceBlockPos(pos);
if (obj instanceof ItemStack stack) return coerceItemStack(stack);
if (obj instanceof Entity entity) return coerceEntity(entity);
if (obj instanceof NbtCompound nbt) return coerceNbtCompound(nbt);
if (obj instanceof NbtElement nbt) return coerceNbtElement(nbt);
if (obj instanceof Identifier id) return coerceIdentifier(id);
if (obj instanceof LuaElement luaElement) return bind(luaElement);
if (obj instanceof AmbleElement element) return bind(new LuaElement(element));
// Fall back to binding the object
return bind(obj);
}
private static LuaTable buildMetatable(Class<?> clazz) {
LuaTable meta = new LuaTable();
LuaTable index = new LuaTable();
for (Method method : clazz.getMethods()) {
LuaExpose expose = method.getAnnotation(LuaExpose.class);
if (expose == null) continue;
String luaName = expose.name().isEmpty()
? method.getName()
: expose.name();
// Convert Method to MethodHandle for better performance
MethodHandle handle;
try {
handle = LOOKUP.unreflect(method);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to create MethodHandle for " + method.getName(), e);
}
Class<?>[] paramTypes = method.getParameterTypes();
String methodName = method.getName();
index.set(luaName, new VarArgFunction() {
@Override
public Varargs invoke(Varargs args) {
try {
LuaValue selfValue = args.arg1();
if (!selfValue.isuserdata()) {
throw new LuaError("Expected userdata but got " + selfValue.typename());
}
Object javaSelf = selfValue.touserdata();
if (javaSelf == null || !clazz.isInstance(javaSelf)) {
throw new LuaError("Expected userdata of type " + clazz.getName() + " but got " + (javaSelf == null ? "null" : javaSelf.getClass().getName()));
}
// Build argument array: [self, arg1, arg2, ...]
Object[] invokeArgs = new Object[paramTypes.length + 1];
invokeArgs[0] = javaSelf;
for (int i = 0; i < paramTypes.length; i++) {
invokeArgs[i + 1] = CoerceLuaToJava.coerce(
args.arg(i + 2),
paramTypes[i]
);
}
Object result = handle.invokeWithArguments(invokeArgs);
return LuaBinder.coerceResult(result);
} catch (LuaError e) {
throw e;
} catch (Throwable e) {
throw new LuaError("Lua call failed: " + methodName + " " + e);
}
}
});
}
meta.set("__index", index);
return meta;
}
}