Skip to content

Instantly share code, notes, and snippets.

@RReverser
Created June 26, 2023 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RReverser/db10fc4dfe039630eb24821e1d619bf7 to your computer and use it in GitHub Desktop.
Save RReverser/db10fc4dfe039630eb24821e1d619bf7 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <mono-wasi/driver.h>
#include <mono/metadata/appdomain.h>
// #include <mono/metadata/mono.h>
#include <mono/metadata/object.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/image.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
// from https://github.com/dotnet/dotnet-wasi-sdk/blob/2dbb00c779180873d3ed985e59e431f56404d8da/src/Wasi.AspNetCore.Server.Native/native/dotnet_method.h
#define DEFINE_DOTNET_METHOD(c_name, assembly_name, namespc, class_name, method_name) \
MonoMethod* method_##c_name;\
MonoObject* c_name(MonoObject* target_instance, void* method_params[]) {\
if (!method_##c_name) {\
method_##c_name = lookup_dotnet_method(assembly_name, namespc, class_name, method_name, -1);\
assert(method_##c_name);\
}\
\
MonoObject* exception;\
MonoObject* res = mono_wasm_invoke_method(method_##c_name, target_instance, method_params, &exception);\
assert(!exception);\
return res;\
}
DEFINE_DOTNET_METHOD(_helloworld, "MyFirstWasiApp.dll", "Atmo", "Interop", "HelloWorld");
__attribute__((export_name("helloworld")))
void helloworld() {
void *params[] = { };
_helloworld(NULL, params);
}
namespace Atmo;
public class Interop
{
public static void HelloWorld()
{
Console.WriteLine(DateTimeOffset.UnixEpoch);
}
static void Main() {
// Console.WriteLine(DateTimeOffset.UnixEpoch);
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- <PublishAot>true</PublishAot> -->
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Wasi.Sdk" Version="0.1.4-preview.10020" />
<WasiNativeFileReference Include="$(MSBuildThisFileDirectory)\host_interop.c" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment