-
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
Merged
Merged
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, for whichever backend | ||
| * is running. Obtained from the generated module's {@code imports()}. | ||
| */ | ||
| public final class ImportFactory { | ||
|
|
||
| private final NativeMachineFactoryProvider provider; | ||
|
|
||
| private ImportFactory(NativeMachineFactoryProvider provider) { | ||
| this.provider = provider; | ||
| } | ||
|
|
||
| /** | ||
| * Picks the backend for a module holding this native code, or none. Called by the | ||
| * generated {@code imports()}. | ||
| */ | ||
| public static ImportFactory forNativeCode(byte[][] nativeCode) { | ||
| if (nativeCode == null) { | ||
| return forBytecode(); | ||
| } | ||
| return NativeMachineFactoryProvider.discover() | ||
| .map(ImportFactory::forNative) | ||
| .orElseGet(ImportFactory::forBytecode); | ||
| } | ||
|
|
||
| /** 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
58 changes: 58 additions & 0 deletions
58
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,58 @@ | ||
| 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 aModuleWithoutNativeCodeGetsTheBytecodeTypes() { | ||
| assertFalse(ImportFactory.forNativeCode(null).isNative()); | ||
| } | ||
|
|
||
| @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
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.
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.
Can we remove this generated code in favor of, maybe, another method or an instance method on ImportFactory?