Skip to content

Commit

Permalink
Added marshaller buffer support. Still not sure if marshallers are th…
Browse files Browse the repository at this point in the history
…e right approach.
  • Loading branch information
Earthmark committed Feb 4, 2024
1 parent d4962fb commit 1db6a37
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 37 deletions.
14 changes: 4 additions & 10 deletions dergwasm_mod/Dergwasm/Modules/ApiData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@ public class ApiFunc
{
public string Module { get; set; }
public string Name { get; set; }
public List<Parameter> Parameters { get; }
public List<Parameter> Returns { get; }
public List<Parameter> Parameters { get; } = new List<Parameter>();
public List<Parameter> Returns { get; } = new List<Parameter>();

[JsonIgnore]
public IEnumerable<ValueType> ParameterValueTypes => Parameters.Select(p => p.Type);
public IEnumerable<ValueType> ParameterValueTypes => Parameters.SelectMany(p => p.Types);

[JsonIgnore]
public IEnumerable<ValueType> ReturnValueTypes => Returns.Select(p => p.Type);

public ApiFunc()
{
Parameters = new List<Parameter>();
Returns = new List<Parameter>();
}
public IEnumerable<ValueType> ReturnValueTypes => Returns.SelectMany(p => p.Types);
}
}
4 changes: 1 addition & 3 deletions dergwasm_mod/Dergwasm/Modules/IWasmMarshaller.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Derg.Runtime;
using Dergwasm.Runtime;
using Elements.Core;
using System;
using System.Collections.Generic;

namespace Derg.Modules {
Expand All @@ -14,13 +13,12 @@ public interface IWasmMarshaller<T> {

public struct DirectMarshaller<T> : IWasmMarshaller<T>
{

public void AddParams(string name, List<Parameter> parameters)
{
parameters.Add(new Parameter
{
Name = name,
Type = ModuleReflector.ValueTypeFor(typeof(T)),
Types = ModuleReflector.ValueTypesFor(typeof(T)),
CSType = typeof(T).GetNiceName()
});
}
Expand Down
8 changes: 4 additions & 4 deletions dergwasm_mod/Dergwasm/Modules/ModuleReflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ private static readonly ConcurrentDictionary<
Func<object, (ApiFunc, HostFunc)[]>
> _reflectedFuncs = new ConcurrentDictionary<Type, Func<object, (ApiFunc, HostFunc)[]>>();

public static Runtime.ValueType ValueTypeFor(Type type)
public static Runtime.ValueType[] ValueTypesFor(Type type)
{
var valueType = (Runtime.ValueType)
var valueTypes = (Runtime.ValueType[])
typeof(Value)
.GetMethod(nameof(Value.ValueType))
.MakeGenericMethod(type)
.Invoke(null, null);
return valueType;
return valueTypes;
}

public static Type MarshallerFor(ParameterInfo info)
Expand All @@ -50,7 +50,7 @@ public static Type MarshallerFor(ParameterInfo info)
if (marshaller.IsGenericTypeDefinition)
{
// If the marshaller is a generic type, assume the marshaller has the same type arguments.
marshaller = marshaller.MakeGenericType(info.ParameterType.GetGenericTypeDefinition());
marshaller = marshaller.MakeGenericType(info.ParameterType.GenericTypeArguments);
}
return marshaller;
}
Expand Down
35 changes: 16 additions & 19 deletions dergwasm_mod/Dergwasm/Wasm/Buff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@ namespace Derg.Wasm
{
public struct BuffMarshaller : IWasmMarshaller<Buff>
{
public void AddParams(string name, List<Parameter> parameters) {
public void AddParams(string name, List<Parameter> parameters)
{
parameters.Add(new Parameter
{
Name = $"{name}_ptr",
Type = ModuleReflector.ValueTypeFor(typeof(int)),
CSType = typeof(int).GetNiceName()
}); parameters.Add(new Parameter
{
Name = $"{name}_len",
Type = ModuleReflector.ValueTypeFor(typeof(int)),
CSType = typeof(int).GetNiceName()
Name = name,
Types = new[] {
ValueType.I32,
ValueType.I32
},
CSType = typeof(Buff).GetNiceName()
});
}

public Buff From(Frame frame, Machine machine)
{
var addr = frame.Pop().s32;
var size = frame.Pop().s32;
var addr = frame.Pop().s32;
return new Buff(addr, size);
}

Expand Down Expand Up @@ -79,21 +78,19 @@ public void AddParams(string name, List<Parameter> parameters)
{
parameters.Add(new Parameter
{
Name = $"{name}_ptr",
Type = ModuleReflector.ValueTypeFor(typeof(int)),
CSType = typeof(int).GetNiceName()
}); parameters.Add(new Parameter
{
Name = $"{name}_len",
Type = ModuleReflector.ValueTypeFor(typeof(int)),
CSType = typeof(int).GetNiceName()
Name = name,
Types = new[] {
ValueType.I32,
ValueType.I32
},
CSType = typeof(Buff<T>).GetNiceName()
});
}

public Buff<T> From(Frame frame, Machine machine)
{
var addr = frame.Pop().s32;
var size = frame.Pop().s32;
var addr = frame.Pop().s32;
return new Buff<T>(addr, size);
}

Expand Down
2 changes: 1 addition & 1 deletion dergwasm_mod/DergwasmTests/Modules/ReflectedModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void BuffArgApiDataIsCorrect()
public void BuffArgPassedCorrectly()
{
var method = module.GetHostFunc("buff_arg");
frame.Push(new Buff<byte>(new Ptr<byte>(5), 10));
new BuffMarshaller<byte>().To(frame, machine, new Buff<byte>(new Ptr<byte>(5), 10));
frame.InvokeFunc(machine, method);
Assert.Equal(10, module.ReceivedBuffArg.Length);
Assert.Equal(5, module.ReceivedBuffArg.Ptr.Addr);
Expand Down

0 comments on commit 1db6a37

Please sign in to comment.