From fd778c2c7608290d16459b05a73e279c4718e09e Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Fri, 21 Mar 2014 07:33:50 -0600 Subject: [PATCH] remove redundant interfaces and generalize shift/reset generics Turns out Function can do the jobs of both CallbackReceiver and FunctionReceiver, so I've removed the latter two. Also, shift and reset should work with a combination of types, not just a single type, so I've expanded their generic signatures. --- classpath/avian/CallbackReceiver.java | 15 ----- classpath/avian/Continuations.java | 28 ++++----- classpath/avian/Function.java | 4 +- classpath/avian/FunctionReceiver.java | 15 ----- src/compile.cpp | 4 +- src/types.def | 2 - test/extra/ComposableContinuations.java | 81 ++++++++++++++++--------- test/extra/Continuations.java | 36 ++++++----- test/extra/Coroutines.java | 12 ++-- test/extra/DynamicWind.java | 43 +++++++------ 10 files changed, 120 insertions(+), 120 deletions(-) delete mode 100644 classpath/avian/CallbackReceiver.java delete mode 100644 classpath/avian/FunctionReceiver.java diff --git a/classpath/avian/CallbackReceiver.java b/classpath/avian/CallbackReceiver.java deleted file mode 100644 index 82c54fba20..0000000000 --- a/classpath/avian/CallbackReceiver.java +++ /dev/null @@ -1,15 +0,0 @@ -/* Copyright (c) 2008-2013, Avian Contributors - - Permission to use, copy, modify, and/or distribute this software - for any purpose with or without fee is hereby granted, provided - that the above copyright notice and this permission notice appear - in all copies. - - There is NO WARRANTY for this software. See license.txt for - details. */ - -package avian; - -public interface CallbackReceiver { - public T receive(Callback callback) throws Exception; -} diff --git a/classpath/avian/Continuations.java b/classpath/avian/Continuations.java index 2ffb7c7417..e751516515 100644 --- a/classpath/avian/Continuations.java +++ b/classpath/avian/Continuations.java @@ -127,14 +127,14 @@ public class Continuations { * specified receiver. * *

This method will either return the result returned by - * receiver.receive(Callback), propagate the exception + * receiver.call(Callback), propagate the exception * thrown by that method, return the result passed to the * handleResult(T) method of the continuation, or throw the * exception passed to the handleException(Throwable) method of the * continuation. */ public static native T callWithCurrentContinuation - (CallbackReceiver receiver) throws Exception; + (Function,T> receiver) throws Exception; /** * Calls the specified "before" and "after" tasks each time a @@ -183,13 +183,13 @@ public class Continuations { } } - public static T reset(final Callable thunk) throws Exception { + public static C reset(final Callable thunk) throws Exception { final Reset reset = new Reset(latestReset.get()); latestReset.set(reset); try { - T result = callWithCurrentContinuation - (new CallbackReceiver() { - public T receive(Callback continuation) throws Exception { + C result = (C) callWithCurrentContinuation + (new Function,Object>() { + public Object call(Callback continuation) throws Exception { reset.continuation = continuation; return thunk.call(); } @@ -207,26 +207,26 @@ public class Continuations { } } - public static T shift - (final FunctionReceiver receiver) + public static A shift + (final Function,C> receiver) throws Exception { - return (T) callWithCurrentContinuation(new CallbackReceiver() { - public Object receive(final Callback continuation) { + return (A) callWithCurrentContinuation + (new Function,Object>() { + public Object call(final Callback continuation) { final Reset reset = latestReset.get(); reset.shifts = new Cell(new Callback() { public void handleResult(Object ignored) { try { reset.continuation.handleResult - (receiver.receive + (receiver.call (new Function() { public Object call(final Object argument) throws Exception { return callWithCurrentContinuation - (new CallbackReceiver() { - public Object receive - (Callback shiftContinuation) + (new Function,Object>() { + public Object call(Callback shiftContinuation) throws Exception { reset.shifts = new Cell diff --git a/classpath/avian/Function.java b/classpath/avian/Function.java index 633a51dfdb..8d42f7b193 100644 --- a/classpath/avian/Function.java +++ b/classpath/avian/Function.java @@ -10,6 +10,6 @@ package avian; -public interface Function { - public T call(T argument) throws Exception; +public interface Function { + public B call(A argument) throws Exception; } diff --git a/classpath/avian/FunctionReceiver.java b/classpath/avian/FunctionReceiver.java deleted file mode 100644 index d526b39240..0000000000 --- a/classpath/avian/FunctionReceiver.java +++ /dev/null @@ -1,15 +0,0 @@ -/* Copyright (c) 2008-2013, Avian Contributors - - Permission to use, copy, modify, and/or distribute this software - for any purpose with or without fee is hereby granted, provided - that the above copyright notice and this permission notice appear - in all copies. - - There is NO WARRANTY for this software. See license.txt for - details. */ - -package avian; - -public interface FunctionReceiver { - public T receive(Function function) throws Exception; -} diff --git a/src/compile.cpp b/src/compile.cpp index 41e63823cd..b974567f4b 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -8044,8 +8044,8 @@ callWithCurrentContinuation(MyThread* t, object receiver) if (root(t, ReceiveMethod) == 0) { object m = resolveMethod - (t, root(t, Machine::BootLoader), "avian/CallbackReceiver", "receive", - "(Lavian/Callback;)Ljava/lang/Object;"); + (t, root(t, Machine::BootLoader), "avian/Function", "call", + "(Ljava/lang/Object;)Ljava/lang/Object;"); if (m) { setRoot(t, ReceiveMethod, m); diff --git a/src/types.def b/src/types.def index 7bb8bc1fb0..82b0e5a5aa 100644 --- a/src/types.def +++ b/src/types.def @@ -176,8 +176,6 @@ (type unwindResult avian/Continuations$UnwindResult) -(type callbackReceiver avian/CallbackReceiver) - (type string java/lang/String (alias data object value) (alias length uint32_t count) diff --git a/test/extra/ComposableContinuations.java b/test/extra/ComposableContinuations.java index 4af6bd8b8c..0a517bb2f6 100644 --- a/test/extra/ComposableContinuations.java +++ b/test/extra/ComposableContinuations.java @@ -1,10 +1,9 @@ package extra; import static avian.Continuations.shift; -import static avian.Continuations.reset; -import avian.FunctionReceiver; import avian.Function; +import avian.Continuations; import java.util.concurrent.Callable; @@ -14,10 +13,11 @@ public class ComposableContinuations { } public static void main(String[] args) throws Exception { - expect(2 * reset(new Callable() { + expect(2 * Continuations.reset(new Callable() { public Integer call() throws Exception { - return 1 + shift(new FunctionReceiver() { - public Integer receive(Function continuation) + return 1 + shift + (new Function,Integer>() { + public Integer call(Function continuation) throws Exception { return continuation.call(5); @@ -26,10 +26,11 @@ public class ComposableContinuations { } }) == 12); - expect(1 + reset(new Callable() { + expect(1 + Continuations.reset(new Callable() { public Integer call() throws Exception { - return 2 * shift(new FunctionReceiver() { - public Integer receive(Function continuation) + return 2 * shift + (new Function,Integer>() { + public Integer call(Function continuation) throws Exception { return continuation.call(continuation.call(4)); @@ -38,29 +39,53 @@ public class ComposableContinuations { } }) == 17); - expect(equal(reset(new Callable>() { - public Cell call() throws Exception { - shift(new FunctionReceiver>() { - public Cell receive - (Function> continuation) + expect + (equal + (Continuations.,Cell>reset + (new Callable>() { + public Cell call() throws Exception { + shift(new Function,Cell>, + Cell>() + { + public Cell call + (Function,Cell> continuation) + throws Exception + { + return cons(1, continuation.call(null)); + } + }); + + shift(new Function,Cell>, + Cell>() + { + public Cell call + (Function,Cell> continuation) + throws Exception + { + return cons(2, continuation.call(null)); + } + }); + + return null; + } + }), cons(1, cons(2, null)))); + + expect + (equal + (Continuations.reset + (new Callable() { + public String call() throws Exception { + return new String + (shift(new Function,Integer>() { + public Integer call(Function continuation) throws Exception { - return cons(1, continuation.call(null)); + return Integer.parseInt + (continuation.call(new byte[] { 0x34, 0x32 })); } - }); - - shift(new FunctionReceiver>() { - public Cell receive - (Function> continuation) - throws Exception - { - return cons(2, continuation.call(null)); - } - }); - - return null; - } - }), cons(1, cons(2, null)))); + }), "UTF-8"); + } + }), 42)); } private static boolean equal(T a, T b) { diff --git a/test/extra/Continuations.java b/test/extra/Continuations.java index a439d8a58d..1186c43a4a 100644 --- a/test/extra/Continuations.java +++ b/test/extra/Continuations.java @@ -2,7 +2,7 @@ package extra; import static avian.Continuations.callWithCurrentContinuation; -import avian.CallbackReceiver; +import avian.Function; import avian.Callback; public class Continuations { @@ -11,22 +11,26 @@ public class Continuations { } public static void main(String[] args) throws Exception { - expect(callWithCurrentContinuation(new CallbackReceiver() { - public Integer receive(Callback continuation) { - continuation.handleResult(42); - throw new AssertionError(); - } - }) == 42); + expect + (callWithCurrentContinuation + (new Function,Integer>() { + public Integer call(Callback continuation) { + continuation.handleResult(42); + throw new AssertionError(); + } + }) == 42); - expect(callWithCurrentContinuation(new CallbackReceiver() { - public Integer receive(Callback continuation) { - return 43; - } - }) == 43); + expect + (callWithCurrentContinuation + (new Function,Integer>() { + public Integer call(Callback continuation) { + return 43; + } + }) == 43); try { - callWithCurrentContinuation(new CallbackReceiver() { - public Integer receive(Callback continuation) { + callWithCurrentContinuation(new Function,Integer>() { + public Integer call(Callback continuation) { continuation.handleException(new MyException()); throw new AssertionError(); } @@ -37,8 +41,8 @@ public class Continuations { } try { - callWithCurrentContinuation(new CallbackReceiver() { - public Integer receive(Callback continuation) + callWithCurrentContinuation(new Function,Integer>() { + public Integer call(Callback continuation) throws MyException { throw new MyException(); diff --git a/test/extra/Coroutines.java b/test/extra/Coroutines.java index 7d4ee9c5a5..3afd2dce73 100644 --- a/test/extra/Coroutines.java +++ b/test/extra/Coroutines.java @@ -2,7 +2,7 @@ package extra; import static avian.Continuations.callWithCurrentContinuation; -import avian.CallbackReceiver; +import avian.Function; import avian.Callback; public class Coroutines { @@ -40,8 +40,8 @@ public class Coroutines { final Consumer consumer = new Consumer() { public void consume(final Character c) throws Exception { - callWithCurrentContinuation(new CallbackReceiver() { - public Object receive(Callback continuation) { + callWithCurrentContinuation(new Function,Object>() { + public Object call(Callback continuation) { state.produceNext = continuation; state.consumeNext.handleResult(c); @@ -53,9 +53,9 @@ public class Coroutines { }; final Producer producer = new Producer() { - final CallbackReceiver receiver - = new CallbackReceiver() { - public Character receive(Callback continuation) + final Function,Character> receiver + = new Function,Character>() { + public Character call(Callback continuation) throws Exception { state.consumeNext = continuation; diff --git a/test/extra/DynamicWind.java b/test/extra/DynamicWind.java index aa617ddc9c..bbc305842c 100644 --- a/test/extra/DynamicWind.java +++ b/test/extra/DynamicWind.java @@ -3,7 +3,7 @@ package extra; import static avian.Continuations.callWithCurrentContinuation; import static avian.Continuations.dynamicWind; -import avian.CallbackReceiver; +import avian.Function; import avian.Callback; import java.util.concurrent.Callable; @@ -86,24 +86,27 @@ public class DynamicWind { }); } - private void continuationUnwindTest(final CallbackReceiver receiver) + private void continuationUnwindTest + (final Function,Integer> receiver) throws Exception { System.out.println("continuationUnwindTest enter"); try { - expect(callWithCurrentContinuation(new CallbackReceiver() { - public Integer receive(final Callback continuation) - throws Exception - { - unwindTest(new Callable() { - public Integer call() throws Exception { - return receiver.receive(continuation); - } - }); - throw new AssertionError(); - } - }) == 42); + expect + (callWithCurrentContinuation + (new Function,Integer>() { + public Integer call(final Callback continuation) + throws Exception + { + unwindTest(new Callable() { + public Integer call() throws Exception { + return receiver.call(continuation); + } + }); + throw new AssertionError(); + } + }) == 42); } catch (MyException e) { e.printStackTrace(); } @@ -118,8 +121,8 @@ public class DynamicWind { } private void continuationResultUnwind() throws Exception { - continuationUnwindTest(new CallbackReceiver() { - public Integer receive(final Callback continuation) { + continuationUnwindTest(new Function,Integer>() { + public Integer call(final Callback continuation) { continuation.handleResult(42); throw new AssertionError(); } @@ -127,8 +130,8 @@ public class DynamicWind { } private void continuationExceptionUnwind() throws Exception { - continuationUnwindTest(new CallbackReceiver() { - public Integer receive(final Callback continuation) { + continuationUnwindTest(new Function,Integer>() { + public Integer call(final Callback continuation) { continuation.handleException(new MyException()); throw new AssertionError(); } @@ -163,8 +166,8 @@ public class DynamicWind { task = 1; return callWithCurrentContinuation - (new CallbackReceiver() { - public Integer receive(final Callback continuation) + (new Function,Integer>() { + public Integer call(final Callback continuation) throws Exception { continuationReference = continuation;