Skip to content

Commit e78c67a

Browse files
authored
Merge pull request #459 from bloomberg/relax_name_conversion
Relax name conversion #457
2 parents dbf12b4 + 051f2c9 commit e78c67a

File tree

369 files changed

+940
-726
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

369 files changed

+940
-726
lines changed

jscomp/bin/compiler.ml

Lines changed: 343 additions & 205 deletions
Large diffs are not rendered by default.

jscomp/common/js_config.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ let gc = "Caml_gc"
213213
let int32 = "Caml_int32"
214214
let block = "Block"
215215
let js_primitive = "Js_primitive"
216-
let version = "0.5.5"
216+
let version = "0.6.0"
217217
let runtime_set =
218218
[
219219
js_primitive;

jscomp/ext/ext_ident.ml

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ let reserved_words =
103103
"debugger";"default";"delete";"do";
104104
"else";
105105
"finally";"for";"function";
106-
"if"; "in";"instanceof";
106+
"if"; "then"; "in";"instanceof";
107107
"new";
108108
"return";
109109
"switch";
@@ -194,50 +194,57 @@ let reserved_map =
194194
List.fold_left (fun acc x -> String_set.add x acc) String_set.empty
195195
reserved_words
196196

197+
198+
199+
200+
197201
(* TODO:
198202
check name conflicts with javascript conventions
199203
{[
200204
Ext_ident.convert "^";;
201205
- : string = "$caret"
202206
]}
203207
*)
204-
let convert (name : string) =
205-
let module E = struct exception Not_normal_letter of int end in
206-
let len = String.length name in
207-
if String_set.mem name reserved_map then "$$" ^ name
208+
let convert keyword (name : string) =
209+
if keyword && String_set.mem name reserved_map then "$$" ^ name
208210
else
209-
try
210-
for i = 0 to len - 1 do
211-
let c = String.unsafe_get name i in
212-
if not ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '_' || c = '$' ) then
213-
raise (E.Not_normal_letter i)
214-
else ()
215-
done;
216-
name
217-
with E.Not_normal_letter i ->
218-
String.sub name 0 i ^
219-
(let buffer = Buffer.create len in
220-
for j = i to len - 1 do
221-
let c = String.unsafe_get name j in
222-
match c with
223-
| '*' -> Buffer.add_string buffer "$star"
224-
| '\'' -> Buffer.add_string buffer "$prime"
225-
| '!' -> Buffer.add_string buffer "$bang"
226-
| '>' -> Buffer.add_string buffer "$great"
227-
| '<' -> Buffer.add_string buffer "$less"
228-
| '=' -> Buffer.add_string buffer "$eq"
229-
| '+' -> Buffer.add_string buffer "$plus"
230-
| '-' -> Buffer.add_string buffer "$neg"
231-
| '@' -> Buffer.add_string buffer "$at"
232-
| '^' -> Buffer.add_string buffer "$caret"
233-
| '/' -> Buffer.add_string buffer "$slash"
234-
| '|' -> Buffer.add_string buffer "$pipe"
235-
| '.' -> Buffer.add_string buffer "$dot"
236-
| '%' -> Buffer.add_string buffer "$percent"
237-
| '~' -> Buffer.add_string buffer "$tilde"
238-
| 'a'..'z' | 'A'..'Z'| '_'|'$' |'0'..'9'-> Buffer.add_char buffer c
239-
| _ -> Buffer.add_string buffer "$unknown"
240-
done; Buffer.contents buffer)
211+
let module E = struct exception Not_normal_letter of int end in
212+
let len = String.length name in
213+
try
214+
for i = 0 to len - 1 do
215+
match String.unsafe_get name i with
216+
| 'a' .. 'z' | 'A' .. 'Z'
217+
| '0' .. '9' | '_' | '$' -> ()
218+
| _ -> raise (E.Not_normal_letter i)
219+
done;
220+
name
221+
with E.Not_normal_letter i ->
222+
String.sub name 0 i ^
223+
(let buffer = Buffer.create len in
224+
for j = i to len - 1 do
225+
let c = String.unsafe_get name j in
226+
match c with
227+
| '*' -> Buffer.add_string buffer "$star"
228+
| '\'' -> Buffer.add_string buffer "$prime"
229+
| '!' -> Buffer.add_string buffer "$bang"
230+
| '>' -> Buffer.add_string buffer "$great"
231+
| '<' -> Buffer.add_string buffer "$less"
232+
| '=' -> Buffer.add_string buffer "$eq"
233+
| '+' -> Buffer.add_string buffer "$plus"
234+
| '-' -> Buffer.add_string buffer "$neg"
235+
| '@' -> Buffer.add_string buffer "$at"
236+
| '^' -> Buffer.add_string buffer "$caret"
237+
| '/' -> Buffer.add_string buffer "$slash"
238+
| '|' -> Buffer.add_string buffer "$pipe"
239+
| '.' -> Buffer.add_string buffer "$dot"
240+
| '%' -> Buffer.add_string buffer "$percent"
241+
| '~' -> Buffer.add_string buffer "$tilde"
242+
| 'a'..'z' | 'A'..'Z'| '_'|'$' |'0'..'9'-> Buffer.add_char buffer c
243+
| _ -> Buffer.add_string buffer "$unknown"
244+
done; Buffer.contents buffer)
245+
246+
let property_no_need_convert s =
247+
s == convert false s
241248

