Skip to content

Relax name conversion #457 #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
548 changes: 343 additions & 205 deletions jscomp/bin/compiler.ml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion jscomp/common/js_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ let gc = "Caml_gc"
let int32 = "Caml_int32"
let block = "Block"
let js_primitive = "Js_primitive"
let version = "0.5.5"
let version = "0.6.0"
let runtime_set =
[
js_primitive;
Expand Down
81 changes: 44 additions & 37 deletions jscomp/ext/ext_ident.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ let reserved_words =
"debugger";"default";"delete";"do";
"else";
"finally";"for";"function";
"if"; "in";"instanceof";
"if"; "then"; "in";"instanceof";
"new";
"return";
"switch";
Expand Down Expand Up @@ -194,50 +194,57 @@ let reserved_map =
List.fold_left (fun acc x -> String_set.add x acc) String_set.empty
reserved_words





(* TODO:
check name conflicts with javascript conventions
{[
Ext_ident.convert "^";;
- : string = "$caret"
]}
*)
let convert (name : string) =
let module E = struct exception Not_normal_letter of int end in
let len = String.length name in
if String_set.mem name reserved_map then "$$" ^ name
let convert keyword (name : string) =
if keyword && String_set.mem name reserved_map then "$$" ^ name
else
try
for i = 0 to len - 1 do
let c = String.unsafe_get name i in
if not ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '_' || c = '$' ) then
raise (E.Not_normal_letter i)
else ()
done;
name
with E.Not_normal_letter i ->
String.sub name 0 i ^
(let buffer = Buffer.create len in
for j = i to len - 1 do
let c = String.unsafe_get name j in
match c with
| '*' -> Buffer.add_string buffer "$star"
| '\'' -> Buffer.add_string buffer "$prime"
| '!' -> Buffer.add_string buffer "$bang"
| '>' -> Buffer.add_string buffer "$great"
| '<' -> Buffer.add_string buffer "$less"
| '=' -> Buffer.add_string buffer "$eq"
| '+' -> Buffer.add_string buffer "$plus"
| '-' -> Buffer.add_string buffer "$neg"
| '@' -> Buffer.add_string buffer "$at"
| '^' -> Buffer.add_string buffer "$caret"
| '/' -> Buffer.add_string buffer "$slash"
| '|' -> Buffer.add_string buffer "$pipe"
| '.' -> Buffer.add_string buffer "$dot"
| '%' -> Buffer.add_string buffer "$percent"
| '~' -> Buffer.add_string buffer "$tilde"
| 'a'..'z' | 'A'..'Z'| '_'|'$' |'0'..'9'-> Buffer.add_char buffer c
| _ -> Buffer.add_string buffer "$unknown"
done; Buffer.contents buffer)
let module E = struct exception Not_normal_letter of int end in
let len = String.length name in
try
for i = 0 to len - 1 do
match String.unsafe_get name i with
| 'a' .. 'z' | 'A' .. 'Z'
| '0' .. '9' | '_' | '$' -> ()
| _ -> raise (E.Not_normal_letter i)
done;
name
with E.Not_normal_letter i ->
String.sub name 0 i ^
(let buffer = Buffer.create len in
for j = i to len - 1 do
let c = String.unsafe_get name j in
match c with
| '*' -> Buffer.add_string buffer "$star"
| '\'' -> Buffer.add_string buffer "$prime"
| '!' -> Buffer.add_string buffer "$bang"
| '>' -> Buffer.add_string buffer "$great"
| '<' -> Buffer.add_string buffer "$less"
| '=' -> Buffer.add_string buffer "$eq"
| '+' -> Buffer.add_string buffer "$plus"
| '-' -> Buffer.add_string buffer "$neg"
| '@' -> Buffer.add_string buffer "$at"
| '^' -> Buffer.add_string buffer "$caret"
| '/' -> Buffer.add_string buffer "$slash"
| '|' -> Buffer.add_string buffer "$pipe"
| '.' -> Buffer.add_string buffer "$dot"
| '%' -> Buffer.add_string buffer "$percent"
| '~' -> Buffer.add_string buffer "$tilde"
| 'a'..'z' | 'A'..'Z'| '_'|'$' |'0'..'9'-> Buffer.add_char buffer c
| _ -> Buffer.add_string buffer "$unknown"
done; Buffer.contents buffer)

let property_no_need_convert s =
s == convert false s

