Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support wasm library using comment #144

Merged
merged 26 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9ce8c69
draft for wasm library type using comment
yviansu Jan 16, 2024
91fce37
simple implementation of wasmArray
yviansu Jan 17, 2024
63c566c
avoid cast value in semantic tree
yviansu Jan 18, 2024
81b5af5
Merge remote-tracking branch 'upstream/main' into wasmLibraryTestUsin…
yviansu Jan 19, 2024
fedf157
change wasmArray comment format
yviansu Jan 19, 2024
f590f7b
fix semantic tree logic
yviansu Jan 19, 2024
804d948
change basic wasm type recording way
yviansu Jan 22, 2024
8281aee
Merge remote-tracking branch 'upstream/main' into wasmLibraryTestUsin…
yviansu Jan 22, 2024
1ba25e4
add parse phase of wasmstruct
yviansu Jan 22, 2024
f6ff63e
support tuple type in frontend
yviansu Jan 23, 2024
196b390
add tuple value in semantic tree
yviansu Jan 24, 2024
5d7fa21
add wasmstructtype in frontend and semantic tree
yviansu Jan 24, 2024
2e73f42
add backend wasmRawType parse
yviansu Jan 24, 2024
98b9d3e
add initValues for wasmArray
yviansu Jan 26, 2024
dab9dfc
support basic ops
yviansu Jan 26, 2024
4d57dd2
add test samples
yviansu Jan 26, 2024
5260a64
add test sample for tuple type
yviansu Jan 26, 2024
078cd8b
support non-constant index get in tuple
yviansu Jan 29, 2024
f9d66ac
add more samples
yviansu Jan 30, 2024
7d062c7
support New Array Expression
yviansu Jan 31, 2024
6bc23f9
update test samples
yviansu Jan 31, 2024
98e2c96
add more test samples
yviansu Feb 2, 2024
423251b
avoid array copy if no spread expr is found
yviansu Feb 5, 2024
97c0f11
Merge remote-tracking branch 'upstream/main' into wasmLibraryTestUsin…
yviansu Feb 5, 2024
12916c5
add doc
yviansu Feb 5, 2024
fe5d026
add doc of example
yviansu Feb 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 80 additions & 3 deletions doc/developer-guide/wasmType_usage.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,68 @@
# Use wasmType in typescript
## wasmType declaration
Now we support use wasmType directly in typescript, and the types must be explicitly specified:
Now we support use wasmType directly in typescript, these below types are supported:
### wasm basic type
- `i32`
- `i64`
- `f32`
- `f64`
- `anyref`
### wasm heap type
- `array`
- `struct`

## wasmType usage
### For basic type
During usage, we must follow some rules. And the wasm basic type rules is differ from wasm heap type rules.
### wasm basic type
We can use the wasm basic type as ts type name directly.
### wasm heap type
We should set a special comment to indicate that a wasm heap type structure will be created.

1. For `array`, we use `comment + array type alias` to represent the raw wasm array type.
```ts
// Wasmnizer-ts: @WASMArray@ <Not_Packed, Mutable, Nullable>
type arrayType1 = string[];
---> will create a raw wasm array type: array<stringref>

// Wasmnizer-ts: @WASMArray@
type arrayType2 = i32[];
---> will create a raw wasm array type: array<i32>
```
**Hint: `// Wasmnizer-ts: @WASMArray@ ` is necessary, and `<Not_Packed, Mutable, Nullable>` is optional. The latter shows that `if the array element is packed`, `if the array element is mutable`, `if the array is nullable`. The default value is `Not_Packed`, `Mutable` and `Nullable`.**

2. For `struct`, we use `comment + tuple type alias` to represent the raw wasm struct type.
```ts
// Wasmnizer-ts: @WASMStruct@ <[Not_Packed, Not_Packed], [Mutable, Mutable], Nullable, NULL>
type structType1 = [arrayType1, i64];
---> will create a raw wasm struct type: struct[array<stringref>, i64]

// Wasmnizer-ts: @WASMStruct@
type structType2 = [i64, i32];
---> will create a raw wasm struct type: struct[i64, i32]
```
**Hint: `// Wasmnizer-ts: @WASMStruct@ ` is necessary, and `<[Not_Packed, ...], [Mutable, ...], Nullable, BaseTypeName>` is optional. The latter shows that `if the struct fields are packed`, `if the struct fields are mutable`, `if the struct is nullable`, `the struct's base type name`. The default value is `[Not_Packed, ...]`, `[Mutable, ...]`, `Nullable` and `NULL`.**

