@@ -122,27 +122,21 @@ function assert_exhaustion(action) {
122
122
123
123
function assert_return(action, expected) {
124
124
let actual = action() ;
125
- if (! Object. is(actual, expected)) {
126
- throw new Error (" Wasm return value " + expected + " expected, got " + actual);
127
- };
128
- }
129
-
130
- function assert_return_canonical_nan(action) {
131
- let actual = action() ;
132
- // Note that JS can't reliably distinguish different NaN values,
133
- // so there's no good way to test that it's a canonical NaN.
134
- if (! Number. isNaN(actual)) {
135
- throw new Error (" Wasm return value NaN expected, got " + actual);
136
- };
137
- }
138
-
139
- function assert_return_arithmetic_nan(action) {
140
- // Note that JS can't reliably distinguish different NaN values,
141
- // so there's no good way to test for specific bitpatterns here.
142
- let actual = action() ;
143
- if (! Number. isNaN(actual)) {
144
- throw new Error (" Wasm return value NaN expected, got " + actual);
145
- };
125
+ switch (expected) {
126
+ case " nan:canonical" :
127
+ case " nan:arithmetic" :
128
+ case " nan:any" :
129
+ // Note that JS can't reliably distinguish different NaN values,
130
+ // so there's no good way to test that it's a canonical NaN.
131
+ if (! Number. isNaN(actual)) {
132
+ throw new Error (" Wasm return value NaN expected, got " + actual);
133
+ };
134
+ return;
135
+ default :
136
+ if (! Object .is (actual , expected )) {
137
+ throw new Error (" Wasm return value " + expected + " expected, got " + actual);
138
+ };
139
+ }
146
140
}
147
141
| }
148
142
@@ -220,36 +214,38 @@ let get t at =
220
214
let run ts at =
221
215
[] , []
222
216
223
- let assert_return lits ts at =
224
- let test lit =
225
- let t', reinterpret = reinterpret_of (Values. type_of lit.it) in
226
- [ reinterpret @@ at;
227
- Const lit @@ at;
228
- reinterpret @@ at;
229
- Compare (eq_of t') @@ at;
230
- Test (Values. I32 I32Op. Eqz ) @@ at;
231
- BrIf (0l @@ at) @@ at ]
232
- in [] , List. flatten (List. rev_map test lits)
233
-
234
- let assert_return_nan_bitpattern nan_bitmask_of ts at =
235
- let test t =
236
- let t', reinterpret = reinterpret_of t in
237
- [ reinterpret @@ at;
238
- Const (nan_bitmask_of t' @@ at) @@ at;
239
- Binary (and_of t') @@ at;
240
- Const (canonical_nan_of t' @@ at) @@ at;
241
- Compare (eq_of t') @@ at;
242
- Test (Values. I32 I32Op. Eqz ) @@ at;
243
- BrIf (0l @@ at) @@ at ]
244
- in [] , List. flatten (List. rev_map test ts)
245
-
246
- let assert_return_canonical_nan =
247
- (* The result may only differ from the canonical NaN in its sign bit *)
248
- assert_return_nan_bitpattern abs_mask_of
249
-
250
- let assert_return_arithmetic_nan =
251
- (* The result can be any NaN that's one everywhere the canonical NaN is one *)
252
- assert_return_nan_bitpattern canonical_nan_of
217
+ let assert_return ress ts at =
218
+ let test res =
219
+ match res.it with
220
+ | LitResult lit ->
221
+ let t', reinterpret = reinterpret_of (Values. type_of lit.it) in
222
+ [ reinterpret @@ at;
223
+ Const lit @@ at;
224
+ reinterpret @@ at;
225
+ Compare (eq_of t') @@ at;
226
+ Test (Values. I32 I32Op. Eqz ) @@ at;
227
+ BrIf (0l @@ at) @@ at ]
228
+ | NanResult nanop ->
229
+ let nan =
230
+ match nanop.it with
231
+ | Values. I32 _ | Values. I64 _ -> assert false
232
+ | Values. F32 n | Values. F64 n -> n
233
+ in
234
+ let nan_bitmask_of =
235
+ match nan with
236
+ | CanonicalNan -> abs_mask_of (* must only differ from the canonical NaN in its sign bit *)
237
+ | ArithmeticNan -> canonical_nan_of (* can be any NaN that's one everywhere the canonical NaN is one *)
238
+ in
239
+ let t = Values. type_of nanop.it in
240
+ let t', reinterpret = reinterpret_of t in
241
+ [ reinterpret @@ at;
242
+ Const (nan_bitmask_of t' @@ at) @@ at;
243
+ Binary (and_of t') @@ at;
244
+ Const (canonical_nan_of t' @@ at) @@ at;
245
+ Compare (eq_of t') @@ at;
246
+ Test (Values. I32 I32Op. Eqz ) @@ at;
247
+ BrIf (0l @@ at) @@ at ]
248
+ in [] , List. flatten (List. rev_map test ress)
253
249
254
250
let wrap module_name item_name wrap_action wrap_assertion at =
255
251
let itypes, idesc, action = wrap_action at in
@@ -321,6 +317,18 @@ let of_literal lit =
321
317
| Values. F32 z -> of_float (F32. to_float z)
322
318
| Values. F64 z -> of_float (F64. to_float z)
323
319
320
+ let of_nan = function
321
+ | CanonicalNan -> " nan:canonical"
322
+ | ArithmeticNan -> " nan:arithmetic"
323
+
324
+ let of_result res =
325
+ match res.it with
326
+ | LitResult lit -> of_literal lit
327
+ | NanResult nanop ->
328
+ match nanop.it with
329
+ | Values. I32 _ | Values. I64 _ -> assert false
330
+ | Values. F32 n | Values. F64 n -> of_nan n
331
+
324
332
let rec of_definition def =
325
333
match def.it with
326
334
| Textual m -> of_bytes (Encode. encode m)
@@ -377,13 +385,9 @@ let of_assertion mods ass =
377
385
" assert_unlinkable(" ^ of_definition def ^ " );"
378
386
| AssertUninstantiable (def , _ ) ->
379
387
" assert_uninstantiable(" ^ of_definition def ^ " );"
380
- | AssertReturn (act , lits ) ->
381
- of_assertion' mods act " assert_return" (List. map of_literal lits)
382
- (Some (assert_return lits))
383
- | AssertReturnCanonicalNaN act ->
384
- of_assertion' mods act " assert_return_canonical_nan" [] (Some assert_return_canonical_nan)
385
- | AssertReturnArithmeticNaN act ->
386
- of_assertion' mods act " assert_return_arithmetic_nan" [] (Some assert_return_arithmetic_nan)
388
+ | AssertReturn (act , ress ) ->
389
+ of_assertion' mods act " assert_return" (List. map of_result ress)
390
+ (Some (assert_return ress))
387
391
| AssertTrap (act , _ ) ->
388
392
of_assertion' mods act " assert_trap" [] None
389
393
| AssertExhaustion (act , _ ) ->
0 commit comments