Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions PdVm.Compiler/PdVmDotNetSourceCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ private static IEnumerable<Binding> ScanMetadataBindings(Type type)
property.Name,
[property.PropertyType],
[new Parameter("handle", "int"), new Parameter("value", setSchema)],
"null"));
"()"));
}
}

Expand Down Expand Up @@ -536,7 +536,7 @@ private static bool TryBuildParameters(

private static bool TryGetSchema(Type type, out string schema)
{
if (type == typeof(void)) { schema = "null"; return true; }
if (type == typeof(void)) { schema = "()"; return true; }
if (type == typeof(string) || type == typeof(char) || type.IsEnum) { schema = "string"; return true; }
if (type == typeof(bool)) { schema = "bool"; return true; }
if (type == typeof(byte[])) { schema = "bytes"; return true; }
Expand Down
6 changes: 6 additions & 0 deletions PdVm.Compiler/PdVmModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public enum PdVmTypeSchemaKind : byte
Object = 14,
Callable = 15,
Optional = 16,
Result = 18,
Option = 19,
Unit = 20,
}

public sealed record PdVmTypeSchema
Expand Down Expand Up @@ -198,6 +201,9 @@ public enum PdVmHostTypeSchemaKind : byte
Callable = 11,
Resource = 12,
Named = 13,
Result = 14,
Option = 15,
Unit = 16,
}

public sealed record PdVmHostImportParam(
Expand Down
18 changes: 17 additions & 1 deletion PdVm.Compiler/PdVmVmbcReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class PdVmVmbcReader
private const int MaximumConstantDepth = 64;
private static readonly byte[] Magic = "VMBC"u8.ToArray();

private const ushort Version = 13;
private const ushort Version = 14;
private const ushort Flags = 0;

public static PdVmProgramModel ReadFile(string path)
Expand Down Expand Up @@ -84,6 +84,10 @@ public static PdVmProgramModel ReadBytes(byte[] bytes)
SkipDebugInfo(ref cursor);
var callableMetadata = ReadCallableMetadata(ref cursor);
var namedStructDecls = ReadNamedStructDecls(ref cursor);
if (cursor.ReadUInt32() != 0)
{
throw new PdVmCompilerException("non-empty return cleanup metadata is not supported");
}

if (!cursor.IsEof)
{
Expand Down Expand Up @@ -462,6 +466,11 @@ private static PdVmTypeSchema ReadSchema(ref Cursor cursor)
items: ReadSchemaList(ref cursor),
result: ReadSchema(ref cursor)),
16 => new PdVmTypeSchema(PdVmTypeSchemaKind.Optional, element: ReadSchema(ref cursor)),
18 => new PdVmTypeSchema(
PdVmTypeSchemaKind.Result,
items: [ReadSchema(ref cursor), ReadSchema(ref cursor)]),
19 => new PdVmTypeSchema(PdVmTypeSchemaKind.Option, element: ReadSchema(ref cursor)),
20 => new PdVmTypeSchema(PdVmTypeSchemaKind.Unit),
_ => throw new PdVmCompilerException($"invalid schema tag {kind} in VMBC payload"),
};
}
Expand Down Expand Up @@ -697,6 +706,13 @@ private static PdVmHostTypeSchema ReadHostTypeSchema(ref Cursor cursor, int dept
11 => ReadHostCallableSchema(ref cursor, depth),
12 => new PdVmHostTypeSchema(PdVmHostTypeSchemaKind.Resource, name: cursor.ReadString()),
13 => ReadHostNamedSchema(ref cursor, depth),
14 => new PdVmHostTypeSchema(
PdVmHostTypeSchemaKind.Result,
items: [ReadHostTypeSchema(ref cursor, depth + 1), ReadHostTypeSchema(ref cursor, depth + 1)]),
15 => new PdVmHostTypeSchema(
PdVmHostTypeSchemaKind.Option,
element: ReadHostTypeSchema(ref cursor, depth + 1)),
16 => new PdVmHostTypeSchema(PdVmHostTypeSchemaKind.Unit),
var tag => throw new PdVmCompilerException($"invalid host schema tag {tag}"),
};
}
Expand Down
2 changes: 1 addition & 1 deletion PdVm.Runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private static async Task<int> RunProgramAssemblyAsync(
{
var callableProgram = program as IPdVmCallableProgram ??
throw new InvalidOperationException(
"the Windows Forms profile requires a callable VMBC v13 program");
"the Windows Forms profile requires a callable VMBC v14 program");
callableProgram.CallbackErrorObserver = exception => Console.Error.WriteLine(exception);
using var application = PdVmWinFormsApplication.Attach(callableProgram, host);
var winFormsResult = PdVmExecution.Run(program, host, maxSteps);
Expand Down
29 changes: 27 additions & 2 deletions PdVm.Runtime/PdVmBuiltins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public enum PdVmBuiltin
MapIterClose,
BindCallable,
DetachLocal,
MapEdit,
}

