-
Notifications
You must be signed in to change notification settings - Fork 54
Added Wasi2Configuration
#339
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
martindevans
wants to merge
2
commits into
bytecodealliance:main
Choose a base branch
from
martindevans:wasip2-config
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using Microsoft.Win32.SafeHandles; | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Wasmtime; | ||
|
||
/// <summary> | ||
/// Represents a WASI2 configuration. | ||
/// </summary> | ||
public class Wasi2Configuration | ||
{ | ||
private bool _inheritStandardInput; | ||
private bool _inheritStandardOutput; | ||
private bool _inheritStandardError; | ||
|
||
/// <summary> | ||
/// Sets the configuration to inherit stdin. | ||
/// </summary> | ||
/// <returns>Returns the current configuration.</returns> | ||
public Wasi2Configuration WithInheritedStandardInput() | ||
{ | ||
_inheritStandardInput = true; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the configuration to inherit stdout. | ||
/// </summary> | ||
/// <returns>Returns the current configuration.</returns> | ||
public Wasi2Configuration WithInheritedStandardOutput() | ||
{ | ||
_inheritStandardOutput = true; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the configuration to inherit stderr. | ||
/// </summary> | ||
/// <returns>Returns the current configuration.</returns> | ||
public Wasi2Configuration WithInheritedStandardError() | ||
{ | ||
_inheritStandardError = true; | ||
return this; | ||
} | ||
|
||
internal Handle Build() | ||
{ | ||
var config = new Handle(Native.wasmtime_wasip2_config_new()); | ||
|
||
SetStandardIn(config); | ||
SetStandardOut(config); | ||
SetStandardError(config); | ||
|
||
return config; | ||
} | ||
|
||
private void SetStandardIn(Handle config) | ||
{ | ||
if (_inheritStandardInput) | ||
Native.wasmtime_wasip2_config_inherit_stdin(config); | ||
} | ||
|
||
private void SetStandardOut(Handle config) | ||
{ | ||
if (_inheritStandardOutput) | ||
Native.wasmtime_wasip2_config_inherit_stdout(config); | ||
} | ||
|
||
private void SetStandardError(Handle config) | ||
{ | ||
if (_inheritStandardError) | ||
Native.wasmtime_wasip2_config_inherit_stderr(config); | ||
} | ||
|
||
internal class Handle : SafeHandleZeroOrMinusOneIsInvalid | ||
{ | ||
public Handle(IntPtr handle) | ||
: base(true) | ||
{ | ||
SetHandle(handle); | ||
} | ||
|
||
protected override bool ReleaseHandle() | ||
{ | ||
Native.wasmtime_wasip2_config_delete(handle); | ||
return true; | ||
} | ||
} | ||
|
||
private static class Native | ||
{ | ||
[DllImport(Engine.LibraryName)] | ||
public static extern IntPtr wasmtime_wasip2_config_new(); | ||
|
||
[DllImport(Engine.LibraryName)] | ||
public static extern void wasmtime_wasip2_config_delete(IntPtr config); | ||
|
||
[DllImport(Engine.LibraryName)] | ||
public static extern void wasmtime_wasip2_config_inherit_stdin(Handle config); | ||
|
||
[DllImport(Engine.LibraryName)] | ||
public static extern void wasmtime_wasip2_config_inherit_stdout(Handle config); | ||
|
||
[DllImport(Engine.LibraryName)] | ||
public static extern void wasmtime_wasip2_config_inherit_stderr(Handle config); | ||
|
||
[DllImport(Engine.LibraryName)] | ||
public static extern unsafe void wasmtime_wasip2_config_arg(Handle config, char* arg, nuint arg_len); | ||
} | ||
} |
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,21 @@ | ||
using System; | ||
using Xunit; | ||
|
||
namespace Wasmtime.Tests; | ||
|
||
public class Wasi2Tests | ||
{ | ||
[Fact] | ||
public void ItSetsWasi2Config() | ||
{ | ||
using var engine = new Engine(); | ||
using var store = new Store(engine); | ||
|
||
var config = new Wasi2Configuration(); | ||
config.WithInheritedStandardInput(); | ||
config.WithInheritedStandardOutput(); | ||
config.WithInheritedStandardError(); | ||
|
||
store.SetWasiConfiguration(config); | ||
} | ||
} |
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.
Any reason to not test the other configs?
Uh oh!
There was an error while loading. Please reload this page.
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.
I'll have a look at adding some more tests later today
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.
I have added some more coverage (just calling other
WithInheritedX
methods).I'm not really happy about these tests though - they don't actually assert anything!
Ideally I guess we'd need a
WASI2.wat
test file that reads some input, logs some out and logs some error output. Then we could assert that all of that is written to the correct outputs.