Skip to content

Commit

Permalink
Document Str
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Jun 30, 2024
1 parent f983f09 commit da1ccad
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
7 changes: 6 additions & 1 deletion lib/orb/str.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Orb.Str do
@moduledoc """
A string slice.
A string slice. Represented under the hood as a `{:i32, :i32}` tuple, the first element being the base address, and the second element the string length.
"""

defstruct push_type: __MODULE__, memory_offset: nil, string: nil
Expand All @@ -10,6 +10,11 @@ defmodule Orb.Str do
def wasm_type, do: {:i32, :i32}
end

@doc """
An empty string, represented by a string slice from offset zero and of zero length.
The ol’ billion-dollar mistake: null.
"""
def empty() do
%__MODULE__{
memory_offset: 0x0,
Expand Down
8 changes: 4 additions & 4 deletions test/defw_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ defmodule DefwTest do
defmodule SharedStringConstants do
use Orb

defw foo(), Orb.Str do
defw foo(), Str do
~S"foo"
end

defw cdata_start(), Orb.Str do
defw cdata_start(), Str do
~S"<![CDATA["
end
end
Expand All @@ -94,11 +94,11 @@ defmodule DefwTest do

Memory.pages(1)

defw cdata_start2(), Orb.Str do
defw cdata_start2(), Str do
~S"<![CDATA["
end

defw use_other(), Orb.Str do
defw use_other(), Str do
cdata_start()
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/examples/memory_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Examples.MemoryTest do
# Or just use Memory.Slice
Copying.memcpy(
0x400,
"hello" |> Orb.Str.get_base_address(),
"hello" |> Str.get_base_address(),
# const("hello").memory_offset,
3
)
Expand Down
4 changes: 2 additions & 2 deletions test/global_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ defmodule GlobalTest do
@empty ""
end

defw mime_type_constant, Orb.Str do
defw mime_type_constant, Str do
"text/html"
end

defw mime_type_global, Orb.Str do
defw mime_type_global, Str do
@mime_type
end

Expand Down
6 changes: 3 additions & 3 deletions test/i32/conveniences_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ defmodule I32ConveniencesTest do
end

defw ptr(), I32 do
@method |> Orb.Memory.Slice.get_byte_offset()
@method |> Memory.Slice.get_byte_offset()
end

defw text_html(), Orb.Str do
if Memory.load!(I32, @method |> Orb.Memory.Slice.get_byte_offset()) !==
defw text_html(), Str do
if Memory.load!(I32, @method |> Memory.Slice.get_byte_offset()) !==
I32.from_4_byte_ascii("GET\0") do
return(~S"""
<!doctype html>
Expand Down
26 changes: 13 additions & 13 deletions test/string_constants_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ defmodule StringConstantsTest do
defmodule ConstHTMLTypes do
use Orb

defw doctype(), Orb.Str do
defw doctype(), Str do
const("<!doctype html>")
end

defw mime_type(), Orb.Str do
defw mime_type(), Str do
const(Enum.join(["text", "/", "html"]))
end
end
Expand All @@ -66,11 +66,11 @@ defmodule StringConstantsTest do
defmodule ConstHTMLTypes2 do
use Orb

defw doctype(), Orb.Str do
defw doctype(), Str do
const(Enum.join(["<!doctype ", "html>"]))
end

defw mime_type(), Orb.Str do
defw mime_type(), Str do
const(Enum.join(["text", "/", "html"]))
end
end
Expand Down Expand Up @@ -104,7 +104,7 @@ defmodule StringConstantsTest do
defmodule ConstantOfOnePage do
use Orb

defw doctype(), Orb.Str do
defw doctype(), Str do
const(BigStrings.string_of_length_1_page())
end
end
Expand All @@ -131,7 +131,7 @@ defmodule StringConstantsTest do
defmodule ExactlySinglePage do
use Orb

defw doctype(), Orb.Str do
defw doctype(), Str do
const(BigStrings.string_should_fit_into_1_page())
end
end
Expand All @@ -156,7 +156,7 @@ defmodule StringConstantsTest do
defmodule TwoPagesJust do
use Orb

defw doctype(), Orb.Str do
defw doctype(), Str do
const(BigStrings.string_should_just_overflow_2_pages())
end
end
Expand All @@ -181,11 +181,11 @@ defmodule StringConstantsTest do
defmodule MultipleConstantsExactlySinglePage do
use Orb

defw first(), Orb.Str do
defw first(), Str do
const(BigStrings.string_should_fit_into_half_page("a"))
end

defw second(), Orb.Str do
defw second(), Str do
const(BigStrings.string_should_fit_into_half_page("b"))
end
end
Expand Down Expand Up @@ -215,15 +215,15 @@ defmodule StringConstantsTest do
defmodule MultipleConstantsTwoPagesJust do
use Orb

defw first(), Orb.Str do
defw first(), Str do
const(BigStrings.string_should_fit_into_half_page("a"))
end

defw second(), Orb.Str do
defw second(), Str do
const(BigStrings.string_should_fit_into_half_page("b"))
end

defw third(), Orb.Str do
defw third(), Str do
const("c")
end
end
Expand Down Expand Up @@ -260,7 +260,7 @@ defmodule StringConstantsTest do
Memory.pages(7)
Memory.pages(11)

defw first(), Orb.Str do
defw first(), Str do
const(BigStrings.string_should_just_overflow_2_pages())
end
end
Expand Down

0 comments on commit da1ccad

Please sign in to comment.