Skip to content

Commit 5321ebb

Browse files
committed
Add web assembly using rustler + wasmtime
1 parent eeb736c commit 5321ebb

File tree

7 files changed

+1133
-8
lines changed

7 files changed

+1133
-8
lines changed

lib/components_guide/rustler/math.ex

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule ComponentsGuide.Rustler.Math do
1919

2020
def add(_, _), do: error()
2121
def reverse_string(_), do: error()
22+
def wasm_example(_, _), do: error()
2223

2324
defp error, do: :erlang.nif_error(:nif_not_loaded)
2425
end

lib/components_guide_web/controllers/cheatsheets_controller.ex

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ defmodule ComponentsGuideWeb.CheatsheetsController do
22
use ComponentsGuideWeb, :controller
33
require Logger
44

5+
@wasm_constant """
6+
(module
7+
(func (export "answer") (result i32)
8+
i32.const 42
9+
)
10+
)
11+
"""
12+
513
def index(conn, _params) do
6-
render(conn, "index.html", article: "intro")
14+
render(conn, "index.html", article: "intro", wasm_constant: @wasm_constant)
715
end
816

917
@articles ["rxjs"]

lib/components_guide_web/templates/cheatsheets/index.html.eex

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
<article>
44
<div class="text-white bg-gray-900">
5-
<div class="content max-w-4xl mx-auto py-8 text-xl" style="--link-decoration: underline;">
6-
<%= render(view_module(@conn), @article <> ".html", conn: @conn) %>
5+
<div class="prose md:prose-xl prose-invert max-w-4xl mx-auto py-16">
6+
<%= render(view_module(@conn), @article <> ".html", assigns) %>
77
</div>
88
</div>
99
</article>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Various cheatsheets will be added over time. If you have ideas for cheatsheets, please submit them.
22

3-
<p><%= 6 + 1 %></p>
4-
<p><%= ComponentsGuide.Rustler.Math.add(5, 9) %></p>
5-
<p><%= ComponentsGuide.Rustler.Math.reverse_string("hello") %></p>
3+
<p>From Elixir: <%= 6 + 1 %></p>
4+
<p>From Rust: <%= ComponentsGuide.Rustler.Math.add(5, 9) %></p>
5+
<p>From Rust: <%= ComponentsGuide.Rustler.Math.reverse_string("hello") %></p>
6+
<p>Fromst WebAssembly + Rust: <%= inspect(ComponentsGuide.Rustler.Math.wasm_example(@wasm_constant, "answer")) %></p>

0 commit comments

Comments
 (0)