bootsharp icon indicating copy to clipboard operation
bootsharp copied to clipboard

Marshal via arrays

Open elringus opened this issue 4 months ago • 0 comments

Instead of JSON, explore marshaling via arrays, similar to Embind (https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html#value-types), eg:

public record Record (string Str, int Int, bool Bool, Record? Other);

[JSExport]
private static void ReceiveRecord ([JSMarshalAs<JSType.Array<JSType.Any>>] object?[] raw)
{
    var record = Unmarshal(raw);
    Console.WriteLine($"Record({record.Str}, {record.Int}, {record.Bool}, 
        Record({record.Other?.Str}, {record.Other?.Int}, {record.Other?.Bool}))");

    static Record Unmarshal (object?[] raw) => new(
        (string)raw[0]!,
        (int)(double)raw[1]!,
        (bool)raw[2]!,
        raw[3] != null ? Unmarshal(raw[3..7]) : null
    );
}
exports.Program.ReceiveRecord(["foo", 1, true, "bar", 2, false, null]);

Should probably be postponed until #138 is solved; otherwise, we'll break interop with tasks of custom data types, that are currently serialized to JSON.

elringus avatar Feb 11 '24 17:02 elringus