6
6
import org .objectweb .asm .Type ;
7
7
import org .openzen .zencode .shared .CodePosition ;
8
8
import org .openzen .zenscript .codemodel .CompareType ;
9
- import org .openzen .zenscript .codemodel .FunctionParameter ;
10
9
import org .openzen .zenscript .codemodel .OperatorType ;
11
10
import org .openzen .zenscript .codemodel .definition .ExpansionDefinition ;
12
11
import org .openzen .zenscript .codemodel .expression .captured .CapturedExpression ;
24
23
import org .openzen .zenscript .javabytecode .JavaMangler ;
25
24
import org .openzen .zenscript .javabytecode .compiler .JavaModificationExpressionVisitor .PushOption ;
26
25
import org .openzen .zenscript .javabytecode .compiler .capturing .*;
26
+ import org .openzen .zenscript .javabytecode .compiler .definitions .JavaMemberVisitor ;
27
27
import org .openzen .zenscript .javashared .*;
28
28
import org .openzen .zenscript .javashared .compiling .JavaCompilingMethod ;
29
29
import org .openzen .zenscript .javashared .expressions .JavaFunctionInterfaceCastExpression ;
@@ -473,79 +473,41 @@ public Void visitFunction(FunctionExpression expression) {
473
473
return null;
474
474
}*/
475
475
476
- final String signature ;
477
476
final String [] interfaces ;
478
- final String descriptor ;
479
-
480
- {//Fill the info above
481
- if (expression .type instanceof JavaFunctionalInterfaceTypeID ) {
482
- //Let's implement the functional Interface instead
483
- JavaFunctionalInterfaceTypeID type = (JavaFunctionalInterfaceTypeID ) expression .type ;
484
- final Method functionalInterfaceMethod = type .functionalInterfaceMethod ;
485
-
486
- //Should be the same, should it not?
487
- signature = context .getMethodSignature (expression .header , true );
488
- descriptor = context .getMethodDescriptor (expression .header );
489
- interfaces = new String []{Type .getInternalName (functionalInterfaceMethod .getDeclaringClass ())};
490
- } else {
491
- //Normal way, no casting to functional interface
492
- signature = context .getMethodSignature (expression .header , true );
493
- descriptor = context .getMethodDescriptor (expression .header );
494
- interfaces = new String []{context .getInternalName (new FunctionTypeID (expression .header ))};
495
- }
477
+
478
+ if (expression .type instanceof JavaFunctionalInterfaceTypeID ) {
479
+ //Let's implement the functional Interface instead
480
+ JavaFunctionalInterfaceTypeID type = (JavaFunctionalInterfaceTypeID ) expression .type ;
481
+ final Method functionalInterfaceMethod = type .functionalInterfaceMethod ;
482
+
483
+ //Should be the same, should it not?
484
+ interfaces = new String []{Type .getInternalName (functionalInterfaceMethod .getDeclaringClass ())};
485
+ } else {
486
+ //Normal way, no casting to functional interface
487
+ interfaces = new String []{context .getInternalName (new FunctionTypeID (expression .header ))};
496
488
}
497
489
498
490
final JavaNativeMethod methodInfo ;
499
491
final String className = this .javaMangler .mangleGeneratedLambdaName (interfaces [0 ]);
500
492
{
501
493
final JavaNativeMethod m = context .getFunctionalInterface (expression .type );
502
- methodInfo = new JavaNativeMethod ( m . cls , m . kind , m . name , m . compile , m . descriptor , m . modifiers & ~JavaModifiers .ABSTRACT , m . genericResult , m . typeParameterArguments );
494
+ methodInfo = m . withModifiers ( m . modifiers & ~JavaModifiers .ABSTRACT );
503
495
}
504
496
final ClassWriter lambdaCW = new JavaClassWriter (ClassWriter .COMPUTE_FRAMES );
505
497
JavaClass lambdaClass = JavaClass .fromInternalName (className , JavaClass .Kind .CLASS );
506
498
lambdaCW .visit (Opcodes .V1_8 , Opcodes .ACC_PUBLIC , className , null , "java/lang/Object" , interfaces );
507
499
final JavaWriter functionWriter ;
508
500
509
- //Bridge method!!!
510
- if (!Objects .equals (methodInfo .descriptor , descriptor )) {
511
- final JavaNativeMethod bridgeMethodInfo = new JavaNativeMethod (methodInfo .cls , methodInfo .kind , methodInfo .name , methodInfo .compile , methodInfo .descriptor , methodInfo .modifiers | JavaModifiers .BRIDGE | JavaModifiers .SYNTHETIC , methodInfo .genericResult , methodInfo .typeParameterArguments );
512
- JavaCompilingMethod compilingBridgeMethod = new JavaCompilingMethod (javaWriter .method .class_ , bridgeMethodInfo , signature );
513
- final JavaWriter bridgeWriter = new JavaWriter (context .logger , expression .position , lambdaCW , compilingBridgeMethod , null );
514
- bridgeWriter .start ();
515
-
516
- //This.name(parameters, casted)
517
- bridgeWriter .loadObject (0 );
518
-
519
- for (int i = 0 ; i < expression .header .parameters .length ; i ++) {
520
- final FunctionParameter functionParameter = expression .header .parameters [i ];
521
- final Type type = context .getType (functionParameter .type );
522
- bridgeWriter .load (type , i + 1 );
523
- if (!CompilerUtils .isPrimitive (functionParameter .type )) {
524
- bridgeWriter .checkCast (type );
525
- }
526
- }
527
-
528
- bridgeWriter .invokeVirtual (new JavaNativeMethod (JavaClass .fromInternalName (className , JavaClass .Kind .CLASS ), JavaNativeMethod .Kind .INSTANCE , methodInfo .name , methodInfo .compile , descriptor , methodInfo .modifiers , methodInfo .genericResult ));
529
- final TypeID returnType = expression .header .getReturnType ();
530
- if (returnType != BasicTypeID .VOID ) {
531
- final Type returnTypeASM = context .getType (returnType );
532
- if (!CompilerUtils .isPrimitive (returnType )) {
533
- bridgeWriter .checkCast (returnTypeASM );
534
- }
535
- bridgeWriter .returnType (returnTypeASM );
536
- }
537
-
538
- bridgeWriter .ret ();
539
- bridgeWriter .end ();
540
-
541
- JavaNativeMethod actualMethod = methodInfo .createBridge (context .getMethodDescriptor (expression .header ));
542
- JavaCompilingMethod actualCompiling = new JavaCompilingMethod (lambdaClass , actualMethod , signature );
543
- //No @Override
544
- functionWriter = new JavaWriter (context .logger , expression .position , lambdaCW , actualCompiling , null );
545
- } else {
546
- JavaCompilingMethod actualCompiling = new JavaCompilingMethod (lambdaClass , methodInfo , signature );
547
- functionWriter = new JavaWriter (context .logger , expression .position , lambdaCW , actualCompiling , null );
548
- }
501
+ JavaCompilingMethod actualCompiling = JavaMemberVisitor .compileBridgeableMethod (
502
+ context ,
503
+ expression .position ,
504
+ lambdaCW ,
505
+ lambdaClass ,
506
+ methodInfo ,
507
+ expression .header ,
508
+ null
509
+ );
510
+ functionWriter = new JavaWriter (context .logger , expression .position , lambdaCW , actualCompiling , null );
549
511
functionWriter .clazzVisitor .visitSource (expression .position .getFilename (), null );
550
512
javaWriter .newObject (className );
551
513
javaWriter .dup ();
@@ -554,7 +516,7 @@ public Void visitFunction(FunctionExpression expression) {
554
516
// To check: write a test where the ctor desc and signature would differ and make sure the program compiles/executes
555
517
final String constructorDescriptorAndSignature = calcFunctionDescriptor (expression .closure );
556
518
JavaNativeMethod constructor = JavaNativeMethod .getConstructor (lambdaClass , constructorDescriptorAndSignature , Opcodes .ACC_PUBLIC );
557
- JavaCompilingMethod constructorCompiling = new JavaCompilingMethod (lambdaClass , constructor , constructorDescriptorAndSignature );
519
+ JavaCompilingMethod constructorCompiling = new JavaCompilingMethod (constructor , constructorDescriptorAndSignature );
558
520
final JavaWriter constructorWriter = new JavaWriter (context .logger , expression .position , lambdaCW , constructorCompiling , null );
559
521
constructorWriter .start ();
560
522
constructorWriter .loadObject (0 );
@@ -1193,7 +1155,7 @@ private FunctionCastWrapperClass generateFunctionCastWrapperClass(CodePosition p
1193
1155
//Constructor
1194
1156
{
1195
1157
JavaNativeMethod constructor = JavaNativeMethod .getConstructor (classInfo , constructorDescriptor , Opcodes .ACC_PUBLIC );
1196
- JavaCompilingMethod compiling = new JavaCompilingMethod (classInfo , constructor , constructorSignature );
1158
+ JavaCompilingMethod compiling = new JavaCompilingMethod (constructor , constructorSignature );
1197
1159
final JavaWriter constructorWriter = new JavaWriter (context .logger , position , lambdaCW , compiling , null );
1198
1160
constructorWriter .start ();
1199
1161
constructorWriter .loadObject (0 );
@@ -1209,7 +1171,7 @@ private FunctionCastWrapperClass generateFunctionCastWrapperClass(CodePosition p
1209
1171
1210
1172
//The actual method
1211
1173
{
1212
- JavaCompilingMethod compiling = new JavaCompilingMethod (classInfo , implementationMethod , methodSignature );
1174
+ JavaCompilingMethod compiling = new JavaCompilingMethod (implementationMethod , methodSignature );
1213
1175
final JavaWriter functionWriter = new JavaWriter (context .logger , position , lambdaCW , compiling , null );
1214
1176
functionWriter .start ();
1215
1177
0 commit comments