Browse Source

Migrated from Wasi.SDK to dotnet wasi-wasm

Krzysztof Krysiński 1 year ago
parent
commit
b19eab6d15

+ 1 - 1
src/PixiEditor.Extensions.Wasm/Interop.cs

@@ -19,7 +19,7 @@ internal static partial class Interop
     [MethodImpl(MethodImplOptions.InternalCall)]
     internal static extern void StateChanged(int uniqueId, IntPtr data, int length);
 
-    internal static void Load()
+    public static void Load()
     {
         Type extensionType = Assembly.GetEntryAssembly().ExportedTypes
             .FirstOrDefault(type => type.IsSubclassOf(typeof(WasmExtension)));

+ 2 - 2
src/PixiEditor.Extensions.Wasm/build/PixiEditor.Extensions.Wasm.targets

@@ -1,7 +1,7 @@
 <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup>
-    <WasiNativeFileReference Include="$(MSBuildThisFileDirectory)..\native\*.c" />
-    <WasiAfterRuntimeLoaded Include="attach_internal_calls" />
+    <_WasiFilePathForFixup Include="$(MSBuildThisFileDirectory)..\native\*.c" />
+    <!--<WasiAfterRuntimeLoaded Include="attach_internal_calls" />-->
   </ItemGroup>
 
  <!-- <Target Name="GenerateLayouts" AfterTargets="Build" Inputs="bin\Debug\$(TargetFramework)\PixiEditor.Extensions.Wasm.dll" Outputs="$(RootFolder)\Layouts\">

+ 27 - 11
src/PixiEditor.Extensions.Wasm/native/api_interop.c

@@ -1,8 +1,29 @@
-#include <mono-wasi/driver.h>
-#include <string.h>
 #include <assert.h>
+#include <driver.h>
+#include <mono/metadata/object.h>
+#include <mono/metadata/exception.h>
 #include "api.h"
 
+extern void _start(void);
+
+void attach_internal_calls()
+{
+    attach_logger_calls();
+    attach_window_calls();
+    attach_layout_builder_calls();
+}
+
+void initialize_runtime(void)
+{
+    static int runtime_initialized = 0;
+
+    if (runtime_initialized == 0) {
+        _start();
+        attach_internal_calls();
+        runtime_initialized = 1;
+    }
+}
+
 MonoMethod* lookup_interop_method(const char* method_name)
 {
     MonoMethod* method = NULL;
@@ -14,8 +35,8 @@ MonoMethod* lookup_interop_method(const char* method_name)
 
 void invoke_interop_method(MonoMethod* method, void* params)
 {
-    MonoObject* exception;
-    mono_wasm_invoke_method(method, NULL, params, &exception);
+    MonoObject* exception = NULL;
+    MonoObject* res = mono_runtime_invoke(method, NULL, params, &exception);
     assert(!exception);
 
     free(exception);
@@ -25,6 +46,8 @@ void invoke_interop_method(MonoMethod* method, void* params)
 __attribute((export_name("load")))
 void load()
 {
+    initialize_runtime();
+
     MonoMethod* metod = lookup_interop_method("Load");
     invoke_interop_method(metod, NULL);
 }
@@ -34,11 +57,4 @@ void initialize()
 {
     MonoMethod* metod = lookup_interop_method("Initialize");
     invoke_interop_method(metod, NULL);
-}
-
-void attach_internal_calls()
-{
-    attach_logger_calls();
-    attach_window_calls();
-    attach_layout_builder_calls();
 }

+ 8 - 4
src/PixiEditor.Extensions.Wasm/native/layout_builder_api.c

@@ -1,6 +1,9 @@
-#include <mono-wasi/driver.h>
-#include <string.h>
 #include <assert.h>
+#include <driver.h>
+#include <string.h>
+#include <mono/metadata/object.h>
+#include <mono/metadata/exception.h>
+#include <mono/metadata/appdomain.h>
 
 #include "api.h"
 
@@ -12,7 +15,7 @@ void state_changed(int32_t elementId, uint8_t* data, int32_t length);
 
 void internal_subscribe_to_event(int32_t elementId, MonoString* eventName)
 {
-    char* eventNameString = mono_wasm_string_get_utf8(eventName);
+    char* eventNameString = mono_string_to_utf8(eventName);
     subscribe_to_event(elementId, eventNameString, strlen(eventNameString));
 }
 
@@ -20,7 +23,8 @@ __attribute((export_name("raise_element_event")))
 void raise_element_event(int32_t elementId, const char* eventName)
 {
     MonoMethod* method = lookup_interop_method("EventRaised");
-    void* args[] = { &elementId, mono_wasm_string_from_js(eventName) };
+    MonoString* monoEventName = mono_string_new(mono_domain_get(), eventName);
+    void* args[] = { &elementId, monoEventName };
     invoke_interop_method(method, args);
 
     free(method);

+ 5 - 3
src/PixiEditor.Extensions.Wasm/native/logger_api.c

@@ -1,13 +1,15 @@
-#include <mono-wasi/driver.h>
-#include <string.h>
 #include <assert.h>
+#include <driver.h>
+#include <mono/metadata/object.h>
+#include <string.h>
+#include <mono/metadata/exception.h>
 
 __attribute__((import_name("log_message")))
 void log_message(const char* message, int32_t messageLength);
 
 void logger_log_message(MonoString* message)
 {
-    char* message_utf8 = mono_wasm_string_get_utf8(message);
+    char* message_utf8 = mono_string_to_utf8(message);
     log_message(message_utf8, strlen(message_utf8));
 }
 

+ 9 - 5
src/PixiEditor.Extensions.Wasm/native/window_api.c

@@ -1,6 +1,9 @@
-#include <mono-wasi/driver.h>
-#include <string.h>
 #include <assert.h>
+#include <driver.h>
+#include <string.h>
+#include <mono/metadata/object.h>
+#include <mono/metadata/exception.h>
+#include <mono/metadata/appdomain.h>
 
 #include "api.h"
 
@@ -10,7 +13,7 @@ int32_t create_popup_window(const char* title, int32_t titleLength, uint8_t* dat
 // content is byte[] from C#
 int32_t internal_create_popup_window(MonoString* title, uint8_t* data, int32_t length)
 {
-    char* title_utf8 = mono_wasm_string_get_utf8(title);
+    char* title_utf8 = mono_string_to_utf8(title);
     return create_popup_window(title_utf8, strlen(title_utf8), data, length);
 }
 
@@ -19,7 +22,7 @@ void set_window_title(int32_t windowHandle, const char* title, int32_t titleLeng
 
 void internal_set_window_title(int32_t windowHandle, MonoString* title)
 {
-    char* title_utf8 = mono_wasm_string_get_utf8(title);
+    char* title_utf8 = mono_string_to_utf8(title);
     set_window_title(windowHandle, title_utf8, strlen(title_utf8));
 }
 
@@ -28,7 +31,8 @@ char* get_window_title(int32_t windowHandle);
 
 MonoString* internal_get_window_title(int32_t windowHandle)
 {
-    return mono_wasm_string_from_js(get_window_title(windowHandle));
+    MonoString* str = mono_string_new(mono_get_root_domain(), get_window_title(windowHandle));
+    return str;
 }
 
 __attribute__((import_name("show_window")))

+ 6 - 7
src/PixiEditor.Extensions.WasmRuntime/WasmExtensionInstance.cs

@@ -40,10 +40,15 @@ public partial class WasmExtensionInstance : Extension
 
         Instance = Linker.Instantiate(Store, Module);
         WasmMemoryUtility = new WasmMemoryUtility(Instance);
-        Instance.GetFunction("_start").Invoke();
         memory = Instance.GetMemory("memory");
     }
 
+    protected override void OnLoaded()
+    {
+        Instance.GetAction("load").Invoke();
+        base.OnLoaded();
+    }
+
     protected override void OnInitialized()
     {
         LayoutBuilder = new LayoutBuilder((ElementMap)Api.Services.GetService(typeof(ElementMap)));
@@ -53,12 +58,6 @@ public partial class WasmExtensionInstance : Extension
         base.OnInitialized();
     }
 
-    protected override void OnLoaded()
-    {
-        Instance.GetAction("load").Invoke();
-        base.OnLoaded();
-    }
-
     private void SetElementMap()
     {
         var elementMap = (ElementMap)Api.Services.GetService(typeof(ElementMap));

+ 2 - 1
src/WasmSampleExtension/SampleExtension.cs

@@ -1,4 +1,5 @@
-using PixiEditor.Extensions.Wasm;
+using System;
+using PixiEditor.Extensions.Wasm;
 using PixiEditor.Extensions.Wasm.Api.FlyUI;
 
 namespace WasmSampleExtension;

+ 2 - 8
src/WasmSampleExtension/WasmSampleExtension.csproj

@@ -2,21 +2,15 @@
 
   <PropertyGroup>
     <TargetFramework>net8.0</TargetFramework>
+    <RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
     <OutputType>Exe</OutputType>
-    <ImplicitUsings>enable</ImplicitUsings>
-    <Nullable>disable</Nullable>
     <PublishTrimmed>true</PublishTrimmed>
-    <MSBuildEnableWorkloadResolver>false</MSBuildEnableWorkloadResolver>
+    <WasmSingleFileBundle>true</WasmSingleFileBundle>
     <OutputPath>..\PixiEditor.AvaloniaUI.Desktop\bin\Debug\net8.0\Extensions</OutputPath>
   </PropertyGroup>
 
   <Import Project="..\PixiEditor.Extensions.Wasm\build\PixiEditor.Extensions.Wasm.targets"/>
   <Import Project="..\PixiEditor.Extensions.Wasm\build\PixiEditor.Extensions.Wasm.props"/>
-  
-  <ItemGroup>
-    <!--<PackageReference Include="Microsoft.DotNet.ILCompiler.LLVM; runtime.win-x64.Microsoft.DotNet.ILCompiler.LLVM" Version="9.0.0-*" />-->
-    <PackageReference Include="Wasi.Sdk" Version="0.1.4-preview.10020" />
-  </ItemGroup>
 
   <ItemGroup>
     <ProjectReference Include="..\PixiEditor.Extensions.Wasm\PixiEditor.Extensions.Wasm.csproj" />