Skip to content

Commit 3b227d4

Browse files
jtennerdcodeIO
authored andcommitted
Support use of static methods as function references (AssemblyScript#721)
1 parent ed52a6f commit 3b227d4

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

src/compiler.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7989,9 +7989,26 @@ export class Compiler extends DiagnosticEmitter {
79897989
);
79907990
}
79917991
case ElementKind.FUNCTION_PROTOTYPE: {
7992+
let prototype = <FunctionPrototype>target;
7993+
7994+
if (prototype.is(CommonFlags.STATIC)) {
7995+
let instance = this.compileFunctionUsingTypeArguments(
7996+
prototype,
7997+
[],
7998+
makeMap<string,Type>(),
7999+
propertyAccess,
8000+
);
8001+
if (instance == null) {
8002+
return module.unreachable();
8003+
} else {
8004+
this.currentType = instance.type;
8005+
return module.i32(this.ensureFunctionTableEntry(instance));
8006+
}
8007+
}
8008+
79928009
this.error(
79938010
DiagnosticCode.Cannot_access_method_0_without_calling_it_as_it_requires_this_to_be_set,
7994-
propertyAccess.range, (<FunctionPrototype>target).name
8011+
propertyAccess.range, prototype.name
79958012
);
79968013
return module.unreachable();
79978014
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"asc_flags": [
3+
"--runtime none"
4+
]
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(module
2+
(type $FUNCSIG$i (func (result i32)))
3+
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
4+
(type $FUNCSIG$v (func))
5+
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
6+
(memory $0 1)
7+
(data (i32.const 8) "0\00\00\00\01\00\00\00\01\00\00\000\00\00\00c\00l\00a\00s\00s\00-\00s\00t\00a\00t\00i\00c\00-\00f\00u\00n\00c\00t\00i\00o\00n\00.\00t\00s")
8+
(table $0 2 funcref)
9+
(elem (i32.const 0) $null $class-static-function/Example.staticFunc)
10+
(global $~lib/argc (mut i32) (i32.const 0))
11+
(export "memory" (memory $0))
12+
(start $start)
13+
(func $class-static-function/Example.staticFunc (; 1 ;) (type $FUNCSIG$i) (result i32)
14+
i32.const 42
15+
)
16+
(func $start (; 2 ;) (type $FUNCSIG$v)
17+
i32.const 0
18+
global.set $~lib/argc
19+
call $class-static-function/Example.staticFunc
20+
i32.const 42
21+
i32.ne
22+
if
23+
i32.const 0
24+
i32.const 24
25+
i32.const 11
26+
i32.const 0
27+
call $~lib/builtins/abort
28+
unreachable
29+
end
30+
)
31+
(func $null (; 3 ;) (type $FUNCSIG$v)
32+
nop
33+
)
34+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Example {
2+
public static staticFunc(): i32 {
3+
return 42;
4+
}
5+
}
6+
7+
function call(func: () => i32): i32 {
8+
return func();
9+
}
10+
11+
assert(call(Example.staticFunc) == 42);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
(module
2+
(type $FUNCSIG$i (func (result i32)))
3+
(type $FUNCSIG$ii (func (param i32) (result i32)))
4+
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
5+
(type $FUNCSIG$v (func))
6+
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
7+
(memory $0 1)
8+
(data (i32.const 8) "0\00\00\00\01\00\00\00\01\00\00\000\00\00\00c\00l\00a\00s\00s\00-\00s\00t\00a\00t\00i\00c\00-\00f\00u\00n\00c\00t\00i\00o\00n\00.\00t\00s\00")
9+
(table $0 2 funcref)
10+
(elem (i32.const 0) $null $class-static-function/Example.staticFunc)
11+
(global $~lib/argc (mut i32) (i32.const 0))
12+
(export "memory" (memory $0))
13+
(start $start)
14+
(func $class-static-function/Example.staticFunc (; 1 ;) (type $FUNCSIG$i) (result i32)
15+
i32.const 42
16+
)
17+
(func $class-static-function/call (; 2 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
18+
i32.const 0
19+
global.set $~lib/argc
20+
local.get $0
21+
call_indirect (type $FUNCSIG$i)
22+
)
23+
(func $start:class-static-function (; 3 ;) (type $FUNCSIG$v)
24+
i32.const 1
25+
call $class-static-function/call
26+
i32.const 42
27+
i32.eq
28+
i32.eqz
29+
if
30+
i32.const 0
31+
i32.const 24
32+
i32.const 11
33+
i32.const 0
34+
call $~lib/builtins/abort
35+
unreachable
36+
end
37+
)
38+
(func $start (; 4 ;) (type $FUNCSIG$v)
39+
call $start:class-static-function
40+
)
41+
(func $null (; 5 ;) (type $FUNCSIG$v)
42+
)
43+
)

0 commit comments

Comments
 (0)