(* It is currently made a persistent ident to avoid fresh ids
which would result in different signature files
Expand Down
7 changes: 6 additions & 1 deletion jscomp/ext/ext_ident.mli
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ val make_unused : unit -> Ident.t

val is_unused_ident : Ident.t -> bool

val convert : string -> string
(**
if name is not converted, the reference should be equal
*)
val convert : bool -> string -> string
val property_no_need_convert : string -> bool

val undefined : Ident.t
val is_js_or_global : Ident.t -> bool
val nil : Ident.t
88 changes: 44 additions & 44 deletions jscomp/js_cmj_datasets.ml

Large diffs are not rendered by default.

65 changes: 27 additions & 38 deletions jscomp/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ let str_of_ident (cxt : Ext_pp_scope.t) (id : Ident.t) =
[Printf.sprintf "%s$%d" name id.stamp] which is
not relevant to the context
*)
let name = Ext_ident.convert id.name in
let name = Ext_ident.convert true id.name in
let i,new_cxt = Ext_pp_scope.add_ident id cxt in
(* Attention:
$$Array.length, due to the fact that global module is
Expand Down Expand Up @@ -237,6 +237,11 @@ let pp_string f ?(quote='"') ?(utf=false) s =
P.string f quote_s
;;

let property_string f s =
if Ext_ident.property_no_need_convert s then
P.string f s
else
pp_string f ~utf:true ~quote:(best_string_quote s) s

(* TODO: check utf's correct semantics *)
let pp_quote_string f s =
Expand Down Expand Up @@ -445,7 +450,7 @@ and vident cxt f (v : J.vident) =
| Qualified (id,_, Some name) ->
let cxt = ident cxt f id in
P.string f L.dot;
P.string f (Ext_ident.convert name);
P.string f (Ext_ident.convert true name);
cxt
end

Expand Down Expand Up @@ -519,17 +524,6 @@ and
(Call ({expression_desc = Dot(a,L.bind, true); comment = None }, [b],
{arity = Full; call_info = Call_na}))
end
(* | Tag_ml_obj e -> *)
(* P.group f 1 (fun _ -> *)
(* P.string f "Object.defineProperty"; *)
(* P.paren_group f 1 (fun _ -> *)
(* let cxt = expression 1 cxt f e in *)
(* P.string f L.comma; *)
(* P.space f ; *)
(* P.string f {|"##ml"|}; *)
(* P.string f L.comma; *)
(* P.string f {|{"value" : true, "writable" : false}|} ; *)
(* cxt )) *)

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

| Dot (e, nm,normal) ->
if normal then
begin
let action () =
let cxt = expression 15 cxt f e in
| Dot (e, s,normal) ->
let action () =
let cxt = expression 15 cxt f e in
if Ext_ident.property_no_need_convert s then
begin
P.string f L.dot;
P.string f (Ext_ident.convert nm);
(* See [Js_program_loader.obj_of_exports]
maybe in the ast level we should have
refer and export
*)
cxt in
if l > 15 then P.paren_group f 1 action else action ()
end
else begin
let action () =
P.group f 1 @@ fun _ ->
let cxt = expression 15 cxt f e in
(P.bracket_group f 1 @@ fun _ ->
pp_string f (* ~utf:(kind = `Utf8) *) ~quote:( best_string_quote nm) nm);
cxt
in
if l > 15 then P.paren_group f 1 action else action ()
end
P.string f s;
end
else
begin
P.bracket_group f 1 @@ fun _ ->
pp_string f (* ~utf:(kind = `Utf8) *) ~quote:( best_string_quote s) s
end;
(* See [Js_program_loader.obj_of_exports]
maybe in the ast level we should have
refer and export
*)
cxt in
if l > 15 then P.paren_group f 1 action else action ()

| New (e, el) ->
let action () =
Expand Down Expand Up @@ -1014,7 +1003,7 @@ and property_name cxt f (s : J.property_name) : unit =
| Tag -> P.string f L.tag
| Length -> P.string f L.length
| Key s ->
pp_string f ~utf:true ~quote:(best_string_quote s) s
property_string f s
| Int_key i -> P.string f (string_of_int i)

and property_name_and_value_list cxt f l : Ext_pp_scope.t =
Expand Down Expand Up @@ -1490,7 +1479,7 @@ and block cxt f b =
let exports cxt f (idents : Ident.t list) =
let outer_cxt, reversed_list, margin =
List.fold_left (fun (cxt, acc, len ) (id : Ident.t) ->
let s = Ext_ident.convert id.name in
let s = Ext_ident.convert true id.name in
let str,cxt = str_of_ident cxt id in
cxt, ( (s,str) :: acc ) , max len (String.length s) )
(cxt, [], 0) idents in
Expand Down
4 changes: 2 additions & 2 deletions jscomp/lam_stats_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let pp = Format.fprintf
let meaningless_names = ["*opt*"; "param";]

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

and dump_arity fmt (arity : Lam_stats.function_arities) =
match arity with
Expand All @@ -50,7 +50,7 @@ and dump_arity fmt (arity : Lam_stats.function_arities) =
Format.pp_print_space fmt ();
)
(fun fmt ident -> pp fmt "@[%s@ :@ any@]"
(Ext_ident.convert @@ Ident.name ident))
(Ext_ident.convert true @@ Ident.name ident))
) args


