Skip to content

Commit 0769d0b

Browse files
committed
[wip] params-multi
1 parent 42e7014 commit 0769d0b

File tree

6 files changed

+320
-0
lines changed

6 files changed

+320
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
],
31+
},
32+
},
33+
ty: TypeUse {
34+
index: None,
35+
inline: Some(
36+
FunctionType {
37+
params: [
38+
(
39+
Some(
40+
"a",
41+
),
42+
None,
43+
I32,
44+
),
45+
],
46+
results: [
47+
I32,
48+
],
49+
},
50+
),
51+
},
52+
},
53+
),
54+
Func(
55+
Func {
56+
span: Span {
57+
offset: 227,
58+
},
59+
id: Some(
60+
"entry",
61+
),
62+
name: None,
63+
exports: InlineExport {
64+
names: [
65+
"entry",
66+
],
67+
},
68+
kind: Inline {
69+
locals: [],
70+
expression: Expression {
71+
instrs: [
72+
LocalGet(
73+
Id(
74+
"third",
75+
),
76+
),
77+
Call(
78+
Id(
79+
"test",
80+
),
81+
),
82+
],
83+
},
84+
},
85+
ty: TypeUse {
86+
index: None,
87+
inline: Some(
88+
FunctionType {
89+
params: [
90+
(
91+
Some(
92+
"first",
93+
),
94+
None,
95+
I32,
96+
),
97+
(
98+
Some(
99+
"second",
100+
),
101+
None,
102+
I32,
103+
),
104+
(
105+
Some(
106+
"third",
107+
),
108+
None,
109+
I32,
110+
),
111+
],
112+
results: [
113+
I32,
114+
],
115+
},
116+
),
117+
},
118+
},
119+
),
120+
],
121+
),
122+
}
123+
124+
125+
126+
127+
{
128+
"LocalGet": 2,
129+
"Call": 1,
130+
}
Lines changed: 110 additions & 0 deletions
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
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { entry } from "./params"
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';
8+
test(name, async () => {
9+
const entry = await getWasm("from-wat", name);
10+
expect(entry(1, 2, 3)).toStrictEqual(3);
11+
expect(entry(2, 3, 4)).toStrictEqual(4);
12+
});
13+
14+
type x = entry<[1, 2, 3]>;
15+
// ^?
16+
17+
type testCases = [
18+
Expect<Equal<entry<[1, 2, 3]>, 3>>,
19+
Expect<Equal<entry<[2, 3, 4]>, 4>>,
20+
]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { Func, bootstrap } from 'wasm-to-typescript-types'
2+
3+
type $test = Satisfies<Func, {
4+
kind: 'func';
5+
params: ['$a'];
6+
paramsTypes: ['i32'];
7+
resultTypes: ['i32'];
8+
locals: [];
9+
instructions: [
10+
{ kind: 'LocalGet'; id: '$a' },
11+
];
12+
}>
13+
14+
type $entry = Satisfies<Func, {
15+
kind: 'func';
16+
params: ['$first', '$second', '$third'];
17+
paramsTypes: ['i32', 'i32', 'i32'];
18+
resultTypes: ['i32'];
19+
locals: [];
20+
instructions: [
21+
{ kind: 'LocalGet'; id: '$third' },
22+
{ kind: 'Call'; id: '$test' },
23+
];
24+
}>
25+
26+
export type funcs = {
27+
$test: $test;
28+
$entry: $entry;
29+
}
30+
31+
export type entry<
32+
arguments extends [number, number, number],
33+
debugMode extends boolean = false,
34+
stopAt extends number = number,
35+
> = bootstrap<
36+
{
37+
arguments: arguments;
38+
funcs: funcs;
39+
globals: {};
40+
memory: {};
41+
memorySize: '00000000000000000000000000000000';
42+
indirect: {};
43+
},
44+
debugMode,
45+
stopAt
46+
>
54 Bytes
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(module
2+
(func $test (param $a i32) (param $b i32) (param $c i32) (result i32)
3+
local.get $a
4+
)
5+
6+
;; an example of a function that takes many more parameters than it returns
7+
;; this is to test that the stack is properly popped from and pushed to
8+
(func $entry (export "entry") (result i32)
9+
i32.const 16
10+
i32.const 32
11+
i32.const 64
12+
call $test
13+
)
14+
)

0 commit comments

Comments
 (0)