public static class PdVmBuiltins
Expand Down Expand Up @@ -139,6 +140,24 @@ public static PdVmValue ArrayPushValue(PdVmValue array, PdVmValue value) =>
public static PdVmValue SetValue(PdVmValue container, PdVmValue key, PdVmValue value) =>
DispatchSet(new[] { container, key, value });

public static PdVmValue MapEditValue(PdVmValue container, PdVmValue key, PdVmValue value, PdVmValue remove)
{
if (key.Kind == PdVmValueKind.Callable)
{
throw new InvalidOperationException("callable values are not supported as map keys");
}
var output = container.AsMap().CloneMap();
if (remove.AsBool())
{
output.Remove(key);
}
else
{
output.Set(key, value);
}
return PdVmValue.FromMap(output);
}

public static PdVmValue VacateMovedField(PdVmValue container, PdVmValue key, PdVmValue movedValue)
{
// ABI 25 lowers MoveField to Get + set-null. Copyable scalars must stay
Expand Down Expand Up @@ -330,6 +349,7 @@ public static ushort GetCallIndex(PdVmBuiltin builtin)
PdVmBuiltin.MapIterClose => (ushort)(BuiltinCallBase - 13),
PdVmBuiltin.BindCallable => (ushort)(BuiltinCallBase - 14),
PdVmBuiltin.DetachLocal => (ushort)(BuiltinCallBase - 15),
PdVmBuiltin.MapEdit => 0xFF8F,
_ when builtin <= PdVmBuiltin.Count => (ushort)(BuiltinCallBase + (ushort)builtin),
_ => throw new ArgumentOutOfRangeException(nameof(builtin)),
};
Expand Down Expand Up @@ -443,13 +463,14 @@ or PdVmBuiltin.JitSetConfig
or PdVmBuiltin.MathClamp
or PdVmBuiltin.MathMulAdd
or PdVmBuiltin.StringReplaceLiteral => 3,
PdVmBuiltin.MapEdit => 4,
_ => throw new ArgumentOutOfRangeException(nameof(builtin)),
};
}

