Skip to content

Commit

Permalink
[wip] params-multi
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitropoulos committed Oct 29, 2024
1 parent 42e7014 commit 0769d0b
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 0 deletions.
130 changes: 130 additions & 0 deletions packages/conformance-tests/from-wat/params-multi.dump
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,
}
110 changes: 110 additions & 0 deletions packages/conformance-tests/from-wat/params-multi.md
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
20 changes: 20 additions & 0 deletions packages/conformance-tests/from-wat/params-multi.test.ts
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>>,
]
46 changes: 46 additions & 0 deletions packages/conformance-tests/from-wat/params-multi.ts
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.
14 changes: 14 additions & 0 deletions packages/conformance-tests/from-wat/params-multi.wat
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
)
)

0 comments on commit 0769d0b

Please sign in to comment.