Loading Blazor and Ren-C Wasm in the Same Page

I was a bit impressed with how well Blazor was working 3 years ago for how new it was. They'd done a pretty good job of making the "DLLs of WebAssembly" (side modules) which I haven't yet gotten a grip on. And their Wasm files for the various modules were surprisingly small.

But it seems in the intervening years, Blazor has gained a fair bit of momentum. A friend of mine is using it and sings its praises. He's using this:

I don't like going off on random tangents when there's still lots of design issues to hammer out. But providing Rebol services to C# running in the browser could be aligned with the browser-focus mission.

While I used C# right about when it came out for a few things, I've forgotten just about all I knew. And even if I did remember it I imagine it's quite different now (and more different if you're using Blazor). So I don't really know what the story is on variadics.

But we live in the age of AI!

I asked ChatGPT if it could handle it, and it says yes... there are variadics and everything can be considered an object, including string literals:

using System;

class Program
{
    static void someFunction(params object[] args)
    {
        foreach (var arg in args)
        {
            Console.Write($"{arg} ");
        }
        Console.WriteLine();
    }

    static int makeInt(int i) => i;

    static void Main()
    {
        // prints "1 hello world 2"
        someFunction(1, "hello", "world", 2);

        // prints "hello 1"
        someFunction("hello", 1);

        // prints "1 hello world 2"
        someFunction(makeInt(1), "hello", "world", makeInt(2));
    }
}

C# apparently uses UTF-16 internally (losers!) but you can convert the literal strings to UTF-8:

using System;
using System.Text;

class Program
{
    static byte[] StringToUtf8(string str)
    {
        return Encoding.UTF8.GetBytes(str);
    }

    static void Main()
    {
        string myString = "hello";
        byte[] utf8Buffer = StringToUtf8(myString);

        // Pass utf8Buffer to WebAssembly function...
    }
}

Here's how you set up Blazor to call JavaScript (which will call the WebAssembly export).

@inject IJSRuntime JS

@code {
    private async Task CallWasmFunction()
    {
        string myString = "hello";
        byte[] utf8Buffer = Encoding.UTF8.GetBytes(myString);

        // Pass the buffer to the JS function
        await JS.InvokeVoidAsync("callWasmFunction", utf8Buffer);
    }
}

Seems Very Promising...

Given the advancements in CSCAPE and all the information about the API already being captured structurally (enough to spit out the C/C++ and JavaScript interface code), adding C# might not be difficult at all.

So maybe if I get bored or blocked I'll hack this up.

1 Like