-
-
Notifications
You must be signed in to change notification settings - Fork 682
Description
Something that would be useful for some applications of wasm modules is to be able to define types that are mapped to primitive wasm types. This is useful in cases where a functions input value is a u64 handle to something on the host side. This is also useful in cases when it is desirable to avoid use of the heap/linear-memory. The user defined type would have an underlying type of that primitive value on the stack, but it could have functions attached to it. From the wasm runtimes POV the type would just be a i64/i32.
This is possible today in Go and Rust that builds to wasm:
type Val uint64
func (v *Val) isX() bool {
// call host function on v to find out if it is X
return __host_is_x(uint64(v))
}#[repr(transparent)]
pub struct Val(u64);
impl Val {
fn is_x(&self) -> bool {
// call host function on v to find out if it is X
return __host_is_x(v.0)
}AssemblyScript already defines new types and makes them to wasm types, since it makes u64 to i64, and u32 to i32. This would be extending that capability to the user being able to define types that make to a primitive type.
To some degree this is already possible since TypeScript (and AssemblyScript) support type aliases. With type aliases we can say the following, which means Val is a u64.
type Val = u64;However, using a type alias it is not possible to attach functions to the Val type, and there is no type safety, because it is truly just an alias and not a new type. Any u64 can be set on a variable of type Val without an explicit cast.