Skip to content

Commit aa2d509

Browse files
committed
params-multi test. this wasn't it.
1 parent 42e7014 commit aa2d509

File tree

6 files changed

+331
-0
lines changed

6 files changed

+331
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
Module {
2+
span: Span {
3+
offset: 1,
4+
},
5+
id: None,
6+
name: None,
7+
kind: Text(
8+
[
9+
Func(
10+
Func {
11+
span: Span {
12+
offset: 11,
13+
},
14+
id: Some(
15+
"test",
16+
),
17+
name: None,
18+
exports: InlineExport {
19+
names: [],
20+
},
21+
kind: Inline {
22+
locals: [],
23+
expression: Expression {
24+
instrs: [
25+
LocalGet(
26+
Id(
27+
"a",
28+
),
29+
),
30+
I32Const(
31+
1,
32+
),
33+
I32Add,
34+
],
35+
},
36+
},
37+
ty: TypeUse {
38+
index: None,
39+
inline: Some(
40+
FunctionType {
41+
params: [
42+
(
43+
Some(
44+
"a",
45+
),
46+
None,
47+
I32,
48+
),
49+
(
50+
Some(
51+
"b",
52+
),
53+
None,
54+
I32,
55+
),
56+
(
57+
Some(
58+
"c",
59+
),
60+
None,
61+
I32,
62+
),
63+
],
64+
results: [
65+
I32,
66+
],
67+
},
68+
),
69+
},
70+
},
71+
),
72+
Func(
73+
Func {
74+
span: Span {
75+
offset: 133,
76+
},
77+
id: Some(
78+
"entry",
79+
),
80+
name: None,
81+
exports: InlineExport {
82+
names: [
83+
"entry",
84+
],
85+
},
86+
kind: Inline {
87+
locals: [],
88+
expression: Expression {
89+
instrs: [
90+
I32Const(
91+
8,
92+
),
93+
I32Const(
94+
16,
95+
),
96+
I32Const(
97+
32,
98+
),
99+
I32Const(
100+
64,
101+
),
102+
Call(
103+
Id(
104+
"test",
105+
),
106+
),
107+
],
108+
},
109+
},
110+
ty: TypeUse {
111+
index: None,
112+
inline: Some(
113+
FunctionType {
114+
params: [],
115+
results: [
116+
I32,
117+
I32,
118+
],
119+
},
120+
),
121+
},
122+
},
123+
),
124+
],
125+
),
126+
}
127+
128+
129+
130+
131+
{
132+
"I32Const": 5,
133+
"LocalGet": 1,
134+
"I32Add": 1,
135+
"Call": 1,
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
Initial State
2+
3+
```
4+
Stack: []
5+
Frames: []
6+
```
7+
8+
## calling `$entry`
9+
10+
if externally, entry is called like `entry(10, 20, 30)`, then those values are put into the stack such that the last value of the function call is the first value on the stack (if by "first" we're talking about it like it's an array).
11+
12+
13+
as part of starting the program, we seed the global stack
14+
15+
```
16+
Stack: [10, 20, 30]
17+
Frames: []
18+
```
19+
20+
Then, to call our first function, we drain as many items from the stack as their are in the stack. we start with the last value (in this case $third) and pop values off the stack.
21+
22+
```
23+
Stack: [10, 20]
24+
Frames: [
25+
{ $entry: { $third: 30 } }
26+
]
27+
```
28+
29+
then the $second parameter
30+
31+
```
32+
Stack: [10]
33+
Frames: [
34+
{ $entry: { $second: 20, $third: 30 } }
35+
]
36+
```
37+
38+
then the $first parameter
39+
40+
41+
```
42+
Stack: []
43+
Frames: [
44+
{ $entry: { $first: 10, $second: 20, $third: 30 } }
45+
]
46+
```
47+
48+
## `local.get $third`
49+
50+
this pushes `ActiveFrame.parameters.$entry.$third` onto the stack
51+
52+
```
53+
Stack: [30]
54+
Frames: [
55+
{ $entry: { $first: 10, $second: 20, $third: 30 } }
56+
]
57+
```
58+
59+
## `call $test`
60+
61+
again, we create a new frame and drain as many values from the stack as there are in the parameters of the function we're calling
62+
63+
```
64+
Stack: []
65+
Frames: [
66+
{ $entry: { $first: 10, $second: 20, $third: 30 } }
67+
{ $test: { $a: 30 } }
68+
]
69+
```
70+
71+
## `local.get $a`
72+
73+
then we take the value in `ActiveFrame.$a` and push it onto the stack.
74+
75+
```
76+
Stack: [30]
77+
Frames: [
78+
{ $entry: { $first: 10, $second: 20, $third: 30 } }
79+
{ $test: { $a: 30 } }
80+
]
81+
```
82+
83+
## `$test` runs out of instructions
84+
85+
we hit the end of the `$test`, so we remove its frame
86+
87+
```
88+
Stack: [30]
89+
Frames: [
90+
{ $entry: { $first: 10, $second: 20, $third: 30 } }
91+
]
92+
```
93+
94+
## `$entry` runs out of instructions
95+
96+
we hit the end of the `$entry`, so we remove its frame
97+
98+
```
99+
Stack: [30]
100+
Frames: []
101+
```
102+
103+
## `$entry` runs out of instructions
104+
105+
we hit the end of the `$entry`, so we remove its frame
106+
107+
108+
## program exit
109+
110+
we've run out of frames, so we can exit the program and return whatever is on the stack
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { entry } from "./params-multi"
2+
import type { Expect, Equal } from "type-testing";
3+
4+
import { test, expect } from 'vitest';
5+
import { getWasm } from '../utils'
6+
7+
const name = 'params-multi';
8+
test(name, async () => {
9+
const entry = await getWasm("from-wat", name);
10+
expect(entry()).toStrictEqual([8, 17]);
11+
});
12+
13+
type x = entry<[]>;
14+
// ^?
15+
16+
type testCases = [
17+
Expect<Equal<entry<[]>, [8, 17]>>,
18+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { Func, bootstrap } from 'wasm-to-typescript-types'
2+
3+
type $test = Satisfies<Func, {
4+
kind: 'func';
5+
params: ['$a', '$b', '$c'];
6+
paramsTypes: ['i32', 'i32', 'i32'];
7+
resultTypes: ['i32'];
8+
locals: [];
9+
instructions: [
10+
{ kind: 'LocalGet'; id: '$a' },
11+
{ kind: 'Const'; value: '00000000000000000000000000000001' },
12+
{ kind: 'Add', type: 'i32' },
13+
];
14+
}>
15+
16+
type $entry = Satisfies<Func, {
17+
kind: 'func';
18+
params: [];
19+
paramsTypes: [];
20+
resultTypes: ['i32', 'i32'];
21+
locals: [];
22+
instructions: [
23+
{ kind: 'Const'; value: '00000000000000000000000000001000' },
24+
{ kind: 'Const'; value: '00000000000000000000000000010000' },
25+
{ kind: 'Const'; value: '00000000000000000000000000100000' },
26+
{ kind: 'Const'; value: '00000000000000000000000001000000' },
27+
{ kind: 'Call'; id: '$test' },
28+
];
29+
}>
30+
31+
export type funcs = {
32+
$test: $test;
33+
$entry: $entry;
34+
}
35+
36+
export type entry<
37+
arguments extends [],
38+
debugMode extends boolean = false,
39+
stopAt extends number = number,
40+
> = bootstrap<
41+
{
42+
arguments: arguments;
43+
funcs: funcs;
44+
globals: {};
45+
memory: {};
46+
memorySize: '00000000000000000000000000000000';
47+
indirect: {};
48+
},
49+
debugMode,
50+
stopAt
51+
>
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(module
2+
(func $test (param $a i32) (param $b i32) (param $c i32) (result i32)
3+
local.get $a
4+
i32.const 1
5+
i32.add
6+
)
7+
8+
(func $entry (export "entry") (result i32 i32)
9+
i32.const 8 ;; this value will be untouched
10+
11+
i32.const 16 ;; this will populate $a
12+
i32.const 32 ;; this will populate $b
13+
i32.const 64 ;; this will populate $c
14+
call $test ;; this will return $a + 1
15+
)
16+
)

0 commit comments

Comments
 (0)