Finish InterpreterEntry deserialization

This commit is contained in:
Yoshi Askharoun
2023-05-25 21:01:48 -05:00
parent 1c5fb20fc9
commit ea08399992
4 changed files with 64 additions and 37 deletions
+1 -1
View File
@@ -38,6 +38,6 @@ internal class Program
private static void Debugger_InterpreterStep(object? sender, InterpreterEntry e) private static void Debugger_InterpreterStep(object? sender, InterpreterEntry e)
{ {
Console.WriteLine($"[Interpreter] {e}");
} }
} }
+50 -21
View File
@@ -2,9 +2,7 @@
using Newtonsoft.Json.Bson; using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
namespace Microsoft.Iris.Debug; namespace Microsoft.Iris.Debug;
@@ -40,29 +38,44 @@ internal class BsonFormatter : IFormatter
public object Deserialize(Stream serializationStream) public object Deserialize(Stream serializationStream)
{ {
using JsonTextReader reader = new(new StreamReader(serializationStream)) using BsonDataReader reader = new(serializationStream)
{ {
CloseInput = false CloseInput = false
}; };
var package = (JArray)_serializer.Deserialize(reader); BsonPackage package = new(_serializer, reader);
return package.Object ?? package.SerializedObject;
// If the type is accessible from the current domain,
// create an instance of it.
var typeName = package[0].Value<string>();
var type = Type.GetType(typeName);
if (type != null)
return package[1].ToObject(type, _serializer);
return package[1];
} }
public void Serialize(Stream serializationStream, object graph) public void Serialize(Stream serializationStream, object graph)
{ {
using JsonTextWriter writer = new(new StreamWriter(serializationStream)); using BsonDataWriter writer = new(serializationStream);
_serializer.Serialize(writer, new[] { graph?.GetType().FullName, graph }); _serializer.Serialize(writer, new[] { graph?.GetType().FullName, graph });
writer.Flush(); writer.Flush();
} }
private class BsonPackage
{
public BsonPackage(JsonSerializer serializer, JsonReader reader)
{
var package = (JObject)serializer.Deserialize(reader);
TypeName = package["0"].Value<string>();
SerializedObject = (JObject)package["1"];
// If the type is accessible from the current domain,
// create an instance of it.
var type = Type.GetType(TypeName);
if (type != null)
Object = SerializedObject.ToObject(type, serializer);
}
public string TypeName { get; set; }
public JObject SerializedObject { get; set; }
public object Object { get; set; }
}
} }
internal class BsonFallbackConverter : JsonConverter internal class BsonFallbackConverter : JsonConverter
@@ -70,20 +83,36 @@ internal class BsonFallbackConverter : JsonConverter
public override bool CanConvert(Type objectType) public override bool CanConvert(Type objectType)
{ {
return !objectType.Attributes.HasFlag(System.Reflection.TypeAttributes.Serializable) return !objectType.Attributes.HasFlag(System.Reflection.TypeAttributes.Serializable)
|| objectType == typeof(uint) || objectType == typeof(ulong); || objectType.IsEnum || objectType == typeof(uint) || objectType == typeof(ulong)
|| objectType == typeof(Type);
} }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) public override object ReadJson(JsonReader reader, Type objectType, object jsonValue, JsonSerializer serializer)
{
if (existingValue != null)
{ {
jsonValue = reader.Value;
if (objectType == reader.ValueType)
return jsonValue;
if (objectType == typeof(uint)) if (objectType == typeof(uint))
return Convert.ToUInt32(existingValue); return BitConverter.ToUInt32((byte[])jsonValue, 0);
else if (objectType == typeof(ulong)) else if (objectType == typeof(ulong))
return Convert.ToUInt64(existingValue); return BitConverter.ToUInt64((byte[])jsonValue, 0);
else if (objectType.IsEnum)
{
if (reader.ValueType == typeof(string))
return Enum.Parse(objectType, (string)reader.Value);
return Enum.ToObject(objectType, reader.Value);
}
else if (objectType == typeof(Type))
{
if (jsonValue is not string typeName)
return null;
return Type.GetType(typeName);
} }
return existingValue?.ToString(); return jsonValue?.ToString();
} }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
@@ -8,27 +8,24 @@ namespace Microsoft.Iris.Debug.Data;
[Serializable] [Serializable]
public class InterpreterEntry public class InterpreterEntry
{ {
public InterpreterEntry(OpCode opCode, uint offset, string loadUri, params InterpreterObject[] args) public InterpreterEntry() { }
public InterpreterEntry(OpCode opCode, uint offset, string loadUri)
{ {
OpCode = opCode; OpCode = opCode;
Offset = offset; Offset = offset;
LoadUri = loadUri; LoadUri = loadUri;
if (args != null && args.Length > 0)
Parameters = args;
else
Parameters = new List<InterpreterObject>();
} }
public OpCode OpCode { get; } public OpCode OpCode { get; set; }
public uint Offset { get; } public uint Offset { get; set; }
public string LoadUri { get; } public string LoadUri { get; set; }
public IList<InterpreterObject> Parameters { get; } public List<InterpreterObject> Parameters { get; } = new();
public IList<InterpreterObject> ReturnValues { get; } = new List<InterpreterObject>(); public List<InterpreterObject> ReturnValues { get; } = new();
public override string ToString() public override string ToString()
{ {
@@ -1,5 +1,4 @@
using System; using System;
using System.Runtime.Serialization;
namespace Microsoft.Iris.Debug.Data; namespace Microsoft.Iris.Debug.Data;
@@ -25,11 +24,13 @@ public class InterpreterObject
TableIndex = tableIndex; TableIndex = tableIndex;
} }
public InterpreterObject(object value) : this(null, value?.GetType() ?? typeof(object), value, InstructionObjectSource.Dynamic) public InterpreterObject(object value) : this(null, value?.GetType(), value, InstructionObjectSource.Dynamic)
{ {
} }
public override string ToString() => $"{Type} {Value}"; public InterpreterObject() { }
public override string ToString() => string.Join(" ", Type?.Name, Value?.ToString() ?? "NULL");
} }
/// <summary> /// <summary>