-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42e7014
commit 0769d0b
Showing
6 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
Module { | ||
span: Span { | ||
offset: 1, | ||
}, | ||
id: None, | ||
name: None, | ||
kind: Text( | ||
[ | ||
Func( | ||
Func { | ||
span: Span { | ||
offset: 11, | ||
}, | ||
id: Some( | ||
"test", | ||
), | ||
name: None, | ||
exports: InlineExport { | ||
names: [], | ||
}, | ||
kind: Inline { | ||
locals: [], | ||
expression: Expression { | ||
instrs: [ | ||
LocalGet( | ||
Id( | ||
"a", | ||
), | ||
), | ||
], | ||
}, | ||
}, | ||
ty: TypeUse { | ||
index: None, | ||
inline: Some( | ||
FunctionType { | ||
params: [ | ||
( | ||
Some( | ||
"a", | ||
), | ||
None, | ||
I32, | ||
), | ||
], | ||
results: [ | ||
I32, | ||
], | ||
}, | ||
), | ||
}, | ||
}, | ||
), | ||
Func( | ||
Func { | ||
span: Span { | ||
offset: 227, | ||
}, | ||
id: Some( | ||
"entry", | ||
), | ||
name: None, | ||
exports: InlineExport { | ||
names: [ | ||
"entry", | ||
], | ||
}, | ||
kind: Inline { | ||
locals: [], | ||
expression: Expression { | ||
instrs: [ | ||
LocalGet( | ||
Id( | ||
"third", | ||
), | ||
), | ||
Call( | ||
Id( | ||
"test", | ||
), | ||
), | ||
], | ||
}, | ||
}, | ||
ty: TypeUse { | ||
index: None, | ||
inline: Some( | ||
FunctionType { | ||
params: [ | ||
( | ||
Some( | ||
"first", | ||
), | ||
None, | ||
I32, | ||
), | ||
( | ||
Some( | ||
"second", | ||
), | ||
None, | ||
I32, | ||
), | ||
( | ||
Some( | ||
"third", | ||
), | ||
None, | ||
I32, | ||
), | ||
], | ||
results: [ | ||
I32, | ||
], | ||
}, | ||
), | ||
}, | ||
}, | ||
), | ||
], | ||
), | ||
} | ||
|
||
|
||
|
||
|
||
{ | ||
"LocalGet": 2, | ||
"Call": 1, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
Initial State | ||
|
||
``` | ||
Stack: [] | ||
Frames: [] | ||
``` | ||
|
||
## calling `$entry` | ||
|
||
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). | ||
|
||
|
||
as part of starting the program, we seed the global stack | ||
|
||
``` | ||
Stack: [10, 20, 30] | ||
Frames: [] | ||
``` | ||
|
||
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. | ||
|
||
``` | ||
Stack: [10, 20] | ||
Frames: [ | ||
{ $entry: { $third: 30 } } | ||
] | ||
``` | ||
|
||
then the $second parameter | ||
|
||
``` | ||
Stack: [10] | ||
Frames: [ | ||
{ $entry: { $second: 20, $third: 30 } } | ||
] | ||
``` | ||
|
||
then the $first parameter | ||
|
||
|
||
``` | ||
Stack: [] | ||
Frames: [ | ||
{ $entry: { $first: 10, $second: 20, $third: 30 } } | ||
] | ||
``` | ||
|
||
## `local.get $third` | ||
|
||
this pushes `ActiveFrame.parameters.$entry.$third` onto the stack | ||
|
||
``` | ||
Stack: [30] | ||
Frames: [ | ||
{ $entry: { $first: 10, $second: 20, $third: 30 } } | ||
] | ||
``` | ||
|
||
## `call $test` | ||
|
||
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 | ||
|
||
``` | ||
Stack: [] | ||
Frames: [ | ||
{ $entry: { $first: 10, $second: 20, $third: 30 } } | ||
{ $test: { $a: 30 } } | ||
] | ||
``` | ||
|
||
## `local.get $a` | ||
|
||
then we take the value in `ActiveFrame.$a` and push it onto the stack. | ||
|
||
``` | ||
Stack: [30] | ||
Frames: [ | ||
{ $entry: { $first: 10, $second: 20, $third: 30 } } | ||
{ $test: { $a: 30 } } | ||
] | ||
``` | ||
|
||
## `$test` runs out of instructions | ||
|
||
we hit the end of the `$test`, so we remove its frame | ||
|
||
``` | ||
Stack: [30] | ||
Frames: [ | ||
{ $entry: { $first: 10, $second: 20, $third: 30 } } | ||
] | ||
``` | ||
|
||
## `$entry` runs out of instructions | ||
|
||
we hit the end of the `$entry`, so we remove its frame | ||
|
||
``` | ||
Stack: [30] | ||
Frames: [] | ||
``` | ||
|
||
## `$entry` runs out of instructions | ||
|
||
we hit the end of the `$entry`, so we remove its frame | ||
|
||
|
||
## program exit | ||
|
||
we've run out of frames, so we can exit the program and return whatever is on the stack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { entry } from "./params" | ||
import type { Expect, Equal } from "type-testing"; | ||
|
||
import { test, expect } from 'vitest'; | ||
import { getWasm } from '../utils' | ||
|
||
const name = 'params'; | ||
test(name, async () => { | ||
const entry = await getWasm("from-wat", name); | ||
expect(entry(1, 2, 3)).toStrictEqual(3); | ||
expect(entry(2, 3, 4)).toStrictEqual(4); | ||
}); | ||
|
||
type x = entry<[1, 2, 3]>; | ||
// ^? | ||
|
||
type testCases = [ | ||
Expect<Equal<entry<[1, 2, 3]>, 3>>, | ||
Expect<Equal<entry<[2, 3, 4]>, 4>>, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { Func, bootstrap } from 'wasm-to-typescript-types' | ||
|
||
type $test = Satisfies<Func, { | ||
kind: 'func'; | ||
params: ['$a']; | ||
paramsTypes: ['i32']; | ||
resultTypes: ['i32']; | ||
locals: []; | ||
instructions: [ | ||
{ kind: 'LocalGet'; id: '$a' }, | ||
]; | ||
}> | ||
|
||
type $entry = Satisfies<Func, { | ||
kind: 'func'; | ||
params: ['$first', '$second', '$third']; | ||
paramsTypes: ['i32', 'i32', 'i32']; | ||
resultTypes: ['i32']; | ||
locals: []; | ||
instructions: [ | ||
{ kind: 'LocalGet'; id: '$third' }, | ||
{ kind: 'Call'; id: '$test' }, | ||
]; | ||
}> | ||
|
||
export type funcs = { | ||
$test: $test; | ||
$entry: $entry; | ||
} | ||
|
||
export type entry< | ||
arguments extends [number, number, number], | ||
debugMode extends boolean = false, | ||
stopAt extends number = number, | ||
> = bootstrap< | ||
{ | ||
arguments: arguments; | ||
funcs: funcs; | ||
globals: {}; | ||
memory: {}; | ||
memorySize: '00000000000000000000000000000000'; | ||
indirect: {}; | ||
}, | ||
debugMode, | ||
stopAt | ||
> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
(module | ||
(func $test (param $a i32) (param $b i32) (param $c i32) (result i32) | ||
local.get $a | ||
) | ||
|
||
;; an example of a function that takes many more parameters than it returns | ||
;; this is to test that the stack is properly popped from and pushed to | ||
(func $entry (export "entry") (result i32) | ||
i32.const 16 | ||
i32.const 32 | ||
i32.const 64 | ||
call $test | ||
) | ||
) |