tsc and WebAssembly with
# Portable Stdlib
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.
The portable standard library is still pretty much a work in progress and we are extending it as we go while working on the compiler. If you need something specific, feel free to improve its definitions and feature set.
There are a few semantics differences that must be taken care of.
asc understands the meaning of
// non-portable let someFloat: f32 = 1.5 let 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.5 var someInt = someFloat
which is obviously wrong. To account for this, portable conversions can be used, resulting in actually portable code. For example
// portable let someFloat: f32 = 1.5 let someInt: i32 = i32(someFloat)
will essentially result in
var someFloat = 1.5 var someInt = someFloat | 0
Likewise, again because
asc knows the meaning but
tsc does not, overflows must be handled explicitly:
// non-portable let someU8: u8 = 255 let someOtherU8: u8 = someU8 + 1
// portable let someU8: u8 = 255 let someOtherU8: u8 = u8(someU8 + 1)
essentially resulting in
let someU8 = 255 let someOtherU8 = (someU8 + 1) & 0xff
# API features
Map#has. The best way of dealing with this is asking yourself the question: What would this code do when compiled to WebAssembly?
2^53 - 1). Hence
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).