-
Notifications
You must be signed in to change notification settings - Fork 20
Create imports through a factory that picks native or bytecode #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andreaTP
wants to merge
1
commit into
bytecodealliance:main
Choose a base branch
from
andreaTP:redline-import-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
redline/api/src/main/java/run/endive/redline/experimental/api/ImportFactory.java
This file contains hidden or 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,73 @@ | ||
| package run.endive.redline.experimental.api; | ||
|
|
||
| import java.util.Objects; | ||
| import run.endive.runtime.ByteBufferMemory; | ||
| import run.endive.runtime.GlobalInstance; | ||
| import run.endive.runtime.Memory; | ||
| import run.endive.runtime.TableInstance; | ||
| import run.endive.wasm.types.MemoryLimits; | ||
| import run.endive.wasm.types.MutabilityType; | ||
| import run.endive.wasm.types.Table; | ||
| import run.endive.wasm.types.Value; | ||
|
|
||
| /** | ||
| * Creates the memories, tables and globals a module imports. | ||
| * | ||
| * <p>Natively compiled code reaches an import through a raw address, so it can only | ||
| * use one the running backend built, while the bytecode path wants the ordinary | ||
| * runtime types. Which of those applies is decided by the platform the code lands | ||
| * on, not by anything the caller can see, so the choice is made here instead of at | ||
| * every import site: | ||
| * | ||
| * <pre> | ||
| * var f = MyModule.imports(); | ||
| * var memory = f.memory(new MemoryLimits(1, 2)); | ||
| * </pre> | ||
| * | ||
| * <p>The same source then runs on a platform redline supports and on one it does | ||
| * not, with no branch of its own. | ||
| */ | ||
| public final class ImportFactory { | ||
|
|
||
| private final NativeMachineFactoryProvider provider; | ||
|
|
||
| private ImportFactory(NativeMachineFactoryProvider provider) { | ||
| this.provider = provider; | ||
| } | ||
|
|
||
| /** Builds imports the given native backend can use. */ | ||
| public static ImportFactory forNative(NativeMachineFactoryProvider provider) { | ||
| return new ImportFactory(Objects.requireNonNull(provider, "provider")); | ||
| } | ||
|
|
||
| /** Builds the ordinary runtime imports, for when there is no native backend. */ | ||
| public static ImportFactory forBytecode() { | ||
| return new ImportFactory(null); | ||
| } | ||
|
|
||
| /** Whether these imports are being built for natively compiled code. */ | ||
| public boolean isNative() { | ||
| return provider != null; | ||
| } | ||
|
|
||
| public Memory memory(MemoryLimits limits) { | ||
| if (provider != null) { | ||
| return provider.createMemory(limits); | ||
| } | ||
| return new ByteBufferMemory(limits); | ||
| } | ||
|
|
||
| public TableInstance table(Table table, int initValue) { | ||
| if (provider != null) { | ||
| return provider.createImportTable(table, initValue); | ||
| } | ||
| return new TableInstance(table, initValue); | ||
| } | ||
|
|
||
| public GlobalInstance global(Value value, MutabilityType mutability) { | ||
| if (provider != null) { | ||
| return provider.createImportGlobal(value, mutability); | ||
| } | ||
| return GlobalInstance.builder().value(value).mutabilityType(mutability).build(); | ||
| } | ||
| } | ||
This file contains hidden or 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
53 changes: 53 additions & 0 deletions
53
redline/api/src/test/java/run/endive/redline/experimental/api/ImportFactoryTest.java
This file contains hidden or 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,53 @@ | ||
| package run.endive.redline.experimental.api; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import run.endive.runtime.ByteBufferMemory; | ||
| import run.endive.runtime.TableInstance; | ||
| import run.endive.wasm.types.MemoryLimits; | ||
| import run.endive.wasm.types.MutabilityType; | ||
| import run.endive.wasm.types.Table; | ||
| import run.endive.wasm.types.TableLimits; | ||
| import run.endive.wasm.types.ValType; | ||
| import run.endive.wasm.types.Value; | ||
|
|
||
| /** | ||
| * With no native provider the factory has to hand back the ordinary runtime types, | ||
| * which is what a platform redline does not compile for ends up running. | ||
| */ | ||
| public class ImportFactoryTest { | ||
|
|
||
| private static final ImportFactory BYTECODE = ImportFactory.forBytecode(); | ||
|
|
||
| @Test | ||
| public void withoutAProviderItBuildsTheBytecodeTypes() { | ||
| assertFalse(BYTECODE.isNative()); | ||
| assertInstanceOf(ByteBufferMemory.class, BYTECODE.memory(new MemoryLimits(1, 2))); | ||
| assertInstanceOf( | ||
| TableInstance.class, | ||
| BYTECODE.table( | ||
| new Table(ValType.FuncRef, new TableLimits(1, 1)), Value.REF_NULL_VALUE)); | ||
| } | ||
|
|
||
| @Test | ||
| public void aBytecodeGlobalKeepsItsValueAndMutability() { | ||
| var global = BYTECODE.global(Value.i32(7), MutabilityType.Var); | ||
|
|
||
| assertEquals(7, global.getValue()); | ||
| assertEquals(MutabilityType.Var, global.getMutabilityType()); | ||
| assertEquals(ValType.I32, global.getType()); | ||
| } | ||
|
|
||
| @Test | ||
| public void aBytecodeTableStartsOnItsInitialiser() { | ||
| var table = | ||
| BYTECODE.table( | ||
| new Table(ValType.FuncRef, new TableLimits(2, 2)), Value.REF_NULL_VALUE); | ||
|
|
||
| assertEquals(2, table.size()); | ||
| assertEquals(Value.REF_NULL_VALUE, table.ref(0)); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -92,6 +92,7 @@ public void extendGeneratedSources() throws IOException { | |
| var type = cu.getClassByName(baseName).orElseThrow(); | ||
|
|
||
| cu.addImport("run.endive.redline.experimental.api.NativeCodeSerializer"); | ||
| cu.addImport("run.endive.redline.experimental.api.ImportFactory"); | ||
| cu.addImport("run.endive.redline.experimental.api.NativeMachineFactoryProvider"); | ||
| cu.addImport("run.endive.redline.experimental.api.internal.RedlineTarget"); | ||
| cu.addImport("java.io.InputStream"); | ||
|
|
@@ -103,6 +104,7 @@ public void extendGeneratedSources() throws IOException { | |
| generateLoadNativeCodeMethod(type); | ||
| generateNativeProviderMethod(type); | ||
| generateBuilderMethod(type, baseName); | ||
| generateImportsMethod(type); | ||
| generateSafeBuilderMethod(type, baseName); | ||
|
|
||
| Files.writeString(sourceFile, cu.toString()); | ||
|
|
@@ -310,6 +312,58 @@ private static void generateNativeProviderMethod(ClassOrInterfaceDeclaration typ | |
| new NameExpr("NativeMachineFactoryProvider"), "discover"))); | ||
| } | ||
|
|
||
| private static void generateImportsMethod(ClassOrInterfaceDeclaration type) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove this generated code in favor of, maybe, another method or an instance method on ImportFactory? |
||
| // Generates: | ||
| // <code> | ||
| // public static ImportFactory imports() { | ||
| // var provider = nativeProvider(); | ||
| // if (provider.isPresent()) { | ||
| // return ImportFactory.forNative(provider.get()); | ||
| // } | ||
| // return ImportFactory.forBytecode(); | ||
| // } | ||
| // </code> | ||
| // | ||
| // Compiled code reaches an imported memory, table or global through a raw | ||
| // address, so it can only use ones the running backend built, while the | ||
| // bytecode path wants the ordinary runtime types. Going through this factory | ||
| // lets the same calling code do both, which is what makes one jar work on a | ||
| // platform redline compiles for and on one it does not. | ||
| // | ||
| // The shape mirrors builder() above, so both generated methods decide the | ||
| // same way. | ||
| var method = | ||
| type.addMethod("imports", Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC) | ||
| .setType(parseClassOrInterfaceType("ImportFactory")); | ||
|
|
||
| var providerVar = | ||
| new ExpressionStmt( | ||
| new VariableDeclarationExpr( | ||
| new VariableDeclarator( | ||
| new VarType(), | ||
| "provider", | ||
| new MethodCallExpr("nativeProvider")))); | ||
|
|
||
| var returnNative = | ||
| new ReturnStmt( | ||
| new MethodCallExpr( | ||
| new NameExpr("ImportFactory"), | ||
| "forNative", | ||
| new NodeList<>( | ||
| new MethodCallExpr(new NameExpr("provider"), "get")))); | ||
|
|
||
| var ifProviderPresent = | ||
| new IfStmt() | ||
| .setCondition(new MethodCallExpr(new NameExpr("provider"), "isPresent")) | ||
| .setThenStmt(new BlockStmt(new NodeList<>(returnNative))); | ||
|
|
||
| var body = method.createBody(); | ||
| body.addStatement(providerVar); | ||
| body.addStatement(ifProviderPresent); | ||
| body.addStatement( | ||
| new ReturnStmt(new MethodCallExpr(new NameExpr("ImportFactory"), "forBytecode"))); | ||
| } | ||
|
|
||
| private static void generateBuilderMethod(ClassOrInterfaceDeclaration type, String moduleName) { | ||
| // Generates: | ||
| // <code> | ||
|
|
||
This file contains hidden or 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 hidden or 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
Binary file added
BIN
+194 Bytes
redline/it/src/it/redline-e2e-panama/src/test/resources/imports.wat.wasm
Binary file not shown.
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is ways too long and it brings history with it which is useless for the user.
Should be very short and just tell the minimum important info.