Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkosertic committed Dec 17, 2018
2 parents 5ace1ae + f5d9ff5 commit 4ed0d92
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package de.mirkosertic.bytecoder.api;

public interface Callback<T extends OpaqueReferenceType> {
public interface Callback {

void run(T aValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.mirkosertic.bytecoder.classlib;
package de.mirkosertic.bytecoder.api.web;

import de.mirkosertic.bytecoder.api.Callback;
import de.mirkosertic.bytecoder.api.Export;
import de.mirkosertic.bytecoder.api.OpaqueReferenceType;

public class Globals {
public interface AnimationFrameCallback extends Callback {

@Export("summonCallback")
public static void summonCallback(final Callback aCallback, final OpaqueReferenceType aArgument) {
aCallback.run(aArgument);
}
void run(int aElapsedTime);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2018 Mirko Sertic
*
* 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 de.mirkosertic.bytecoder.api.web;

import de.mirkosertic.bytecoder.api.Callback;

public interface EventListener<T extends Event> extends Callback {

void run(T aEvent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/
package de.mirkosertic.bytecoder.api.web;

import de.mirkosertic.bytecoder.api.Callback;
import de.mirkosertic.bytecoder.api.OpaqueReferenceType;

public interface EventTarget extends OpaqueReferenceType {

<T extends Event> void addEventListener(final String eventType, final Callback<T> aEventListener);
<T extends Event> void addEventListener(final String eventType, final EventListener<T> aEventListener);

<T extends Event> void dispatchEvent(T aEvent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package de.mirkosertic.bytecoder.api.web;

import de.mirkosertic.bytecoder.api.Callback;
import de.mirkosertic.bytecoder.api.Import;
import de.mirkosertic.bytecoder.api.OpaqueProperty;

Expand All @@ -27,5 +26,5 @@ public abstract class Window implements EventTarget {
@OpaqueProperty("document")
public abstract HTMLDocument document();

public abstract void requestAnimationFrame(Callback<Event> callback);
public abstract void requestAnimationFrame(AnimationFrameCallback aCallback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/
package de.mirkosertic.bytecoder.backend;

import java.lang.reflect.Method;

import de.mirkosertic.bytecoder.backend.js.JSSSACompilerBackend;
import de.mirkosertic.bytecoder.backend.js.JSWriterUtils;
import de.mirkosertic.bytecoder.backend.wasm.WASMSSAASTCompilerBackend;
import de.mirkosertic.bytecoder.classlib.Globals;
import de.mirkosertic.bytecoder.classlib.VM;
import de.mirkosertic.bytecoder.core.BytecodeArrayTypeRef;
import de.mirkosertic.bytecoder.core.BytecodeClass;
import de.mirkosertic.bytecoder.core.BytecodeLinkedClass;
import de.mirkosertic.bytecoder.core.BytecodeLinkerContext;
import de.mirkosertic.bytecoder.core.BytecodeLoader;
Expand All @@ -33,6 +31,10 @@
import de.mirkosertic.bytecoder.core.BytecodeTypeRef;
import de.mirkosertic.bytecoder.ssa.NaiveProgramGenerator;

import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;

public class CompileTarget {

public enum BackendType {
Expand Down Expand Up @@ -68,8 +70,6 @@ public CompileResult compile(
theClassLinkedCass.resolveConstructorInvocation(new BytecodeMethodSignature(
BytecodePrimitiveTypeRef.VOID, new BytecodeTypeRef[] {}));

theLinkerContext.resolveClass(BytecodeObjectTypeRef.fromRuntimeClass(Globals.class));

// Lambda handling
final BytecodeLinkedClass theCallsite = theLinkerContext.resolveClass(BytecodeObjectTypeRef.fromRuntimeClass(VM.ImplementingCallsite.class));
theCallsite.resolveVirtualMethod("invokeExact", new BytecodeMethodSignature(BytecodeObjectTypeRef.fromRuntimeClass(Object.class),
Expand All @@ -85,6 +85,21 @@ public CompileResult compile(
theClass.resolveVirtualMethod(aMethodName, aSignature);
}

// We have to link all callback implementations. They are not part of the dependency yet as
// they are not invoked by the bytecode, but from the outside world. By adding them to the
// dependency tree, we make sure they are available for invocation.
List<BytecodeLinkedClass> theLinkedClasses = theLinkerContext.linkedClasses().map(t -> t.targetNode()).collect(Collectors.toList());
for (BytecodeLinkedClass theLinkedClass : theLinkedClasses) {
if (theLinkedClass.isCallback()) {
BytecodeClass theBytecodeClass = theLinkedClass.getBytecodeClass();
for (BytecodeMethod theCallbackMethod : theBytecodeClass.getMethods()) {
if (!theCallbackMethod.isConstructor() && !theCallbackMethod.isClassInitializer()) {
theLinkedClass.resolveVirtualMethod(theCallbackMethod.getName().stringValue(), theCallbackMethod.getSignature());
}
}
}
}

// Before code generation we have to make sure that all abstract method implementations are linked correctly
aOptions.getLogger().info("Resolving abstract method hierarchy");
theLinkerContext.resolveAbstractMethodsInSubclasses();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import de.mirkosertic.bytecoder.core.BytecodeMethodSignature;
import de.mirkosertic.bytecoder.core.BytecodeObjectTypeRef;
import de.mirkosertic.bytecoder.core.BytecodeOpcodeAddress;
import de.mirkosertic.bytecoder.core.BytecodeResolvedMethods;
import de.mirkosertic.bytecoder.core.BytecodeTypeRef;
import de.mirkosertic.bytecoder.core.BytecodeUtf8Constant;
import de.mirkosertic.bytecoder.core.BytecodeVirtualMethodIdentifier;
Expand Down Expand Up @@ -641,11 +642,24 @@ private void printToJSConvertedValue(final BytecodeTypeRef aTypeRef, final Value
if (theLinkedClass.isOpaqueType()) {
print(aValue);
} else if (theLinkedClass.isCallback()) {
print("function(event) {");

BytecodeResolvedMethods theMethods = theLinkedClass.resolvedMethods();
List<BytecodeMethod> availableCallbacks = theMethods.stream().filter(t -> !t.getValue().isConstructor() && !t.getValue().isClassInitializer()
&& !t.getProvidingClass().getClassName().name().equals(Object.class.getName())).map(t -> t.getValue()).collect(Collectors.toList());
if (availableCallbacks.size() != 1) {
throw new IllegalStateException("Unvalid number of callback methods available for type " + theLinkedClass.getClassName().name() + ", expected 1, got " + availableCallbacks.size());
}

BytecodeMethod theCallbackMethod = availableCallbacks.get(0);
String theMethodName = JSWriterUtils.toMethodName(theCallbackMethod.getName().stringValue(), theCallbackMethod.getSignature());

print("function() {");
print("var v = ");
print(aValue);
print(";v");
print(".VOIDrundmbaOpaqueReferenceType(v, event);");
print(";var args = Array.prototype.slice.call(arguments);args.unshift(v);v");
print(".");
print(theMethodName);
print(".apply(v, args);");
print("}");
} else {
throw new IllegalStateException("Type conversion to " + aTypeRef.name() + " is not supported!");
Expand Down
Loading

0 comments on commit 4ed0d92

Please sign in to comment.