Skip to content
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

Add a wasmtime-specific wasmtime_wat2wasm C API #1206

Merged
merged 7 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ doctest = false
wasmtime = { path = "../api" }
wasi-common = { path = "../wasi-common" }
wasmtime-wasi = { path = "../wasi" }
wat = "1.0"
6 changes: 6 additions & 0 deletions crates/c-api/include/wasmtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ WASMTIME_CONFIG_PROP(cranelift_opt_level, wasmtime_opt_level_t)

///////////////////////////////////////////////////////////////////////////////

bool wasmtime_wat2wasm(
wasm_byte_vec_t *ret,
wasm_engine_t *engine,
const char* wat,
);
yurydelendik marked this conversation as resolved.
Show resolved Hide resolved

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
23 changes: 22 additions & 1 deletion crates/c-api/src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! This file defines the extern "C" API extension, which are specific
//! to the wasmtime implementation.

use crate::wasm_config_t;
use crate::{wasm_byte_vec_t, wasm_config_t, wasm_engine_t};
use std::ffi::CStr;
use std::os::raw::c_char;
use wasmtime::{OptLevel, Strategy};

#[repr(u8)]
Expand Down Expand Up @@ -86,3 +88,22 @@ pub unsafe extern "C" fn wasmtime_config_cranelift_opt_level_set(
WASMTIME_OPT_LEVEL_SPEED_AND_SIZE => OptLevel::SpeedAndSize,
});
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_wat2wasm(
ret: *mut wasm_byte_vec_t,
_engine: *mut wasm_engine_t,
wat: *const c_char,
) -> bool {
let wat = match CStr::from_ptr(wat).to_str() {
Ok(s) => s,
Err(_) => return false,
};
match wat::parse_str(wat) {
Ok(bytes) => {
(*ret).set_from_slice(&bytes);
true
}
Err(_) => false,
}
}
18 changes: 18 additions & 0 deletions crates/misc/dotnet/src/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ public Store CreateStore()
return new Store(this);
}

/// <summary>
/// Converts the WebAssembly text format to the binary format
/// </summary>
/// <returns>Returns the binary-encoded wasm module.</returns>
public byte[] WatToWasm(string wat)
{
unsafe
{
if (!Interop.wasmtime_wat2wasm(out var bytes, Handle, wat)) {
throw new WasmtimeException("failed to parse input wat");
}
var byteSpan = new ReadOnlySpan<byte>(bytes.data, checked((int)bytes.size));
var ret = byteSpan.ToArray();
Interop.wasm_byte_vec_delete(ref bytes);
return ret;
}
}

/// <inheritdoc/>
public void Dispose()
{
Expand Down
14 changes: 11 additions & 3 deletions crates/misc/dotnet/src/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ public static extern unsafe void wasi_config_set_env(
public static extern bool wasi_config_set_stdin_file(
WasiConfigHandle config,
[MarshalAs(UnmanagedType.LPUTF8Str)] string path
);
);

[DllImport(LibraryName)]
public static extern void wasi_config_inherit_stdin(WasiConfigHandle config);
Expand All @@ -909,7 +909,7 @@ public static extern bool wasi_config_set_stdin_file(
public static extern bool wasi_config_set_stdout_file(
WasiConfigHandle config,
[MarshalAs(UnmanagedType.LPUTF8Str)] string path
);
);

[DllImport(LibraryName)]
public static extern void wasi_config_inherit_stdout(WasiConfigHandle config);
Expand All @@ -919,7 +919,7 @@ public static extern bool wasi_config_set_stdout_file(
public static extern bool wasi_config_set_stderr_file(
WasiConfigHandle config,
[MarshalAs(UnmanagedType.LPUTF8Str)] string path
);
);

[DllImport(LibraryName)]
public static extern void wasi_config_inherit_stderr(WasiConfigHandle config);
Expand Down Expand Up @@ -974,5 +974,13 @@ out IntPtr trap

[DllImport(LibraryName)]
public static extern void wasmtime_config_cranelift_opt_level_set(WasmConfigHandle config, wasmtime_opt_level_t level);

[DllImport(LibraryName, CharSet=CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool wasmtime_wat2wasm(
out wasm_byte_vec_t vec,
EngineHandle engine,
string wat
);
}
}
4 changes: 3 additions & 1 deletion crates/misc/dotnet/tests/Fixtures/ModuleFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public ModuleFixture()
.WithReferenceTypes(true)
.Build();
Store = Engine.CreateStore();
Module = Store.CreateModule(Path.Combine("Modules", ModuleFileName));
var wat = Path.Combine("Modules", ModuleFileName);
var wasm = Engine.WatToWasm(File.ReadAllText(wat));
Module = Store.CreateModule(wat, wasm);
}

