@@ -62,15 +62,25 @@ public static MethodHandle callTarget(LuaType[] argTypes, LuaFunction function,
62
62
var ctx = LuaContext .forFunction (function .owner (), function .type (), truncateReturn , argTypes );
63
63
var code = generateCode (ctx , function .type (), argTypes , upvalueTypes );
64
64
try {
65
- var lookup = LOOKUP .defineHiddenClassWithClassData (code , ctx .allClassData (), true );
65
+ // Load the class with single-use class loader
66
+ // Using hidden classes would be preferable, but JVM hides them from stack frames
67
+ // ... which really screws up stack traces of Lua code
68
+ // See https://bugs.openjdk.org/browse/JDK-8212620
69
+ var implClass = SingleClassLoader .load ("unknown" , code );
70
+ try {
71
+ LOOKUP .findStaticSetter (implClass , ClassData .FIELD_NAME , Object [].class )
72
+ .invokeExact (ctx .allClassData ());
73
+ } catch (Throwable e ) {
74
+ throw new AssertionError (e ); // We just generated the field!
75
+ }
66
76
67
77
// Cache the constructor and actual function MHs
68
78
// They'll hold references to the underlying class
69
79
var jvmUpvalueTypes = Arrays .stream (upvalueTypes )
70
80
.map (LuaType ::backingType )
71
81
.map (Type ::loadedClass )
72
82
.toArray (Class []::new );
73
- var constructor = lookup .findConstructor (lookup . lookupClass () ,
83
+ var constructor = LOOKUP .findConstructor (implClass ,
74
84
MethodType .methodType (void .class , jvmUpvalueTypes ));
75
85
76
86
var normalArgCount = function .type ().acceptedArgs ().size ();
@@ -90,7 +100,7 @@ public static MethodHandle callTarget(LuaType[] argTypes, LuaFunction function,
90
100
91
101
var jvmReturnType = ctx .returnType ().equals (LuaType .NIL )
92
102
? Type .VOID : ctx .returnType ().backingType ();
93
- var method = LOOKUP .findVirtual (lookup . lookupClass () , "call" ,
103
+ var method = LOOKUP .findVirtual (implClass , "call" ,
94
104
MethodType .methodType (jvmReturnType .loadedClass (), jvmArgTypes ));
95
105
96
106
return new CompiledFunction (constructor , method );
@@ -118,7 +128,10 @@ private static byte[] generateCode(LuaContext ctx, LuaType.Function type,
118
128
LuaType [] argTypes , LuaType [] upvalueTypes ) {
119
129
// Create class that wraps the method acting as function body
120
130
// TODO store name in function if available?
121
- var def = ClassDef .create ("fi.benjami.code4jvm.lua.compiler.CompiledFunction" , Access .PUBLIC );
131
+ var def = ClassDef .create ("unknown" , Access .PUBLIC );
132
+
133
+ // Class data constants
134
+ def .addStaticField (Access .PUBLIC , Type .OBJECT .array (1 ), ClassData .FIELD_NAME );
122
135
123
136
// Add fields for upvalues
124
137
for (var i = 0 ; i < upvalueTypes .length ; i ++) {
0 commit comments