| title | Examples |
|---|---|
| section | AngleSharp.Wasm |
This is a (growing) list of examples for every-day usage of AngleSharp.Wasm.
var config = Configuration.Default.WithWasm();
using var context = BrowsingContext.New(config);
using var document = await context.OpenNewAsync();var module = WebAssembly.Compile(context.Current!, wasmBytes);
var instance = WebAssembly.Instantiate(context.Current!, module);var result = instance.Invoke("answer");You can also invoke through the export wrapper:
var exported = (WasmJsExportedFunction?)instance.Exports["answer"];
var result = exported?.Invoke();var add = (WasmJsExportedFunction?)instance.Exports["add"];
var result = add?.Invoke(20, 22); // 42foreach (var exportEntry in module.Exports())
{
Console.WriteLine($"Export: {exportEntry.Name} ({exportEntry.Kind})");
}
foreach (var importEntry in module.Imports())
{
Console.WriteLine($"Import: {importEntry.Module}.{importEntry.Name} ({importEntry.Kind})");
}var sections = module.CustomSections("meta");
foreach (var payload in sections)
{
Console.WriteLine($"Section payload length: {payload.Length}");
}var config = Configuration.Default
.WithWasm()
.WithWasmImports(_ =>
{
return new[]
{
new WasmImportFunction(
"host",
"answer",
Array.Empty<WasmValueType>(),
new[] { WasmValueType.Int32 },
_ => 41),
};
});With a module importing host.answer, an export such as answer_plus_one can now be invoked as expected.
var maybe = instance.Exports["does_not_exist"];
if (maybe is null)
{
Console.WriteLine("Export not found.");
}var keys = instance.Exports.Keys();