RoyalIcing / Orb

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

Alternative syntax for globals #3

Closed RoyalIcing closed 6 months ago

RoyalIcing commented 11 months ago

Right now we declare globals using the type e.g. I32.global(name: 42) or F32.global(blah: 1.5)

What if instead we allowed module attributes to be the globals? This would let normal Elixir code read their values too, as we’d keep the attribute definitions. It would also match the current @some_global syntax to read a global in functions.

It could look like:

export_global :mutable do
  @some_int32 42
  @xn 0.96422
  @yn 1.00000
  @zn 0.82521
  @k :math.pow(29, 3) / :math.pow(3, 3)
  @e :math.pow(6, 3) / :math.pow(29, 3)
end

You can see that whether it is an integer or float is detected by from its Erlang type (is_integer(42) vs is_float(42.0))

In a future world with 64-bit numbers, they would be enabled using a parameter export_global :mutable, 64 do or a name like export_global64 :mutable do


Another alternative is Global.expose_attributes([:xn, :yn, :zn, :k, :e])

RoyalIcing commented 6 months ago

This has been added to Orb. The approach to 32 vs 64-bit is still to be worked out.