This repo is AssemblyScript transforms to enhance AssemblyScript features
This is a transform of AssemblyScript to store const utf8 string literal.
The string literal in AssemblyScript is utf16 encoded into Wasm data section, this brings two problems:
- String storage has too much overhead for ascii strings
- Need to convert utf16 to utf8 string when calling native API which receives utf8/ascii strings.
To solve this problem, this repo provides a transform of AssemblyScript to define utf8 string literals
This transform scans function call pattern
utf8.build("string literal");
and replace it to
utf8.build_internal(memory.data<u8>([bytes in utf8]), length of bytes);
For example:
utf8.build("test3");
is transferred to
utf8.build_internal(memory.data<u8>([116, 101, 115, 116, 51]), 5);
Attentions:
-
The AST transform is based on string pattern matcher, so alias is not supported. Please don't use name alias of import and indirect function call to
utf8.build
, for exampleimport {utf8 as utf_8}
orlet foo = utf8.build; foo("abc")
. -
The parameter of
utf8.build
must be string literal
Install assemblyscript-transform
npm install @schleifner/assemblyscript-transform
In AssemblyScript code, src/index.ts
import {utf8} from "../node_modules/@schleifner/assemblyscript-transform/as/utf8ConstStr"
const str = utf8.build("test3");
Build with transform
asc -o out.wasm --transform ./node_modules/@schleifner/assemblyscript-transform/dist/transformBuildUtf8ConstStr.mjs ./src/index.ts