|
@@ -7,8 +7,8 @@ public static class MemoryUtility
|
|
{
|
|
{
|
|
public static string GetStringFromWasmMemory(int offset, int length, Memory memory)
|
|
public static string GetStringFromWasmMemory(int offset, int length, Memory memory)
|
|
{
|
|
{
|
|
|
|
+ //TODO: memory.ReadString is a thing
|
|
var span = memory.GetSpan<byte>(offset, length);
|
|
var span = memory.GetSpan<byte>(offset, length);
|
|
-
|
|
|
|
return Encoding.UTF8.GetString(span);
|
|
return Encoding.UTF8.GetString(span);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -17,4 +17,25 @@ public static class MemoryUtility
|
|
var span = memory.GetSpan<T>(bodyOffset, bodyLength);
|
|
var span = memory.GetSpan<T>(bodyOffset, bodyLength);
|
|
return span;
|
|
return span;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public static int WriteInt32(Instance instance, Memory memory, int value)
|
|
|
|
+ {
|
|
|
|
+ // TODO: cache malloc function
|
|
|
|
+ var malloc = instance.GetFunction<int, int>("malloc");
|
|
|
|
+
|
|
|
|
+ const int length = 4;
|
|
|
|
+ var ptr = malloc.Invoke(length);
|
|
|
|
+ memory.WriteInt32(ptr, value);
|
|
|
|
+ return ptr;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static int WriteString(Instance instance, Memory memory, string value)
|
|
|
|
+ {
|
|
|
|
+ var malloc = instance.GetFunction<int, int>("malloc");
|
|
|
|
+
|
|
|
|
+ var length = value.Length;
|
|
|
|
+ var ptr = malloc.Invoke(length);
|
|
|
|
+ memory.WriteString(ptr, value, Encoding.UTF8);
|
|
|
|
+ return ptr;
|
|
|
|
+ }
|
|
}
|
|
}
|