public void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/FunctionExportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Wasmtime.Tests
{
public class FunctionExportsFixture : ModuleFixture
{
protected override string ModuleFileName => "FunctionExports.wasm";
protected override string ModuleFileName => "FunctionExports.wat";
}

public class FunctionExportsTests : IClassFixture<FunctionExportsFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/FunctionImportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Wasmtime.Tests
{
public class FunctionImportsFixture : ModuleFixture
{
protected override string ModuleFileName => "FunctionImports.wasm";
protected override string ModuleFileName => "FunctionImports.wat";
}

public class FunctionImportsTests : IClassFixture<FunctionImportsFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/FunctionThunkingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Wasmtime.Tests
{
public class FunctionThunkingFixture : ModuleFixture
{
protected override string ModuleFileName => "FunctionThunking.wasm";
protected override string ModuleFileName => "FunctionThunking.wat";
}

public class FunctionThunkingTests : IClassFixture<FunctionThunkingFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/GlobalExportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Wasmtime.Tests
{
public class GlobalExportsFixture : ModuleFixture
{
protected override string ModuleFileName => "GlobalExports.wasm";
protected override string ModuleFileName => "GlobalExports.wat";
}

public class GlobalExportsTests : IClassFixture<GlobalExportsFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/GlobalImportBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Wasmtime.Tests
{
public class GlobalImportBindingFixture : ModuleFixture
{
protected override string ModuleFileName => "GlobalImportBindings.wasm";
protected override string ModuleFileName => "GlobalImportBindings.wat";
}

public class GlobalImportBindingTests : IClassFixture<GlobalImportBindingFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/GlobalImportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Wasmtime.Tests
{
public class GlobalImportsFixture : ModuleFixture
{
protected override string ModuleFileName => "GlobalImports.wasm";
protected override string ModuleFileName => "GlobalImports.wat";
}

public class GlobalImportsTests : IClassFixture<GlobalImportsFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/MemoryExportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Wasmtime.Tests
{
public class MemoryExportsFixture : ModuleFixture
{
protected override string ModuleFileName => "MemoryExports.wasm";
protected override string ModuleFileName => "MemoryExports.wat";
}

public class MemoryExportsTests : IClassFixture<MemoryExportsFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/MemoryImportBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Wasmtime.Tests
{
public class MemoryImportBindingFixture : ModuleFixture
{
protected override string ModuleFileName => "MemoryImportBinding.wasm";
protected override string ModuleFileName => "MemoryImportBinding.wat";
}

public class MemoryImportBindingTests : IClassFixture<MemoryImportBindingFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/MemoryImportFromModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Wasmtime.Tests
{
public class MemoryImportFromModuleFixture : ModuleFixture
{
protected override string ModuleFileName => "MemoryImportFromModule.wasm";
protected override string ModuleFileName => "MemoryImportFromModule.wat";
}

public class MemoryImportFromModuleTests : IClassFixture<MemoryImportFromModuleFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/MemoryImportNoUpperBoundTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Wasmtime.Tests
{
public class MemoryImportNoUpperBoundFixture : ModuleFixture
{
protected override string ModuleFileName => "MemoryImportNoUpperBound.wasm";
protected override string ModuleFileName => "MemoryImportNoUpperBound.wat";
}

public class MemoryImportNoUpperBoundTests : IClassFixture<MemoryImportNoUpperBoundFixture>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Wasmtime.Tests
{
public class MemoryImportWithUpperBoundFixture : ModuleFixture
{
protected override string ModuleFileName => "MemoryImportWithUpperBound.wasm";
protected override string ModuleFileName => "MemoryImportWithUpperBound.wat";
}

public class MemoryImportWithUpperBoundTests : IClassFixture<MemoryImportWithUpperBoundFixture>
Expand Down
Binary file removed crates/misc/dotnet/tests/Modules/FunctionExports.wasm
Binary file not shown.
Binary file removed crates/misc/dotnet/tests/Modules/FunctionImports.wasm
Binary file not shown.
Binary file removed crates/misc/dotnet/tests/Modules/FunctionThunking.wasm
Binary file not shown.
Binary file removed crates/misc/dotnet/tests/Modules/GlobalExports.wasm
Binary file not shown.
Binary file not shown.
Binary file removed crates/misc/dotnet/tests/Modules/GlobalImports.wasm
Binary file not shown.
Binary file removed crates/misc/dotnet/tests/Modules/MemoryExports.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed crates/misc/dotnet/tests/Modules/TableExports.wasm
Binary file not shown.
Binary file removed crates/misc/dotnet/tests/Modules/TableImports.wasm
Binary file not shown.
Binary file removed crates/misc/dotnet/tests/Modules/Wasi.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/TableExportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Wasmtime.Tests
{
public class TableExportsFixture : ModuleFixture
{
protected override string ModuleFileName => "TableExports.wasm";
protected override string ModuleFileName => "TableExports.wat";
}

public class TableExportsTests : IClassFixture<TableExportsFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/TableImportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Wasmtime.Tests
{
public class TableImportsFixture : ModuleFixture
{
protected override string ModuleFileName => "TableImports.wasm";
protected override string ModuleFileName => "TableImports.wat";
}

public class TableImportsTests : IClassFixture<TableImportsFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/TempFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ public void Dispose()

public string Path { get; private set; }
}
}
}
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/WasiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Wasmtime.Tests
{
public class WasiFixture : ModuleFixture
{
protected override string ModuleFileName => "Wasi.wasm";
protected override string ModuleFileName => "Wasi.wat";
}

public class WasiTests : IClassFixture<WasiFixture>
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/dotnet/tests/Wasmtime.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</Target>

<ItemGroup>
<None Update="Modules/*.wasm" CopyToOutputDirectory="PreserveNewest" />
<None Update="Modules/*.wat" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>