Skip to content

Commit 35e1c60

Browse files
committed
minor enhancement
1 parent 2ccc106 commit 35e1c60

10 files changed

+154
-23
lines changed

jscomp/core/js_exp_make.ml

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,9 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t =
827827

828828

829829
let int_equal = float_equal
830+
831+
832+
830833
let rec string_equal ?comment (e0 : t) (e1 : t) : t =
831834
match e0.expression_desc, e1.expression_desc with
832835
| Str (_, a0), Str(_, b0)
@@ -1280,7 +1283,7 @@ let of_block ?comment ?e block : t =
12801283
, Js_fun_env.empty 0)
12811284
} []
12821285

1283-
let is_nil ?comment x = triple_equal ?comment x nil
1286+
let is_null ?comment x = triple_equal ?comment x nil
12841287

12851288
let js_true : t = {comment = None; expression_desc = Bool true}
12861289
let js_false : t = {comment = None; expression_desc = Bool false}
@@ -1306,6 +1309,55 @@ let is_null_undefined ?comment (x: t) : t =
13061309
{ comment ;
13071310
expression_desc = Is_null_undefined_to_boolean x
13081311
}
1312+
1313+
let eq_null_undefined_boolean ?comment (a : t) (b : t) =
1314+
match a.expression_desc, b.expression_desc with
1315+
| Var (Id ({name = "null" | "undefined"} as id) ),
1316+
(Char_of_int _ | Char_to_int _
1317+
| Bool _ | Number _ | Typeof _ | Int_of_boolean _
1318+
| Fun _ | Array _ | Caml_block _ )
1319+
when Ext_ident.is_js id ->
1320+
caml_false
1321+
| (Char_of_int _ | Char_to_int _
1322+
| Bool _ | Number _ | Typeof _ | Int_of_boolean _
1323+
| Fun _ | Array _ | Caml_block _ ),
1324+
Var (Id ({name = "null" | "undefined"} as id) )
1325+
when Ext_ident.is_js id ->
1326+
caml_false
1327+
| Var (Id ({name = "null" | "undefined" as n1 } as id1) ),
1328+
Var (Id ({name = "null" | "undefined" as n2 } as id2) )
1329+
when Ext_ident.is_js id1 && Ext_ident.is_js id2
1330+
->
1331+
if n1 = n2 then caml_true else caml_false
1332+
| _ ->
1333+
bool_of_boolean {expression_desc = Bin(EqEqEq, a, b); comment}
1334+
1335+
1336+
1337+
let neq_null_undefined_boolean ?comment (a : t) (b : t) =
1338+
match a.expression_desc, b.expression_desc with
1339+
| Var (Id ({name = "null" | "undefined"} as id) ),
1340+
(Char_of_int _ | Char_to_int _
1341+
| Bool _ | Number _ | Typeof _ | Int_of_boolean _
1342+
| Fun _ | Array _ | Caml_block _ )
1343+
when Ext_ident.is_js id ->
1344+
caml_true
1345+
| (Char_of_int _ | Char_to_int _
1346+
| Bool _ | Number _ | Typeof _ | Int_of_boolean _
1347+
| Fun _ | Array _ | Caml_block _ ),
1348+
Var (Id ({name = "null" | "undefined"} as id) )
1349+
when Ext_ident.is_js id ->
1350+
caml_true
1351+
| Var (Id ({name = "null" | "undefined" as n1 } as id1) ),
1352+
Var (Id ({name = "null" | "undefined" as n2 } as id2) )
1353+
when Ext_ident.is_js id1 && Ext_ident.is_js id2
1354+
->
1355+
if n1 <> n2 then caml_true else caml_false
1356+
| _ ->
1357+
bool_of_boolean {expression_desc = Bin(NotEqEq, a, b); comment}
1358+
1359+
1360+
13091361
let not_implemented ?comment (s : string) : t =
13101362
runtime_call
13111363
Js_runtime_modules.missing_polyfill

jscomp/core/js_exp_make.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ val triple_equal : binary_op
208208
val float_equal : binary_op
209209
val int_equal : binary_op
210210
val string_equal : binary_op
211+
val eq_null_undefined_boolean: binary_op
212+
val neq_null_undefined_boolean: binary_op
211213
val is_type_number : unary_op
212214
val typeof : unary_op
213215