Expand Down
4 changes: 4 additions & 0 deletions jscomp/test/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ printf_sim.cmj : ../stdlib/printf.cmi
printf_sim.cmx : ../stdlib/printf.cmx
printf_test.cmj : ../stdlib/printf.cmi mt.cmi ../stdlib/format.cmi
printf_test.cmx : ../stdlib/printf.cmx mt.cmx ../stdlib/format.cmx
promise.cmj : ../runtime/js.cmj
promise.cmx : ../runtime/js.cmx
qcc.cmj : ../stdlib/sys.cmi ../stdlib/string.cmi ../stdlib/printf.cmi \
../stdlib/list.cmi ../stdlib/char.cmi ../stdlib/bytes.cmi \
../stdlib/array.cmi
Expand Down Expand Up @@ -1050,6 +1052,8 @@ printf_sim.cmo : ../stdlib/printf.cmi
printf_sim.cmj : ../stdlib/printf.cmj
printf_test.cmo : ../stdlib/printf.cmi mt.cmi ../stdlib/format.cmi
printf_test.cmj : ../stdlib/printf.cmj mt.cmj ../stdlib/format.cmj
promise.cmo : ../runtime/js.cmo
promise.cmj : ../runtime/js.cmj
qcc.cmo : ../stdlib/sys.cmi ../stdlib/string.cmi ../stdlib/printf.cmi \
../stdlib/list.cmi ../stdlib/char.cmi ../stdlib/bytes.cmi \
../stdlib/array.cmi
Expand Down
34 changes: 34 additions & 0 deletions jscomp/test/promise.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

type t

external catch : t -> 'a -> 'b = "catch" [@@bs.send]

let f p =
catch p 3

class type promise =
object
method then_ : 'a -> 'b
method catch : 'a -> 'b
end [@uncurry]

external new_promise : unit -> promise Js.t =
"Promise" [@@bs.new] [@@bs.module "sys-bluebird"]

let () =
let p = new_promise() in
(p##then_(fun x -> x + 3))##catch_(fun reason -> reason)


let u =
{ then_ = 3 ;
catch = 32
} [@bs.obj]


let uu = {
_'x_ = 3
} [@bs.obj]


let hh = uu##_'x_
4 changes: 3 additions & 1 deletion jscomp/test/test.mllib
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,6 @@ noassert

test_unsafe_cmp

gpr_441
gpr_441

promise
2 changes: 1 addition & 1 deletion lib/js/arg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';

var Caml_obj = require("./caml_obj");
Expand Down
2 changes: 1 addition & 1 deletion lib/js/array.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';

var Caml_builtin_exceptions = require("./caml_builtin_exceptions");
Expand Down
2 changes: 1 addition & 1 deletion lib/js/arrayLabels.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';

var $$Array = require("./array");
Expand Down
2 changes: 1 addition & 1 deletion lib/js/bigarray.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';

var Caml_builtin_exceptions = require("./caml_builtin_exceptions");
Expand Down
2 changes: 1 addition & 1 deletion lib/js/block.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';


Expand Down
2 changes: 1 addition & 1 deletion lib/js/buffer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';

var Bytes = require("./bytes");
Expand Down
2 changes: 1 addition & 1 deletion lib/js/bytes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';

var Caml_builtin_exceptions = require("./caml_builtin_exceptions");
Expand Down
2 changes: 1 addition & 1 deletion lib/js/bytesLabels.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';

var Bytes = require("./bytes");
Expand Down
2 changes: 1 addition & 1 deletion lib/js/callback.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';

var Obj = require("./obj");
Expand Down
2 changes: 1 addition & 1 deletion lib/js/caml_array.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';


Expand Down
2 changes: 1 addition & 1 deletion lib/js/caml_backtrace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';

var Caml_builtin_exceptions = require("./caml_builtin_exceptions");
Expand Down
2 changes: 1 addition & 1 deletion lib/js/caml_basic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';


Expand Down
2 changes: 1 addition & 1 deletion lib/js/caml_builtin_exceptions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.5.5 , PLEASE EDIT WITH CARE
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.0 , PLEASE EDIT WITH CARE
'use strict';


Expand Down
Loading