generated from Over-Run/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Over-Run/feature
- Loading branch information
Showing
11 changed files
with
960 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
src/main/java/overrun/marshal/gen/processor/ArgumentProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Overrun Organization | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
*/ | ||
|
||
package overrun.marshal.gen.processor; | ||
|
||
import overrun.marshal.gen.Type; | ||
import overrun.marshal.internal.StringCharset; | ||
|
||
import java.lang.classfile.CodeBuilder; | ||
import java.lang.constant.MethodTypeDesc; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static java.lang.constant.ConstantDescs.CD_boolean; | ||
import static overrun.marshal.internal.Constants.*; | ||
|
||
/** | ||
* Method argument processor | ||
* | ||
* @author squid233 | ||
* @since 0.1.0 | ||
*/ | ||
public final class ArgumentProcessor implements Processor<ProcessorType, ArgumentProcessorContext> { | ||
private static final ArgumentProcessor INSTANCE = new ArgumentProcessor(); | ||
private final List<Processor<ProcessorType, ArgumentProcessorContext>> list = new ArrayList<>(0); | ||
|
||
private ArgumentProcessor() { | ||
} | ||
|
||
@SuppressWarnings("preview") | ||
public boolean process(CodeBuilder builder, ProcessorType type, ArgumentProcessorContext context) { | ||
switch (type) { | ||
case ProcessorType.Value value -> { | ||
if (value == ProcessorType.Value.BOOLEAN && | ||
context.convert() != null) { | ||
final Type convertType = context.convert().value(); | ||
builder.loadInstruction( | ||
value.typeKind(), | ||
context.parameterSlot() | ||
).invokestatic(CD_Marshal, | ||
marshalFromBooleanMethod(convertType), | ||
MethodTypeDesc.of(convertType.classDesc(), CD_boolean)); | ||
} else { | ||
builder.loadInstruction( | ||
value.typeKind().asLoadable(), | ||
context.parameterSlot() | ||
); | ||
} | ||
} | ||
case ProcessorType.Allocator _ -> builder.aload(context.parameterSlot()); | ||
case ProcessorType.Str _ -> { | ||
builder.aload(context.allocatorSlot()) | ||
.aload(context.parameterSlot()); | ||
if (StringCharset.getCharset(builder, context.parameter())) { | ||
builder.invokestatic(CD_Marshal, | ||
"marshal", | ||
MTD_MemorySegment_SegmentAllocator_String_Charset); | ||
} else { | ||
builder.invokestatic(CD_Marshal, | ||
"marshal", | ||
MTD_MemorySegment_SegmentAllocator_String); | ||
} | ||
} | ||
case ProcessorType.Addr _, ProcessorType.CEnum _ -> builder | ||
.aload(context.parameterSlot()) | ||
.invokestatic(CD_Marshal, | ||
"marshal", | ||
MethodTypeDesc.of(type.downcallClassDesc(), type.marshalClassDesc())); | ||
case ProcessorType.Upcall _ -> builder | ||
.aload(context.allocatorSlot()) | ||
.checkcast(CD_Arena) | ||
.aload(context.parameterSlot()) | ||
.invokestatic(CD_Marshal, | ||
"marshal", | ||
MTD_MemorySegment_Arena_Upcall); | ||
case ProcessorType.Array array -> { | ||
final ProcessorType componentType = array.componentType(); | ||
final boolean isStringArray = componentType instanceof ProcessorType.Str; | ||
final boolean isUpcallArray = componentType instanceof ProcessorType.Upcall; | ||
builder.aload(context.allocatorSlot()); | ||
if (isUpcallArray) { | ||
builder.checkcast(CD_Arena); | ||
} | ||
builder.aload(context.parameterSlot()); | ||
if (isStringArray && StringCharset.getCharset(builder, context.parameter())) { | ||
builder.invokestatic(CD_Marshal, | ||
"marshal", | ||
MTD_MemorySegment_SegmentAllocator_StringArray_Charset); | ||
} else { | ||
builder.invokestatic(CD_Marshal, | ||
"marshal", | ||
MethodTypeDesc.of(CD_MemorySegment, | ||
isUpcallArray ? CD_Arena : CD_SegmentAllocator, | ||
array.marshalClassDesc())); | ||
} | ||
} | ||
default -> { | ||
for (var processor : list) { | ||
if (!processor.process(builder, type, context)) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Registers a processor | ||
* | ||
* @param processor the processor | ||
*/ | ||
public void registerProcessor(Processor<ProcessorType, ArgumentProcessorContext> processor) { | ||
list.add(processor); | ||
} | ||
|
||
/** | ||
* {@return this} | ||
*/ | ||
public static ArgumentProcessor getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
private static String marshalFromBooleanMethod(Type convertType) { | ||
return switch (convertType) { | ||
case CHAR -> "marshalAsChar"; | ||
case BYTE -> "marshalAsByte"; | ||
case SHORT -> "marshalAsShort"; | ||
case INT -> "marshalAsInt"; | ||
case LONG -> "marshalAsLong"; | ||
case FLOAT -> "marshalAsFloat"; | ||
case DOUBLE -> "marshalAsDouble"; | ||
}; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/overrun/marshal/gen/processor/ArgumentProcessorContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Overrun Organization | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
*/ | ||
|
||
package overrun.marshal.gen.processor; | ||
|
||
import overrun.marshal.gen.Convert; | ||
|
||
import java.lang.reflect.Parameter; | ||
|
||
/** | ||
* The argument processor context | ||
* | ||
* @param parameter parameter | ||
* @param parameterSlot parameter slot | ||
* @param allocatorSlot allocator slot | ||
* @param convert boolean convert | ||
* @author squid233 | ||
* @since 0.1.0 | ||
*/ | ||
public record ArgumentProcessorContext( | ||
Parameter parameter, | ||
int parameterSlot, | ||
int allocatorSlot, | ||
Convert convert | ||
) { | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/overrun/marshal/gen/processor/Processor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Overrun Organization | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
*/ | ||
|
||
package overrun.marshal.gen.processor; | ||
|
||
import java.lang.classfile.CodeBuilder; | ||
|
||
/** | ||
* Processor | ||
* | ||
* @param <T> target type | ||
* @param <C> context type | ||
* @author squid233 | ||
* @since 0.1.0 | ||
*/ | ||
public interface Processor<T, C> { | ||
/** | ||
* Processes with the context | ||
* | ||
* @param builder the code builder | ||
* @param type the type | ||
* @param context the context | ||
* @return {@code true} if should continue; {@code false} otherwise | ||
*/ | ||
boolean process(CodeBuilder builder, T type, C context); | ||
} |
Oops, something went wrong.