mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-24 12:11:50 +00:00
Generic function type aliases
This commit is contained in:
@ -2,18 +2,12 @@
|
||||
(type $ii (func (param i32) (result i32)))
|
||||
(type $iiiiv (func (param i32 i32 i32 i32)))
|
||||
(type $v (func))
|
||||
(type $i (func (result i32)))
|
||||
(type $iii (func (param i32 i32) (result i32)))
|
||||
(type $fff (func (param f32 f32) (result f32)))
|
||||
(import "env" "abort" (func $abort (param i32 i32 i32 i32)))
|
||||
(global $function-expression/f1 (mut i32) (i32.const 0))
|
||||
(global $function-expression/f2 (mut i32) (i32.const 1))
|
||||
(global $function-expression/f3 (mut i32) (i32.const 2))
|
||||
(global $function-expression/i32Adder (mut i32) (i32.const 0))
|
||||
(global $function-expression/f32Adder (mut i32) (i32.const 0))
|
||||
(global $function-expression/i8Adder (mut i32) (i32.const 0))
|
||||
(table 6 6 anyfunc)
|
||||
(elem (i32.const 0) $start~anonymous|0 $start~anonymous|0 $start~someName|2 $function-expression/makeAdder<i32>~theAdder|3 $function-expression/makeAdder<f32>~theAdder|4 $function-expression/makeAdderArrow<i8>~anonymous|5)
|
||||
(table 3 3 anyfunc)
|
||||
(elem (i32.const 0) $start~anonymous|0 $start~anonymous|0 $start~someName|2)
|
||||
(memory $0 1)
|
||||
(data (i32.const 4) "\16\00\00\00f\00u\00n\00c\00t\00i\00o\00n\00-\00e\00x\00p\00r\00e\00s\00s\00i\00o\00n\00.\00t\00s")
|
||||
(export "memory" (memory $0))
|
||||
@ -24,40 +18,7 @@
|
||||
(func $start~someName|2 (; 2 ;) (type $v)
|
||||
(nop)
|
||||
)
|
||||
(func $function-expression/makeAdder<i32>~theAdder|3 (; 3 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
|
||||
(i32.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
)
|
||||
(func $function-expression/makeAdder<i32> (; 4 ;) (type $i) (result i32)
|
||||
(i32.const 3)
|
||||
)
|
||||
(func $function-expression/makeAdder<f32>~theAdder|4 (; 5 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
|
||||
(f32.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
)
|
||||
(func $function-expression/makeAdder<f32> (; 6 ;) (type $i) (result i32)
|
||||
(i32.const 4)
|
||||
)
|
||||
(func $function-expression/makeAdderArrow<i8>~anonymous|5 (; 7 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
(i32.const 24)
|
||||
)
|
||||
(i32.const 24)
|
||||
)
|
||||
)
|
||||
(func $function-expression/makeAdderArrow<i8> (; 8 ;) (type $i) (result i32)
|
||||
(i32.const 5)
|
||||
)
|
||||
(func $start (; 9 ;) (type $v)
|
||||
(func $start (; 3 ;) (type $v)
|
||||
(if
|
||||
(i32.ne
|
||||
(call_indirect (type $ii)
|
||||
@ -97,92 +58,5 @@
|
||||
(call_indirect (type $v)
|
||||
(get_global $function-expression/f3)
|
||||
)
|
||||
(set_global $function-expression/i32Adder
|
||||
(call $function-expression/makeAdder<i32>)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call_indirect (type $iii)
|
||||
(i32.const 4)
|
||||
(i32.const 2)
|
||||
(get_global $function-expression/i32Adder)
|
||||
)
|
||||
(i32.const 6)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 22)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(set_global $function-expression/f32Adder
|
||||
(call $function-expression/makeAdder<f32>)
|
||||
)
|
||||
(if
|
||||
(f32.ne
|
||||
(call_indirect (type $fff)
|
||||
(f32.const 1.5)
|
||||
(f32.const 2.5)
|
||||
(get_global $function-expression/f32Adder)
|
||||
)
|
||||
(f32.const 4)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 25)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(set_global $function-expression/i8Adder
|
||||
(call $function-expression/makeAdderArrow<i8>)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call_indirect (type $iii)
|
||||
(i32.const 127)
|
||||
(i32.const 127)
|
||||
(get_global $function-expression/i8Adder)
|
||||
)
|
||||
(i32.const -2)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 34)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(set_global $function-expression/f1
|
||||
(get_global $function-expression/f2)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call_indirect (type $ii)
|
||||
(i32.const 4)
|
||||
(get_global $function-expression/f1)
|
||||
)
|
||||
(i32.const 4)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 37)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -11,27 +11,3 @@ assert(f2(2) == 2);
|
||||
var f3 = function someName(): void {
|
||||
};
|
||||
f3();
|
||||
|
||||
function makeAdder<T>(): (a: T, b: T) => T {
|
||||
return function theAdder(a: T, b: T): T {
|
||||
return a + b;
|
||||
};
|
||||
}
|
||||
|
||||
var i32Adder = makeAdder<i32>();
|
||||
assert(i32Adder(4, 2) == 6);
|
||||
|
||||
var f32Adder = makeAdder<f32>();
|
||||
assert(f32Adder(1.5, 2.5) == 4.0);
|
||||
|
||||
function makeAdderArrow<T>(): (a: T, b: T) => T {
|
||||
return (a: T, b: T): T => {
|
||||
return a + b;
|
||||
};
|
||||
}
|
||||
|
||||
var i8Adder = makeAdderArrow<i8>();
|
||||
assert(i8Adder(127, 127) == -2);
|
||||
|
||||
f1 = f2;
|
||||
assert(f1(4) == 4);
|
||||
|
@ -2,19 +2,13 @@
|
||||
(type $ii (func (param i32) (result i32)))
|
||||
(type $iiiiv (func (param i32 i32 i32 i32)))
|
||||
(type $v (func))
|
||||
(type $i (func (result i32)))
|
||||
(type $iii (func (param i32 i32) (result i32)))
|
||||
(type $fff (func (param f32 f32) (result f32)))
|
||||
(import "env" "abort" (func $abort (param i32 i32 i32 i32)))
|
||||
(global $function-expression/f1 (mut i32) (i32.const 0))
|
||||
(global $function-expression/f2 (mut i32) (i32.const 1))
|
||||
(global $function-expression/f3 (mut i32) (i32.const 2))
|
||||
(global $function-expression/i32Adder (mut i32) (i32.const 0))
|
||||
(global $function-expression/f32Adder (mut i32) (i32.const 0))
|
||||
(global $function-expression/i8Adder (mut i32) (i32.const 0))
|
||||
(global $HEAP_BASE i32 (i32.const 52))
|
||||
(table 6 6 anyfunc)
|
||||
(elem (i32.const 0) $start~anonymous|0 $start~anonymous|1 $start~someName|2 $function-expression/makeAdder<i32>~theAdder|3 $function-expression/makeAdder<f32>~theAdder|4 $function-expression/makeAdderArrow<i8>~anonymous|5)
|
||||
(table 3 3 anyfunc)
|
||||
(elem (i32.const 0) $start~anonymous|0 $start~anonymous|1 $start~someName|2)
|
||||
(memory $0 1)
|
||||
(data (i32.const 4) "\16\00\00\00f\00u\00n\00c\00t\00i\00o\00n\00-\00e\00x\00p\00r\00e\00s\00s\00i\00o\00n\00.\00t\00s\00")
|
||||
(export "memory" (memory $0))
|
||||
@ -31,52 +25,7 @@
|
||||
)
|
||||
(func $start~someName|2 (; 3 ;) (type $v)
|
||||
)
|
||||
(func $function-expression/makeAdder<i32>~theAdder|3 (; 4 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
|
||||
(return
|
||||
(i32.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $function-expression/makeAdder<i32> (; 5 ;) (type $i) (result i32)
|
||||
(return
|
||||
(i32.const 3)
|
||||
)
|
||||
)
|
||||
(func $function-expression/makeAdder<f32>~theAdder|4 (; 6 ;) (type $fff) (param $0 f32) (param $1 f32) (result f32)
|
||||
(return
|
||||
(f32.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $function-expression/makeAdder<f32> (; 7 ;) (type $i) (result i32)
|
||||
(return
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(func $function-expression/makeAdderArrow<i8>~anonymous|5 (; 8 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
|
||||
(return
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
(i32.const 24)
|
||||
)
|
||||
(i32.const 24)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $function-expression/makeAdderArrow<i8> (; 9 ;) (type $i) (result i32)
|
||||
(return
|
||||
(i32.const 5)
|
||||
)
|
||||
)
|
||||
(func $start (; 10 ;) (type $v)
|
||||
(func $start (; 4 ;) (type $v)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
@ -120,100 +69,5 @@
|
||||
(call_indirect (type $v)
|
||||
(get_global $function-expression/f3)
|
||||
)
|
||||
(set_global $function-expression/i32Adder
|
||||
(call $function-expression/makeAdder<i32>)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(call_indirect (type $iii)
|
||||
(i32.const 4)
|
||||
(i32.const 2)
|
||||
(get_global $function-expression/i32Adder)
|
||||
)
|
||||
(i32.const 6)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 22)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(set_global $function-expression/f32Adder
|
||||
(call $function-expression/makeAdder<f32>)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(f32.eq
|
||||
(call_indirect (type $fff)
|
||||
(f32.const 1.5)
|
||||
(f32.const 2.5)
|
||||
(get_global $function-expression/f32Adder)
|
||||
)
|
||||
(f32.const 4)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 25)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(set_global $function-expression/i8Adder
|
||||
(call $function-expression/makeAdderArrow<i8>)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(call_indirect (type $iii)
|
||||
(i32.const 127)
|
||||
(i32.const 127)
|
||||
(get_global $function-expression/i8Adder)
|
||||
)
|
||||
(i32.const -2)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 34)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(set_global $function-expression/f1
|
||||
(get_global $function-expression/f2)
|
||||
)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.eq
|
||||
(call_indirect (type $ii)
|
||||
(i32.const 4)
|
||||
(get_global $function-expression/f1)
|
||||
)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 37)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
66
tests/compiler/function-types.optimized.wat
Normal file
66
tests/compiler/function-types.optimized.wat
Normal file
@ -0,0 +1,66 @@
|
||||
(module
|
||||
(type $i (func (result i32)))
|
||||
(type $iii (func (param i32 i32) (result i32)))
|
||||
(type $III (func (param i64 i64) (result i64)))
|
||||
(type $FFF (func (param f64 f64) (result f64)))
|
||||
(type $v (func))
|
||||
(global $function-types/i32Adder (mut i32) (i32.const 0))
|
||||
(global $function-types/i64Adder (mut i32) (i32.const 0))
|
||||
(table 3 3 anyfunc)
|
||||
(elem (i32.const 0) $function-types/makeAdder<i32>~anonymous|0 $function-types/makeAdder<i64>~anonymous|1 $function-types/makeAdder<f64>~anonymous|2)
|
||||
(memory $0 1)
|
||||
(export "memory" (memory $0))
|
||||
(start $start)
|
||||
(func $function-types/makeAdder<i32>~anonymous|0 (; 0 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
|
||||
(i32.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
)
|
||||
(func $function-types/makeAdder<i32> (; 1 ;) (type $i) (result i32)
|
||||
(i32.const 0)
|
||||
)
|
||||
(func $function-types/makeAdder<i64>~anonymous|1 (; 2 ;) (type $III) (param $0 i64) (param $1 i64) (result i64)
|
||||
(i64.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
)
|
||||
(func $function-types/makeAdder<i64> (; 3 ;) (type $i) (result i32)
|
||||
(i32.const 1)
|
||||
)
|
||||
(func $function-types/makeAdder<f64>~anonymous|2 (; 4 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
|
||||
(f64.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
)
|
||||
(func $function-types/makeAdder<f64> (; 5 ;) (type $i) (result i32)
|
||||
(i32.const 2)
|
||||
)
|
||||
(func $start (; 6 ;) (type $v)
|
||||
(set_global $function-types/i32Adder
|
||||
(call $function-types/makeAdder<i32>)
|
||||
)
|
||||
(drop
|
||||
(call_indirect (type $iii)
|
||||
(i32.const 1)
|
||||
(i32.const 2)
|
||||
(get_global $function-types/i32Adder)
|
||||
)
|
||||
)
|
||||
(set_global $function-types/i64Adder
|
||||
(call $function-types/makeAdder<i64>)
|
||||
)
|
||||
(drop
|
||||
(call_indirect (type $III)
|
||||
(i64.const 1)
|
||||
(i64.const 2)
|
||||
(get_global $function-types/i64Adder)
|
||||
)
|
||||
)
|
||||
(drop
|
||||
(call $function-types/makeAdder<f64>)
|
||||
)
|
||||
)
|
||||
)
|
15
tests/compiler/function-types.ts
Normal file
15
tests/compiler/function-types.ts
Normal file
@ -0,0 +1,15 @@
|
||||
type Adder<T> = (a: T, b: T) => T;
|
||||
|
||||
function makeAdder<T>(): Adder<T> {
|
||||
return (a: T, b: T): T => {
|
||||
return a + b;
|
||||
};
|
||||
}
|
||||
|
||||
var i32Adder = makeAdder<i32>();
|
||||
i32Adder(1, 2);
|
||||
|
||||
var i64Adder = makeAdder<i64>();
|
||||
i64Adder(1, 2);
|
||||
|
||||
makeAdder<f64>()(1, 2);
|
88
tests/compiler/function-types.untouched.wat
Normal file
88
tests/compiler/function-types.untouched.wat
Normal file
@ -0,0 +1,88 @@
|
||||
(module
|
||||
(type $i (func (result i32)))
|
||||
(type $iii (func (param i32 i32) (result i32)))
|
||||
(type $III (func (param i64 i64) (result i64)))
|
||||
(type $FFF (func (param f64 f64) (result f64)))
|
||||
(type $v (func))
|
||||
(global $function-types/i32Adder (mut i32) (i32.const 0))
|
||||
(global $function-types/i64Adder (mut i32) (i32.const 0))
|
||||
(global $HEAP_BASE i32 (i32.const 4))
|
||||
(table 3 3 anyfunc)
|
||||
(elem (i32.const 0) $function-types/makeAdder<i32>~anonymous|0 $function-types/makeAdder<i64>~anonymous|1 $function-types/makeAdder<f64>~anonymous|2)
|
||||
(memory $0 1)
|
||||
(export "memory" (memory $0))
|
||||
(start $start)
|
||||
(func $function-types/makeAdder<i32>~anonymous|0 (; 0 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
|
||||
(return
|
||||
(i32.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $function-types/makeAdder<i32> (; 1 ;) (type $i) (result i32)
|
||||
(return
|
||||
(i32.const 0)
|
||||
)
|
||||
)
|
||||
(func $function-types/makeAdder<i64>~anonymous|1 (; 2 ;) (type $III) (param $0 i64) (param $1 i64) (result i64)
|
||||
(return
|
||||
(i64.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $function-types/makeAdder<i64> (; 3 ;) (type $i) (result i32)
|
||||
(return
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(func $function-types/makeAdder<f64>~anonymous|2 (; 4 ;) (type $FFF) (param $0 f64) (param $1 f64) (result f64)
|
||||
(return
|
||||
(f64.add
|
||||
(get_local $0)
|
||||
(get_local $1)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $function-types/makeAdder<f64> (; 5 ;) (type $i) (result i32)
|
||||
(return
|
||||
(i32.const 2)
|
||||
)
|
||||
)
|
||||
(func $start (; 6 ;) (type $v)
|
||||
(nop)
|
||||
(set_global $function-types/i32Adder
|
||||
(call $function-types/makeAdder<i32>)
|
||||
)
|
||||
(drop
|
||||
(call_indirect (type $iii)
|
||||
(i32.const 1)
|
||||
(i32.const 2)
|
||||
(get_global $function-types/i32Adder)
|
||||
)
|
||||
)
|
||||
(set_global $function-types/i64Adder
|
||||
(call $function-types/makeAdder<i64>)
|
||||
)
|
||||
(drop
|
||||
(call_indirect (type $III)
|
||||
(i64.const 1)
|
||||
(i64.const 2)
|
||||
(get_global $function-types/i64Adder)
|
||||
)
|
||||
)
|
||||
(drop
|
||||
(call $function-types/makeAdder<f64>)
|
||||
)
|
||||
(block
|
||||
(drop
|
||||
(i32.const 1)
|
||||
)
|
||||
(drop
|
||||
(i32.const 2)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
Reference in New Issue
Block a user