witx_codegen/assemblyscript/
header.rs

1use std::io::Write;
2
3use super::*;
4
5impl AssemblyScriptGenerator {
6    pub fn header<T: Write>(w: &mut PrettyWriter<T>) -> Result<(), Error> {
7        w.write_lines(
8            "
9/*
10 * This file was automatically generated by witx-codegen - Do not edit manually.
11 */",
12        )?;
13        w.write_lines(
14            "
15export type WasiHandle = i32;
16export type Char8 = u8;
17export type Char32 = u32;
18export type WasiPtr<T> = usize;
19export type WasiMutPtr<T> = usize;
20export type WasiStringBytesPtr = WasiPtr<Char8>;
21",
22        )?;
23        w.write_lines(
24            "
25// @ts-ignore: decorator
26@unmanaged
27export class WasiString {
28    ptr: WasiStringBytesPtr;
29    length: usize;
30
31    constructor(str: string) {
32        let wasiString = String.UTF8.encode(str, false);
33        // @ts-ignore: cast
34        this.ptr = changetype<WasiStringBytesPtr>(wasiString);
35        this.length = wasiString.byteLength;
36    }
37
38    toString(): string {
39        let tmp = new ArrayBuffer(this.length as u32);
40        memory.copy(changetype<usize>(tmp), this.ptr, this.length);
41        return String.UTF8.decode(tmp);
42    }
43}
44
45// @ts-ignore: decorator
46@unmanaged
47export class WasiSlice<T> {
48    ptr: WasiPtr<T>;
49    length: usize;
50
51    constructor(array: ArrayBufferView) {
52        // @ts-ignore: cast
53        this.ptr = array.dataStart;
54        this.length = array.byteLength;
55    }
56}
57
58// @ts-ignore: decorator
59@unmanaged
60export class WasiMutSlice<T> {
61    ptr: WasiMutPtr<T>;
62    length: usize;
63
64    constructor(array: ArrayBufferView) {
65        // @ts-ignore: cast
66        this.ptr = array.dataStart;
67        this.length = array.byteLength;
68    }
69}
70",
71        )?
72        .eob()?;
73        Ok(())
74    }
75}