The comments' optional attributes can be one of these enum value:
```ts
export enum PackedTypeKind {
Not_Packed = 'Not_Packed',
I8 = 'I8',
I16 = 'I16',
}

export enum MutabilityKind {
Immutable = 'Immutable',
Mutable = 'Mutable',
}

export enum NullabilityKind {
NonNullable = 'NonNullable',
Nullable = 'Nullable',
}
```

## Example in ts
### Used as basic type
If we define the wasmtype for variables, and the right value is LiteralValue or variables with the same wasmtype, the **no cast** will be generated.
```ts
const a: i32 = 100;
Expand All @@ -32,6 +88,27 @@ const a: f64 = 100;
(f64.const 100)
```

```ts
// Wasmnizer-ts: @WASMArray@
type arrayType2 = i32[];
const a: arrayType2 = [100];
-->
(array.new_fixed $array0 1
(i32.const 100)
)
```

```ts
// Wasmnizer-ts: @WASMStruct@
type structType2 = [i64, i32];
const a: structType2 = [100, 200]
--->
(struct.new $45
(i64.const 100)
(i32.const 200)
)
```

If we don't define the wasmtype explicitly, then the variable will be regard as `number` type, **one cast** will be occurs.
```ts
const a = 100 as i32;
Expand All @@ -42,7 +119,7 @@ const a = 100 as i32;
```


### For array type
### Used as array element type
yviansu marked this conversation as resolved.
Show resolved Hide resolved
The array type should be explicitly specified too.
```ts
const a: i32[] = [1, 2];
Expand Down
1 change: 1 addition & 0 deletions lib/builtin/builtin_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export namespace BuiltinNames {
export const findPropertyFlagAndIndex = 'find_property_flag_and_index';
export const findPropertyType = 'find_property_type';
export const getInfcProperty = 'get_infc_property';
export const getTupleField = 'get_tuple_field';

// builtin globals
export const builtinTypeManglePrefix = 'lib/builtin/lib.type.d';
Expand Down
2 changes: 2 additions & 0 deletions src/backend/binaryen/glue/packType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import {
arrayBufferType,
dataViewType,
numberArrayStructType,
i32ArrayType,
} from './transform.js';
import { typeInfo } from './utils.js';

export const i8ArrayTypeInfo: typeInfo = i8ArrayType;
export const stringTypeInfo: typeInfo = stringType;
export const numberArrayTypeInfo = numberArrayType;
export const i32ArrayTypeInfo = i32ArrayType;
export const stringArrayTypeInfo = stringArrayType;
export const stringArrayStructTypeInfo = stringArrayStructType;
export const stringrefArrayTypeInfo = stringrefArrayType;
Expand Down
15 changes: 15 additions & 0 deletions src/backend/binaryen/glue/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ export function initStructType(
export const i8ArrayType = genarateI8ArrayTypeInfo();
/* array(f64) */
export const numberArrayType = genarateNumberArrayTypeInfo();
/* array(i32) */
export const i32ArrayType = genarateI32ArrayTypeInfo();
/* array(stringref) */
export const stringrefArrayType = genarateStringrefArrayTypeInfo(false);
/* array(i32) */
Expand Down Expand Up @@ -355,6 +357,19 @@ function genarateNumberArrayTypeInfo(): typeInfo {
return numberArrayTypeInfo;
}

// generate i32 array type
function genarateI32ArrayTypeInfo(): typeInfo {
const i32ArrayTypeInfo = initArrayType(
binaryen.i32,
Packed.Not,
true,
true,
-1,
binaryenCAPI._TypeBuilderCreate(1),
);
return i32ArrayTypeInfo;
}

// generate string array type
function genarateStringArrayTypeInfo(struct_wrap: boolean): typeInfo {
const stringTypeInfo = stringType;
Expand Down
Loading
Loading