@@ -159,7 +159,7 @@ let%expect_test "static eval of string get" =
159
159
}
160
160
// end | }]
161
161
162
- let % expect_test " static eval of tags" =
162
+ let % expect_test " static eval of tags (optimized switch) " =
163
163
let program =
164
164
compile_and_parse
165
165
{|
@@ -191,11 +191,54 @@ let%expect_test "static eval of tags" =
191
191
}
192
192
var
193
193
global_data = runtime.caml_get_global_data() ,
194
- Stdlib_Random = global_data.Stdlib__Random ,
195
- _a_ = [0 , [1 , 0 ]],
196
- _b_ = [1 , 0 ],
197
- x = 1 < caml_call1(Stdlib_Random [5 ], 3 ) ? _a_ : _b_ ;
198
- x[0 ];
194
+ Stdlib_Random = global_data.Stdlib__Random ;
195
+ 1 < caml_call1(Stdlib_Random [5 ], 3 );
196
+ var
197
+ foobar = 3 ,
198
+ export$ 0 = [0 , foobar, foobar],
199
+ Test = [0 , foobar, export$ 0 ];
200
+ runtime.caml_register_global(3 , Test , " Test" );
201
+ return;
202
+ }
203
+ (globalThis));
204
+ // end
205
+ | }]
206
+
207
+ let % expect_test " static eval of tags" =
208
+ let program =
209
+ compile_and_parse
210
+ {|
211
+
212
+ type t = A | B | C of t | D of t | E of t | F of t
213
+
214
+ let foobar =
215
+ let x = if Random. int 3 > 1 then C (D A ) else D (A ) in
216
+ match x with
217
+ | A -> 1
218
+ | B -> 2
219
+ | C _
220
+ | D _ -> 3
221
+ | E _ -> 5
222
+ | F _ -> 7
223
+
224
+ let export = [|foobar;foobar|]
225
+ | }
226
+ in
227
+ print_program program;
228
+ [% expect
229
+ {|
230
+ (function(globalThis){
231
+ " use strict" ;
232
+ var runtime = globalThis.jsoo_runtime;
233
+ function caml_call1(f, a0){
234
+ return (f.l > = 0 ? f.l : f.l = f.length ) === 1
235
+ ? f(a0)
236
+ : runtime.caml_call_gen(f, [a0]);
237
+ }
238
+ var
239
+ global_data = runtime.caml_get_global_data() ,
240
+ Stdlib_Random = global_data.Stdlib__Random ;
241
+ 1 < caml_call1(Stdlib_Random [5 ], 3 );
199
242
var
200
243
foobar = 3 ,
201
244
export$ 0 = [0 , foobar, foobar],
@@ -206,3 +249,69 @@ let%expect_test "static eval of tags" =
206
249
(globalThis));
207
250
// end
208
251
| }]
252
+
253
+ let % expect_test " static eval int prims" =
254
+ let program =
255
+ compile_and_parse
256
+ {|
257
+
258
+ let lt =
259
+ let x = if Random. int 3 > 1 then 1 else 2 in
260
+ x < 5
261
+
262
+ let le =
263
+ let x = if Random. int 3 > 1 then 1 else 2 in
264
+ x < = 5
265
+
266
+ let eq =
267
+ let x = if Random. int 3 > 1 then 1 else 2 in
268
+ x = 3
269
+
270
+ let neq =
271
+ let x = if Random. int 3 > 1 then 1 else 2 in
272
+ x <> 3
273
+
274
+ type ult = A | B | C | D
275
+
276
+ let ult =
277
+ let x = if Random. int 3 > 1 then A else D in
278
+ match x with
279
+ | A | D -> true
280
+ | B | C -> false
281
+
282
+ let export = [| lt; le; eq; neq; ult |]
283
+ | }
284
+ in
285
+ print_program program;
286
+ [% expect
287
+ {|
288
+ (function(globalThis){
289
+ " use strict" ;
290
+ var runtime = globalThis.jsoo_runtime;
291
+ function caml_call1(f, a0){
292
+ return (f.l > = 0 ? f.l : f.l = f.length ) === 1
293
+ ? f(a0)
294
+ : runtime.caml_call_gen(f, [a0]);
295
+ }
296
+ var
297
+ global_data = runtime.caml_get_global_data() ,
298
+ Stdlib_Random = global_data.Stdlib__Random ;
299
+ 1 < caml_call1(Stdlib_Random [5 ], 3 );
300
+ var lt = 1 ;
301
+ 1 < caml_call1(Stdlib_Random [5 ], 3 );
302
+ var le = 1 ;
303
+ 1 < caml_call1(Stdlib_Random [5 ], 3 );
304
+ var eq = 0 ;
305
+ 1 < caml_call1(Stdlib_Random [5 ], 3 );
306
+ var neq = 1 ;
307
+ 1 < caml_call1(Stdlib_Random [5 ], 3 );
308
+ var
309
+ ult = 1 ,
310
+ export$ 0 = [0 , lt, le, eq, neq, ult],
311
+ Test = [0 , lt, le, eq, neq, ult, export$ 0 ];
312
+ runtime.caml_register_global(1 , Test , " Test" );
313
+ return;
314
+ }
315
+ (globalThis));
316
+ // end
317
+ | }]
0 commit comments