242249
(* It is currently made a persistent ident to avoid fresh ids
243250
which would result in different signature files

jscomp/ext/ext_ident.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ val make_unused : unit -> Ident.t
5252

5353
val is_unused_ident : Ident.t -> bool
5454

55-
val convert : string -> string
55+
(**
56+
if name is not converted, the reference should be equal
57+
*)
58+
val convert : bool -> string -> string
59+
val property_no_need_convert : string -> bool
60+
5661
val undefined : Ident.t
5762
val is_js_or_global : Ident.t -> bool
5863
val nil : Ident.t

jscomp/js_cmj_datasets.ml

Lines changed: 44 additions & 44 deletions
Large diffs are not rendered by default.

jscomp/js_dump.ml

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ let str_of_ident (cxt : Ext_pp_scope.t) (id : Ident.t) =
148148
[Printf.sprintf "%s$%d" name id.stamp] which is
149149
not relevant to the context
150150
*)
151-
let name = Ext_ident.convert id.name in
151+
let name = Ext_ident.convert true id.name in
152152
let i,new_cxt = Ext_pp_scope.add_ident id cxt in
153153
(* Attention:
154154
$$Array.length, due to the fact that global module is
@@ -237,6 +237,11 @@ let pp_string f ?(quote='"') ?(utf=false) s =
237237
P.string f quote_s
238238
;;
239239

240+
let property_string f s =
241+
if Ext_ident.property_no_need_convert s then
242+
P.string f s
243+
else
244+
pp_string f ~utf:true ~quote:(best_string_quote s) s
240245

241246
(* TODO: check utf's correct semantics *)
242247
let pp_quote_string f s =
@@ -445,7 +450,7 @@ and vident cxt f (v : J.vident) =
445450
| Qualified (id,_, Some name) ->
446451
let cxt = ident cxt f id in
447452
P.string f L.dot;
448-
P.string f (Ext_ident.convert name);
453+
P.string f (Ext_ident.convert true name);
449454
cxt
450455
end
451456

@@ -519,17 +524,6 @@ and
519524
(Call ({expression_desc = Dot(a,L.bind, true); comment = None }, [b],
520525
{arity = Full; call_info = Call_na}))
521526
end
522-
(* | Tag_ml_obj e -> *)
523-
(* P.group f 1 (fun _ -> *)
524-
(* P.string f "Object.defineProperty"; *)
525-
(* P.paren_group f 1 (fun _ -> *)
526-
(* let cxt = expression 1 cxt f e in *)
527-
(* P.string f L.comma; *)
528-
(* P.space f ; *)
529-
(* P.string f {|"##ml"|}; *)
530-
(* P.string f L.comma; *)
531-
(* P.string f {|{"value" : true, "writable" : false}|} ; *)
532-
(* cxt )) *)
533527

534528
| FlatCall(e,el) ->
535529
P.group f 1 (fun _ ->
@@ -934,30 +928,25 @@ and
934928
cxt in
935929
if l > 15 then P.paren_group f 1 action else action ()
936930

937-
| Dot (e, nm,normal) ->
938-
if normal then
939-
begin
940-
let action () =
941-
let cxt = expression 15 cxt f e in
931+
| Dot (e, s,normal) ->
932+
let action () =
933+
let cxt = expression 15 cxt f e in
934+
if Ext_ident.property_no_need_convert s then
935+
begin
942936
P.string f L.dot;
943-
P.string f (Ext_ident.convert nm);
944-
(* See [Js_program_loader.obj_of_exports]
945-
maybe in the ast level we should have
946-
refer and export
947-
*)
948-
cxt in
949-
if l > 15 then P.paren_group f 1 action else action ()
950-
end
951-
else begin
952-
let action () =
953-
P.group f 1 @@ fun _ ->
954-
let cxt = expression 15 cxt f e in
955-
(P.bracket_group f 1 @@ fun _ ->
956-
pp_string f (* ~utf:(kind = `Utf8) *) ~quote:( best_string_quote nm) nm);
957-
cxt
958-
in
959-
if l > 15 then P.paren_group f 1 action else action ()
960-
end
937+
P.string f s;
938+
end
939+
else
940+
begin
941+
P.bracket_group f 1 @@ fun _ ->
942+
pp_string f (* ~utf:(kind = `Utf8) *) ~quote:( best_string_quote s) s
943+
end;
944+
(* See [Js_program_loader.obj_of_exports]
945+
maybe in the ast level we should have
946+
refer and export
947+
*)
948+
cxt in
949+
if l > 15 then P.paren_group f 1 action else action ()
961950

962951
| New (e, el) ->
963952
let action () =
@@ -1014,7 +1003,7 @@ and property_name cxt f (s : J.property_name) : unit =
10141003
| Tag -> P.string f L.tag
10151004
| Length -> P.string f L.length
10161005
| Key s ->
1017-
pp_string f ~utf:true ~quote:(best_string_quote s) s
1006+
property_string f s
10181007
| Int_key i -> P.string f (string_of_int i)
10191008

10201009
and property_name_and_value_list cxt f l : Ext_pp_scope.t =
@@ -1490,7 +1479,7 @@ and block cxt f b =
14901479
let exports cxt f (idents : Ident.t list) =
14911480
let outer_cxt, reversed_list, margin =
14921481
List.fold_left (fun (cxt, acc, len ) (id : Ident.t) ->
1493-
let s = Ext_ident.convert id.name in
1482+
let s = Ext_ident.convert true id.name in
14941483
let str,cxt = str_of_ident cxt id in
14951484
cxt, ( (s,str) :: acc ) , max len (String.length s) )
14961485
(cxt, [], 0) idents in

jscomp/lam_stats_export.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let pp = Format.fprintf
3333
let meaningless_names = ["*opt*"; "param";]
3434

3535
let rec dump_ident fmt (id : Ident.t) (arity : Lam_stats.function_arities) =
36-
pp fmt "@[<2>export var %s:@ %a@ ;@]" (Ext_ident.convert id.name ) dump_arity arity
36+
pp fmt "@[<2>export var %s:@ %a@ ;@]" (Ext_ident.convert true id.name ) dump_arity arity
3737

3838
and dump_arity fmt (arity : Lam_stats.function_arities) =
3939
match arity with
@@ -50,7 +50,7 @@ and dump_arity fmt (arity : Lam_stats.function_arities) =
5050
Format.pp_print_space fmt ();
5151
)
5252
(fun fmt ident -> pp fmt "@[%s@ :@ any@]"
53-
(Ext_ident.convert @@ Ident.name ident))
53+
(Ext_ident.convert true @@ Ident.name ident))
5454
) args
5555

5656

jscomp/test/.depend

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ printf_sim.cmj : ../stdlib/printf.cmi
384384
printf_sim.cmx : ../stdlib/printf.cmx
385385
printf_test.cmj : ../stdlib/printf.cmi mt.cmi ../stdlib/format.cmi
386386
printf_test.cmx : ../stdlib/printf.cmx mt.cmx ../stdlib/format.cmx
387+
promise.cmj : ../runtime/js.cmj
388+
promise.cmx : ../runtime/js.cmx
387389
qcc.cmj : ../stdlib/sys.cmi ../stdlib/string.cmi ../stdlib/printf.cmi \
388390
../stdlib/list.cmi ../stdlib/char.cmi ../stdlib/bytes.cmi \
389391
../stdlib/array.cmi
@@ -1050,6 +1052,8 @@ printf_sim.cmo : ../stdlib/printf.cmi
10501052
printf_sim.cmj : ../stdlib/printf.cmj
10511053
printf_test.cmo : ../stdlib/printf.cmi mt.cmi ../stdlib/format.cmi
10521054
printf_test.cmj : ../stdlib/printf.cmj mt.cmj ../stdlib/format.cmj
1055+
promise.cmo : ../runtime/js.cmo
1056+
promise.cmj : ../runtime/js.cmj
10531057
qcc.cmo : ../stdlib/sys.cmi ../stdlib/string.cmi ../stdlib/printf.cmi \
10541058
../stdlib/list.cmi ../stdlib/char.cmi ../stdlib/bytes.cmi \
10551059
../stdlib/array.cmi

jscomp/test/promise.ml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
type t
3+
4+
external catch : t -> 'a -> 'b = "catch" [@@bs.send]
5+
6+
let f p =
7+
catch p 3
8+
9+
class type promise =
10+
object
11+
method then_ : 'a -> 'b
12+
method catch : 'a -> 'b
13+
end [@uncurry]
14+
15+
external new_promise : unit -> promise Js.t =
16+
"Promise" [@@bs.new] [@@bs.module "sys-bluebird"]
17+
18+
let () =
19+
let p = new_promise() in
20+
(p##then_(fun x -> x + 3))##catch_(fun reason -> reason)
21+
22+
23+
let u =
24+
{ then_ = 3 ;
25+
catch = 32
26+
} [@bs.obj]
27+
28+
29+
let uu = {
30+
_'x_ = 3
31+
} [@bs.obj]
32+
33+
34+
let hh = uu##_'x_

jscomp/test/test.mllib

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,6 @@ noassert
331331

332332
test_unsafe_cmp
333333

334-
gpr_441
334+
gpr_441
335+
336+
promise

lib/js/arg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44
var Caml_obj = require("./caml_obj");

lib/js/array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44
var Caml_builtin_exceptions = require("./caml_builtin_exceptions");

lib/js/arrayLabels.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44
var $$Array = require("./array");

lib/js/bigarray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44
var Caml_builtin_exceptions = require("./caml_builtin_exceptions");

lib/js/block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44

lib/js/buffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44
var Bytes = require("./bytes");

lib/js/bytes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44
var Caml_builtin_exceptions = require("./caml_builtin_exceptions");

lib/js/bytesLabels.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44
var Bytes = require("./bytes");

lib/js/callback.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44
var Obj = require("./obj");

lib/js/caml_array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44

lib/js/caml_backtrace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44
var Caml_builtin_exceptions = require("./caml_builtin_exceptions");

lib/js/caml_basic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44

lib/js/caml_builtin_exceptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
22
'use strict';
33

44

0 commit comments

Comments
 (0)