RoyalIcing / Orb

Write WebAssembly with Elixir
https://useorb.dev
BSD 3-Clause "New" or "Revised" License
198 stars 1 forks source link

Math operations seem to assume I32 #17

Closed superchris closed 7 months ago

superchris commented 7 months ago

Example:

  defw fahrenheit_to_celsius(fahrenheit: F32), F32 do
    (fahrenheit - 32.0) * 5.0 / 9.0
  end

error:

iex(1)> Orb.to_wat TemperatureConverter
** (Orb.TypeCheckError) Instruction i32.sub expected type i32, found f32 via Orb.F32.
    (orb 0.0.33) lib/orb/instruction.ex:300: Orb.Instruction.types_must_match!/3
    (orb 0.0.33) lib/orb/instruction.ex:215: Orb.Instruction.type_check_operand!/4
    (orb 0.0.33) lib/orb/instruction.ex:175: anonymous fn/4 in Orb.Instruction.type_check_operands!/3
    (elixir 1.15.7) lib/enum.ex:4789: Enum.with_index_list/3
    (orb 0.0.33) lib/orb/instruction.ex:25: Orb.Instruction.new/3
    (codebeam_2024 0.1.0) lib/temperature_converter.ex:9: TemperatureConverter."__wasm_body__ (overridable 3)"/1
    (codebeam_2024 0.1.0) lib/temperature_converter.ex:12: TemperatureConverter."__wasm_body__ (overridable 4)"/1
    (codebeam_2024 0.1.0) lib/temperature_converter.ex:16: TemperatureConverter.__wasm_body__/1
RoyalIcing commented 7 months ago

Yes this needs to be fixed! For now, you can switch modes by adding wasm_mode(Orb.F32). e.g:

defmodule Example do
  use Orb

  wasm_mode(Orb.F32)

  defw fahrenheit_to_celsius(fahrenheit: F32), F32 do
    (fahrenheit - 32.0) * 5.0 / 9.0
  end
end
RoyalIcing commented 7 months ago

I’ve written up this example as a Livebook with it running in Wasmex: https://github.com/RoyalIcing/Orb/blob/main/examples/temperature-converter.livemd

RoyalIcing commented 7 months ago

With Orb v0.0.34 this has been fixed.