Skip to content

Commit 97aef33

Browse files
committed
[refact & fix] fix null_def case, add a regression test and clean up to-do lists (#344)
1 parent 83e7273 commit 97aef33

15 files changed

+258
-149
lines changed

jscomp/ext_ident.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929

3030

3131

32-
let js_flag = 8 (* check with ocaml compiler *)
32+
let js_flag = 0b1000 (* check with ocaml compiler *)
3333

34-
let js_module_flag = 16 (* javascript external modules *)
34+
let js_module_flag = 0b1_0000 (* javascript external modules *)
3535
(* TODO:
3636
check name conflicts with javascript conventions
3737
{[
3838
Ext_ident.convert "^";;
3939
- : string = "$caret"
4040
]}
4141
*)
42-
let js_object_flag = 32 (* javascript object flags *)
42+
let js_object_flag = 0b10_0000 (* javascript object flags *)
4343

4444
let is_js (i : Ident.t) =
4545
i.flags land js_flag <> 0

jscomp/ident_map.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@
3333

3434
include Map.Make(struct
3535
type t = Ident.t
36-
let compare = Pervasives.compare (**TODO: fix me*)
36+
37+
let compare (x : t) (y : t) =
38+
(* Can not overflow *)
39+
let u = x.stamp - y.stamp in
40+
if u = 0 then
41+
let u = String.compare x.name y.name in
42+
if u = 0 then
43+
x.flags - y.flags
44+
else u
45+
else u
3746
end)
3847

3948
let of_list lst =

jscomp/ident_map.mli

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
include Map.S with type key = Ident.t
26+
27+
val of_list : (key * 'a) list -> 'a t
28+
29+
val keys : 'a t -> key list
30+
31+
val add_if_not_exist : key -> 'a -> 'a t -> 'a t
32+
33+
val merge_disjoint : 'a t -> 'a t -> 'a t

jscomp/lam_comb.ml

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828

2929
type t = Lambda.lambda
30-
30+
type binop = t -> t -> t
31+
type triop = t -> t -> t -> t
32+
type unop = t -> t
3133
let if_ a (b : t) c =
3234
match a with
3335
| Lambda.Lconst v ->
@@ -65,3 +67,56 @@ let stringswitch lam cases default =
6567
end
6668
end
6769
| _ -> Lambda.Lstringswitch(lam, cases, default)
70+
71+
72+
let true_ : Lambda.lambda =
73+
Lconst (Const_pointer ( 1, Pt_constructor "true"))
74+
75+
let false_ : Lambda.lambda =
76+
Lconst (Const_pointer( 0, Pt_constructor "false"))
77+
78+
let unit : Lambda.lambda =
79+
Lconst (Const_pointer( 0, Pt_constructor "()"))
80+
81+
let not x : t =
82+
Lambda.Lprim (Pnot, [x])
83+
84+
(** [l || r ] *)
85+
let sequor l r = if_ l true_ r
86+
87+
(** [l && r ] *)
88+
let sequand l r = if_ l r false_
89+
90+
module Prim = struct
91+
type t = Lambda.primitive
92+
let js_is_nil : t =
93+
Lambda.Pccall{ prim_name = "js_is_nil";
94+
prim_arity = 1 ;
95+
prim_alloc = false;
96+
prim_native_name = "js_is_nil";
97+
prim_native_float = false;
98+
prim_attributes = [];
99+
prim_ty = None
100+
}
101+
102+
let js_is_undef : t =
103+
Lambda.Pccall{ prim_name = "js_is_undef";
104+
prim_arity = 1 ;
105+
prim_alloc = false;
106+
prim_native_name = "js_is_undef";
107+
prim_native_float = false;
108+
prim_attributes = [];
109+
prim_ty = None
110+
}
111+
112+
let js_is_nil_undef : t =
113+
Lambda.Pccall{ prim_name = "js_is_nil_undef";
114+
prim_arity = 1 ;
115+
prim_alloc = false;
116+
prim_native_name = "js_is_nil_undef";
117+
prim_native_float = false;
118+
prim_attributes = [];
119+
prim_ty = None
120+
}
121+
122+
end

jscomp/lam_comb.mli

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,29 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

26+
type t = Lambda.lambda
2627

28+
type binop = t -> t -> t
2729

30+
type triop = t -> t -> t -> t
2831

32+
type unop = t -> t
2933

30-
(* BuckleScript compiler
31-
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
32-
*
33-
* This program is free software; you can redistribute it and/or modify
34-
* it under the terms of the GNU Lesser General Public License as published by
35-
* the Free Software Foundation, with linking exception;
36-
* either version 2.1 of the License, or (at your option) any later version.
37-
*
38-
* This program is distributed in the hope that it will be useful,
39-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
40-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41-
* GNU Lesser General Public License for more details.
42-
*
43-
* You should have received a copy of the GNU Lesser General Public License
44-
* along with this program; if not, write to the Free Software
45-
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
46-
*)
47-
48-
(* Author: Hongbo Zhang *)
34+
val if_ : triop
35+
val switch : t -> Lambda.lambda_switch -> t
36+
val stringswitch : t -> (string * t) list -> t option -> t
4937

50-
type t = Lambda.lambda
38+
val true_ : t
39+
val false_ : t
40+
val unit : t
5141

42+
val sequor : binop
43+
val sequand : binop
44+
val not : unop
5245

53-
val if_ : t -> t -> t -> t
54-
val switch : t -> Lambda.lambda_switch -> t
55-
val stringswitch : t -> (string * t) list -> t option -> t
46+
module Prim : sig
47+
type t = Lambda.primitive
48+
val js_is_nil : t
49+
val js_is_undef : t
50+
val js_is_nil_undef : t
51+
end

jscomp/lam_compile.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ and
677677
| {should_return = True _ }
678678
(* Invariant: if [should_return], then [st] will not be [NeedValue] *)
679679
->
680-
compile_lambda cxt (Lam_comb.if_ l r Lam_util.lam_false )
680+
compile_lambda cxt (Lam_comb.sequand l r )
681681
| _ ->
682682
let l_block,l_expr =
683683
match compile_lambda {cxt with st = NeedValue; should_return = False} l with
@@ -700,7 +700,7 @@ and
700700
| {should_return = True _ }
701701
(* Invariant: if [should_return], then [st] will not be [NeedValue] *)
702702
->
703-
compile_lambda cxt @@ Lam_comb.if_ l Lam_util.lam_true r
703+
compile_lambda cxt @@ Lam_comb.sequor l r
704704
| _ ->
705705
let l_block,l_expr =
706706
match compile_lambda {cxt with st = NeedValue; should_return = False} l with
@@ -772,7 +772,7 @@ and
772772
compile_lambda cxt
773773
(Lfunction (Lambda.Curried, [],
774774
Lambda.Lapply(fn,
775-
[Lam_util.lam_unit],
775+
[Lam_comb.unit],
776776
Lam_util.default_apply_info
777777
)))
778778
else

jscomp/lam_pass_remove_alias.ml

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,20 @@ let simplify_alias
6767
-> simpl l2
6868
| ImmutableBlock ( [| SimpleForm l |] , x)
6969
->
70-
begin match x with
71-
| Null
72-
->
73-
Lam_comb.if_
74-
( Lprim (Lam_util.js_is_nil_primitive, [l]))
75-
(simpl l3)
76-
(simpl l2)
77-
| Undefined
78-
->
79-
Lam_comb.if_
80-
(Lprim (Lam_util.js_is_undef_primitive, [l]))
81-
(simpl l3)
82-
(simpl l2)
83-
| Null_undefined (* TODO: fix me*)
84-
->
85-
Lam_comb.if_
86-
( Lprim (Pintcomp Ceq, [l; Lvar Ext_ident.nil]))
87-
(simpl l3)
88-
(simpl l2)
89-
90-
| Normal ->
91-
Lam_comb.if_ l1 (simpl l2) (simpl l3)
92-
end
70+
let l1 =
71+
match x with
72+
| Null
73+
-> Lam_comb.not ( Lprim (Lam_comb.Prim.js_is_nil, [l]))
74+
| Undefined
75+
->
76+
Lam_comb.not (Lprim (Lam_comb.Prim.js_is_undef, [l]))
77+
| Null_undefined
78+
->
79+
Lam_comb.not
80+
( Lprim (Lam_comb.Prim.js_is_nil_undef, [l]))
81+
| Normal -> l1
82+
in
83+
Lam_comb.if_ l1 (simpl l2) (simpl l3)
9384
| _ -> Lam_comb.if_ l1 (simpl l2) (simpl l3)
9485

9586
| exception Not_found -> Lam_comb.if_ l1 (simpl l2) (simpl l3)

jscomp/lam_util.ml

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,6 @@ let mk_apply_info ?(loc = Location.none) apply_status : Lambda.apply_info =
324324
{ apply_loc = loc; apply_status }
325325

326326

327-
let lam_true : Lambda.lambda =
328-
Lconst (Const_pointer ( 1, Pt_constructor "true"))
329-
330-
let lam_false : Lambda.lambda =
331-
Lconst (Const_pointer( 0, Pt_constructor "false"))
332-
333-
let lam_unit : Lambda.lambda =
334-
Lconst (Const_pointer( 0, Pt_constructor "()"))
335327

336328
let is_function (lam : Lambda.lambda) =
337329
match lam with
@@ -404,32 +396,3 @@ let eta_conversion n info fn args =
404396
let default_apply_info : Lambda.apply_info =
405397
{ apply_status = App_na ; apply_loc = Location.none }
406398

407-
let js_is_nil_primitive =
408-
Lambda.Pccall{ prim_name = "js_is_nil";
409-
prim_arity = 1 ;
410-
prim_alloc = false;
411-
prim_native_name = "js_is_nil";
412-
prim_native_float = false;
413-
prim_attributes = [];
414-
prim_ty = None
415-
}
416-
417-
let js_is_undef_primitive =
418-
Lambda.Pccall{ prim_name = "js_is_undef";
419-
prim_arity = 1 ;
420-
prim_alloc = false;
421-
prim_native_name = "js_is_undef";
422-
prim_native_float = false;
423-
prim_attributes = [];
424-
prim_ty = None
425-
}
426-
427-
let js_is_nil_undef_primitive =
428-
Lambda.Pccall{ prim_name = "js_is_nil_undef";
429-
prim_arity = 1 ;
430-
prim_alloc = false;
431-
prim_native_name = "js_is_nil_undef";
432-
prim_native_float = false;
433-
prim_attributes = [];
434-
prim_ty = None
435-
}

jscomp/lam_util.mli

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ val print_ident_set : Format.formatter -> Ident_set.t -> unit
6868

6969
val mk_apply_info : ?loc:Location.t -> Lambda.apply_status -> Lambda.apply_info
7070

71-
val lam_true : Lambda.lambda
72-
val lam_false : Lambda.lambda
73-
val lam_unit : Lambda.lambda
71+
7472

7573
val not_function : Lambda.lambda -> bool
7674
val is_function : Lambda.lambda -> bool
@@ -82,6 +80,3 @@ val eta_conversion :
8280

8381
val default_apply_info : Lambda.apply_info
8482

85-
val js_is_nil_primitive : Lambda.primitive
86-
val js_is_undef_primitive : Lambda.primitive
87-
val js_is_nil_undef_primitive : Lambda.primitive

jscomp/runtime/caml_weak.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ function caml_weak_get(xs, i) {
3030

3131
function caml_weak_get_copy(xs, i) {
3232
var match = xs[i];
33-
if (match === undefined) {
34-
return /* None */0;
33+
if (match !== undefined) {
34+
return /* Some */[Caml_obj.caml_obj_dup(match)];
3535
}
3636
else {
37-
return /* Some */[Caml_obj.caml_obj_dup(match)];
37+
return /* None */0;
3838
}
3939
}
4040

jscomp/test/.depend

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,8 @@ test_while_closure.cmo : ../stdlib/array.cmi
593593
test_while_closure.cmx : ../stdlib/array.cmx
594594
test_while_side_effect.cmo :
595595
test_while_side_effect.cmx :
596-
test_zero_nullable.cmo : ../lib/js.cmo
597-
test_zero_nullable.cmx : ../lib/js.cmx
596+
test_zero_nullable.cmo : mt.cmi ../lib/js.cmo
597+
test_zero_nullable.cmx : mt.cmx ../lib/js.cmx
598598
testing.cmo : ../stdlib/scanf.cmi ../stdlib/printf.cmi testing.cmi
599599
testing.cmx : ../stdlib/scanf.cmx ../stdlib/printf.cmx testing.cmi
600600
tfloat_record_test.cmo : mt_global.cmi mt.cmi ../stdlib/format.cmi \
@@ -1201,8 +1201,8 @@ test_while_closure.cmo : ../stdlib/array.cmi
12011201
test_while_closure.cmj : ../stdlib/array.cmj
12021202
test_while_side_effect.cmo :
12031203
test_while_side_effect.cmj :
1204-
test_zero_nullable.cmo : ../lib/js.cmo
1205-
test_zero_nullable.cmj : ../lib/js.cmj
1204+
test_zero_nullable.cmo : mt.cmi ../lib/js.cmo
1205+
test_zero_nullable.cmj : mt.cmj ../lib/js.cmj
12061206
testing.cmo : ../stdlib/scanf.cmi ../stdlib/printf.cmi testing.cmi
12071207
testing.cmj : ../stdlib/scanf.cmj ../stdlib/printf.cmj testing.cmi
12081208
tfloat_record_test.cmo : mt_global.cmi mt.cmi ../stdlib/format.cmi \

0 commit comments

Comments
 (0)