Skip to content

Commit 4f4dc7a

Browse files
jayphelpsdcodeIO
authored andcommitted
Support scoped anyref variable declarations (#958)
1 parent 6fbc639 commit 4f4dc7a

File tree

6 files changed

+28
-2
lines changed

6 files changed

+28
-2
lines changed

NOTICE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ under the licensing terms detailed in LICENSE:
1717
* Bowen Wang <[email protected]>
1818
* Emil Laine <[email protected]>
1919
* Stephen Paul Weber <[email protected]>
20+
* Jay Phelps <[email protected]>
2021

2122
Portions of this software are derived from third-party works licensed under
2223
the following terms:

src/flow.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ export class Flow {
324324
case NativeType.F32: { temps = parentFunction.tempF32s; break; }
325325
case NativeType.F64: { temps = parentFunction.tempF64s; break; }
326326
case NativeType.V128: { temps = parentFunction.tempV128s; break; }
327+
case NativeType.Anyref: { temps = parentFunction.tempAnyrefs; break; }
327328
default: throw new Error("concrete type expected");
328329
}
329330
var local: Local;
@@ -395,6 +396,10 @@ export class Flow {
395396
temps = parentFunction.tempV128s || (parentFunction.tempV128s = []);
396397
break;
397398
}
399+
case NativeType.Anyref: {
400+
temps = parentFunction.tempAnyrefs || (parentFunction.tempAnyrefs = []);
401+
break;
402+
}
398403
default: throw new Error("concrete type expected");
399404
}
400405
assert(local.index >= 0);

src/module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,10 @@ export class Module {
14231423
// TODO
14241424
return 0;
14251425
}
1426+
// Not possible to clone an anyref as it is opaque
1427+
case NativeType.Anyref: {
1428+
return 0;
1429+
}
14261430
default: {
14271431
throw new Error("concrete type expected");
14281432
}

src/program.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,7 @@ export class Function extends TypedElement {
29002900
tempF32s: Local[] | null = null;
29012901
tempF64s: Local[] | null = null;
29022902
tempV128s: Local[] | null = null;
2903+
tempAnyrefs: Local[] | null = null;
29032904

29042905
// used by flows to keep track of break labels
29052906
nextBreakId: i32 = 0;

tests/compiler/features/reference-types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
export declare function external(a: anyref): anyref;
44

55
export function internal(a: anyref): anyref {
6-
return a;
6+
const b = external(a);
7+
let c = external(b);
8+
var d = external(c);
9+
return d;
710
}
811

912
// can use reflection to work with anyref values

tests/compiler/features/reference-types.untouched.wat

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
if
3131
i32.const 0
3232
i32.const 24
33-
i32.const 16
33+
i32.const 19
3434
i32.const 0
3535
call $~lib/builtins/abort
3636
unreachable
@@ -45,7 +45,19 @@
4545
call $~lib/bindings/console/log
4646
)
4747
(func $features/reference-types/internal (; 6 ;) (type $FUNCSIG$aa) (param $0 anyref) (result anyref)
48+
(local $1 anyref)
49+
(local $2 anyref)
50+
(local $3 anyref)
4851
local.get $0
52+
call $features/reference-types/external
53+
local.set $1
54+
local.get $1
55+
call $features/reference-types/external
56+
local.set $2
57+
local.get $2
58+
call $features/reference-types/external
59+
local.set $3
60+
local.get $3
4961
)
5062
(func $start (; 7 ;) (type $FUNCSIG$v)
5163
call $start:features/reference-types

0 commit comments

Comments
 (0)