Skip to content

Commit 1adf5de

Browse files
committed
Make work again
1 parent 054cdbe commit 1adf5de

File tree

13 files changed

+75
-14
lines changed

13 files changed

+75
-14
lines changed

lib/components_guide/wasm/examples/calendar/calendar_grid.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ defmodule ComponentsGuide.Wasm.Examples.Calendar.Grid do
22
# Displays an HTML calendar grid for a given year and month.
33
# Uses Elixir’s Date, Date.Range, Calendar.ISO to grab all the data for each year.
44

5-
for year <- 1970..2100 do
6-
7-
end
5+
# for year <- 1970..2100 do
6+
# year_days = if Date.leap_year?(year), do: 366, else: 365
7+
# for year_day <- 1..year_days do
88

9+
# end
10+
# end
911

1012
# testw "leap_year?" do
1113
# assert call(:leap_year?, 2000) = Calendar.ISO.leap_year?(2000)

lib/components_guide/wasm/examples/color_picker/lab_swatch.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ defmodule ComponentsGuide.Wasm.Examples.LabSwatch do
4848
# @last_changed_component = @component_l
4949
# end
5050

51-
wasm F32 do
51+
Orb.__append_body F32 do
5252
ColorConversion.funcp()
5353

5454
func l_changed(new_value: F32) do

lib/components_guide/wasm/examples/examples.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule ComponentsGuide.Wasm.Examples do
2727

2828
# Check equality to each weekday as a i32 e.g. `Mon\0`
2929
inline for {day_i32!, index!} <- Enum.with_index(@weekdays_i32, 1) do
30-
Orb.__append_body do
30+
wasm do
3131
if I32.eq(i, day_i32!), do: return(index!)
3232
end
3333
end
@@ -83,7 +83,7 @@ defmodule ComponentsGuide.Wasm.Examples do
8383
@chunk2 = inline(do: @strings.charset_utf8.offset)
8484
end
8585

86-
func Orb.__append_body() do
86+
func wasm do
8787
@chunk1 = inline(do: @strings.application_wasm.offset)
8888
@chunk2 = 0x0
8989
end

lib/components_guide/wasm/examples/examples_format.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ defmodule ComponentsGuide.Wasm.Examples.Format do
7070

7171
Memory.pages(2)
7272

73-
wasm F32 do
73+
Orb.__append_body F32 do
7474
func format_f32(value: F32, str_ptr: I32.U8.UnsafePointer, precision: I32),
7575
digit: I32 do
7676
# TODO: handle -0

lib/components_guide/wasm/examples/examples_html.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule ComponentsGuide.Wasm.Examples.HTML do
4242
quote do
4343
import unquote(__MODULE__)
4444

45-
Orb.Orb.__append_body do
45+
Orb.__append_body do
4646
unquote(__MODULE__).funcp()
4747
end
4848
end
@@ -78,10 +78,10 @@ defmodule ComponentsGuide.Wasm.Examples.HTML do
7878
read_offset = read_offset + 1
7979

8080
inline for {char_to_match!, chars_out!} <- @escaped_html_table do
81-
Orb.__append_body do
81+
wasm do
8282
if I32.eq(char, char_to_match!) do
8383
inline for char_out! <- chars_out! do
84-
Orb.__append_body do
84+
wasm do
8585
write_offset[at!: bytes_written] = char_out!
8686
bytes_written = bytes_written + 1
8787
end

lib/components_guide/wasm/examples/string_builder.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule ComponentsGuide.Wasm.Examples.StringBuilder do
2727

2828
I32.global(bump_write_level: 0)
2929

30-
Orb.Orb.__append_body do
30+
Orb.__append_body do
3131
unquote(__MODULE__).funcp()
3232
end
3333
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule ComponentsGuide.Wasm.Examples.TemperatureConverter do
2+
use Orb
3+
4+
global do
5+
6+
end
7+
end

lib/components_guide/wasm/examples/url_encoded.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule ComponentsGuide.Wasm.Examples.URLEncoded do
2121

2222
import unquote(__MODULE__)
2323

24-
Orb.Orb.__append_body do
24+
Orb.__append_body do
2525
unquote(__MODULE__).funcp()
2626
end
2727
end

lib/components_guide_web/controllers/wasm_controller.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ defmodule ComponentsGuideWeb.WasmController do
6464
render(conn, :index, assigns)
6565
end
6666

67+
@articles ["custom_elements"]
68+
69+
def show(conn, %{"id" => article}) when article in @articles do
70+
render(conn, article <> ".html")
71+
end
72+
6773
def module(conn, %{"module" => name}) when is_map_key(@modules, name) do
6874
mod = @modules[name]
6975

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# WebAssembly Custom Elements
2+
3+
## MemoryIO
4+
5+
```js
6+
const utf8Encoder = new TextEncoder();
7+
const utf8Decoder = new TextDecoder();
8+
9+
export class MemoryIO {
10+
constructor(exports) {
11+
this.memoryBytes = new Uint8Array(exports.memory.buffer);
12+
this.alloc = exports.alloc;
13+
}
14+
15+
readString(ptr) {
16+
const { memoryBytes } = this;
17+
18+
// Search for null-terminating byte.
19+
const endPtr = memoryBytes.indexOf(0, ptr);
20+
// Get subsection of memory between start and end, and decode it as UTF-8.
21+
return utf8Decoder.decode(memoryBytes.subarray(ptr, endPtr));
22+
}
23+
24+
writeStringAt(stringValue, memoryOffset) {
25+
const { memoryBytes } = this;
26+
27+
stringValue = stringValue.toString();
28+
const bytes = utf8Encoder.encode(stringValue);
29+
utf8Encoder.encodeInto(stringValue, memoryBytes.subarray(memoryOffset));
30+
memoryBytes[memoryOffset + bytes.length] = 0x0;
31+
return bytes.byteLength;
32+
}
33+
34+
writeString(stringValue) {
35+
const { memoryBytes, alloc } = this;
36+
37+
stringValue = stringValue.toString();
38+
const bytes = utf8Encoder.encode(stringValue);
39+
const strPtr = alloc(bytes.length + 1);
40+
utf8Encoder.encodeInto(stringValue, memoryBytes.subarray(strPtr));
41+
memoryBytes[strPtr + bytes.length] = 0x0;
42+
return Object.freeze([strPtr, bytes.byteLength]);
43+
}
44+
}
45+
```

0 commit comments

Comments
 (0)