@@ -335,7 +337,7 @@ val of_block : ?comment:string -> ?e:J.expression -> J.statement list -> t
335337
val raw_js_code : ?comment:string -> J.code_info -> string -> t
336338

337339
val nil : t
338-
val is_nil : unary_op
340+
val is_null : unary_op
339341

340342
val js_bool : bool -> t
341343
val is_undef : unary_op

jscomp/core/lam_compile_primitive.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let translate loc
7676
| [e] ->
7777
begin match e.expression_desc with
7878
| Var _ ->
79-
E.econd (E.is_nil e) Js_of_lam_option.none (Js_of_lam_option.some e)
79+
E.econd (E.is_null e) Js_of_lam_option.none (Js_of_lam_option.some e)
8080
| _ ->
8181
E.runtime_call Js_runtime_modules.js_primitive
8282
"null_to_opt" args
@@ -151,7 +151,7 @@ let translate loc
151151

152152
| Pis_null ->
153153
begin match args with
154-
| [e] -> E.is_nil e
154+
| [e] -> E.is_null e
155155
| _ -> assert false
156156
end
157157
| Pis_undefined ->

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,21 +672,33 @@ let translate loc (prim_name : string)
672672
->
673673
call Js_runtime_modules.obj_runtime
674674

675+
| "caml_notequal" ->
676+
begin match args with
677+
| [a1;b1] when
678+
E.for_sure_js_null_undefined_boolean a1
679+
|| E.for_sure_js_null_undefined_boolean b1
680+
->
681+
E.neq_null_undefined_boolean a1 b1
682+
(* FIXME address_equal *)
683+
| _ ->
684+
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison ;
685+
call Js_runtime_modules.obj_runtime
686+
end
675687
| "caml_equal" ->
676688
begin match args with
677689
| [a1;b1] when
678690
E.for_sure_js_null_undefined_boolean a1 || E.for_sure_js_null_undefined_boolean b1
679691
->
680-
E.int_comp Ceq a1 b1
692+
E.eq_null_undefined_boolean a1 b1
681693
(* FIXME address_equal *)
682694
| _ ->
683695
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison ;
684696
call Js_runtime_modules.obj_runtime
685697
end
698+
686699
| "caml_min"
687700
| "caml_max"
688701
| "caml_compare"
689-
| "caml_notequal"
690702
| "caml_greaterequal"
691703
| "caml_greaterthan"
692704
| "caml_lessequal"

jscomp/runtime/caml_weak.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ let caml_weak_get_copy xs i =
4747
| Some x -> Some (Obj.magic (Obj.dup (Obj.repr x) ))
4848

4949
let caml_weak_check xs i =
50-
not (xs.(i) = Js.undefined)
50+
xs.(i) <> Js.undefined
5151

5252
let caml_weak_blit = Caml_array.caml_array_blit

jscomp/test/js_null_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var suites_001 = /* :: */[
6161
(function () {
6262
return /* Eq */Block.__(0, [
6363
/* true */1,
64-
+(null === null)
64+
/* true */1
6565
]);
6666
})
6767
],
@@ -71,7 +71,7 @@ var suites_001 = /* :: */[
7171
(function () {
7272
return /* Eq */Block.__(0, [
7373
/* false */0,
74-
+(/* () */0 === null)
74+
/* false */0
7575
]);
7676
})
7777
],

jscomp/test/js_null_undefined_test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
var Mt = require("./mt.js");
44
var Block = require("../../lib/js/block.js");
5-
var Caml_obj = require("../../lib/js/caml_obj.js");
65
var Js_primitive = require("../../lib/js/js_primitive.js");
76
var Js_null_undefined = require("../../lib/js/js_null_undefined.js");
87

