Skip to content

Commit 3361bbf

Browse files
authored
LegalizeAndPruneJSInterface: Handle non-nullable results (#7944)
When we turn an import into a non-import, we fill in a body. The body can be a null, normally, but for non-nullable things we must trap.
1 parent a6e2620 commit 3361bbf

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/passes/LegalizeJSInterface.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,12 @@ struct LegalizeAndPruneJSInterface : public LegalizeJSInterface {
391391
Builder builder(*module);
392392
if (sig.results == Type::none) {
393393
func->body = builder.makeNop();
394-
} else {
394+
} else if (sig.results.isDefaultable()) {
395395
func->body =
396396
builder.makeConstantExpression(Literal::makeZeros(sig.results));
397+
} else {
398+
// We have nothing better than to trap here.
399+
func->body = builder.makeUnreachable();
397400
}
398401
}
399402

test/lit/passes/legalize-and-prune-js-interface.wast

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
(import "env" "imported-v128-param-noresult" (func $imported-v128-param-noresult (param v128)))
1616

17+
;; The results here include a nullable value, which we will emit a null for.
18+
(import "env" "imported-v128-defaultable" (func $imported-v128-defaultable (result v128 anyref)))
19+
20+
;; The results here include a non-nullable value, which will force us to emit
21+
;; an unreachable.
22+
(import "env" "imported-v128-nondefaultable" (func $imported-v128-nondefaultable (result v128 (ref any))))
23+
1724
;; CHECK: (type $0 (func (result v128)))
1825

1926
;; CHECK: (type $1 (func (result i32 f64)))
@@ -22,17 +29,21 @@
2229

2330
;; CHECK: (type $3 (func (param v128)))
2431

25-
;; CHECK: (type $4 (func))
32+
;; CHECK: (type $4 (func (result v128 anyref)))
33+
34+
;; CHECK: (type $5 (func (result v128 (ref any))))
2635

27-
;; CHECK: (type $5 (func (result i32)))
36+
;; CHECK: (type $6 (func))
2837

29-
;; CHECK: (type $6 (func (param i32 f64) (result i64)))
38+
;; CHECK: (type $7 (func (result i32)))
3039

31-
;; CHECK: (type $7 (func (param i32 f64) (result i32)))
40+
;; CHECK: (type $8 (func (param i32 f64) (result i64)))
3241

33-
;; CHECK: (import "env" "getTempRet0" (func $getTempRet0 (type $5) (result i32)))
42+
;; CHECK: (type $9 (func (param i32 f64) (result i32)))
3443

35-
;; CHECK: (import "env" "imported-64" (func $legalimport$imported-64 (type $7) (param i32 f64) (result i32)))
44+
;; CHECK: (import "env" "getTempRet0" (func $getTempRet0 (type $7) (result i32)))
45+
46+
;; CHECK: (import "env" "imported-64" (func $legalimport$imported-64 (type $9) (param i32 f64) (result i32)))
3647

3748
;; CHECK: (func $imported-v128 (type $0) (result v128)
3849
;; CHECK-NEXT: (v128.const i32x4 0x00000000 0x00000000 0x00000000 0x00000000)
@@ -53,7 +64,18 @@
5364
;; CHECK-NEXT: (nop)
5465
;; CHECK-NEXT: )
5566

56-
;; CHECK: (func $call-64 (type $4)
67+
;; CHECK: (func $imported-v128-defaultable (type $4) (result v128 anyref)
68+
;; CHECK-NEXT: (tuple.make 2
69+
;; CHECK-NEXT: (v128.const i32x4 0x00000000 0x00000000 0x00000000 0x00000000)
70+
;; CHECK-NEXT: (ref.null none)
71+
;; CHECK-NEXT: )
72+
;; CHECK-NEXT: )
73+
74+
;; CHECK: (func $imported-v128-nondefaultable (type $5) (result v128 (ref any))
75+
;; CHECK-NEXT: (unreachable)
76+
;; CHECK-NEXT: )
77+
78+
;; CHECK: (func $call-64 (type $6)
5779
;; CHECK-NEXT: (drop
5880
;; CHECK-NEXT: (call $legalfunc$imported-64
5981
;; CHECK-NEXT: (i32.const 0)
@@ -103,7 +125,7 @@
103125
)
104126
)
105127

106-
;; CHECK: (func $legalfunc$imported-64 (type $6) (param $0 i32) (param $1 f64) (result i64)
128+
;; CHECK: (func $legalfunc$imported-64 (type $8) (param $0 i32) (param $1 f64) (result i64)
107129
;; CHECK-NEXT: (i64.or
108130
;; CHECK-NEXT: (i64.extend_i32_u
109131
;; CHECK-NEXT: (call $legalimport$imported-64

0 commit comments

Comments
 (0)