Skip to content

Commit

Permalink
Add support for altMetafactory
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Jun 18, 2017
1 parent 6653118 commit 40b29cd
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ public void install(TeaVMHost host) {

host.add(new ClassForNameTransformer());
host.add(new AnnotationDependencyListener());

LambdaMetafactorySubstitutor lms = new LambdaMetafactorySubstitutor();
host.add(new MethodReference(LambdaMetafactory.class, "metafactory", MethodHandles.Lookup.class,
String.class, MethodType.class, MethodType.class, MethodHandle.class, MethodType.class,
CallSite.class), new LambdaMetafactorySubstitutor());
CallSite.class), lms);
host.add(new MethodReference(LambdaMetafactory.class, "altMetafactory", MethodHandles.Lookup.class,
String.class, MethodType.class, Object[].class, CallSite.class), lms);

host.add(new ScalaHacks());

host.add(new NumericClassTransformer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import org.teavm.model.emit.ValueEmitter;

public class LambdaMetafactorySubstitutor implements BootstrapMethodSubstitutor {
private static final int FLAG_SERIALIZABLE = 1;
private static final int FLAG_MARKERS = 2;
private static final int FLAG_BRIDGES = 4;
private int lambdaIndex;

@Override
Expand Down Expand Up @@ -94,6 +97,33 @@ public ValueEmitter substitute(DynamicCallSite callSite, ProgramEmitter callerPe

implementor.addMethod(worker);

// Handle altMetafactory case
if (callSite.getBootstrapArguments().size() > 3) {
int flags = callSite.getBootstrapArguments().get(3).getInt();

if ((flags & FLAG_SERIALIZABLE) != 0) {
implementor.getInterfaces().add("java.io.Serializable");
}

int bootstrapArgIndex = 4;
if ((flags & FLAG_MARKERS) != 0) {
int markerCount = callSite.getBootstrapArguments().get(bootstrapArgIndex++).getInt();
for (int i = 0; i < markerCount; ++i) {
ValueType markerType = callSite.getBootstrapArguments().get(bootstrapArgIndex++).getValueType();
implementor.getInterfaces().add(((ValueType.Object) markerType).getClassName());
}
}

if ((flags & FLAG_BRIDGES) != 0) {
int bridgeCount = callSite.getBootstrapArguments().get(bootstrapArgIndex++).getInt();
for (int i = 0; i < bridgeCount; ++i) {
ValueType[] bridgeType = callSite.getBootstrapArguments().get(bootstrapArgIndex++).getMethodType();
createBridge(classSource, implementor, callSite.getCalledMethod().getName(), instantiatedMethodType,
bridgeType);
}
}
}

callSite.getAgent().submitClass(implementor);
return callerPe.construct(ctor.getOwnerName(), callSite.getArguments().toArray(new ValueEmitter[0]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
import java.util.List;
import org.teavm.model.instructions.InstructionVisitor;

/**
*
* @author Alexey Andreev
*/
public class InvokeDynamicInstruction extends Instruction {
private MethodDescriptor method;
private MethodHandle bootstrapMethod;
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/java/org/teavm/model/RuntimeConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
*/
package org.teavm.model;

/**
*
* @author Alexey Andreev
*/
public final class RuntimeConstant {
public static final byte INT = 0;
public static final byte LONG = 1;
Expand Down
46 changes: 46 additions & 0 deletions tests/src/test/java/org/teavm/vm/LambdaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2017 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.vm;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.Serializable;
import java.util.function.Supplier;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner;

@RunWith(TeaVMTestRunner.class)
public class LambdaTest {
@Test
public void lambdaWithMarkers() {
Supplier<String> supplier = (Supplier<String> & A) () -> "OK";

assertEquals("OK", supplier.get());
assertTrue("Supplier is expected to implement A", supplier instanceof A);
}

@Test
public void serializableLambda() {
Supplier<String> supplier = (Supplier<String> & Serializable) () -> "OK";

assertEquals("OK", supplier.get());
assertTrue("Supplier is expected to implement Serializable", supplier instanceof Serializable);
}

interface A {
}
}

0 comments on commit 40b29cd

Please sign in to comment.