Search code examples
webassembly

Can I do Math on LEB128 in WebAssembly?


I am new to WebAssembly and still learning it and may just be misunderstanding things.

My understanding is that WebAssembly uses LEB128 to encode arbitrary long integers efficiently. But it seems that the integer Math functions are all defined exclusively on i32 and i64. It is indeed the case that, although I have to use LEB128 to represent the number literals, I cannot actually do Math on the full range of LEB128? Put differently, is there something like BigInt in WebAssembly natively?

I was reading tutorials and documentation to see if there are some operations directly defined on LEB128 numbers, but I only could find operations on i32 and i64.


Solution

  • The standard says that all the integers (including i32 and i64) are encoded using LEB128, which means that you get the size benefits of the variable-sized encoding even if you don't have arbitrary large integers. But you are correct that there is not BigInt for WebAssembly.

    Nothing really more to add, just that, in this case, your intuition is correct. WebAssembly is designed to be simple, so no BigInt by default helps with that.

    You can, in practice, use the bindings generation of your language to transparently use BigInts in WebAssembly.