Skip to content

Commit

Permalink
Merge pull request #24 from peterhuene/use-different-store
Browse files Browse the repository at this point in the history
Use a different store for the host.
  • Loading branch information
peterhuene authored Jul 1, 2020
2 parents 8396b59 + d719298 commit 95cff5f
Show file tree
Hide file tree
Showing 26 changed files with 250 additions and 88 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ jobs:
run: dotnet build -c ${{ matrix.config }} --no-restore
- name: Test
run: dotnet test -c ${{ matrix.config }}
- name: Benchmark
if: matrix.config == 'release'
run: dotnet run -c ${{ matrix.config }} --project benchmarks/simple/simple.csproj
- name: Run examples
run: |
cd examples/hello
dotnet run
cd ../global
dotnet run
cd ../memory
dotnet run
- name: Create package
run: |
cd src
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@

bin/
obj/

BenchmarkDotNet.Artifacts/
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
You can add a package reference with the [.NET Core SDK](https://dotnet.microsoft.com/):

```text
$ dotnet add package --version 0.15.0-preview1 wasmtime
$ dotnet add package --version 0.18.1-preview1 wasmtime
```

_Note that the `--version` option is required because the package is currently prerelease._
Expand All @@ -54,7 +54,7 @@ $ dotnet new console
Next, add a reference to the [Wasmtime package](https://www.nuget.org/packages/Wasmtime):

```
$ dotnet add package --version 0.15.0-preview1 wasmtime
$ dotnet add package --version 0.18.1-preview1 wasmtime
```

Replace the contents of `Program.cs` with the following code:
Expand All @@ -69,14 +69,15 @@ namespace Tutorial
{
static void Main(string[] args)
{
using var store = new Store();
using var engine = new Engine();
using var store = new Store(engine);

using var module = store.LoadModuleText(
"hello",
"(module (func $hello (import \"\" \"hello\")) (func (export \"run\") (call $hello)))"
);

using var host = new Host(store);
using var host = new Host(engine);

host.DefineFunction(
"",
Expand Down
79 changes: 79 additions & 0 deletions benchmarks/simple/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using Wasmtime;

namespace Simple
{
[Config(typeof(Config))]
public class Benchmark
{
private class Config : ManualConfig
{
public Config()
{
AddJob(Job.MediumRun
.WithLaunchCount(1)
.WithToolchain(InProcessEmitToolchain.Instance)
.WithId("InProcess"));
}
}

public Benchmark()
{
_engine = new Engine();
_store = new Store(_engine);
_module = _store.LoadModuleText("hello", @"
(module
(type $t0 (func))
(import """" ""hello"" (func $.hello (type $t0)))
(func $run
call $.hello
)
(export ""run"" (func $run))
)");
}

[Benchmark]
public void SayHello()
{
using var host = new Host(_engine);

host.DefineFunction("", "hello", () => { });

// Define a memory to add memory pressure if not disposed in the benchmark test
using var memory = host.DefineMemory("", "memory", 3);

using dynamic instance = host.Instantiate(_module);
instance.run();
}

private Engine _engine;
private Store _store;
private Module _module;
}

class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Benchmark>();

var report = summary[summary.BenchmarksCases.Where(c => c.Descriptor.Type == typeof(Benchmark)).Single()];
if (!(report is null))
{
if (report.ExecuteResults.All(r => r.ExitCode == 0))
{
return;
}
}

Console.Error.WriteLine("Benchmark failed.");
Environment.Exit(1);
}
}
}
13 changes: 13 additions & 0 deletions benchmarks/simple/simple.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<ProjectReference Include="..\..\src\Wasmtime.csproj" />
</ItemGroup>

</Project>
7 changes: 4 additions & 3 deletions docs/articles/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dotnet new console
To use the .NET embedding of Wasmtime from the project, we need to add a reference to the [Wasmtime NuGet package](https://www.nuget.org/packages/Wasmtime):

```text
dotnet add package --version 0.15.0-preview1 wasmtime
dotnet add package --version 0.18.1-preview1 wasmtime
```

_Note that the `--version` option is required because the package is currently prerelease._
Expand All @@ -65,9 +65,10 @@ namespace Tutorial
{
static void Main(string[] args)
{
using var store = new Store();
using var engine = new Engine();
using var store = new Store(engine);
using var module = store.LoadModuleText("hello", "(module (func $hello (import \"\" \"hello\")) (func (export \"run\") (call $hello)))");
using var host = new Host(store);
using var host = new Host(engine);

host.DefineFunction(
"",
Expand Down
5 changes: 3 additions & 2 deletions examples/global/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class Program
{
static void Main(string[] args)
{
using var store = new Store();
using var engine = new Engine();
using var store = new Store(engine);
using var module = store.LoadModuleText("global.wat");
using var host = new Host(store);
using var host = new Host(engine);

var global = host.DefineMutableGlobal("", "global", 1);

Expand Down
5 changes: 3 additions & 2 deletions examples/hello/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class Program
{
static void Main(string[] args)
{
using var store = new Store();
using var engine = new Engine();
using var store = new Store(engine);
using var module = store.LoadModuleText("hello.wat");
using var host = new Host(store);
using var host = new Host(engine);

host.DefineFunction(
"",
Expand Down
5 changes: 3 additions & 2 deletions examples/memory/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class Program
{
static void Main(string[] args)
{
using var store = new Store();
using var engine = new Engine();
using var store = new Store(engine);
using var module = store.LoadModuleText("memory.wat");
using var host = new Host(store);
using var host = new Host(engine);

host.DefineFunction(
"",
Expand Down
57 changes: 57 additions & 0 deletions src/Engine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace Wasmtime
{
/// <summary>
/// Represents a WebAssembly engine.
/// </summary>
public class Engine : IDisposable
{
/// <summary>
/// Constructs a new default engine.
/// </summary>
public Engine()
{
_handle = Interop.wasm_engine_new();
}

/// <inheritdoc/>
public void Dispose()
{
if (!_handle.IsInvalid)
{
_handle.Dispose();
_handle.SetHandleAsInvalid();
}
}

internal Engine(Interop.WasmConfigHandle config)
{
if (config.IsInvalid)
{
throw new WasmtimeException("Failed to create Wasmtime engine.");
}

_handle = Interop.wasm_engine_new_with_config(config);
config.SetHandleAsInvalid();
}

internal Interop.EngineHandle Handle
{
get
{
if (_handle.IsInvalid)
{
throw new ObjectDisposedException(typeof(Engine).FullName);
}

return _handle;
}
}

private Interop.EngineHandle _handle;
}
}
30 changes: 15 additions & 15 deletions src/StoreBuilder.cs → src/EngineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public enum OptimizationLevel
}

/// <summary>
/// Represents a builder of <see cref="Store"/> instances.
/// Represents a builder of <see cref="Engine"/> instances.
/// </summary>
public class StoreBuilder
public class EngineBuilder
{
/// <summary>
/// Sets whether or not to enable debug information.
/// </summary>
/// <param name="enable">True to enable debug information or false to disable.</param>
/// <returns>Returns the current builder.</returns>
public StoreBuilder WithDebugInfo(bool enable)
public EngineBuilder WithDebugInfo(bool enable)
{
_enableDebugInfo = enable;
return this;
Expand All @@ -63,7 +63,7 @@ public StoreBuilder WithDebugInfo(bool enable)
/// </summary>
/// <param name="enable">True to enable WebAssembly threads support or false to disable.</param>
/// <returns>Returns the current builder.</returns>
public StoreBuilder WithWasmThreads(bool enable)
public EngineBuilder WithWasmThreads(bool enable)
{
_enableWasmThreads = enable;
return this;
Expand All @@ -74,7 +74,7 @@ public StoreBuilder WithWasmThreads(bool enable)
/// </summary>
/// <param name="enable">True to enable WebAssembly reference types support or false to disable.</param>
/// <returns>Returns the current builder.</returns>
public StoreBuilder WithReferenceTypes(bool enable)
public EngineBuilder WithReferenceTypes(bool enable)
{
_enableReferenceTypes = enable;
return this;
Expand All @@ -85,7 +85,7 @@ public StoreBuilder WithReferenceTypes(bool enable)
/// </summary>
/// <param name="enable">True to enable WebAssembly SIMD support or false to disable.</param>
/// <returns>Returns the current builder.</returns>
public StoreBuilder WithSIMD(bool enable)
public EngineBuilder WithSIMD(bool enable)
{
_enableSIMD = enable;
return this;
Expand All @@ -96,7 +96,7 @@ public StoreBuilder WithSIMD(bool enable)
/// </summary>
/// <param name="enable">True to enable WebAssembly multi-value support or false to disable.</param>
/// <returns>Returns the current builder.</returns>
public StoreBuilder WithMultiValue(bool enable)
public EngineBuilder WithMultiValue(bool enable)
{
_enableMultiValue = enable;
return this;
Expand All @@ -107,7 +107,7 @@ public StoreBuilder WithMultiValue(bool enable)
/// </summary>
/// <param name="enable">True to enable WebAssembly bulk memory support or false to disable.</param>
/// <returns>Returns the current builder.</returns>
public StoreBuilder WithBulkMemory(bool enable)
public EngineBuilder WithBulkMemory(bool enable)
{
_enableBulkMemory = enable;
return this;
Expand All @@ -118,7 +118,7 @@ public StoreBuilder WithBulkMemory(bool enable)
/// </summary>
/// <param name="strategy">The compiler strategy to use.</param>
/// <returns>Returns the current builder.</returns>
public StoreBuilder WithCompilerStrategy(CompilerStrategy strategy)
public EngineBuilder WithCompilerStrategy(CompilerStrategy strategy)
{
switch (strategy)
{
Expand All @@ -145,7 +145,7 @@ public StoreBuilder WithCompilerStrategy(CompilerStrategy strategy)
/// </summary>
/// <param name="enable">True to enable the Cranelift debug verifier or false to disable.</param>
/// <returns>Returns the current builder.</returns>
public StoreBuilder WithCraneliftDebugVerifier(bool enable)
public EngineBuilder WithCraneliftDebugVerifier(bool enable)
{
_enableCraneliftDebugVerifier = enable;
return this;
Expand All @@ -156,7 +156,7 @@ public StoreBuilder WithCraneliftDebugVerifier(bool enable)
/// </summary>
/// <param name="level">The optimization level to use.</param>
/// <returns>Returns the current builder.</returns>
public StoreBuilder WithOptimizationLevel(OptimizationLevel level)
public EngineBuilder WithOptimizationLevel(OptimizationLevel level)
{
switch (level)
{
Expand All @@ -179,10 +179,10 @@ public StoreBuilder WithOptimizationLevel(OptimizationLevel level)
}

/// <summary>
/// Builds the <see cref="Store" /> instance.
/// Builds the <see cref="Engine" /> instance.
/// </summary>
/// <returns>Returns the new <see cref="Store" /> instance.</returns>
public Store Build()
/// <returns>Returns the new <see cref="Engine" /> instance.</returns>
public Engine Build()
{
var config = Interop.wasm_config_new();

Expand Down Expand Up @@ -231,7 +231,7 @@ public Store Build()
Interop.wasmtime_config_cranelift_opt_level_set(config, _optLevel.Value);
}

return new Store(config);
return new Engine(config);
}

private bool? _enableDebugInfo;
Expand Down
Loading

0 comments on commit 95cff5f

Please sign in to comment.