ITエンジニア/デザイナ向けにオープンソースを毎日紹介

WebAssemblyを記述できるのはC/C++/Rustだけだと思っていないでしょうか。実はLLVMにさえなれば、他の言語でも記述できます。そのため、やろうと思えばSwiftから作ることもできるそうです。

そこで注目したいのがmono-wasmです。Monoを使ってC#で記述された内容を WebAssembly にするソフトウェアです。

mono-wasmの使い方

ごく簡単なコードです。コンソールを使うことでデバッグメッセージも流せます。

  1. class Hello {
  2. static int Main(string[] args) {
  3. System.Console.WriteLine("hello world!");
  4. return 0;
  5. }
  6. }

実行結果です。

Main関数が必ず実行されるようになっていますが、他の関数を呼ばせることもできます。以下はクラスにメソッドが多数定義されている例です。

  1. using Mono.WebAssembly;
  2. using System;
  3. class Hello
  4. {
  5. static int Factorial(int n)
  6. {
  7. if (n == 0) {
  8. return 1;
  9. }
  10. return n * Factorial(n - 1);
  11. }
  12. // This function is called from the browser by JavaScript.
  13. // Here we calculate the factorial of the given number then use the
  14. // Mono.WebAssembly API to retrieve the element from the DOM and set its
  15. // innerText property to the factorial result.
  16. static void FactorialInElement(int n, string element_id)
  17. {
  18. Console.WriteLine(
  19. "Calculating factorial of {0} into DOM element {1}",
  20. n, element_id);
  21. int f = Factorial(n);
  22. var elem = HtmlPage.Document.GetElementById(element_id);
  23. elem.InnerText = f.ToString();
  24. }
  25. static void PrintHtmlElements(HtmlElement elem, int level)
  26. {
  27. string str = "";
  28. for (int i = 0; i < level; i++) {
  29. str += " ";
  30. }
  31. str += $"<{elem.TagName}";
  32. foreach (var name in elem.AttributeNames) {
  33. var value = elem.GetAttribute(name);
  34. str += $" {name}='{value}'";
  35. }
  36. str += ">";
  37. Console.WriteLine(str);
  38. var list = elem.Children;
  39. for (int i = 0; i < list.Count; i++) {
  40. var child = list[i];
  41. PrintHtmlElements(child, level + 1);
  42. }
  43. }
  44. static int Main(string[] args)
  45. {
  46. int f = Factorial(6);
  47. HtmlPage.Window.Alert($"Hello world! factorial(6) -> {f}");
  48. var bi = HtmlPage.BrowserInformation;
  49. Console.WriteLine($"BrowserInformation: Name {bi.Name} BrowserVersion {bi.BrowserVersion} UserAgent {bi.UserAgent} Platform {bi.Platform} CookiesEnabled {bi.CookiesEnabled} ProductName {bi.ProductName}");
  50. var d = HtmlPage.Document;
  51. Console.WriteLine($"Document Location: {d.Location}");
  52. PrintHtmlElements(d.DocumentElement, 0);
  53. var p = d.CreateElement("p");
  54. p.InnerText = "This text was added at runtime.";
  55. d.Body.AppendChild(p);
  56. if (args.Length > 0) FactorialInElement(0, ""); // this is a hack so that the linker does not remove the FactorialInElement() method
  57. return f;
  58. }
  59. }

実行結果です。フラクタルが計算されています。DOMにメッセージを出力することもできます。

mono-wasmは単体の WebAssembly を出力しますが、同時にDLLと実行ファイルも読み込んで動作する仕組みです。そのためサイズは若干大きくなったり、読み込みに時間がかかりますが、将来的には改善予定です。C#で WebAssembly を記述する上で有望なソフトウェアです。

mono-wasmはC++製のオープンソース・ソフトウェア(MIT License)です。

lrz/mono-wasm

MOONGIFTプレミアムに登録して運営をサポートしてください!月額500円の他、半年(3,000円)、年間パック(6,000円)もあります。企業向けに3アカウント以上で請求書払いも可能です(年間一括のみ)。従業員の方向けのサービスにいかがですか? プレミアムユーザのログインはこちらから

 

MOONGIFTの関連記事

コメント

  • DevRel
  • Com2