public static bool IsBuiltinIndex(ushort callIndex)
{
var specialStart = BuiltinCallBase - 15;
var specialStart = 0xFF8C;
var mainEnd = BuiltinCallBase + BuiltinCallCount - 1;
return callIndex >= specialStart && callIndex <= mainEnd;
}
Expand All @@ -458,6 +479,9 @@ public static bool TryGetBuiltin(ushort callIndex, out PdVmBuiltin builtin)
{
switch (callIndex)
{
case 0xFF8F:
builtin = PdVmBuiltin.MapEdit;
return true;
case var index when index == BuiltinCallBase - 4:
builtin = PdVmBuiltin.FormatTemplate;
return true;
Expand Down Expand Up @@ -536,6 +560,7 @@ public static PdVmCallOutcome Dispatch(ushort callIndex, IReadOnlyList<PdVmValue
PdVmBuiltin.Get => ReturnOne(GetValue(GetArg(args, 0), GetArg(args, 1))),
PdVmBuiltin.Has => ReturnOne(HasValue(GetArg(args, 0), GetArg(args, 1))),
PdVmBuiltin.Set => ReturnOne(SetValue(GetArg(args, 0), GetArg(args, 1), GetArg(args, 2))),
PdVmBuiltin.MapEdit => ReturnOne(MapEditValue(GetArg(args, 0), GetArg(args, 1), GetArg(args, 2), GetArg(args, 3))),
PdVmBuiltin.Keys => ReturnOne(KeysValue(GetArg(args, 0))),
PdVmBuiltin.BytesFromUtf8 => ReturnOne(BytesFromUtf8Value(GetArg(args, 0))),
PdVmBuiltin.BytesToUtf8 => ReturnOne(BytesToUtf8Value(GetArg(args, 0))),
Expand Down Expand Up @@ -582,7 +607,7 @@ or PdVmBuiltin.MapIterTakeValue
or PdVmBuiltin.MapIterClose
or PdVmBuiltin.BindCallable
or PdVmBuiltin.DetachLocal => throw new NotSupportedException(
$"builtin {builtin} requires the VMBC v13 callable runtime"),
$"builtin {builtin} requires the VMBC v14 callable runtime"),
PdVmBuiltin.JitSetConfig
or PdVmBuiltin.JitGetConfig
or PdVmBuiltin.JitSetEnabled
Expand Down
3 changes: 3 additions & 0 deletions PdVm.Runtime/PdVmCallableRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public enum PdVmRuntimeTypeSchemaKind : byte
Object = 14,
Callable = 15,
Optional = 16,
Result = 18,
Option = 19,
Unit = 20,
}

public sealed record PdVmRuntimeTypeSchema(
Expand Down
4 changes: 2 additions & 2 deletions PdVm.Runtime/PdVmProgramBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ PdVmRuntimeTypeSchemaKind.GenericParameter or

return schema.Kind switch
{
PdVmRuntimeTypeSchemaKind.Null => declaredType == PdVmValueType.Null,
PdVmRuntimeTypeSchemaKind.Null or PdVmRuntimeTypeSchemaKind.Unit => declaredType == PdVmValueType.Null,
PdVmRuntimeTypeSchemaKind.Int => declaredType == PdVmValueType.Int,
PdVmRuntimeTypeSchemaKind.Float => declaredType == PdVmValueType.Float,
PdVmRuntimeTypeSchemaKind.Number => declaredType is PdVmValueType.Int or PdVmValueType.Float,
Expand All @@ -1279,7 +1279,7 @@ internal static bool MatchesSchema(PdVmValue value, PdVmRuntimeTypeSchema schema
return schema.Kind switch
{
PdVmRuntimeTypeSchemaKind.Unknown or PdVmRuntimeTypeSchemaKind.GenericParameter => true,
PdVmRuntimeTypeSchemaKind.Null => value.Kind == PdVmValueKind.Null,
PdVmRuntimeTypeSchemaKind.Null or PdVmRuntimeTypeSchemaKind.Unit => value.Kind == PdVmValueKind.Null,
PdVmRuntimeTypeSchemaKind.Int => value.Kind == PdVmValueKind.Int,
PdVmRuntimeTypeSchemaKind.Float => value.Kind == PdVmValueKind.Float,
PdVmRuntimeTypeSchemaKind.Number => value.Kind is PdVmValueKind.Int or PdVmValueKind.Float,
Expand Down
28 changes: 14 additions & 14 deletions PdVm.Runtime/PdVmWinFormsEventLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static void Show(object form) =>
public static void BindClick(
object form,
object control,
[PdVmInteropSchema("fn() -> null")] PdVmCallableValue callable)
[PdVmInteropSchema("fn() -> ()")] PdVmCallableValue callable)
{
var session = PdVmWinFormsApplication.RequireCurrent();
var callback = session.CreateUnitCallback(form, callable);
Expand All @@ -108,19 +108,19 @@ public static void BindDialog(
object form,
object control,
object dialog,
[PdVmInteropSchema("fn() -> null")] PdVmCallableValue callable) =>
[PdVmInteropSchema("fn() -> ()")] PdVmCallableValue callable) =>
BindClick(form, control, callable);

public static void BindAction(
object form,
object control,
[PdVmInteropSchema("fn(map<string>) -> null")] PdVmCallableValue callable,
[PdVmInteropSchema("fn(map<string>) -> ()")] PdVmCallableValue callable,
string action) =>
BindEventAction(form, control, "Click", callable, action);

public static void BindShown(
object form,
[PdVmInteropSchema("fn() -> null")] PdVmCallableValue callable)
[PdVmInteropSchema("fn() -> ()")] PdVmCallableValue callable)
{
var session = PdVmWinFormsApplication.RequireCurrent();
var callback = session.CreateUnitCallback(form, callable);
Expand All @@ -132,7 +132,7 @@ public static void BindShown(

public static void BindShownAction(
object form,
[PdVmInteropSchema("fn(map<string>) -> null")] PdVmCallableValue callable,
[PdVmInteropSchema("fn(map<string>) -> ()")] PdVmCallableValue callable,
string action) =>
BindEventAction(form, form, "Shown", callable, action);

Expand All @@ -156,31 +156,31 @@ private static void BindEventAction(
public static void BindMouseDown(
object form,
object control,
[PdVmInteropSchema("fn(map<string>) -> null")] PdVmCallableValue callable) =>
[PdVmInteropSchema("fn(map<string>) -> ()")] PdVmCallableValue callable) =>
BindPointerEvent(form, control, "MouseDown", callable, hasCoordinates: true, action: string.Empty);

public static void BindMouseUp(
object form,
object control,
[PdVmInteropSchema("fn(map<string>) -> null")] PdVmCallableValue callable) =>
[PdVmInteropSchema("fn(map<string>) -> ()")] PdVmCallableValue callable) =>
BindPointerEvent(form, control, "MouseUp", callable, hasCoordinates: true, action: string.Empty);

public static void BindMouseDoubleClick(
object form,
object control,
[PdVmInteropSchema("fn(map<string>) -> null")] PdVmCallableValue callable) =>
[PdVmInteropSchema("fn(map<string>) -> ()")] PdVmCallableValue callable) =>
BindPointerEvent(form, control, "MouseDoubleClick", callable, hasCoordinates: true, action: string.Empty);

public static void BindMouseLeave(
object form,
object control,
[PdVmInteropSchema("fn(map<string>) -> null")] PdVmCallableValue callable) =>
[PdVmInteropSchema("fn(map<string>) -> ()")] PdVmCallableValue callable) =>
BindPointerEvent(form, control, "MouseLeave", callable, hasCoordinates: false, action: string.Empty);

public static void BindPointer(
object form,
object control,
[PdVmInteropSchema("fn(map<string>) -> null")] PdVmCallableValue callable)
[PdVmInteropSchema("fn(map<string>) -> ()")] PdVmCallableValue callable)
{
BindPointerEvent(form, control, "MouseDown", callable, hasCoordinates: true, action: "board_down");
BindPointerEvent(form, control, "MouseUp", callable, hasCoordinates: true, action: "board_up");
Expand All @@ -190,7 +190,7 @@ public static void BindPointer(

public static void BindClosing(
object form,
[PdVmInteropSchema("fn() -> null")] PdVmCallableValue callable)
[PdVmInteropSchema("fn() -> ()")] PdVmCallableValue callable)
{
var session = PdVmWinFormsApplication.RequireCurrent();
var state = GetState(form);
Expand All @@ -207,7 +207,7 @@ public static void BindClosing(

public static void BindClosingAction(
object form,
[PdVmInteropSchema("fn(map<string>) -> null")] PdVmCallableValue callable,
[PdVmInteropSchema("fn(map<string>) -> ()")] PdVmCallableValue callable,
string action)
{
ArgumentNullException.ThrowIfNull(action);
Expand All @@ -227,15 +227,15 @@ public static void BindClosingAction(
public static void BindTimer(
object form,
long intervalMilliseconds,
[PdVmInteropSchema("fn() -> null")] PdVmCallableValue callable)
[PdVmInteropSchema("fn() -> ()")] PdVmCallableValue callable)
{
BindTimerCore(form, intervalMilliseconds, callable, action: null);
}

public static void BindTimerAction(
object form,
long intervalMilliseconds,
[PdVmInteropSchema("fn(map<string>) -> null")] PdVmCallableValue callable,
[PdVmInteropSchema("fn(map<string>) -> ()")] PdVmCallableValue callable,
string action)
{
ArgumentNullException.ThrowIfNull(action);
Expand Down
2 changes: 1 addition & 1 deletion PdVm.Tests/PdVmAbi25ExampleMatrixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void CheckedInExamplesHaveExactCountAndCompileThroughNativeCompilerAndClr

var bytes = PdVmNativeCompiler.CompileFile(path);
Assert.True(bytes.AsSpan(0, 4).SequenceEqual("VMBC"u8));
Assert.Equal((ushort)13, BitConverter.ToUInt16(bytes, 4));
Assert.Equal((ushort)14, BitConverter.ToUInt16(bytes, 4));
var model = PdVmVmbcReader.ReadBytes(bytes);
Assert.NotEmpty(model.Code);
if (source.Contains("http::", StringComparison.Ordinal) ||
Expand Down
Loading
Loading