Skip to content

Commit

Permalink
Breaking change: Flip Memory.Slice 64-bit representation around
Browse files Browse the repository at this point in the history
Smallest digits are on the right
  • Loading branch information
RoyalIcing committed Jun 30, 2024
1 parent e5909de commit 5c2a2c2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
14 changes: 7 additions & 7 deletions lib/orb/memory/slice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ defmodule Orb.Memory.Slice do
byte_length::unsigned-little-integer-size(32)>>

import Bitwise
byte_offset <<< 32 ||| byte_length
byte_length <<< 32 ||| byte_offset
end

def from(byte_offset = %{push_type: _}, byte_length = %{push_type: _}) do
byte_length
byte_offset
|> I64.extend_i32_u()
|> I64.or(
I64.extend_i32_u(byte_offset)
I64.extend_i32_u(byte_length)
|> I64.shl(32)
)
end
Expand All @@ -36,7 +36,7 @@ defmodule Orb.Memory.Slice do
# <<n::unsigned-little-integer-size(64)>>

import Bitwise
n >>> 32
n &&& 0xFFFFFFFF
end

def get_byte_offset(
Expand All @@ -46,7 +46,7 @@ defmodule Orb.Memory.Slice do
end

def get_byte_offset(range = %{push_type: _}) do
I64.shr_u(range, 32) |> I32.wrap_i64()
I32.wrap_i64(range)
end

def get_byte_length(n) when is_integer(n) do
Expand All @@ -56,7 +56,7 @@ defmodule Orb.Memory.Slice do
# byte_length

import Bitwise
n &&& 0xFFFFFFFF
n >>> 32
end

def get_byte_length(
Expand All @@ -66,7 +66,7 @@ defmodule Orb.Memory.Slice do
end

def get_byte_length(range = %{push_type: _}) do
I32.wrap_i64(range)
I64.shr_u(range, 32) |> I32.wrap_i64()
end

# defimpl Orb.ToWat do
Expand Down
1 change: 0 additions & 1 deletion test/i32/conveniences_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ defmodule I32ConveniencesTest do
end

assert wat =~ "Method not allowed"
IO.puts(wat)

assert 255 = Instance.run(wat) |> Instance.call(:ptr)

Expand Down
13 changes: 6 additions & 7 deletions test/memory_slice_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,19 @@ defmodule MemorySliceTest do
b = 2
c = Memory.Slice.from(a, b)

# assert!(c === i64(0x0000000F00000002))
# a = Memory.Slice.get_byte_offset(c)
# assert!(a === i32(0x0F))
# b = Memory.Slice.get_byte_length(c)
# assert!(b === i32(2))
assert!(c === i64(0x000000020000000F))
a = Memory.Slice.get_byte_offset(c)
assert!(a === i32(0x0F))
b = Memory.Slice.get_byte_length(c)
assert!(b === i32(2))

{c, Memory.Slice.get_byte_offset(c), Memory.Slice.get_byte_length(c)}
end
end

test "in WebAssembly" do
MemorySliceSubject |> Orb.to_wat() |> IO.puts()
{c, a, b} = OrbWasmtime.Wasm.call(MemorySliceSubject, :test)
assert c === 0x0000000F00000002
assert c === 0x000000020000000F
assert a === 0x0F
assert b === 2
end
Expand Down

0 comments on commit 5c2a2c2

Please sign in to comment.