From 5c2a2c277d4098deca2e7346d675f6a47a91e761 Mon Sep 17 00:00:00 2001 From: Patrick Smith Date: Sun, 30 Jun 2024 14:16:37 +1000 Subject: [PATCH] Breaking change: Flip Memory.Slice 64-bit representation around Smallest digits are on the right --- lib/orb/memory/slice.ex | 14 +++++++------- test/i32/conveniences_test.exs | 1 - test/memory_slice_test.exs | 13 ++++++------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/orb/memory/slice.ex b/lib/orb/memory/slice.ex index 561c921..079d1cd 100644 --- a/lib/orb/memory/slice.ex +++ b/lib/orb/memory/slice.ex @@ -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 @@ -36,7 +36,7 @@ defmodule Orb.Memory.Slice do # <> import Bitwise - n >>> 32 + n &&& 0xFFFFFFFF end def get_byte_offset( @@ -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 @@ -56,7 +56,7 @@ defmodule Orb.Memory.Slice do # byte_length import Bitwise - n &&& 0xFFFFFFFF + n >>> 32 end def get_byte_length( @@ -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 diff --git a/test/i32/conveniences_test.exs b/test/i32/conveniences_test.exs index 12082ca..0b86573 100644 --- a/test/i32/conveniences_test.exs +++ b/test/i32/conveniences_test.exs @@ -110,7 +110,6 @@ defmodule I32ConveniencesTest do end assert wat =~ "Method not allowed" - IO.puts(wat) assert 255 = Instance.run(wat) |> Instance.call(:ptr) diff --git a/test/memory_slice_test.exs b/test/memory_slice_test.exs index 2427da3..0029abb 100644 --- a/test/memory_slice_test.exs +++ b/test/memory_slice_test.exs @@ -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