Skip to content

Commit

Permalink
Moves address conversion functions to runtime module
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendoCosta committed Oct 14, 2024
1 parent 3a760f4 commit 89e9908
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
23 changes: 2 additions & 21 deletions src/gwr/execution/machine.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn initialize(from module: module.Module) -> Result(Machine, String)
types: module.types,
function_addresses: store.functions
|> dict.to_list
|> list.map(fn (x) { #(address_to_int(x.0), x.0) })
|> list.map(fn (x) { #(runtime.address_to_int(x.0), x.0) })
|> dict.from_list,
table_addresses: [],
memory_addresses: list.index_map(store.memories, fn (_, index) { runtime.MemoryAddress(index) }),
Expand Down Expand Up @@ -244,7 +244,7 @@ pub fn invoke(state: MachineState, address: runtime.Address) -> Result(MachineSt
{
// 1. Assert: due to validation, S.{\mathsf{funcs}}[a] exists.
// 2. Let f be the function instance, S.{\mathsf{funcs}}[a].
use function_instance <- result.try(result.replace_error(dict.get(state.store.functions, address), "gwr/execution/machine.invoke: couldn't find the function instance with address " <> address_to_string(address)))
use function_instance <- result.try(result.replace_error(dict.get(state.store.functions, address), "gwr/execution/machine.invoke: couldn't find the function instance with address " <> runtime.address_to_string(address)))
case function_instance
{
runtime.HostFunctionInstance(_, _) -> Error("@TODO: call host function")
Expand Down Expand Up @@ -451,25 +451,6 @@ pub fn execute_with_label(state: MachineState, label: runtime.Label, instruction
Ok(state)
}

pub fn address_to_int(address: runtime.Address) -> Int
{
case address
{
runtime.FunctionAddress(addr) -> addr
runtime.TableAddress(addr) -> addr
runtime.MemoryAddress(addr) -> addr
runtime.GlobalAddress(addr) -> addr
runtime.ElementAddress(addr) -> addr
runtime.DataAddress(addr) -> addr
runtime.ExternAddress(addr) -> addr
}
}

pub fn address_to_string(address: runtime.Address) -> String
{
int.to_string(address_to_int(address))
}

pub fn expand_block_type(framestate: runtime.FrameState, block_type: instruction.BlockType) -> Result(types.FunctionType, String)
{
case block_type
Expand Down
20 changes: 20 additions & 0 deletions src/gwr/execution/runtime.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gleam/dict
import gleam/dynamic
import gleam/int
import gleam/order
import gleam/result

Expand Down Expand Up @@ -226,3 +227,22 @@ pub fn builtin_float_to_ieee_float(value: FloatValue) -> Result(ieee_float.IEEEF
NaN -> Ok(ieee_float.nan())
}
}

pub fn address_to_int(address: Address) -> Int
{
case address
{
FunctionAddress(addr) -> addr
TableAddress(addr) -> addr
MemoryAddress(addr) -> addr
GlobalAddress(addr) -> addr
ElementAddress(addr) -> addr
DataAddress(addr) -> addr
ExternAddress(addr) -> addr
}
}

pub fn address_to_string(address: Address) -> String
{
int.to_string(address_to_int(address))
}

0 comments on commit 89e9908

Please sign in to comment.