Skip to content

Commit a57c01a

Browse files
authored
Merge pull request #2572 from BuckleScript/remove_is_nil_undefined_special_handling
remove #is_nil, #is_undef
2 parents 68af95d + 35e1c60 commit a57c01a

22 files changed

+203
-67
lines changed

jscomp/core/js_exp_make.ml

Lines changed: 56 additions & 5 deletions
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,12 +1283,11 @@ 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

1285-
let js_bool ?comment x : t =
1286-
{ comment;
1287-
expression_desc = Bool x
1288-
}
1288+
let js_true : t = {comment = None; expression_desc = Bool true}
1289+
let js_false : t = {comment = None; expression_desc = Bool false}
1290+
let js_bool x : t = if x then js_true else js_false
12891291

12901292
let is_undef ?comment x = triple_equal ?comment x undefined
12911293

@@ -1307,6 +1309,55 @@ let is_null_undefined ?comment (x: t) : t =
13071309
{ comment ;
13081310
expression_desc = Is_null_undefined_to_boolean x
13091311
}
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+
13101361
let not_implemented ?comment (s : string) : t =
13111362
runtime_call
13121363
Js_runtime_modules.missing_polyfill

jscomp/core/js_exp_make.mli

Lines changed: 5 additions & 7 deletions
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

@@ -253,7 +255,7 @@ val flat_call : binary_op
253255

254256
val dump : ?comment:string -> Js_op.level -> t list -> t
255257

256-
(* val anything_to_string : unary_op *)
258+
257259

258260
(** see {!https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus}*)
259261
(* val to_number : unary_op *)
@@ -309,9 +311,6 @@ val undefined : t
309311
val is_caml_block : ?comment:string -> t -> t
310312

311313

312-
313-
314-
315314
val tag : ?comment:string -> J.expression -> t
316315
val set_tag : ?comment:string -> J.expression -> J.expression -> t
317316

@@ -328,7 +327,6 @@ val and_ : binary_op
328327
val or_ : binary_op
329328

330329
(** we don't expose a general interface, since a general interface is generally not safe *)
331-
(* val is_instance_array : unary_op *)
332330

333331
(** used combined with [caml_update_dummy]*)
334332
val dummy_obj : ?comment:string -> unit -> t
@@ -339,9 +337,9 @@ val of_block : ?comment:string -> ?e:J.expression -> J.statement list -> t
339337
val raw_js_code : ?comment:string -> J.code_info -> string -> t
340338

341339
val nil : t
342-
val is_nil : unary_op
340+
val is_null : unary_op
343341

344-
val js_bool : ?comment:string -> bool -> t
342+
val js_bool : bool -> t
345343
val is_undef : unary_op
346344
val for_sure_js_null_undefined_boolean : J.expression -> bool
347345
val is_null_undefined : unary_op

jscomp/core/lam.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,12 +1862,10 @@ let convert exports lam : _ * _ =
18621862
| "#undefined_to_opt" -> Pundefined_to_opt
18631863
| "#null_undefined_to_opt" -> Pnull_undefined_to_opt
18641864
| "#null_to_opt" -> Pnull_to_opt
1865-
| "#is_nil" -> Pis_null
1866-
| "#is_undef" -> Pis_undefined
18671865
| "#is_nil_undef" -> Pis_null_undefined
18681866
| "#string_append" -> Pstringadd
18691867

1870-
(* | "#is_instance_array" -> Pjs_is_instance_array *)
1868+
18711869
| "#string_of_small_int_array" -> Pjs_string_of_small_array
18721870
(* {[String.fromCharCode.apply(null,x)]}
18731871
Note if we have better suport [@bs.splice],

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/others/belt_MutableQueue.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ let pop q =
7979
| None -> None
8080
| Some x ->
8181
let next = next x in
82-
if Js.Null.test next then
82+
if next = Js.null then
8383
begin (* only one element*)
8484
clear q;
8585
Some (content x)
@@ -95,7 +95,7 @@ let popExn q =
9595
| None -> [%assert "Empty"]
9696
| Some x ->
9797
let next = next x in
98-
if Js.Null.test next then
98+
if next = Js.null then
9999
begin (* only one element*)
100100
clear q;
101101
content x
@@ -111,7 +111,7 @@ let popUndefined q =
111111
| None -> Js.undefined
112112
| Some x ->
113113
let next = next x in
114-
if Js.Null.test next then
114+
if next = Js.null then
115115
begin (* only one element*)
116116
clear q;
117117
Js.Undefined.return (content x)

jscomp/others/belt_MutableStack.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ let top s =
5050
| None -> None
5151
| Some x -> Some (head x)
5252

53-
let isEmpty s = Js.Null.test (root s)
53+
let isEmpty s = root s = Js.null
5454

5555
let popUndefined s =
5656
match Js.nullToOption (root s) with

jscomp/runtime/.depend

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ caml_bytes.cmj : caml_bytes.cmi
44
caml_obj.cmj : js_null.cmj js.cmj caml_array.cmj bs_obj.cmj caml_obj.cmi
55
caml_int64.cmj : js_typed_array.cmj js_float.cmj js.cmj caml_utils.cmj \
66
caml_int32.cmj bs_string.cmj caml_int64.cmi
7-
caml_exceptions.cmj : caml_builtin_exceptions.cmj caml_exceptions.cmi
7+
caml_exceptions.cmj : js.cmj caml_builtin_exceptions.cmj caml_exceptions.cmi
88
caml_utils.cmj : caml_utils.cmi
99
caml_sys.cmj : js.cmj caml_sys.cmi
1010
caml_io.cmj : js_undefined.cmj js.cmj bs_string.cmj

jscomp/runtime/caml_exceptions.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ let create (str : string) : Caml_builtin_exceptions.exception_block =
6767
(* Obj.set_tag (Obj.repr v) object_tag; *)
6868
(* v *)
6969

70-
external isUndefined : 'a -> bool = "#is_undef"
7170

7271

7372
(**
@@ -102,12 +101,12 @@ external isUndefined : 'a -> bool = "#is_undef"
102101
This is not a problem in `try .. with` since the logic above is not expressible, see more design in [destruct_exn.md]
103102
*)
104103
let isCamlExceptionOrOpenVariant e =
105-
if isUndefined e then false
104+
if Obj.magic e = Js.undefined then false
106105
else
107106
Obj.tag (Obj.repr e) = object_tag (* nullary exception *)
108107
||
109108
let slot = Obj.field (Obj.repr e) 0 in
110-
not (isUndefined slot) &&
109+
not (Obj.magic slot = Js.undefined) &&
111110
(Obj.tag slot = object_tag)
112111

113112

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 @@ Js_undefined.test xs.(i)
50+
xs.(i) <> Js.undefined
5151

5252
let caml_weak_blit = Caml_array.caml_array_blit

0 commit comments

Comments
 (0)