tsc and WebAssembly with
To use the portable library, add the following somewhere along your build step so the portable features are present in the environment
assemblyscript/std/portable.json instead of
assemblyscript/std/assembly.json within your
tsconfig.json. The AssemblyScript compiler itself is an example of how this can be done.
There are a few semantics differences that must be taken care of.
asc understands the meaning of
// non-portablelet someFloat: f32 = 1.5let someInt: i32 = <i32>someFloat
and then inserts the correct conversion steps,
tsc does not because all numeric types are just aliases of
tsc, the above will result in
var someFloat = 1.5var someInt = someFloat
which is obviously wrong. To account for this, portable conversions can be used, resulting in actually portable code. For example
// portablelet someFloat: f32 = 1.5let someInt: i32 = i32(someFloat)
will essentially result in
var someFloat = 1.5var someInt = someFloat | 0
Likewise, again because
asc knows the meaning but
tsc does not, overflows must be handled explicitly:
// non-portablelet someU8: u8 = 255let someOtherU8: u8 = someU8 + 1
// portablelet someU8: u8 = 255let someOtherU8: u8 = u8(someU8 + 1)
essentially resulting in
let someU8 = 255let someOtherU8 = (someU8 + 1) & 0xff
Map#has. The best way of dealing with this is asking yourself the question: What would this code do when compiled to WebAssembly?
u64 are not portable and not present in
std/portable. There are several ways to deal with this. One is to use an i64 polyfill like in this example.
store implementations. Technically this can be polyfilled in various ways, but no default is provided since actual implementations are expected to be relatively specific (for instance: the portable compiler accesses Binaryen's memory).