Browse Source

Give strings a length

CPKreuz 1 year ago
parent
commit
d58df6b78c

+ 4 - 4
src/PixiEditor.Extensions.Wasm/native/api_interop.c

@@ -5,10 +5,10 @@
 MonoMethod* method_init;
 
 __attribute__((import_name("log_message")))
-void log_message(const char* message);
+void log_message(const char* message, int32_t messageLength);
 
 __attribute__((import_name("create_popup_window")))
-void create_popup_window(const char* title, const char* content);
+void create_popup_window(const char* title, int32_t titleLength, const char* content, int32_t contentLength);
 
 __attribute((export_name("initialize")))
 void initialize()
@@ -27,14 +27,14 @@ void initialize()
 void logger_log_message(MonoString* message)
 {
     char* message_utf8 = mono_wasm_string_get_utf8(message);
-    log_message(message_utf8);
+    log_message(message_utf8, strlen(message_utf8));
 }
 
 void logger_create_popup_window(MonoString* title, MonoString* content)
 {
     char* title_utf8 = mono_wasm_string_get_utf8(title);
     char* content_utf8 = mono_wasm_string_get_utf8(content);
-    create_popup_window(title_utf8, content_utf8);
+    create_popup_window(title_utf8, strlen(title_utf8), content_utf8, strlen(content_utf8));
 }
 
 void attach_internal_calls()

+ 4 - 11
src/PixiEditor.Extensions.WasmRuntime/MemoryUtility.cs

@@ -5,17 +5,10 @@ namespace PixiEditor.Extensions.WasmRuntime;
 
 public static class MemoryUtility
 {
-    public static string GetStringFromWasmMemory(int offset, Memory memory)
+    public static string GetStringFromWasmMemory(int offset, int length, Memory memory)
     {
-        var span = memory.GetSpan<byte>(offset);
-        int length = 0;
-        while (span[length] != 0)
-        {
-            length++;
-        }
-
-        var buffer = new byte[length];
-        span[..length].CopyTo(buffer);
-        return Encoding.UTF8.GetString(buffer);
+        var span = memory.GetSpan<byte>(offset, length);
+        
+        return Encoding.UTF8.GetString(span);
     }
 }

+ 5 - 5
src/PixiEditor.Extensions.WasmRuntime/PixiEditorApiLinkerExtensions.cs

@@ -6,16 +6,16 @@ public static class PixiEditorApiLinkerExtensions
 {
     public static void DefinePixiEditorApi(this Linker linker, WasmExtensionInstance instance)
     {
-        linker.DefineFunction("env", "log_message",(int messageOffset) =>
+        linker.DefineFunction("env", "log_message",(int messageOffset, int messageLength) =>
         {
-            string messageString = MemoryUtility.GetStringFromWasmMemory(messageOffset, instance.Instance.GetMemory("memory"));
+            string messageString = MemoryUtility.GetStringFromWasmMemory(messageOffset, messageLength, instance.Instance.GetMemory("memory"));
             Console.WriteLine(messageString.ReplaceLineEndings());
         });
 
-        linker.DefineFunction("env", "create_popup_window",(int titleOffset, int bodyOffset) =>
+        linker.DefineFunction("env", "create_popup_window",(int titleOffset, int titleLength, int bodyOffset, int bodyLength) =>
         {
-            string title = MemoryUtility.GetStringFromWasmMemory(titleOffset, instance.Instance.GetMemory("memory"));
-            string body = MemoryUtility.GetStringFromWasmMemory(bodyOffset, instance.Instance.GetMemory("memory"));
+            string title = MemoryUtility.GetStringFromWasmMemory(titleOffset, titleLength, instance.Instance.GetMemory("memory"));
+            string body = MemoryUtility.GetStringFromWasmMemory(bodyOffset, bodyLength, instance.Instance.GetMemory("memory"));
             instance.Api.WindowProvider.CreatePopupWindow(title, body).ShowDialog();
         });
     }