@@ -228,21 +227,21 @@ var suites_001 = /* :: */[
228227
/* tuple */[
229228
"null <> undefined",
230229
(function () {
231-
return /* Ok */Block.__(4, [Caml_obj.caml_notequal(null, undefined)]);
230+
return /* Ok */Block.__(4, [/* true */1]);
232231
})
233232
],
234233
/* :: */[
235234
/* tuple */[
236235
"null <> empty",
237236
(function () {
238-
return /* Ok */Block.__(4, [Caml_obj.caml_notequal(null, undefined)]);
237+
return /* Ok */Block.__(4, [/* true */1]);
239238
})
240239
],
241240
/* :: */[
242241
/* tuple */[
243242
"undefined = empty",
244243
(function () {
245-
return /* Ok */Block.__(4, [+(undefined === undefined)]);
244+
return /* Ok */Block.__(4, [/* true */1]);
246245
})
247246
],
248247
/* :: */[

jscomp/test/js_undefined_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var suites_001 = /* :: */[
4141
(function () {
4242
return /* Eq */Block.__(0, [
4343
/* true */1,
44-
+(undefined === undefined)
44+
/* true */1
4545
]);
4646
})
4747
],
@@ -51,7 +51,7 @@ var suites_001 = /* :: */[
5151
(function () {
5252
return /* Eq */Block.__(0, [
5353
/* false */0,
54-
+(/* () */0 === undefined)
54+
/* false */0
5555
]);
5656
})
5757
],

jscomp/test/test_zero_nullable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function f10(x) {
9494
return +(x === null);
9595
}
9696

97-
var f11 = +(3 === null);
97+
var f11 = /* false */0;
9898

9999
var Test_null = /* module */[
100100
/* f1 */f1,
@@ -179,7 +179,7 @@ function f10$1(x) {
179179
return +(x === undefined);
180180
}
181181

182-
var f11$1 = +(3 === undefined);
182+
var f11$1 = /* false */0;
183183

184184
var Test_def = /* module */[
185185
/* f1 */f1$1,

lib/whole_compiler.ml

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71035,6 +71035,8 @@ val triple_equal : binary_op
7103571035
val float_equal : binary_op
7103671036
val int_equal : binary_op
7103771037
val string_equal : binary_op
71038+
val eq_null_undefined_boolean: binary_op
71039+
val neq_null_undefined_boolean: binary_op
7103871040
val is_type_number : unary_op
7103971041
val typeof : unary_op
7104071042

@@ -71162,7 +71164,7 @@ val of_block : ?comment:string -> ?e:J.expression -> J.statement list -> t
7116271164
val raw_js_code : ?comment:string -> J.code_info -> string -> t
7116371165

7116471166
val nil : t
71165-
val is_nil : unary_op
71167+
val is_null : unary_op
7116671168

7116771169
val js_bool : bool -> t
7116871170
val is_undef : unary_op
@@ -72001,6 +72003,9 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t =
7200172003

7200272004

7200372005
let int_equal = float_equal
72006+
72007+
72008+
7200472009
let rec string_equal ?comment (e0 : t) (e1 : t) : t =
7200572010
match e0.expression_desc, e1.expression_desc with
7200672011
| Str (_, a0), Str(_, b0)
@@ -72454,7 +72459,7 @@ let of_block ?comment ?e block : t =
7245472459
, Js_fun_env.empty 0)
7245572460
} []
7245672461

72457-
let is_nil ?comment x = triple_equal ?comment x nil
72462+
let is_null ?comment x = triple_equal ?comment x nil
7245872463

7245972464
let js_true : t = {comment = None; expression_desc = Bool true}
7246072465
let js_false : t = {comment = None; expression_desc = Bool false}
@@ -72480,6 +72485,55 @@ let is_null_undefined ?comment (x: t) : t =
7248072485
{ comment ;
7248172486
expression_desc = Is_null_undefined_to_boolean x
7248272487
}
72488+
72489+
let eq_null_undefined_boolean ?comment (a : t) (b : t) =
72490+
match a.expression_desc, b.expression_desc with
72491+
| Var (Id ({name = "null" | "undefined"} as id) ),
72492+
(Char_of_int _ | Char_to_int _
72493+
| Bool _ | Number _ | Typeof _ | Int_of_boolean _
72494+
| Fun _ | Array _ | Caml_block _ )
72495+
when Ext_ident.is_js id ->
72496+
caml_false
72497+
| (Char_of_int _ | Char_to_int _
72498+
| Bool _ | Number _ | Typeof _ | Int_of_boolean _
72499+
| Fun _ | Array _ | Caml_block _ ),
72500+
Var (Id ({name = "null" | "undefined"} as id) )
72501+
when Ext_ident.is_js id ->
72502+
caml_false
72503+
| Var (Id ({name = "null" | "undefined" as n1 } as id1) ),
72504+
Var (Id ({name = "null" | "undefined" as n2 } as id2) )
72505+
when Ext_ident.is_js id1 && Ext_ident.is_js id2
72506+
->
72507+
if n1 = n2 then caml_true else caml_false
72508+
| _ ->
72509+
bool_of_boolean {expression_desc = Bin(EqEqEq, a, b); comment}
72510+
72511+
72512+
72513+
let neq_null_undefined_boolean ?comment (a : t) (b : t) =
72514+
match a.expression_desc, b.expression_desc with
72515+
| Var (Id ({name = "null" | "undefined"} as id) ),
72516+
(Char_of_int _ | Char_to_int _
72517+
| Bool _ | Number _ | Typeof _ | Int_of_boolean _
72518+
| Fun _ | Array _ | Caml_block _ )
72519+
when Ext_ident.is_js id ->
72520+
caml_true
72521+
| (Char_of_int _ | Char_to_int _
72522+
| Bool _ | Number _ | Typeof _ | Int_of_boolean _
72523+
| Fun _ | Array _ | Caml_block _ ),
72524+
Var (Id ({name = "null" | "undefined"} as id) )
72525+
when Ext_ident.is_js id ->
72526+
caml_true
72527+
| Var (Id ({name = "null" | "undefined" as n1 } as id1) ),
72528+
Var (Id ({name = "null" | "undefined" as n2 } as id2) )
72529+
when Ext_ident.is_js id1 && Ext_ident.is_js id2
72530+
->
72531+
if n1 <> n2 then caml_true else caml_false
72532+
| _ ->
72533+
bool_of_boolean {expression_desc = Bin(NotEqEq, a, b); comment}
72534+
72535+
72536+
7248372537
let not_implemented ?comment (s : string) : t =
7248472538
runtime_call
7248572539
Js_runtime_modules.missing_polyfill
@@ -95336,21 +95390,33 @@ let translate loc (prim_name : string)
9533695390
->
9533795391
call Js_runtime_modules.obj_runtime
9533895392

95393+
| "caml_notequal" ->
95394+
begin match args with
95395+
| [a1;b1] when
95396+
E.for_sure_js_null_undefined_boolean a1
95397+
|| E.for_sure_js_null_undefined_boolean b1
95398+
->
95399+
E.neq_null_undefined_boolean a1 b1
95400+
(* FIXME address_equal *)
95401+
| _ ->
95402+
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison ;
95403+
call Js_runtime_modules.obj_runtime
95404+
end
9533995405
| "caml_equal" ->
9534095406
begin match args with
9534195407
| [a1;b1] when
9534295408
E.for_sure_js_null_undefined_boolean a1 || E.for_sure_js_null_undefined_boolean b1
9534395409
->
95344-
E.int_comp Ceq a1 b1
95410+
E.eq_null_undefined_boolean a1 b1
9534595411
(* FIXME address_equal *)
9534695412
| _ ->
9534795413
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison ;
9534895414
call Js_runtime_modules.obj_runtime
9534995415
end
95416+
9535095417
| "caml_min"
9535195418
| "caml_max"
9535295419
| "caml_compare"
95353-
| "caml_notequal"
9535495420
| "caml_greaterequal"
9535595421
| "caml_greaterthan"
9535695422
| "caml_lessequal"
@@ -95613,7 +95679,7 @@ let translate loc
9561395679
| [e] ->
9561495680
begin match e.expression_desc with
9561595681
| Var _ ->
95616-
E.econd (E.is_nil e) Js_of_lam_option.none (Js_of_lam_option.some e)
95682+
E.econd (E.is_null e) Js_of_lam_option.none (Js_of_lam_option.some e)
9561795683
| _ ->
9561895684
E.runtime_call Js_runtime_modules.js_primitive
9561995685
"null_to_opt" args
@@ -95688,7 +95754,7 @@ let translate loc
9568895754

9568995755
| Pis_null ->
9569095756
begin match args with
95691-
| [e] -> E.is_nil e
95757+
| [e] -> E.is_null e
9569295758
| _ -> assert false
9569395759
end
9569495760
| Pis_undefined ->

0 commit comments

Comments
 (0)