Skip to content

Commit 14c0425

Browse files
committed
the most intense debugging journey of my whole career
1 parent 2be64e5 commit 14c0425

File tree

7 files changed

+218
-1
lines changed

7 files changed

+218
-1
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
Module {
2+
span: Span {
3+
offset: 1,
4+
},
5+
id: None,
6+
name: None,
7+
kind: Text(
8+
[
9+
Memory(
10+
Memory {
11+
span: Span {
12+
offset: 11,
13+
},
14+
id: Some(
15+
"memory",
16+
),
17+
name: None,
18+
exports: InlineExport {
19+
names: [
20+
"memory",
21+
],
22+
},
23+
kind: Normal(
24+
B32 {
25+
limits: Limits {
26+
min: 42,
27+
max: None,
28+
},
29+
shared: false,
30+
},
31+
),
32+
},
33+
),
34+
Func(
35+
Func {
36+
span: Span {
37+
offset: 52,
38+
},
39+
id: Some(
40+
"entry",
41+
),
42+
name: None,
43+
exports: InlineExport {
44+
names: [
45+
"entry",
46+
],
47+
},
48+
kind: Inline {
49+
locals: [],
50+
expression: Expression {
51+
instrs: [
52+
MemorySize(
53+
MemoryArg {
54+
mem: Num(
55+
0,
56+
Span {
57+
offset: 117,
58+
},
59+
),
60+
},
61+
),
62+
I32Const(
63+
27,
64+
),
65+
MemoryGrow(
66+
MemoryArg {
67+
mem: Num(
68+
0,
69+
Span {
70+
offset: 205,
71+
},
72+
),
73+
},
74+
),
75+
MemorySize(
76+
MemoryArg {
77+
mem: Num(
78+
0,
79+
Span {
80+
offset: 258,
81+
},
82+
),
83+
},
84+
),
85+
],
86+
},
87+
},
88+
ty: TypeUse {
89+
index: None,
90+
inline: Some(
91+
FunctionType {
92+
params: [],
93+
results: [
94+
I32,
95+
I32,
96+
I32,
97+
],
98+
},
99+
),
100+
},
101+
},
102+
),
103+
],
104+
),
105+
}
106+
107+
108+
109+
110+
{
111+
"MemorySize": 2,
112+
"I32Const": 1,
113+
"MemoryGrow": 1,
114+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { Expect, Equal } from 'type-testing';
2+
import type { entry } from "./memory-grow"
3+
4+
import { getWasm } from '../utils'
5+
import { expect, test } from 'vitest';
6+
7+
const name = 'memory-grow';
8+
test(name, async () => {
9+
const entry = await getWasm("from-wat", name);
10+
expect(entry()).toStrictEqual([42, 42, 69]);
11+
});
12+
13+
type result = entry<[]>;
14+
// ^?
15+
16+
type testCases = [
17+
Expect<Equal<result, [42, 42, 69]>>,
18+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { Func, bootstrap } from 'wasm-to-typescript-types'
2+
3+
type $entry = Satisfies<Func, {
4+
kind: 'func';
5+
params: [];
6+
paramsTypes: [];
7+
resultTypes: ['i32', 'i32', 'i32'];
8+
locals: [];
9+
instructions: [
10+
{ kind: 'MemorySize' },
11+
{ kind: 'Const'; value: '00000000000000000000000000011011' },
12+
{ kind: 'MemoryGrow' },
13+
{ kind: 'MemorySize' },
14+
];
15+
}>
16+
17+
export type funcs = {
18+
$entry: $entry;
19+
}
20+
21+
export type entry<
22+
arguments extends [],
23+
debugMode extends boolean = false,
24+
stopAt extends number = number,
25+
> = bootstrap<
26+
{
27+
arguments: arguments;
28+
funcs: funcs;
29+
globals: {};
30+
memory: {};
31+
memorySize: '00000000000000000000000000101010';
32+
indirect: {};
33+
},
34+
debugMode,
35+
stopAt
36+
>
60 Bytes
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
(memory $memory (export "memory") 42)
3+
4+
(func $entry (export "entry") (result i32 i32 i32)
5+
memory.size ;; pushes 42 to the stack
6+
i32.const 27 ;; we will grow by 27 pages
7+
memory.grow ;; grows and pushes 69 to the stack
8+
memory.size ;; pushes 69 to the stack
9+
)
10+
)

packages/wasm-to-typescript-types/instructions/memory.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,21 @@ export type MemoryGrow<
8181
instruction extends IMemoryGrow, // unused
8282
state extends ProgramState
8383
> = Satisfies<ProgramState,
84-
state // noop
84+
state['stack'] extends [
85+
...infer remaining extends WasmValue[],
86+
infer growBy extends WasmValue
87+
]
88+
? State.Stack.set<
89+
[
90+
...remaining,
91+
state['memorySize']
92+
],
93+
State.Memory.grow<
94+
state,
95+
growBy
96+
>
97+
>
98+
: State.error<"stack exhausted", instruction, state>
8599
>
86100

87101
export type Load<

packages/wasm-to-typescript-types/state.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,31 @@ export namespace State {
492492
}
493493
// >
494494
>
495+
496+
export type grow<
497+
state extends ProgramState,
498+
growBy extends WasmValue,
499+
> = Satisfies<ProgramState,
500+
{
501+
count: state['count'];
502+
stack: state['stack'];
503+
activeFuncId: state['activeFuncId'];
504+
activeStackDepth: state['activeStackDepth'];
505+
activeLocals: state['activeLocals'];
506+
instructions: state['instructions'];
507+
activeBranches: state['activeBranches'];
508+
L1Cache: state['L1Cache'];
509+
memory: state['memory'];
510+
executionContexts: state['executionContexts'];
511+
funcs: state['funcs'];
512+
garbageCollection: state['garbageCollection'];
513+
globals: state['globals'];
514+
memorySize: I32AddBinary<state['memorySize'], growBy>; // increment
515+
indirect: state['indirect'];
516+
results: state['results'];
517+
callHistory: state['callHistory'];
518+
}
519+
>
495520
}
496521

497522
export namespace GarbageCollection {

0 commit comments

Comments
 (0)