Skip to content

Commit

Permalink
Allow returning atoms using str constant system
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Nov 4, 2024
1 parent 736ed75 commit d4b0384
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/orb/constants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ defmodule Orb.Constants do
def expand_if_needed(value) when is_binary(value), do: expand_string!(value)
def expand_if_needed(value) when is_list(value), do: :lists.map(&expand_if_needed/1, value)
def expand_if_needed(value) when is_struct(value, Orb.IfElse), do: Orb.IfElse.expand(value)
def expand_if_needed(nil), do: nil

def expand_if_needed(value) when is_atom(value) do
str = value |> Atom.to_string() |> expand_string!()
str.memory_offset
end

# Handles Orb.InstructionSequence or anything with a `body`
def expand_if_needed(%_{body: _} = struct) do
Expand Down
35 changes: 35 additions & 0 deletions test/string_constants_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,41 @@ defmodule StringConstantsTest do
"""
end

test "works with atoms" do
defmodule ABCAtom do
use Orb

defw alphabet(), I32 do
:abc
end

defw always_be_closing(), I32 do
:abc
end

defw australian_broadcasting_corporation(), Str do
"abc"
end
end

assert to_wat(ABCAtom) == """
(module $ABCAtom
(memory (export "memory") 1)
(; constants 4 bytes ;)
(data (i32.const 255) "abc")
(func $alphabet (export "alphabet") (result i32)
(i32.const 255)
)
(func $always_be_closing (export "always_be_closing") (result i32)
(i32.const 255)
)
(func $australian_broadcasting_corporation (export "australian_broadcasting_corporation") (result i32 i32)
(i32.const 255) (i32.const 3)
)
)
"""
end

describe "const/1" do
test "assigns data" do
defmodule ConstHTMLTypes do
Expand Down

0 comments on commit d4b0384

Please sign in to comment.