Compare commits

..

No commits in common. "main" and "0.1.0" have entirely different histories.
main ... 0.1.0

39 changed files with 177 additions and 212 deletions

101
README.md
View file

@ -1,6 +1,6 @@
# Coroutines for Java 24+
# Generators/Futures for Java 24+
Coroutines implemented as state machines integrated into standard java.
Futures/Generators implemented as state machines integrated into standard java.
- Futures/Generators are lazy and only make progress when called upon
- Integrated as much as possible into java to work with the type system as much as possible
@ -36,31 +36,6 @@ public static Gen<Long, Void> primes() {
}
```
## Use this library
```kotlin
// settings.gradle.kts
sourceControl {
gitRepository(URI.create("https://github.com/ParkerTenBroeck/coroutines.git")) {
producesModule("com.parkertenbroeck.coroutines:lib")
}
}
// build.gradle.kts
implementation("com.parkertenbroeck.coroutines:lib:0.1.1")
```
This library requires the application be ran with a custom class loader, there is a utility provided to make this easier.
```java
public static void main(String[] args) {
// loads the current class with a custom class loader and calls *this* method with the provided arguments.
// *this* method - the one used to call `runWithCoroutine`
RT.runWithCoroutine(CoroutineClassLoader.Config.builtin(), (Object) args);
// past this point Coroutines will be created on methods which match the criteria
}
```
## How does it know what functions to transform?
Detection is done by
@ -86,10 +61,6 @@ public static Future<Void, RuntimeException> regular_function() {
}
```
### Why not annotations?
Unfortunately annotations cannot be put on lambdas.
## Oddities
```java
@ -110,38 +81,7 @@ public static Future<Void, IOException> echo(🔷@Cancellation("close") Socket s
## Things to watch out for
### Await function works specifically for the Future type
```java
class MyFuture implements Future<String, RuntimeException>{
public Object poll(Waker waker){ /* code */ }
}
void Future<Void, RuntimeException> wrong() {
new MyFuture().await();//⚡
return Future.ret();
}
```
Internally the await call will become a `invokevirtual` call to `await` on `MyFuture`. Which will not currently be recognized as a "special" method to transform.
To avoid this use static methods to return a future and make manually implemented futures have private/protected constructors
```java
class MyFuture implements Future<String, RuntimeException>{
private MyFuture(){}
public static Future<String, RuntimeException> make(){
return new MyFuture();
}
public Object poll(Waker waker){ /* code */ }
}
void Future<Void, RuntimeException> wrong() {
MyFuture.make().await();
return Future.ret();
}
```
### Future exceptions are not type checked
### Future errors are not type checked
```java
public static Future<Void, RuntimeException> wrong() throws Exception {
Waker.waker().wake();
@ -190,7 +130,7 @@ public static Future<Void, RuntimeException> add_reorder() {
### synchronized
when a coroutine method is declared as `synchronized` the `next`/`poll`/`cancel` functions will all be synchronized over the instance or class the method was declared in.
when a generator/future method is declared as `synchronized` the `next`/`poll`/`cancel` functions will all be synchronized over the instance or class which the method was declared in.
it is important to note that the monitor is **NOT** held across suspend points where the function returns.
@ -206,7 +146,22 @@ static Future<Void, RuntimeException> unsync(Object value) {
## How this works
Say we have some async function like this.
This library requires the application be ran with a custom class loader as follows (this can be done manually if needed)
```java
public static void main(String[] args) {
// loads the current class with a custom class loader and calls *this* method with the provided arguments.
// *this* method - the one used to call `runWithStateMachines`
RT.runWithStateMachines(StateMachineClassLoader.Config.builtin(), (Object) args);
// past this point generators will be created on methods which match the criteria
}
```
Once finished any class loaded will be scanned for methods which match a particular signature.
When matched a shim is introduced into the source method and a class is constructed which stores all state for the future/generator
A basic example here where we have some parameters
```java
public static Future<Void, IOException> example(@Cancellation("close") Socket socket, String message) throws IOException {
try(socket){
@ -215,8 +170,6 @@ public static Future<Void, IOException> example(@Cancellation("close") Socket so
return Future.ret();
}
```
The loader will see the methods which it needs to transform, then modify the bytecode, transforming the function into a class where state across suspend points is saved/restored (local variables & the current state of the stack).
This is a (modified) decompiled version of the generated method.
```java
public static Future<Void, IOException> example(Socket socket, String message) {
@ -320,10 +273,10 @@ public static Future<Void, IOException> example(Socket socket, String message) {
};
}
```
## Potential additions
- Type checking during transformations
- Better debugger support (Currently line stepping is generally supported but most breakpoint sets are not working)
- Annotations (possibly integration during build time)
- More library features (async)
### Steps
- A functions signature and code is determined to be "of interest"
- When a function is found we wish to modify it first builds a frame(locals, stack, annotations) for every area of interest in the function
- The number of unique locations which can be suspended from are kept track of
- A switch is built which handles each state the function can resume from setting up any locals/stack that needs to be resumed as well as setting up the code for saving locals/stack state
- the locations which special methods are found are modified to perform their particular function and potentially save state and return
- (Futures) generate cancellation code for locals which have specified so, and cancellation for a pending future.

View file

@ -1,4 +1,7 @@
package basic;
import com.parkertenbroeck.future.Future;
import com.parkertenbroeck.future.Waker;
public class Futures {
}

View file

@ -1,6 +1,6 @@
package basic;
import com.parkertenbroeck.coroutines.generator.Gen;
import com.parkertenbroeck.generator.Gen;
public class Gens {
public static Gen<Long, Void> primes(){

View file

@ -1,14 +1,14 @@
package basic;
import com.parkertenbroeck.coroutines.bcsm.RT;
import com.parkertenbroeck.coroutines.bcsm.loadtime.CoroutineClassLoader;
import com.parkertenbroeck.coroutines.generator.Gen;
import com.parkertenbroeck.bcsm.RT;
import com.parkertenbroeck.bcsm.loadtime.StateMachineClassLoader;
import com.parkertenbroeck.generator.Gen;
public class Main {
public static void main(String[] args) {
RT.runWithCoroutine(CoroutineClassLoader.Config.builtin().write_classes("build/modified/generators/"), (Object) args);
RT.runWithStateMachines(StateMachineClassLoader.Config.builtin().write_classes("build/modified/generators/"), (Object) args);
primes();
primes();
}
static void primes(){

View file

@ -1,6 +1,6 @@
package lexer;
import com.parkertenbroeck.coroutines.generator.Gen;
import com.parkertenbroeck.generator.Gen;
public class Lexer {
public sealed interface Token{}

View file

@ -1,12 +1,12 @@
package lexer;
import com.parkertenbroeck.coroutines.generator.Gen;
import com.parkertenbroeck.coroutines.bcsm.RT;
import com.parkertenbroeck.coroutines.bcsm.loadtime.CoroutineClassLoader;
import com.parkertenbroeck.generator.Gen;
import com.parkertenbroeck.bcsm.RT;
import com.parkertenbroeck.bcsm.loadtime.StateMachineClassLoader;
public class Main {
public static void main(String[] args) {
RT.runWithCoroutine(CoroutineClassLoader.Config.builtin().write_classes("build/modified/generators/"), (Object) args);
RT.runWithStateMachines(StateMachineClassLoader.Config.builtin().write_classes("build/modified/generators/"), (Object) args);
lexer();
}

View file

@ -1,12 +1,12 @@
package sockets;
import com.parkertenbroeck.coroutines.async_runtime.Jokio;
import com.parkertenbroeck.coroutines.bcsm.RT;
import com.parkertenbroeck.coroutines.bcsm.loadtime.CoroutineClassLoader;
import com.parkertenbroeck.async_runtime.Jokio;
import com.parkertenbroeck.bcsm.RT;
import com.parkertenbroeck.bcsm.loadtime.StateMachineClassLoader;
public class Main {
public static void main(String[] args) {
RT.runWithCoroutine(CoroutineClassLoader.Config.builtin().write_classes("build/modified/generators/"), (Object) args);
RT.runWithStateMachines(StateMachineClassLoader.Config.builtin().write_classes("build/modified/generators/"), (Object) args);
await();
}

View file

@ -1,11 +1,12 @@
package sockets;
import com.parkertenbroeck.coroutines.async_runtime.Delay;
import com.parkertenbroeck.coroutines.async_runtime.Jokio;
import com.parkertenbroeck.coroutines.async_runtime.io.net.ServerSocket;
import com.parkertenbroeck.coroutines.async_runtime.io.net.Socket;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.coroutines.bcsm.loadtime.future.Cancellation;
import com.parkertenbroeck.async_runtime.Delay;
import com.parkertenbroeck.async_runtime.Jokio;
import com.parkertenbroeck.async_runtime.io.net.ServerSocket;
import com.parkertenbroeck.async_runtime.io.net.Socket;
import com.parkertenbroeck.future.Future;
import com.parkertenbroeck.bcsm.loadtime.future.Cancellation;
import com.parkertenbroeck.future.Waker;
import java.io.IOException;
import java.net.InetSocketAddress;

View file

@ -1,5 +1,5 @@
version = "0.1.1"
group = "com.parkertenbroeck.coroutines"
version = "0.1.0"
group = "com.parkertenbroeck.generators"
plugins {
`java-library`

View file

@ -1,16 +1,21 @@
package com.parkertenbroeck.coroutines.async_runtime;
package com.parkertenbroeck.async_runtime;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.coroutines.future.Waker;
import com.parkertenbroeck.future.Future;
import com.parkertenbroeck.future.Waker;
import java.util.Timer;
import java.util.TimerTask;
public class Delay implements Future<Void, RuntimeException> {
private final static Timer timer = new Timer(true);
private final static Timer timer;
private TimerTask task;
static {
timer = new Timer(true);
}
private int delay;
private boolean ready;
protected Delay(int ms) {
if (ms < 0) throw new IllegalArgumentException("async_example.Delay cannot be negative");
@ -28,12 +33,16 @@ public class Delay implements Future<Void, RuntimeException> {
@Override
public synchronized Object poll(Waker waker) {
if (delay == 0) return null;
if (delay > 0) {
if (delay == 0) {
ready = true;
delay = -1;
return null;
}
if (delay != -1) {
task = new TimerTask() {
@Override
public void run() {
delay = 0;
ready = true;
waker.wake();
}
};
@ -41,6 +50,7 @@ public class Delay implements Future<Void, RuntimeException> {
delay = -1;
}
if (ready) return null;
return Pending.INSTANCE;
}
}

View file

@ -1,7 +1,7 @@
package com.parkertenbroeck.coroutines.async_runtime;
package com.parkertenbroeck.async_runtime;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.coroutines.future.Waker;
import com.parkertenbroeck.future.Future;
import com.parkertenbroeck.future.Waker;
import java.util.ArrayDeque;
import java.util.HashSet;

View file

@ -1,8 +1,9 @@
package com.parkertenbroeck.coroutines.async_runtime;
package com.parkertenbroeck.async_runtime;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.future.Future;
import java.util.*;
import java.util.function.Function;
public class Util {

View file

@ -1,6 +1,6 @@
package com.parkertenbroeck.coroutines.async_runtime.io;
package com.parkertenbroeck.async_runtime.io;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.future.Future;
import java.nio.ByteBuffer;

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.async_runtime.io;
package com.parkertenbroeck.async_runtime.io;
import java.io.IOException;
import java.nio.channels.*;

View file

@ -1,6 +1,6 @@
package com.parkertenbroeck.coroutines.async_runtime.io;
package com.parkertenbroeck.async_runtime.io;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.future.Future;
import java.nio.ByteBuffer;

View file

@ -1,9 +1,9 @@
package com.parkertenbroeck.coroutines.async_runtime.io.fs;
package com.parkertenbroeck.async_runtime.io.fs;
import com.parkertenbroeck.coroutines.async_runtime.io.Readable;
import com.parkertenbroeck.coroutines.async_runtime.io.Writable;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.coroutines.future.Waker;
import com.parkertenbroeck.async_runtime.io.Readable;
import com.parkertenbroeck.async_runtime.io.Writable;
import com.parkertenbroeck.future.Future;
import com.parkertenbroeck.future.Waker;
import java.io.IOException;
import java.nio.ByteBuffer;

View file

@ -1,10 +1,10 @@
package com.parkertenbroeck.coroutines.async_runtime.io.net;
package com.parkertenbroeck.async_runtime.io.net;
import com.parkertenbroeck.coroutines.async_runtime.io.Readable;
import com.parkertenbroeck.coroutines.async_runtime.io.SelectorThread;
import com.parkertenbroeck.coroutines.async_runtime.io.Writable;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.coroutines.future.Waker;
import com.parkertenbroeck.async_runtime.io.Readable;
import com.parkertenbroeck.async_runtime.io.SelectorThread;
import com.parkertenbroeck.async_runtime.io.Writable;
import com.parkertenbroeck.future.Future;
import com.parkertenbroeck.future.Waker;
import java.io.IOException;
import java.net.InetSocketAddress;

View file

@ -1,8 +1,8 @@
package com.parkertenbroeck.coroutines.async_runtime.io.net;
package com.parkertenbroeck.async_runtime.io.net;
import com.parkertenbroeck.coroutines.async_runtime.io.SelectorThread;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.coroutines.future.Waker;
import com.parkertenbroeck.async_runtime.io.SelectorThread;
import com.parkertenbroeck.future.Future;
import com.parkertenbroeck.future.Waker;
import java.io.IOException;
import java.net.InetSocketAddress;

View file

@ -1,10 +1,10 @@
package com.parkertenbroeck.coroutines.async_runtime.io.net;
package com.parkertenbroeck.async_runtime.io.net;
import com.parkertenbroeck.coroutines.async_runtime.io.Readable;
import com.parkertenbroeck.coroutines.async_runtime.io.SelectorThread;
import com.parkertenbroeck.coroutines.async_runtime.io.Writable;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.coroutines.future.Waker;
import com.parkertenbroeck.async_runtime.io.Readable;
import com.parkertenbroeck.async_runtime.io.SelectorThread;
import com.parkertenbroeck.async_runtime.io.Writable;
import com.parkertenbroeck.future.Future;
import com.parkertenbroeck.future.Waker;
import java.io.IOException;
import java.net.InetSocketAddress;

View file

@ -1,7 +1,7 @@
package com.parkertenbroeck.coroutines.bcsm;
package com.parkertenbroeck.bcsm;
import com.parkertenbroeck.coroutines.bcsm.loadtime.CoroutineClassLoader;
import com.parkertenbroeck.bcsm.loadtime.StateMachineClassLoader;
import java.lang.reflect.Modifier;
import java.util.Set;
@ -9,10 +9,10 @@ import java.util.Set;
import static java.lang.StackWalker.Option.*;
public class RT {
public static void runWithCoroutine(CoroutineClassLoader.Config config, Object... params){
if(RT.class.getClassLoader() instanceof CoroutineClassLoader)return;
public static void runWithStateMachines(StateMachineClassLoader.Config config, Object... params){
if(RT.class.getClassLoader() instanceof StateMachineClassLoader)return;
var loader = new CoroutineClassLoader(RT.class.getClassLoader(), config);
var loader = new StateMachineClassLoader(RT.class.getClassLoader(), config);
try{
StackWalker walker = StackWalker.getInstance(Set.of(RETAIN_CLASS_REFERENCE, SHOW_REFLECT_FRAMES, SHOW_HIDDEN_FRAMES));
var frame = walker.walk(s -> s.skip(1).findFirst()).get();

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
import java.lang.classfile.CodeBuilder;
import java.lang.classfile.instruction.LineNumber;
@ -16,7 +16,7 @@ public record Frame(FrameTracker.Type[] locals, FrameTracker.Type[] stack, int b
+ "]";
}
public void save_locals(CoroutineBuilder smb, CodeBuilder cob, SavedStateTracker sst, int loc_off){
public void save_locals(StateMachineBuilder smb, CodeBuilder cob, SavedStateTracker sst, int loc_off){
int slot = 0;
for (var entry : locals) {
slot++;// <-----
@ -28,20 +28,20 @@ public record Frame(FrameTracker.Type[] locals, FrameTracker.Type[] stack, int b
sst.save_local(smb, cob, entry.toCD(), slot - smb.paramSlotOff + loc_off - 1); //minus one cause increment before here
}
}
public void save_stack(CoroutineBuilder smb, CodeBuilder cob, SavedStateTracker sst, int stack_off) {
public void save_stack(StateMachineBuilder smb, CodeBuilder cob, SavedStateTracker sst, int stack_off) {
for(int i = stack.length-1-stack_off; i >= 0; i --){
if(stack[i].isCategory2_2nd())continue;
sst.save_stack(smb, cob, stack[i].toCD());
}
}
public SavedStateTracker save(CoroutineBuilder smb, CodeBuilder cob, SavedStateTracker sst, int loc_off, int stack_off) {
public SavedStateTracker save(StateMachineBuilder smb, CodeBuilder cob, SavedStateTracker sst, int loc_off, int stack_off) {
save_locals(smb, cob, sst, loc_off);
save_stack(smb, cob, sst, stack_off);
return sst;
}
public SavedStateTracker save(CoroutineBuilder smb, CodeBuilder cob, int loc_off, int stack_off) {
public SavedStateTracker save(StateMachineBuilder smb, CodeBuilder cob, int loc_off, int stack_off) {
var sst = new SavedStateTracker();
return save(smb, cob, sst, loc_off, stack_off);
}

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
import java.lang.classfile.*;
import java.lang.classfile.attribute.RuntimeInvisibleTypeAnnotationsAttribute;
@ -7,6 +7,7 @@ import java.lang.classfile.attribute.StackMapFrameInfo;
import java.lang.classfile.attribute.StackMapTableAttribute;
import java.lang.classfile.instruction.*;
import java.lang.constant.*;
import java.lang.reflect.AccessFlag;
import java.util.*;
import static java.lang.constant.ConstantDescs.*;
@ -29,7 +30,7 @@ public class FrameTracker {
ITEM_LONG_2ND = 13,
ITEM_DOUBLE_2ND = 14;
public static ArrayList<Frame> frames(CoroutineBuilder<?> smb, CodeModel src_com) {
public static ArrayList<Frame> frames(StateMachineBuilder<?> smb, CodeModel src_com) {
var ft = new FrameTracker(smb, src_com);
var frames = new ArrayList<Frame>();
for(var coe : src_com){
@ -162,7 +163,7 @@ public class FrameTracker {
final ArrayList<Type> stack = new ArrayList<>();
final ArrayList<Type> locals = new ArrayList<>();
final CoroutineBuilder smb;
final StateMachineBuilder smb;
LineNumber current_line_number = null;
int bci = 0;
@ -175,7 +176,7 @@ public class FrameTracker {
final HashMap<Label, List<LocalVariableAnnotation>> annotationEndMap = new HashMap<>();
FrameTracker(CoroutineBuilder smb, CodeModel com) {
FrameTracker(StateMachineBuilder smb, CodeModel com) {
this.smb = smb;
int offset = 0;

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
import java.lang.constant.ClassDesc;

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
public enum ReplacementKind {
ImmediateReplacingPop,

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
import java.lang.classfile.CodeBuilder;
import java.lang.classfile.TypeKind;
@ -18,18 +18,18 @@ public class SavedStateTracker {
}
private String get_name(CoroutineBuilder<?> smb, ClassDesc desc){
private String get_name(StateMachineBuilder<?> smb, ClassDesc desc){
var value = smb.lstate.stream() // find unused state
.filter(v -> saved.stream().noneMatch(s -> s.name().equals(v.name())))
.filter(v -> v.cd().equals(desc)).findFirst();
if(value.isPresent())
return value.get().name();
var name = CoroutineBuilder.LOCAL_PREFIX+smb.lstate.size();
smb.lstate.add(new CoroutineBuilder.LState(name, desc));
var name = StateMachineBuilder.LOCAL_PREFIX+smb.lstate.size();
smb.lstate.add(new StateMachineBuilder.LState(name, desc));
return name;
}
public SavedState save_stack(CoroutineBuilder<?> smb, CodeBuilder cob, ClassDesc desc){
public SavedState save_stack(StateMachineBuilder<?> smb, CodeBuilder cob, ClassDesc desc){
var name = get_name(smb, desc);
if(TypeKind.from(desc).slotSize()==2){
@ -43,7 +43,7 @@ public class SavedStateTracker {
return s;
}
public SavedState save_local(CoroutineBuilder<?> smb, CodeBuilder cob, ClassDesc desc, int slot){
public SavedState save_local(StateMachineBuilder<?> smb, CodeBuilder cob, ClassDesc desc, int slot){
var name = get_name(smb, desc);
cob.aload(0).loadLocal(TypeKind.from(desc), slot).putfield(smb.CD_this, name, desc);
@ -63,7 +63,7 @@ public class SavedStateTracker {
throw new RuntimeException();
}
public SavedStateTracker restore(CoroutineBuilder<?> smb, CodeBuilder cob, SavedState s){
public SavedStateTracker restore(StateMachineBuilder<?> smb, CodeBuilder cob, SavedState s){
if(!saved.remove(s))throw new IllegalStateException();
switch(s){
case LocalState(var name, var desc, int slot) ->
@ -74,21 +74,21 @@ public class SavedStateTracker {
return this;
}
public void restore_stack(CoroutineBuilder<?> smb, CodeBuilder cob){
public void restore_stack(StateMachineBuilder<?> smb, CodeBuilder cob){
for(int i = saved.size()-1; i >= 0; i --){
if(saved.get(i) instanceof StackState)
restore(smb, cob, saved.get(i));
}
}
public void restore_locals(CoroutineBuilder<?> smb, CodeBuilder cob){
public void restore_locals(StateMachineBuilder<?> smb, CodeBuilder cob){
for(int i = saved.size()-1; i >= 0; i --){
if(saved.get(i) instanceof StackState)
restore(smb, cob, saved.get(i));
}
}
public void restore_all(CoroutineBuilder<?> smb, CodeBuilder cob) {
public void restore_all(StateMachineBuilder<?> smb, CodeBuilder cob) {
while(!saved.isEmpty())
restore(smb, cob, saved.getLast());
}

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;

View file

@ -1,7 +1,7 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
import java.lang.classfile.CodeBuilder;
public interface SpecialMethodBuilder<T extends CoroutineBuilder<T>> {
public interface SpecialMethodBuilder<T extends StateMachineBuilder<T>> {
SpecialMethodHandler<T> build(T smb, CodeBuilder cob, Frame frame, StateBuilder sb);
}

View file

@ -1,8 +1,8 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
import java.lang.classfile.CodeBuilder;
public interface SpecialMethodHandler<T extends CoroutineBuilder> {
public interface SpecialMethodHandler<T extends StateMachineBuilder> {
void build_prelude(T smb, CodeBuilder cob, Frame frame);
default boolean removeCall() {

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
import java.lang.classfile.CodeBuilder;
import java.lang.classfile.Label;
@ -12,8 +12,8 @@ public class StateBuilder {
cob.labelBinding(label);
}
public void setState(CoroutineBuilder smb, CodeBuilder cob){
cob.aload(0).loadConstant(id).putfield(smb.CD_this, CoroutineBuilder.STATE_NAME, ConstantDescs.CD_int);
public void setState(StateMachineBuilder smb, CodeBuilder cob){
cob.aload(0).loadConstant(id).putfield(smb.CD_this, StateMachineBuilder.STATE_NAME, ConstantDescs.CD_int);
}
}

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
import java.lang.classfile.*;
import java.lang.classfile.attribute.InnerClassInfo;
@ -12,7 +12,9 @@ import java.lang.reflect.AccessFlag;
import java.util.*;
import java.util.stream.Collectors;
public abstract class CoroutineBuilder<T extends CoroutineBuilder<T>> {
import static java.lang.constant.ConstantDescs.*;
public abstract class StateMachineBuilder<T extends StateMachineBuilder<T>> {
public final static String PARAM_PREFIX = "param_";
public final static String LOCAL_PREFIX = "local_";
public final static String STATE_NAME = "state";
@ -48,7 +50,7 @@ public abstract class CoroutineBuilder<T extends CoroutineBuilder<T>> {
}
}
public CoroutineBuilder(ClassModel src_clm, MethodModel src_mem, CodeModel src_com, String namePostfix){
public StateMachineBuilder(ClassModel src_clm, MethodModel src_mem, CodeModel src_com, String namePostfix){
this.src_clm = src_clm;
this.src_mem = src_mem;
this.src_com = src_com;
@ -91,7 +93,7 @@ public abstract class CoroutineBuilder<T extends CoroutineBuilder<T>> {
for (int i = 0; i < params.length; i++) {
var param = params[i];
for(var attr : param_attrs.get(i))
parameterVariableAnnotations.add(new ParameterVariableAnnotation(attr, CoroutineBuilder.PARAM_PREFIX + offset, param));
parameterVariableAnnotations.add(new ParameterVariableAnnotation(attr, StateMachineBuilder.PARAM_PREFIX + offset, param));
offset += TypeKind.from(param).slotSize();
}

View file

@ -1,9 +1,9 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime;
package com.parkertenbroeck.bcsm.loadtime;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.coroutines.generator.Gen;
import com.parkertenbroeck.coroutines.bcsm.loadtime.future.FutureSMBuilder;
import com.parkertenbroeck.coroutines.bcsm.loadtime.gen.GenSMBuilder;
import com.parkertenbroeck.future.Future;
import com.parkertenbroeck.generator.Gen;
import com.parkertenbroeck.bcsm.loadtime.future.FutureSMBuilder;
import com.parkertenbroeck.bcsm.loadtime.gen.GenSMBuilder;
import java.io.IOException;
import java.lang.classfile.*;
@ -14,14 +14,14 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public class CoroutineClassLoader extends ClassLoader {
public class StateMachineClassLoader extends ClassLoader {
private final HashMap<String, Class<?>> customClazzMap = new HashMap<>();
private final List<String> skip;
private final HashMap<ClassDesc, SMBB> builders;
private final String write_classes_path;
public interface SMBB{
CoroutineBuilder<?> build(ClassModel src_clm, MethodModel src_mem, CodeModel src_com);
StateMachineBuilder<?> build(ClassModel src_clm, MethodModel src_mem, CodeModel src_com);
}
public static class Config{
HashSet<String> skip = new HashSet<>();
@ -33,7 +33,7 @@ public class CoroutineClassLoader extends ClassLoader {
}
public static Config builtin(){
return empty()
.skip("java", "jdk", "jre", "com.parkertenbroeck.coroutines.bcsm.loadtime")
.skip("java", "jdk", "jre", "com.parkertenbroeck.bcsm.loadtime")
.with(Future.class, FutureSMBuilder::new)
.with(Gen.class, GenSMBuilder::new);
}
@ -59,11 +59,11 @@ public class CoroutineClassLoader extends ClassLoader {
}
}
public CoroutineClassLoader(ClassLoader parent) {
public StateMachineClassLoader(ClassLoader parent) {
this(parent, Config.builtin());
}
public CoroutineClassLoader(ClassLoader parent, Config config) {
public StateMachineClassLoader(ClassLoader parent, Config config) {
super(parent);
skip = config.skip.stream().toList();
builders = new HashMap<>(config.builders);
@ -93,7 +93,7 @@ public class CoroutineClassLoader extends ClassLoader {
var p = "/" + name.replace('.', '/') + ".class";
try (var stream = CoroutineClassLoader.class.getResourceAsStream(p)) {
try (var stream = StateMachineClassLoader.class.getResourceAsStream(p)) {
var bytes = Objects.requireNonNull(stream).readAllBytes();
add(name, searchReplaceableMethods(bytes));
return customClazzMap.get(name);
@ -114,7 +114,7 @@ public class CoroutineClassLoader extends ClassLoader {
return ClassFile.of(ClassFile.AttributesProcessingOption.PASS_ALL_ATTRIBUTES, ClassFile.StackMapsOption.STACK_MAPS_WHEN_REQUIRED).build(clm.thisClass().asSymbol(), cb -> {
for (var ce : clm) {
if (ce instanceof MethodModel mem && mem.code().isPresent()) {
CoroutineBuilder<?> builder = builders
StateMachineBuilder<?> builder = builders
.getOrDefault(
mem.methodTypeSymbol().returnType(),
(_, _, _) -> null

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime.future;
package com.parkertenbroeck.bcsm.loadtime.future;
import java.lang.annotation.*;

View file

@ -1,9 +1,8 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime.future;
package com.parkertenbroeck.bcsm.loadtime.future;
import com.parkertenbroeck.coroutines.bcsm.loadtime.*;
import com.parkertenbroeck.coroutines.future.Future;
import com.parkertenbroeck.coroutines.future.Waker;
import com.parkertenbroeck.coroutines.bcsm.loadtime.*;
import com.parkertenbroeck.future.Future;
import com.parkertenbroeck.future.Waker;
import com.parkertenbroeck.bcsm.loadtime.*;
import java.lang.classfile.*;
import java.lang.classfile.instruction.SwitchCase;
@ -12,7 +11,7 @@ import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Consumer;
public class FutureSMBuilder extends CoroutineBuilder<FutureSMBuilder> {
public class FutureSMBuilder extends StateMachineBuilder<FutureSMBuilder> {
public final static ClassDesc CD_Future = ClassDesc.ofDescriptor(Future.class.descriptorString());
public final static ClassDesc CD_Waker = ClassDesc.ofDescriptor(Waker.class.descriptorString());
@ -27,7 +26,7 @@ public class FutureSMBuilder extends CoroutineBuilder<FutureSMBuilder> {
private final HashMap<Integer, Consumer<CodeBuilder>> cancellation_behavior = new HashMap<>();
static class AwaitHandler implements SpecialMethodHandler<FutureSMBuilder> {
static class AwaitHandler implements SpecialMethodHandler<FutureSMBuilder>{
final StateBuilder.State awaiting;
final Label save_label;
final Label resume_inline;

View file

@ -1,8 +1,7 @@
package com.parkertenbroeck.coroutines.bcsm.loadtime.gen;
package com.parkertenbroeck.bcsm.loadtime.gen;
import com.parkertenbroeck.coroutines.bcsm.loadtime.*;
import com.parkertenbroeck.coroutines.generator.Gen;
import com.parkertenbroeck.coroutines.bcsm.loadtime.*;
import com.parkertenbroeck.generator.Gen;
import com.parkertenbroeck.bcsm.loadtime.*;
import java.lang.classfile.*;
import java.lang.constant.ClassDesc;
@ -10,7 +9,7 @@ import java.lang.constant.ConstantDescs;
import java.lang.constant.MethodTypeDesc;
import java.util.List;
public class GenSMBuilder extends CoroutineBuilder<GenSMBuilder> {
public class GenSMBuilder extends StateMachineBuilder<GenSMBuilder> {
public final static ClassDesc CD_Gen = ClassDesc.ofDescriptor(Gen.class.descriptorString());
public final static ClassDesc CD_Res = ClassDesc.ofDescriptor(Gen.Res.class.descriptorString());

View file

@ -1,9 +0,0 @@
package com.parkertenbroeck.coroutines.async_runtime;
import java.util.concurrent.Executors;
public class AsyncExecutor {
void test(){
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}
}

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.future;
package com.parkertenbroeck.future;
public interface Future<R, E extends Throwable> {

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.future;
package com.parkertenbroeck.future;
public interface Waker {

View file

@ -1,4 +1,4 @@
package com.parkertenbroeck.coroutines.generator;
package com.parkertenbroeck.generator;
public interface Gen<Y, R> {
Res<Y, R> next();

View file

@ -1,2 +1,7 @@
rootProject.name = "coroutines"
plugins {
// Apply the foojay-resolver plugin to allow automatic download of JDKs
id("org.gradle.toolchains.foojay-resolver-convention") version "0.10.0"
}
rootProject.name = "generators"
include("lib", "app")