mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Support for default and static methods in Functors
This commit is contained in:
parent
eba3fa3ac4
commit
5ecfb3620c
|
@ -991,7 +991,9 @@ class JSClassProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isProperFunctor(ClassReader type) {
|
private boolean isProperFunctor(ClassReader type) {
|
||||||
return type.hasModifier(ElementModifier.INTERFACE) && type.getMethods().size() == 1;
|
return type.hasModifier(ElementModifier.INTERFACE)
|
||||||
|
&& type.getMethods().stream().filter(it->it.hasModifier(ElementModifier.ABSTRACT))
|
||||||
|
.toArray().length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Variable wrapFunctor(CallLocation location, Variable var, ClassReader type) {
|
private Variable wrapFunctor(CallLocation location, Variable var, ClassReader type) {
|
||||||
|
@ -999,7 +1001,8 @@ class JSClassProcessor {
|
||||||
diagnostics.error(location, "Wrong functor: {{c0}}", type.getName());
|
diagnostics.error(location, "Wrong functor: {{c0}}", type.getName());
|
||||||
return var;
|
return var;
|
||||||
}
|
}
|
||||||
String name = type.getMethods().iterator().next().getName();
|
String name = type.getMethods().stream()
|
||||||
|
.filter(it->it.hasModifier(ElementModifier.ABSTRACT)).findFirst().get().getName();
|
||||||
Variable functor = program.createVariable();
|
Variable functor = program.createVariable();
|
||||||
Variable nameVar = addStringWrap(addString(name, location.getSourceLocation()), location.getSourceLocation());
|
Variable nameVar = addStringWrap(addString(name, location.getSourceLocation()), location.getSourceLocation());
|
||||||
InvokeInstruction insn = new InvokeInstruction();
|
InvokeInstruction insn = new InvokeInstruction();
|
||||||
|
|
|
@ -41,6 +41,24 @@ public class FunctorTest {
|
||||||
assertSame(firstRef, secondRef);
|
assertSame(firstRef, secondRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void functorWithDefaultMethodPassed(){
|
||||||
|
JSFunctionWithDefaultMethod javaFunction = (s) -> s+" returned";
|
||||||
|
|
||||||
|
String returned = javaFunction.defaultMethod();
|
||||||
|
|
||||||
|
assertEquals(returned, "Content returned");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void functorWithStaticMethodPassed(){
|
||||||
|
JSFunctionWithStaticMethod javaFunction = (s) -> s+" returned";
|
||||||
|
|
||||||
|
String returned = javaFunction.apply(JSFunctionWithStaticMethod.staticMethod());
|
||||||
|
|
||||||
|
assertEquals(returned, "Content returned");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void propertyWithNonAlphabeticFirstChar() {
|
public void propertyWithNonAlphabeticFirstChar() {
|
||||||
WithProperties wp = getWithPropertiesInstance();
|
WithProperties wp = getWithPropertiesInstance();
|
||||||
|
@ -63,6 +81,24 @@ public class FunctorTest {
|
||||||
int apply(int a, int b);
|
int apply(int a, int b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JSFunctor
|
||||||
|
interface JSFunctionWithDefaultMethod extends JSObject {
|
||||||
|
String apply(String a);
|
||||||
|
|
||||||
|
default String defaultMethod(){
|
||||||
|
return apply("Content");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSFunctor
|
||||||
|
interface JSFunctionWithStaticMethod extends JSObject {
|
||||||
|
String apply(String a);
|
||||||
|
|
||||||
|
public static String staticMethod(){
|
||||||
|
return "Content";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
interface WithProperties extends JSObject {
|
interface WithProperties extends JSObject {
|
||||||
@JSProperty
|
@JSProperty
|
||||||
String get_foo();
|
String get_foo();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user