From 133141bda8aab8908d2a3d3fd080637d1deb68a1 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 10:42:25 +0800 Subject: [PATCH 01/11] remove unused code --- jscomp/core/bs_conditional_initial.ml | 2 +- jscomp/core/j.ml | 8 --- jscomp/core/js_analyzer.ml | 6 -- jscomp/core/js_dump.ml | 26 +-------- jscomp/core/js_fold.ml | 10 +--- jscomp/core/js_map.ml | 12 +--- jscomp/core/js_pass_tailcall_inline.ml | 11 ++-- jscomp/core/lam_compile_primitive.ml | 4 +- jscomp/core/lam_dispatch_primitive.ml | 2 - jscomp/test/.depend | 1 + jscomp/test/Makefile | 1 + jscomp/test/gpr_2700_test.js | 13 +++++ jscomp/test/gpr_2700_test.ml | 4 ++ lib/whole_compiler.ml | 79 +++----------------------- 14 files changed, 38 insertions(+), 141 deletions(-) create mode 100644 jscomp/test/gpr_2700_test.js create mode 100644 jscomp/test/gpr_2700_test.ml diff --git a/jscomp/core/bs_conditional_initial.ml b/jscomp/core/bs_conditional_initial.ml index 9b0bf7299b..df5ba896e9 100644 --- a/jscomp/core/bs_conditional_initial.ml +++ b/jscomp/core/bs_conditional_initial.ml @@ -25,7 +25,7 @@ let setup_env () = #if BS_DEBUG then - Js_config.set_debug_file "pipe_syntax.ml"; + Js_config.set_debug_file "gpr_2700_test.ml"; #end Lexer.replace_directive_bool "BS" true; Lexer.replace_directive_string "BS_VERSION" Bs_version.version diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index af5bebecfd..8e9fc7f0c7 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -111,10 +111,7 @@ and expression_desc = *) | Array_copy of expression (* shallow copy, like [x.slice] *) | Array_append of expression * expression (* For [caml_array_append]*) - (* | Tag_ml_obj of expression *) | String_append of expression * expression - - | Anything_to_number of expression | Bool of bool (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator @@ -178,11 +175,6 @@ and expression_desc = if it's know at compile time, we can turn it into f(args[0], args[1], ... ) *) - | Bind of expression * expression - (* {[ Bind (a,b) ]} - is literally - {[ a.bind(b) ]} - *) | Call of expression * expression list * Js_call_info.t (* Analysze over J expression is hard since, some primitive call is translated diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index 0ac91704fd..83d5eed8cf 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -100,7 +100,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = the block is mutable does not mean this operation is non-pure *) List.for_all no_side_effect xs - | Bind(fn, obj) -> no_side_effect fn && no_side_effect obj | Object kvs -> List.for_all (fun (_property_name, y) -> no_side_effect y ) kvs | Array_append (a,b) @@ -117,8 +116,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Math _ | Array_of_size _ | Array_copy _ - (* | Tag_ml_obj _ *) - | J.Anything_to_number _ | Js_not _ | String_of_small_int_array _ | Json_stringify _ @@ -269,8 +266,6 @@ let rec eq_expression | Array_copy _ | Array_append _ | String_append _ - | Anything_to_number _ - | Typeof _ | Js_not _ | String_of_small_int_array _ @@ -280,7 +275,6 @@ let rec eq_expression | Cond _ | FlatCall _ - | Bind _ | String_access _ | New _ diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index d73b48f037..72f68ece45 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -476,16 +476,7 @@ and in if l > 15 then P.paren_group f 1 action else action () - | Bind (a,b) -> - (* a.bind(b) - {[ fun b -> a.bind(b) ==? a.bind ]} - *) - begin - expression_desc cxt l f - (Call ({expression_desc = Dot(a,L.bind, true); comment = None }, [b], - {arity = Full; call_info = Call_na})) - end - + | FlatCall(e,el) -> P.group f 1 (fun _ -> let cxt = expression 15 cxt f e in @@ -640,19 +631,6 @@ and else action () ); cxt - | J.Anything_to_number e - -> - let action () = - P.group f 0 @@ fun _ -> - P.string f "+" ; - expression 13 cxt f e - in - (* need to tweak precedence carefully - here [++x --> +(+x)] - *) - if l > 12 - then P.paren_group f 1 action - else action () | Is_null_undefined_to_boolean e -> let action = (fun _ -> let cxt = expression 1 cxt f e in @@ -1146,12 +1124,10 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Caml_block _ | FlatCall _ | Typeof _ - | Bind _ | Number _ | Js_not _ | Bool _ | New _ - | J.Anything_to_number _ -> false (* e = function(x){...}(x); is good *) diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index d5b2d7aa36..3bd620f88f 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -130,8 +130,7 @@ class virtual fold = since GC does not rely on it *) (* shallow copy, like [x.slice] *) - (* For [caml_array_append]*) - (* | Tag_ml_obj of expression *) (* js true/false*) + (* For [caml_array_append]*) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) @@ -182,10 +181,6 @@ class virtual fold = if it's know at compile time, we can turn it into f(args[0], args[1], ... ) *) - (* {[ Bind (a,b) ]} - is literally - {[ a.bind(b) ]} - *) (* Analysze over J expression is hard since, some primitive call is translated into a plain call, it's better to keep them @@ -373,7 +368,6 @@ class virtual fold = let o = o#expression _x in let o = o#expression _x_i1 in o | String_append (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o - | Anything_to_number _x -> let o = o#expression _x in o | Bool _x -> let o = o#bool _x in o | Typeof _x -> let o = o#expression _x in o | Js_not _x -> let o = o#expression _x in o @@ -393,8 +387,6 @@ class virtual fold = let o = o#expression _x_i1 in let o = o#expression _x_i2 in o | FlatCall (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o - | Bind (_x, _x_i1) -> - let o = o#expression _x in let o = o#expression _x_i1 in o | Call (_x, _x_i1, _x_i2) -> let o = o#expression _x in let o = o#list (fun o -> o#expression) _x_i1 in diff --git a/jscomp/core/js_map.ml b/jscomp/core/js_map.ml index 73c6f97b21..478cdf4018 100644 --- a/jscomp/core/js_map.ml +++ b/jscomp/core/js_map.ml @@ -143,8 +143,7 @@ class virtual map = since GC does not rely on it *) (* shallow copy, like [x.slice] *) - (* For [caml_array_append]*) - (* | Tag_ml_obj of expression *) (* js true/false*) + (* For [caml_array_append]*) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) @@ -195,10 +194,6 @@ class virtual map = if it's know at compile time, we can turn it into f(args[0], args[1], ... ) *) - (* {[ Bind (a,b) ]} - is literally - {[ a.bind(b) ]} - *) (* Analysze over J expression is hard since, some primitive call is translated into a plain call, it's better to keep them @@ -401,8 +396,6 @@ class virtual map = | String_append (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in String_append (_x, _x_i1) - | Anything_to_number _x -> - let _x = o#expression _x in Anything_to_number _x | Bool _x -> let _x = o#bool _x in Bool _x | Typeof _x -> let _x = o#expression _x in Typeof _x | Js_not _x -> let _x = o#expression _x in Js_not _x @@ -429,9 +422,6 @@ class virtual map = | FlatCall (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in FlatCall (_x, _x_i1) - | Bind (_x, _x_i1) -> - let _x = o#expression _x in - let _x_i1 = o#expression _x_i1 in Bind (_x, _x_i1) | Call (_x, _x_i1, _x_i2) -> let _x = o#expression _x in let _x_i1 = o#list (fun o -> o#expression) _x_i1 in diff --git a/jscomp/core/js_pass_tailcall_inline.ml b/jscomp/core/js_pass_tailcall_inline.ml index 66e46e834c..a7faaeaabb 100644 --- a/jscomp/core/js_pass_tailcall_inline.ml +++ b/jscomp/core/js_pass_tailcall_inline.ml @@ -220,7 +220,7 @@ let subst name export_set stats = (* Mark a function as dead means it will never be scanned, here we inline the function *) - Ext_list.append block @@ self#block rest + Ext_list.append block (self#block rest) | (None | Some _) -> self#statement st :: self#block rest end @@ -235,9 +235,8 @@ let subst name export_set stats = let tailcall_inline (program : J.program) = - let _stats = get_stats program in - let _export_set = program.export_set in - program - |> (subst program.name _export_set _stats )# program - (* |> pass_beta #program *) + let stats = get_stats program in + let export_set = program.export_set in + (subst program.name export_set stats )#program program + diff --git a/jscomp/core/lam_compile_primitive.ml b/jscomp/core/lam_compile_primitive.ml index 54fbbb956f..3f724bf387 100644 --- a/jscomp/core/lam_compile_primitive.ml +++ b/jscomp/core/lam_compile_primitive.ml @@ -703,9 +703,7 @@ let translate loc (* assert false (\* already handled *\) *) (* assert false *) | Pduprecord ((Record_regular - | Record_float ),0) - | Pduprecord ((Record_regular - | Record_float ),_) -> + | Record_float ),_size) -> (* _size is the length of all_lables*) begin match args with | [e] -> Js_of_lam_record.copy e | _ -> assert false diff --git a/jscomp/core/lam_dispatch_primitive.ml b/jscomp/core/lam_dispatch_primitive.ml index 2c4b743ede..721dae85f7 100644 --- a/jscomp/core/lam_dispatch_primitive.ml +++ b/jscomp/core/lam_dispatch_primitive.ml @@ -659,8 +659,6 @@ let translate loc (prim_name : string) *) | _ -> E.array_copy a end - (* if Js_analyzer.is_constant a then a - else E.array_copy a *) | _ -> assert false end | "caml_obj_block" -> diff --git a/jscomp/test/.depend b/jscomp/test/.depend index ab49dcecc2..38d30d61b0 100644 --- a/jscomp/test/.depend +++ b/jscomp/test/.depend @@ -311,6 +311,7 @@ gpr_2614_test.cmj : gpr_2633_test.cmj : gpr_2642_test.cmj : gpr_2652_test.cmj : ../others/node.cmj ../stdlib/buffer.cmj +gpr_2700_test.cmj : gpr_405_test.cmj : ../stdlib/hashtbl.cmj gpr_405_test.cmi gpr_441.cmj : gpr_459_test.cmj : mt.cmj diff --git a/jscomp/test/Makefile b/jscomp/test/Makefile index 52fcd4a8db..105721d2d1 100644 --- a/jscomp/test/Makefile +++ b/jscomp/test/Makefile @@ -249,6 +249,7 @@ OTHERS := test_literals a test_ari test_export2 test_internalOO test_obj_simple_ gpr_2642_test\ gpr_2652_test\ gpr_2614_test\ + gpr_2700_test\ # bs_uncurry_test # needs Lam to get rid of Uncurry arity first # simple_derive_test diff --git a/jscomp/test/gpr_2700_test.js b/jscomp/test/gpr_2700_test.js new file mode 100644 index 0000000000..886e951c8a --- /dev/null +++ b/jscomp/test/gpr_2700_test.js @@ -0,0 +1,13 @@ +'use strict'; + + +function f(x) { + if (x === 3) { + return true; + } else { + return x === 4; + } +} + +exports.f = f; +/* No side effect */ diff --git a/jscomp/test/gpr_2700_test.ml b/jscomp/test/gpr_2700_test.ml new file mode 100644 index 0000000000..a08820c13c --- /dev/null +++ b/jscomp/test/gpr_2700_test.ml @@ -0,0 +1,4 @@ + + + +let f x = x = 3 || x = 4 \ No newline at end of file diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index ea1d40da01..2bac446c41 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -63310,10 +63310,7 @@ and expression_desc = *) | Array_copy of expression (* shallow copy, like [x.slice] *) | Array_append of expression * expression (* For [caml_array_append]*) - (* | Tag_ml_obj of expression *) | String_append of expression * expression - - | Anything_to_number of expression | Bool of bool (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator @@ -63377,11 +63374,6 @@ and expression_desc = if it's know at compile time, we can turn it into f(args[0], args[1], ... ) *) - | Bind of expression * expression - (* {[ Bind (a,b) ]} - is literally - {[ a.bind(b) ]} - *) | Call of expression * expression list * Js_call_info.t (* Analysze over J expression is hard since, some primitive call is translated @@ -69618,8 +69610,7 @@ class virtual fold = since GC does not rely on it *) (* shallow copy, like [x.slice] *) - (* For [caml_array_append]*) - (* | Tag_ml_obj of expression *) (* js true/false*) + (* For [caml_array_append]*) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) @@ -69670,10 +69661,6 @@ class virtual fold = if it's know at compile time, we can turn it into f(args[0], args[1], ... ) *) - (* {[ Bind (a,b) ]} - is literally - {[ a.bind(b) ]} - *) (* Analysze over J expression is hard since, some primitive call is translated into a plain call, it's better to keep them @@ -69861,7 +69848,6 @@ class virtual fold = let o = o#expression _x in let o = o#expression _x_i1 in o | String_append (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o - | Anything_to_number _x -> let o = o#expression _x in o | Bool _x -> let o = o#bool _x in o | Typeof _x -> let o = o#expression _x in o | Js_not _x -> let o = o#expression _x in o @@ -69881,8 +69867,6 @@ class virtual fold = let o = o#expression _x_i1 in let o = o#expression _x_i2 in o | FlatCall (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o - | Bind (_x, _x_i1) -> - let o = o#expression _x in let o = o#expression _x_i1 in o | Call (_x, _x_i1, _x_i2) -> let o = o#expression _x in let o = o#list (fun o -> o#expression) _x_i1 in @@ -70155,7 +70139,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = the block is mutable does not mean this operation is non-pure *) List.for_all no_side_effect xs - | Bind(fn, obj) -> no_side_effect fn && no_side_effect obj | Object kvs -> List.for_all (fun (_property_name, y) -> no_side_effect y ) kvs | Array_append (a,b) @@ -70172,8 +70155,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Math _ | Array_of_size _ | Array_copy _ - (* | Tag_ml_obj _ *) - | J.Anything_to_number _ | Js_not _ | String_of_small_int_array _ | Json_stringify _ @@ -70324,8 +70305,6 @@ let rec eq_expression | Array_copy _ | Array_append _ | String_append _ - | Anything_to_number _ - | Typeof _ | Js_not _ | String_of_small_int_array _ @@ -70335,7 +70314,6 @@ let rec eq_expression | Cond _ | FlatCall _ - | Bind _ | String_access _ | New _ @@ -84701,16 +84679,7 @@ and in if l > 15 then P.paren_group f 1 action else action () - | Bind (a,b) -> - (* a.bind(b) - {[ fun b -> a.bind(b) ==? a.bind ]} - *) - begin - expression_desc cxt l f - (Call ({expression_desc = Dot(a,L.bind, true); comment = None }, [b], - {arity = Full; call_info = Call_na})) - end - + | FlatCall(e,el) -> P.group f 1 (fun _ -> let cxt = expression 15 cxt f e in @@ -84865,19 +84834,6 @@ and else action () ); cxt - | J.Anything_to_number e - -> - let action () = - P.group f 0 @@ fun _ -> - P.string f "+" ; - expression 13 cxt f e - in - (* need to tweak precedence carefully - here [++x --> +(+x)] - *) - if l > 12 - then P.paren_group f 1 action - else action () | Is_null_undefined_to_boolean e -> let action = (fun _ -> let cxt = expression 1 cxt f e in @@ -85371,12 +85327,10 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Caml_block _ | FlatCall _ | Typeof _ - | Bind _ | Number _ | Js_not _ | Bool _ | New _ - | J.Anything_to_number _ -> false (* e = function(x){...}(x); is good *) @@ -88388,8 +88342,7 @@ class virtual map = since GC does not rely on it *) (* shallow copy, like [x.slice] *) - (* For [caml_array_append]*) - (* | Tag_ml_obj of expression *) (* js true/false*) + (* For [caml_array_append]*) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) @@ -88440,10 +88393,6 @@ class virtual map = if it's know at compile time, we can turn it into f(args[0], args[1], ... ) *) - (* {[ Bind (a,b) ]} - is literally - {[ a.bind(b) ]} - *) (* Analysze over J expression is hard since, some primitive call is translated into a plain call, it's better to keep them @@ -88646,8 +88595,6 @@ class virtual map = | String_append (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in String_append (_x, _x_i1) - | Anything_to_number _x -> - let _x = o#expression _x in Anything_to_number _x | Bool _x -> let _x = o#bool _x in Bool _x | Typeof _x -> let _x = o#expression _x in Typeof _x | Js_not _x -> let _x = o#expression _x in Js_not _x @@ -88674,9 +88621,6 @@ class virtual map = | FlatCall (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in FlatCall (_x, _x_i1) - | Bind (_x, _x_i1) -> - let _x = o#expression _x in - let _x_i1 = o#expression _x_i1 in Bind (_x, _x_i1) | Call (_x, _x_i1, _x_i2) -> let _x = o#expression _x in let _x_i1 = o#list (fun o -> o#expression) _x_i1 in @@ -89907,7 +89851,7 @@ let subst name export_set stats = (* Mark a function as dead means it will never be scanned, here we inline the function *) - Ext_list.append block @@ self#block rest + Ext_list.append block (self#block rest) | (None | Some _) -> self#statement st :: self#block rest end @@ -89922,11 +89866,10 @@ let subst name export_set stats = let tailcall_inline (program : J.program) = - let _stats = get_stats program in - let _export_set = program.export_set in - program - |> (subst program.name _export_set _stats )# program - (* |> pass_beta #program *) + let stats = get_stats program in + let export_set = program.export_set in + (subst program.name export_set stats )#program program + end @@ -95580,8 +95523,6 @@ let translate loc (prim_name : string) *) | _ -> E.array_copy a end - (* if Js_analyzer.is_constant a then a - else E.array_copy a *) | _ -> assert false end | "caml_obj_block" -> @@ -96560,9 +96501,7 @@ let translate loc (* assert false (\* already handled *\) *) (* assert false *) | Pduprecord ((Record_regular - | Record_float ),0) - | Pduprecord ((Record_regular - | Record_float ),_) -> + | Record_float ),_size) -> (* _size is the length of all_lables*) begin match args with | [e] -> Js_of_lam_record.copy e | _ -> assert false From a0838bb88207a9bf6451e21e0a3099caae3158f2 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 11:12:51 +0800 Subject: [PATCH 02/11] continue removing unused code --- jscomp/core/j.ml | 2 +- jscomp/core/js_analyzer.ml | 2 -- jscomp/core/js_dump.ml | 8 ------ jscomp/core/js_exp_make.ml | 2 -- jscomp/core/js_exp_make.mli | 3 --- jscomp/core/js_fold.ml | 5 +--- jscomp/core/js_map.ml | 6 +---- jscomp/core/lam_dispatch_primitive.ml | 7 ------ jscomp/stdlib/array.ml | 5 ++++ lib/js/array.js | 2 +- lib/whole_compiler.ml | 35 +++------------------------ 11 files changed, 12 insertions(+), 65 deletions(-) diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index 8e9fc7f0c7..593baf9b62 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -110,7 +110,7 @@ and expression_desc = since GC does not rely on it *) | Array_copy of expression (* shallow copy, like [x.slice] *) - | Array_append of expression * expression (* For [caml_array_append]*) + | String_append of expression * expression | Bool of bool (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index 83d5eed8cf..69ccc3607b 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -102,7 +102,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = List.for_all no_side_effect xs | Object kvs -> List.for_all (fun (_property_name, y) -> no_side_effect y ) kvs - | Array_append (a,b) | String_append (a,b) | Seq (a,b) -> no_side_effect a && no_side_effect b | Length (e, _) @@ -264,7 +263,6 @@ let rec eq_expression | Is_null_undefined_to_boolean _ | Array_of_size _ | Array_copy _ - | Array_append _ | String_append _ | Typeof _ | Js_not _ diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 72f68ece45..2867087237 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -511,13 +511,6 @@ and if l > 15 then P.paren_group f 1 action else action () - - | Array_append (e, el) -> - P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in - P.string f ".concat"; - P.paren_group f 1 (fun _ -> arguments cxt f [el])) - | Array_copy e -> P.group f 1 (fun _ -> let cxt = expression 15 cxt f e in @@ -1100,7 +1093,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Anything_to_string _ | String_of_small_int_array _ | Call _ - | Array_append _ | Array_copy _ | Caml_block_tag _ | Seq _ diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index 0a4de5223a..6791779336 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -448,8 +448,6 @@ let char_to_int ?comment (v : t) : t = | Char_of_int v -> v | _ -> {comment; expression_desc = Char_to_int v } -let array_append ?comment e el : t = - { comment ; expression_desc = Array_append (e, el)} let array_copy ?comment e : t = { comment ; expression_desc = Array_copy e} diff --git a/jscomp/core/js_exp_make.mli b/jscomp/core/js_exp_make.mli index 52a1ebad19..3ffaca104b 100644 --- a/jscomp/core/js_exp_make.mli +++ b/jscomp/core/js_exp_make.mli @@ -148,9 +148,6 @@ val function_length : unary_op val char_of_int : unary_op val char_to_int : unary_op - -val array_append : binary_op - val array_copy : unary_op val string_append : binary_op (** diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index 3bd620f88f..0e75f5216e 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -129,8 +129,7 @@ class virtual fold = uninitilized array is not as bad as in ocaml, since GC does not rely on it *) - (* shallow copy, like [x.slice] *) - (* For [caml_array_append]*) (* js true/false*) + (* shallow copy, like [x.slice] *) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) @@ -364,8 +363,6 @@ class virtual fold = | Is_null_undefined_to_boolean _x -> let o = o#expression _x in o | Array_of_size _x -> let o = o#expression _x in o | Array_copy _x -> let o = o#expression _x in o - | Array_append (_x, _x_i1) -> - let o = o#expression _x in let o = o#expression _x_i1 in o | String_append (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o | Bool _x -> let o = o#bool _x in o diff --git a/jscomp/core/js_map.ml b/jscomp/core/js_map.ml index 478cdf4018..6bb857454e 100644 --- a/jscomp/core/js_map.ml +++ b/jscomp/core/js_map.ml @@ -142,8 +142,7 @@ class virtual map = uninitilized array is not as bad as in ocaml, since GC does not rely on it *) - (* shallow copy, like [x.slice] *) - (* For [caml_array_append]*) (* js true/false*) + (* shallow copy, like [x.slice] *) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) @@ -390,9 +389,6 @@ class virtual map = let _x = o#expression _x in Is_null_undefined_to_boolean _x | Array_of_size _x -> let _x = o#expression _x in Array_of_size _x | Array_copy _x -> let _x = o#expression _x in Array_copy _x - | Array_append (_x, _x_i1) -> - let _x = o#expression _x in - let _x_i1 = o#expression _x_i1 in Array_append (_x, _x_i1) | String_append (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in String_append (_x, _x_i1) diff --git a/jscomp/core/lam_dispatch_primitive.ml b/jscomp/core/lam_dispatch_primitive.ml index 721dae85f7..5b7fd043ab 100644 --- a/jscomp/core/lam_dispatch_primitive.ml +++ b/jscomp/core/lam_dispatch_primitive.ml @@ -138,13 +138,6 @@ let translate loc (prim_name : string) | "caml_power_float" -> E.math "pow" args - - | "caml_array_append" -> - begin match args with - | [e0;e1] -> E.array_append e0 e1 - | _ -> assert false - end - | "caml_array_get" -> call Js_runtime_modules.array | "caml_array_get_addr" diff --git a/jscomp/stdlib/array.ml b/jscomp/stdlib/array.ml index 243eeade17..8d6d5aadcd 100644 --- a/jscomp/stdlib/array.ml +++ b/jscomp/stdlib/array.ml @@ -21,7 +21,12 @@ external unsafe_set: 'a array -> int -> 'a -> unit = "%array_unsafe_set" external make: int -> 'a -> 'a array = "caml_make_vect" external create: int -> 'a -> 'a array = "caml_make_vect" external unsafe_sub : 'a array -> int -> int -> 'a array = "caml_array_sub" +#if BS then +external append_prim : 'a array -> 'a array -> 'a array = "concat" +[@@bs.send] +#else external append_prim : 'a array -> 'a array -> 'a array = "caml_array_append" +#end external concat : 'a array list -> 'a array = "caml_array_concat" external unsafe_blit : 'a array -> int -> 'a array -> int -> int -> unit = "caml_array_blit" diff --git a/lib/js/array.js b/lib/js/array.js index a7dfc2ecef..185d81e57d 100644 --- a/lib/js/array.js +++ b/lib/js/array.js @@ -279,7 +279,7 @@ function sort(cmp, a) { Caml_builtin_exceptions.assert_failure, [ "array.ml", - 168, + 173, 4 ] ]; diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index 2bac446c41..7b63a4b958 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -63309,7 +63309,7 @@ and expression_desc = since GC does not rely on it *) | Array_copy of expression (* shallow copy, like [x.slice] *) - | Array_append of expression * expression (* For [caml_array_append]*) + | String_append of expression * expression | Bool of bool (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence @@ -69609,8 +69609,7 @@ class virtual fold = uninitilized array is not as bad as in ocaml, since GC does not rely on it *) - (* shallow copy, like [x.slice] *) - (* For [caml_array_append]*) (* js true/false*) + (* shallow copy, like [x.slice] *) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) @@ -69844,8 +69843,6 @@ class virtual fold = | Is_null_undefined_to_boolean _x -> let o = o#expression _x in o | Array_of_size _x -> let o = o#expression _x in o | Array_copy _x -> let o = o#expression _x in o - | Array_append (_x, _x_i1) -> - let o = o#expression _x in let o = o#expression _x_i1 in o | String_append (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o | Bool _x -> let o = o#bool _x in o @@ -70141,7 +70138,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = List.for_all no_side_effect xs | Object kvs -> List.for_all (fun (_property_name, y) -> no_side_effect y ) kvs - | Array_append (a,b) | String_append (a,b) | Seq (a,b) -> no_side_effect a && no_side_effect b | Length (e, _) @@ -70303,7 +70299,6 @@ let rec eq_expression | Is_null_undefined_to_boolean _ | Array_of_size _ | Array_copy _ - | Array_append _ | String_append _ | Typeof _ | Js_not _ @@ -70952,9 +70947,6 @@ val function_length : unary_op val char_of_int : unary_op val char_to_int : unary_op - -val array_append : binary_op - val array_copy : unary_op val string_append : binary_op (** @@ -71597,8 +71589,6 @@ let char_to_int ?comment (v : t) : t = | Char_of_int v -> v | _ -> {comment; expression_desc = Char_to_int v } -let array_append ?comment e el : t = - { comment ; expression_desc = Array_append (e, el)} let array_copy ?comment e : t = { comment ; expression_desc = Array_copy e} @@ -84714,13 +84704,6 @@ and if l > 15 then P.paren_group f 1 action else action () - - | Array_append (e, el) -> - P.group f 1 (fun _ -> - let cxt = expression 15 cxt f e in - P.string f ".concat"; - P.paren_group f 1 (fun _ -> arguments cxt f [el])) - | Array_copy e -> P.group f 1 (fun _ -> let cxt = expression 15 cxt f e in @@ -85303,7 +85286,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Anything_to_string _ | String_of_small_int_array _ | Call _ - | Array_append _ | Array_copy _ | Caml_block_tag _ | Seq _ @@ -88341,8 +88323,7 @@ class virtual map = uninitilized array is not as bad as in ocaml, since GC does not rely on it *) - (* shallow copy, like [x.slice] *) - (* For [caml_array_append]*) (* js true/false*) + (* shallow copy, like [x.slice] *) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) @@ -88589,9 +88570,6 @@ class virtual map = let _x = o#expression _x in Is_null_undefined_to_boolean _x | Array_of_size _x -> let _x = o#expression _x in Array_of_size _x | Array_copy _x -> let _x = o#expression _x in Array_copy _x - | Array_append (_x, _x_i1) -> - let _x = o#expression _x in - let _x_i1 = o#expression _x_i1 in Array_append (_x, _x_i1) | String_append (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in String_append (_x, _x_i1) @@ -95002,13 +94980,6 @@ let translate loc (prim_name : string) | "caml_power_float" -> E.math "pow" args - - | "caml_array_append" -> - begin match args with - | [e0;e1] -> E.array_append e0 e1 - | _ -> assert false - end - | "caml_array_get" -> call Js_runtime_modules.array | "caml_array_get_addr" From 2a58d7a5afa986212c1ba0ae98223396ff55f6e3 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 11:32:03 +0800 Subject: [PATCH 03/11] remove staled code --- jscomp/core/j.ml | 30 +---- jscomp/core/js_analyzer.ml | 11 -- jscomp/core/js_dump.ml | 20 ---- jscomp/core/js_exp_make.ml | 10 -- jscomp/core/js_exp_make.mli | 9 -- jscomp/core/js_fold.ml | 29 ----- jscomp/core/js_map.ml | 31 ------ jscomp/core/lam_dispatch_primitive.ml | 11 +- jscomp/test/test_per.js | 6 +- lib/whole_compiler.ml | 151 +------------------------- 10 files changed, 11 insertions(+), 297 deletions(-) diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index 593baf9b62..84959446a6 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -129,37 +129,13 @@ and expression_desc = which makes optimizations easier {[ JSON.stringify(value, replacer[, space]) ]} *) - | Anything_to_string of expression - (* for debugging utitlites, - TODO: [Dump] is not necessary with this primitive - Note that the semantics is slightly different from [JSON.stringify] - {[ - JSON.stringify("x") - ]} - {[ - ""x"" - ]} - {[ - JSON.stringify(undefined) - ]} - {[ - undefined - ]} - {[ '' + undefined - ]} - {[ 'undefined' - ]} - *) - | Dump of Js_op.level * expression list + + (* TODO: add {[ Assert of bool * expression ]} *) - (* to support - val log1 : 'a -> unit - val log2 : 'a -> 'b -> unit - val log3 : 'a -> 'b -> 'c -> unit - *) + (* TODO: Add some primitives so that [js inliner] can do a better job *) | Seq of expression * expression diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index 69ccc3607b..4c9da6e887 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -118,8 +118,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Js_not _ | String_of_small_int_array _ | Json_stringify _ - | Anything_to_string _ - | Dump _ | Cond _ | FlatCall _ @@ -240,12 +238,6 @@ let rec eq_expression p0 = p1 && b0 = b1 && eq_expression e0 e1 | _ -> false end - | Dump (l0,es0) -> - begin match y0 with - | Dump(l1,es1) -> - l0 = l1 && eq_expression_list es0 es1 - | _ -> false - end | Seq (a0,b0) -> begin match y0 with | Seq(a1,b1) -> @@ -268,9 +260,6 @@ let rec eq_expression | Js_not _ | String_of_small_int_array _ | Json_stringify _ - | Anything_to_string _ - - | Cond _ | FlatCall _ | String_access _ diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 2867087237..0d5f157b7a 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -518,19 +518,6 @@ and P.string f "()" ; cxt ) - - | Dump (level, el) -> - let obj = - match level with - | Log -> "log" - | Info -> "info" - | Warn -> "warn" - | Error -> "error" in - P.group f 1 (fun _ -> - P.string f L.console; - P.string f L.dot; - P.string f obj ; - P.paren_group f 1 (fun _ -> arguments cxt f el)) | Json_stringify e -> P.group f 1 (fun _ -> @@ -762,11 +749,6 @@ and P.space f ; expression 13 cxt f delta end - | Anything_to_string e -> - (* Note that we should not apply any smart construtor here, - it's purely a convenice for pretty-printing - *) - expression_desc cxt l f (Bin (Plus, E.empty_string_literal , e)) | Bin (Minus, {expression_desc = Number (Int {i=0l;_} | Float {f = "0."})}, e) (* TODO: @@ -1090,7 +1072,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Caml_block_set_tag _ | Length _ | Caml_block_set_length _ - | Anything_to_string _ | String_of_small_int_array _ | Call _ | Array_copy _ @@ -1106,7 +1087,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | String_append _ | Char_of_int _ | Char_to_int _ - | Dump _ | Json_stringify _ | Math _ | Var _ diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index 6791779336..7d8e8ab590 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -222,8 +222,6 @@ let math ?comment v args : t = Used in [string_of_int] and format "%d" TODO: optimize *) -let int_to_string ?comment (e : t) : t = - {expression_desc = Anything_to_string e ; comment} (* Attention: Shared *mutable state* is evil, [Js_fun_env.empty] is a mutable state .. @@ -452,12 +450,6 @@ let char_to_int ?comment (v : t) : t = let array_copy ?comment e : t = { comment ; expression_desc = Array_copy e} -(* Note that this return [undefined] in JS, - it should be wrapped to avoid leak [undefined] into - OCaml -*) -let dump ?comment level el : t = - {comment ; expression_desc = Dump(level,el)} (* let to_json_string ?comment e : t = { comment; expression_desc = Json_stringify e } *) @@ -472,8 +464,6 @@ let rec string_append ?comment (e : t) (el : t) : t = String_append ({expression_desc = Str(_,c)} ,d) -> string_append ?comment (string_append a (str (b ^ c))) d | Str (_,a), Str (_,b) -> str ?comment (a ^ b) - | _, Anything_to_string b -> string_append ?comment e b - | Anything_to_string b, _ -> string_append ?comment b el | _, _ -> {comment ; expression_desc = String_append(e,el)} diff --git a/jscomp/core/js_exp_make.mli b/jscomp/core/js_exp_make.mli index 3ffaca104b..dac87cc518 100644 --- a/jscomp/core/js_exp_make.mli +++ b/jscomp/core/js_exp_make.mli @@ -246,15 +246,6 @@ val call : ?comment:string -> info:Js_call_info.t -> t -> t list -> t val flat_call : binary_op -val dump : ?comment:string -> Js_op.level -> t list -> t - - - -(** see {!https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus}*) -(* val to_number : unary_op *) -val int_to_string : unary_op - - val new_ : ?comment:string -> J.expression -> J.expression list -> t val array : diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index 0e75f5216e..8f49da48c2 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -141,36 +141,11 @@ class virtual fold = (* TODO: in the future, it might make sense to group primitivie by type, which makes optimizations easier {[ JSON.stringify(value, replacer[, space]) ]} - *) - (* for debugging utitlites, - TODO: [Dump] is not necessary with this primitive - Note that the semantics is slightly different from [JSON.stringify] - {[ - JSON.stringify("x") - ]} - {[ - ""x"" - ]} - {[ - JSON.stringify(undefined) - ]} - {[ - undefined - ]} - {[ '' + undefined - ]} - {[ 'undefined' - ]} *) (* TODO: add {[ Assert of bool * expression ]} *) - (* to support - val log1 : 'a -> unit - val log2 : 'a -> 'b -> unit - val log3 : 'a -> 'b -> 'c -> unit - *) (* TODO: Add some primitives so that [js inliner] can do a better job *) (* [int_op] will guarantee return [int32] bits https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators *) @@ -370,10 +345,6 @@ class virtual fold = | Js_not _x -> let o = o#expression _x in o | String_of_small_int_array _x -> let o = o#expression _x in o | Json_stringify _x -> let o = o#expression _x in o - | Anything_to_string _x -> let o = o#expression _x in o - | Dump (_x, _x_i1) -> - let o = o#unknown _x in - let o = o#list (fun o -> o#expression) _x_i1 in o | Seq (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o | Cond (_x, _x_i1, _x_i2) -> diff --git a/jscomp/core/js_map.ml b/jscomp/core/js_map.ml index 6bb857454e..6ea59ecf05 100644 --- a/jscomp/core/js_map.ml +++ b/jscomp/core/js_map.ml @@ -154,36 +154,11 @@ class virtual map = (* TODO: in the future, it might make sense to group primitivie by type, which makes optimizations easier {[ JSON.stringify(value, replacer[, space]) ]} - *) - (* for debugging utitlites, - TODO: [Dump] is not necessary with this primitive - Note that the semantics is slightly different from [JSON.stringify] - {[ - JSON.stringify("x") - ]} - {[ - ""x"" - ]} - {[ - JSON.stringify(undefined) - ]} - {[ - undefined - ]} - {[ '' + undefined - ]} - {[ 'undefined' - ]} *) (* TODO: add {[ Assert of bool * expression ]} *) - (* to support - val log1 : 'a -> unit - val log2 : 'a -> 'b -> unit - val log3 : 'a -> 'b -> 'c -> unit - *) (* TODO: Add some primitives so that [js inliner] can do a better job *) (* [int_op] will guarantee return [int32] bits https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators *) @@ -398,12 +373,6 @@ class virtual map = | String_of_small_int_array _x -> let _x = o#expression _x in String_of_small_int_array _x | Json_stringify _x -> let _x = o#expression _x in Json_stringify _x - | Anything_to_string _x -> - let _x = o#expression _x in Anything_to_string _x - | Dump (_x, _x_i1) -> - let _x = o#unknown _x in - let _x_i1 = o#list (fun o -> o#expression) _x_i1 - in Dump (_x, _x_i1) | Seq (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in Seq (_x, _x_i1) diff --git a/jscomp/core/lam_dispatch_primitive.ml b/jscomp/core/lam_dispatch_primitive.ml index 5b7fd043ab..25132a2c57 100644 --- a/jscomp/core/lam_dispatch_primitive.ml +++ b/jscomp/core/lam_dispatch_primitive.ml @@ -685,20 +685,11 @@ let translate loc (prim_name : string) | "caml_nativeint_of_string" | "caml_int64_format" | "caml_int64_of_string" + | "caml_format_int" -> call Js_runtime_modules.format - | "caml_format_int" -> - begin match args with - | [ {expression_desc = Str (_, "%d"); _}; v] - -> - E.int_to_string v - | _ -> - call Js_runtime_modules.format - end (* "caml_alloc_dummy"; *) (* TODO: "caml_alloc_dummy_float"; *) - - | "caml_obj_is_block" -> begin match args with diff --git a/jscomp/test/test_per.js b/jscomp/test/test_per.js index 900e09542b..b9f793f670 100644 --- a/jscomp/test/test_per.js +++ b/jscomp/test/test_per.js @@ -131,7 +131,7 @@ function bool_of_string(param) { } function string_of_int(n) { - return "" + n; + return Caml_format.caml_format_int("%d", n); } function valid_float_lexem(s) { @@ -438,7 +438,7 @@ function print_bytes(s) { } function print_int(i) { - return output_string(stdout, "" + i); + return output_string(stdout, Caml_format.caml_format_int("%d", i)); } function print_float(f) { @@ -469,7 +469,7 @@ function prerr_bytes(s) { } function prerr_int(i) { - return output_string(stderr, "" + i); + return output_string(stderr, Caml_format.caml_format_int("%d", i)); } function prerr_float(f) { diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index 7b63a4b958..8b5a7dd5be 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -63328,37 +63328,13 @@ and expression_desc = which makes optimizations easier {[ JSON.stringify(value, replacer[, space]) ]} *) - | Anything_to_string of expression - (* for debugging utitlites, - TODO: [Dump] is not necessary with this primitive - Note that the semantics is slightly different from [JSON.stringify] - {[ - JSON.stringify("x") - ]} - {[ - ""x"" - ]} - {[ - JSON.stringify(undefined) - ]} - {[ - undefined - ]} - {[ '' + undefined - ]} - {[ 'undefined' - ]} - *) - | Dump of Js_op.level * expression list + + (* TODO: add {[ Assert of bool * expression ]} *) - (* to support - val log1 : 'a -> unit - val log2 : 'a -> 'b -> unit - val log3 : 'a -> 'b -> 'c -> unit - *) + (* TODO: Add some primitives so that [js inliner] can do a better job *) | Seq of expression * expression @@ -69621,36 +69597,11 @@ class virtual fold = (* TODO: in the future, it might make sense to group primitivie by type, which makes optimizations easier {[ JSON.stringify(value, replacer[, space]) ]} - *) - (* for debugging utitlites, - TODO: [Dump] is not necessary with this primitive - Note that the semantics is slightly different from [JSON.stringify] - {[ - JSON.stringify("x") - ]} - {[ - ""x"" - ]} - {[ - JSON.stringify(undefined) - ]} - {[ - undefined - ]} - {[ '' + undefined - ]} - {[ 'undefined' - ]} *) (* TODO: add {[ Assert of bool * expression ]} *) - (* to support - val log1 : 'a -> unit - val log2 : 'a -> 'b -> unit - val log3 : 'a -> 'b -> 'c -> unit - *) (* TODO: Add some primitives so that [js inliner] can do a better job *) (* [int_op] will guarantee return [int32] bits https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators *) @@ -69850,10 +69801,6 @@ class virtual fold = | Js_not _x -> let o = o#expression _x in o | String_of_small_int_array _x -> let o = o#expression _x in o | Json_stringify _x -> let o = o#expression _x in o - | Anything_to_string _x -> let o = o#expression _x in o - | Dump (_x, _x_i1) -> - let o = o#unknown _x in - let o = o#list (fun o -> o#expression) _x_i1 in o | Seq (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o | Cond (_x, _x_i1, _x_i2) -> @@ -70154,8 +70101,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Js_not _ | String_of_small_int_array _ | Json_stringify _ - | Anything_to_string _ - | Dump _ | Cond _ | FlatCall _ @@ -70276,12 +70221,6 @@ let rec eq_expression p0 = p1 && b0 = b1 && eq_expression e0 e1 | _ -> false end - | Dump (l0,es0) -> - begin match y0 with - | Dump(l1,es1) -> - l0 = l1 && eq_expression_list es0 es1 - | _ -> false - end | Seq (a0,b0) -> begin match y0 with | Seq(a1,b1) -> @@ -70304,9 +70243,6 @@ let rec eq_expression | Js_not _ | String_of_small_int_array _ | Json_stringify _ - | Anything_to_string _ - - | Cond _ | FlatCall _ | String_access _ @@ -71045,15 +70981,6 @@ val call : ?comment:string -> info:Js_call_info.t -> t -> t list -> t val flat_call : binary_op -val dump : ?comment:string -> Js_op.level -> t list -> t - - - -(** see {!https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus}*) -(* val to_number : unary_op *) -val int_to_string : unary_op - - val new_ : ?comment:string -> J.expression -> J.expression list -> t val array : @@ -71363,8 +71290,6 @@ let math ?comment v args : t = Used in [string_of_int] and format "%d" TODO: optimize *) -let int_to_string ?comment (e : t) : t = - {expression_desc = Anything_to_string e ; comment} (* Attention: Shared *mutable state* is evil, [Js_fun_env.empty] is a mutable state .. @@ -71593,12 +71518,6 @@ let char_to_int ?comment (v : t) : t = let array_copy ?comment e : t = { comment ; expression_desc = Array_copy e} -(* Note that this return [undefined] in JS, - it should be wrapped to avoid leak [undefined] into - OCaml -*) -let dump ?comment level el : t = - {comment ; expression_desc = Dump(level,el)} (* let to_json_string ?comment e : t = { comment; expression_desc = Json_stringify e } *) @@ -71613,8 +71532,6 @@ let rec string_append ?comment (e : t) (el : t) : t = String_append ({expression_desc = Str(_,c)} ,d) -> string_append ?comment (string_append a (str (b ^ c))) d | Str (_,a), Str (_,b) -> str ?comment (a ^ b) - | _, Anything_to_string b -> string_append ?comment e b - | Anything_to_string b, _ -> string_append ?comment b el | _, _ -> {comment ; expression_desc = String_append(e,el)} @@ -84711,19 +84628,6 @@ and P.string f "()" ; cxt ) - - | Dump (level, el) -> - let obj = - match level with - | Log -> "log" - | Info -> "info" - | Warn -> "warn" - | Error -> "error" in - P.group f 1 (fun _ -> - P.string f L.console; - P.string f L.dot; - P.string f obj ; - P.paren_group f 1 (fun _ -> arguments cxt f el)) | Json_stringify e -> P.group f 1 (fun _ -> @@ -84955,11 +84859,6 @@ and P.space f ; expression 13 cxt f delta end - | Anything_to_string e -> - (* Note that we should not apply any smart construtor here, - it's purely a convenice for pretty-printing - *) - expression_desc cxt l f (Bin (Plus, E.empty_string_literal , e)) | Bin (Minus, {expression_desc = Number (Int {i=0l;_} | Float {f = "0."})}, e) (* TODO: @@ -85283,7 +85182,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Caml_block_set_tag _ | Length _ | Caml_block_set_length _ - | Anything_to_string _ | String_of_small_int_array _ | Call _ | Array_copy _ @@ -85299,7 +85197,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | String_append _ | Char_of_int _ | Char_to_int _ - | Dump _ | Json_stringify _ | Math _ | Var _ @@ -88335,36 +88232,11 @@ class virtual map = (* TODO: in the future, it might make sense to group primitivie by type, which makes optimizations easier {[ JSON.stringify(value, replacer[, space]) ]} - *) - (* for debugging utitlites, - TODO: [Dump] is not necessary with this primitive - Note that the semantics is slightly different from [JSON.stringify] - {[ - JSON.stringify("x") - ]} - {[ - ""x"" - ]} - {[ - JSON.stringify(undefined) - ]} - {[ - undefined - ]} - {[ '' + undefined - ]} - {[ 'undefined' - ]} *) (* TODO: add {[ Assert of bool * expression ]} *) - (* to support - val log1 : 'a -> unit - val log2 : 'a -> 'b -> unit - val log3 : 'a -> 'b -> 'c -> unit - *) (* TODO: Add some primitives so that [js inliner] can do a better job *) (* [int_op] will guarantee return [int32] bits https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators *) @@ -88579,12 +88451,6 @@ class virtual map = | String_of_small_int_array _x -> let _x = o#expression _x in String_of_small_int_array _x | Json_stringify _x -> let _x = o#expression _x in Json_stringify _x - | Anything_to_string _x -> - let _x = o#expression _x in Anything_to_string _x - | Dump (_x, _x_i1) -> - let _x = o#unknown _x in - let _x_i1 = o#list (fun o -> o#expression) _x_i1 - in Dump (_x, _x_i1) | Seq (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in Seq (_x, _x_i1) @@ -95527,20 +95393,11 @@ let translate loc (prim_name : string) | "caml_nativeint_of_string" | "caml_int64_format" | "caml_int64_of_string" + | "caml_format_int" -> call Js_runtime_modules.format - | "caml_format_int" -> - begin match args with - | [ {expression_desc = Str (_, "%d"); _}; v] - -> - E.int_to_string v - | _ -> - call Js_runtime_modules.format - end (* "caml_alloc_dummy"; *) (* TODO: "caml_alloc_dummy_float"; *) - - | "caml_obj_is_block" -> begin match args with From adcbb6801675376bd05dd6ebed208e53eb8f991b Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 11:34:11 +0800 Subject: [PATCH 04/11] continue --- jscomp/core/j.ml | 6 ------ jscomp/core/js_analyzer.ml | 2 -- jscomp/core/js_dump.ml | 9 --------- jscomp/core/js_exp_make.ml | 4 ---- jscomp/core/js_fold.ml | 5 ----- jscomp/core/js_map.ml | 5 ----- lib/whole_compiler.ml | 31 ------------------------------- 7 files changed, 62 deletions(-) diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index 84959446a6..61b7bdfc05 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -124,12 +124,6 @@ and expression_desc = like [+true], note this ast talks using js terminnology unless explicity stated *) - | Json_stringify of expression - (* TODO: in the future, it might make sense to group primitivie by type, - which makes optimizations easier - {[ JSON.stringify(value, replacer[, space]) ]} - *) - (* TODO: add diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index 4c9da6e887..baa3c63f3c 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -117,7 +117,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Array_copy _ | Js_not _ | String_of_small_int_array _ - | Json_stringify _ | Cond _ | FlatCall _ @@ -259,7 +258,6 @@ let rec eq_expression | Typeof _ | Js_not _ | String_of_small_int_array _ - | Json_stringify _ | Cond _ | FlatCall _ | String_access _ diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 0d5f157b7a..669ce6fc08 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -518,14 +518,6 @@ and P.string f "()" ; cxt ) - | Json_stringify e - -> - P.group f 1 (fun _ -> - P.string f L.json ; - P.string f L.dot; - P.string f L.stringify; - P.paren_group f 1 (fun _ -> expression 0 cxt f e ) - ) | Char_to_int e -> begin match e.expression_desc with | String_access (a,b) -> @@ -1087,7 +1079,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | String_append _ | Char_of_int _ | Char_to_int _ - | Json_stringify _ | Math _ | Var _ | Str _ diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index 7d8e8ab590..def68b7f22 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -450,10 +450,6 @@ let char_to_int ?comment (v : t) : t = let array_copy ?comment e : t = { comment ; expression_desc = Array_copy e} - -(* let to_json_string ?comment e : t = - { comment; expression_desc = Json_stringify e } *) - let rec string_append ?comment (e : t) (el : t) : t = match e.expression_desc , el.expression_desc with | Str(_,a), String_append ({expression_desc = Str(_,b)}, c) -> diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index 8f49da48c2..04eb44de66 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -138,10 +138,6 @@ class virtual fold = like [+true], note this ast talks using js terminnology unless explicity stated *) - (* TODO: in the future, it might make sense to group primitivie by type, - which makes optimizations easier - {[ JSON.stringify(value, replacer[, space]) ]} - *) (* TODO: add {[ Assert of bool * expression ]} @@ -344,7 +340,6 @@ class virtual fold = | Typeof _x -> let o = o#expression _x in o | Js_not _x -> let o = o#expression _x in o | String_of_small_int_array _x -> let o = o#expression _x in o - | Json_stringify _x -> let o = o#expression _x in o | Seq (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o | Cond (_x, _x_i1, _x_i2) -> diff --git a/jscomp/core/js_map.ml b/jscomp/core/js_map.ml index 6ea59ecf05..74bc72b7c0 100644 --- a/jscomp/core/js_map.ml +++ b/jscomp/core/js_map.ml @@ -151,10 +151,6 @@ class virtual map = like [+true], note this ast talks using js terminnology unless explicity stated *) - (* TODO: in the future, it might make sense to group primitivie by type, - which makes optimizations easier - {[ JSON.stringify(value, replacer[, space]) ]} - *) (* TODO: add {[ Assert of bool * expression ]} @@ -372,7 +368,6 @@ class virtual map = | Js_not _x -> let _x = o#expression _x in Js_not _x | String_of_small_int_array _x -> let _x = o#expression _x in String_of_small_int_array _x - | Json_stringify _x -> let _x = o#expression _x in Json_stringify _x | Seq (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in Seq (_x, _x_i1) diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index 8b5a7dd5be..34d79e61df 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -63323,12 +63323,6 @@ and expression_desc = like [+true], note this ast talks using js terminnology unless explicity stated *) - | Json_stringify of expression - (* TODO: in the future, it might make sense to group primitivie by type, - which makes optimizations easier - {[ JSON.stringify(value, replacer[, space]) ]} - *) - (* TODO: add @@ -69594,10 +69588,6 @@ class virtual fold = like [+true], note this ast talks using js terminnology unless explicity stated *) - (* TODO: in the future, it might make sense to group primitivie by type, - which makes optimizations easier - {[ JSON.stringify(value, replacer[, space]) ]} - *) (* TODO: add {[ Assert of bool * expression ]} @@ -69800,7 +69790,6 @@ class virtual fold = | Typeof _x -> let o = o#expression _x in o | Js_not _x -> let o = o#expression _x in o | String_of_small_int_array _x -> let o = o#expression _x in o - | Json_stringify _x -> let o = o#expression _x in o | Seq (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o | Cond (_x, _x_i1, _x_i2) -> @@ -70100,7 +70089,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Array_copy _ | Js_not _ | String_of_small_int_array _ - | Json_stringify _ | Cond _ | FlatCall _ @@ -70242,7 +70230,6 @@ let rec eq_expression | Typeof _ | Js_not _ | String_of_small_int_array _ - | Json_stringify _ | Cond _ | FlatCall _ | String_access _ @@ -71518,10 +71505,6 @@ let char_to_int ?comment (v : t) : t = let array_copy ?comment e : t = { comment ; expression_desc = Array_copy e} - -(* let to_json_string ?comment e : t = - { comment; expression_desc = Json_stringify e } *) - let rec string_append ?comment (e : t) (el : t) : t = match e.expression_desc , el.expression_desc with | Str(_,a), String_append ({expression_desc = Str(_,b)}, c) -> @@ -84628,14 +84611,6 @@ and P.string f "()" ; cxt ) - | Json_stringify e - -> - P.group f 1 (fun _ -> - P.string f L.json ; - P.string f L.dot; - P.string f L.stringify; - P.paren_group f 1 (fun _ -> expression 0 cxt f e ) - ) | Char_to_int e -> begin match e.expression_desc with | String_access (a,b) -> @@ -85197,7 +85172,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | String_append _ | Char_of_int _ | Char_to_int _ - | Json_stringify _ | Math _ | Var _ | Str _ @@ -88229,10 +88203,6 @@ class virtual map = like [+true], note this ast talks using js terminnology unless explicity stated *) - (* TODO: in the future, it might make sense to group primitivie by type, - which makes optimizations easier - {[ JSON.stringify(value, replacer[, space]) ]} - *) (* TODO: add {[ Assert of bool * expression ]} @@ -88450,7 +88420,6 @@ class virtual map = | Js_not _x -> let _x = o#expression _x in Js_not _x | String_of_small_int_array _x -> let _x = o#expression _x in String_of_small_int_array _x - | Json_stringify _x -> let _x = o#expression _x in Json_stringify _x | Seq (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in Seq (_x, _x_i1) From 4401d7e1d06875b7c2f2baa7f2f3cff58a22aa21 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 11:42:16 +0800 Subject: [PATCH 05/11] continue --- jscomp/core/j.ml | 2 +- jscomp/core/js_analyzer.ml | 4 ++-- jscomp/core/js_dump.ml | 4 ++-- jscomp/core/js_exp_make.ml | 13 ++----------- jscomp/core/js_fold.ml | 2 +- jscomp/core/js_map.ml | 4 ++-- lib/whole_compiler.ml | 29 ++++++++++------------------- 7 files changed, 20 insertions(+), 38 deletions(-) diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index 61b7bdfc05..e96e9b47b5 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -102,7 +102,7 @@ and expression_desc = | Length of expression * length_object | Char_of_int of expression | Char_to_int of expression - | Is_null_undefined_to_boolean of expression + | Is_null_or_undefined of expression (** where we use a trick [== null ] *) | Array_of_size of expression (* used in [#create_array] primitive, note having diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index baa3c63f3c..72390c8f69 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -89,7 +89,7 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Fun _ -> true | Number _ -> true (* Can be refined later *) | Access (a,b) -> no_side_effect a && no_side_effect b - | Is_null_undefined_to_boolean b -> no_side_effect b + | Is_null_or_undefined b -> no_side_effect b | Str (b,_) -> b | Array (xs,_mutable_flag) | Caml_block (xs, _mutable_flag, _, _) @@ -251,7 +251,7 @@ let rec eq_expression | Length _ | Char_of_int _ | Char_to_int _ - | Is_null_undefined_to_boolean _ + | Is_null_or_undefined _ | Array_of_size _ | Array_copy _ | String_append _ diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 669ce6fc08..92515a1090 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -603,7 +603,7 @@ and else action () ); cxt - | Is_null_undefined_to_boolean e -> + | Is_null_or_undefined e -> let action = (fun _ -> let cxt = expression 1 cxt f e in P.space f ; @@ -1072,7 +1072,7 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Dot _ | Cond _ | Bin _ - | Is_null_undefined_to_boolean _ + | Is_null_or_undefined _ | String_access _ | Access _ | Array_of_size _ diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index def68b7f22..57f1b2ab7f 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -173,14 +173,6 @@ let make_block ?comment tag tag_info es mutable_flag : t = comment } -(* let uninitialized_object ?comment tag size : t = - { expression_desc = Caml_uninitialized_obj(tag,size); comment } *) - -(* let uninitialized_array ?comment (e : t) : t = - match e.expression_desc with - | Number (Int {i = 0l; _}) -> array ?comment NA [] - | _ -> {comment; expression_desc = Array_of_size e} *) - module L = Literals (* Invariant: this is relevant to how we encode string @@ -1246,10 +1238,9 @@ let is_null_undefined ?comment (x: t) : t = when Ext_ident.is_js id -> caml_true | Number _ | Array _ | Caml_block _ -> caml_false - | _ -> - + | _ -> { comment ; - expression_desc = Is_null_undefined_to_boolean x + expression_desc = Is_null_or_undefined x } let eq_null_undefined_boolean ?comment (a : t) (b : t) = diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index 04eb44de66..87868836f6 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -331,7 +331,7 @@ class virtual fold = let o = o#expression _x in let o = o#length_object _x_i1 in o | Char_of_int _x -> let o = o#expression _x in o | Char_to_int _x -> let o = o#expression _x in o - | Is_null_undefined_to_boolean _x -> let o = o#expression _x in o + | Is_null_or_undefined _x -> let o = o#expression _x in o | Array_of_size _x -> let o = o#expression _x in o | Array_copy _x -> let o = o#expression _x in o | String_append (_x, _x_i1) -> diff --git a/jscomp/core/js_map.ml b/jscomp/core/js_map.ml index 74bc72b7c0..22bcb32dff 100644 --- a/jscomp/core/js_map.ml +++ b/jscomp/core/js_map.ml @@ -356,8 +356,8 @@ class virtual map = let _x_i1 = o#length_object _x_i1 in Length (_x, _x_i1) | Char_of_int _x -> let _x = o#expression _x in Char_of_int _x | Char_to_int _x -> let _x = o#expression _x in Char_to_int _x - | Is_null_undefined_to_boolean _x -> - let _x = o#expression _x in Is_null_undefined_to_boolean _x + | Is_null_or_undefined _x -> + let _x = o#expression _x in Is_null_or_undefined _x | Array_of_size _x -> let _x = o#expression _x in Array_of_size _x | Array_copy _x -> let _x = o#expression _x in Array_copy _x | String_append (_x, _x_i1) -> diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index 34d79e61df..c35638e243 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -63301,7 +63301,7 @@ and expression_desc = | Length of expression * length_object | Char_of_int of expression | Char_to_int of expression - | Is_null_undefined_to_boolean of expression + | Is_null_or_undefined of expression (** where we use a trick [== null ] *) | Array_of_size of expression (* used in [#create_array] primitive, note having @@ -69781,7 +69781,7 @@ class virtual fold = let o = o#expression _x in let o = o#length_object _x_i1 in o | Char_of_int _x -> let o = o#expression _x in o | Char_to_int _x -> let o = o#expression _x in o - | Is_null_undefined_to_boolean _x -> let o = o#expression _x in o + | Is_null_or_undefined _x -> let o = o#expression _x in o | Array_of_size _x -> let o = o#expression _x in o | Array_copy _x -> let o = o#expression _x in o | String_append (_x, _x_i1) -> @@ -70061,7 +70061,7 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Fun _ -> true | Number _ -> true (* Can be refined later *) | Access (a,b) -> no_side_effect a && no_side_effect b - | Is_null_undefined_to_boolean b -> no_side_effect b + | Is_null_or_undefined b -> no_side_effect b | Str (b,_) -> b | Array (xs,_mutable_flag) | Caml_block (xs, _mutable_flag, _, _) @@ -70223,7 +70223,7 @@ let rec eq_expression | Length _ | Char_of_int _ | Char_to_int _ - | Is_null_undefined_to_boolean _ + | Is_null_or_undefined _ | Array_of_size _ | Array_copy _ | String_append _ @@ -71228,14 +71228,6 @@ let make_block ?comment tag tag_info es mutable_flag : t = comment } -(* let uninitialized_object ?comment tag size : t = - { expression_desc = Caml_uninitialized_obj(tag,size); comment } *) - -(* let uninitialized_array ?comment (e : t) : t = - match e.expression_desc with - | Number (Int {i = 0l; _}) -> array ?comment NA [] - | _ -> {comment; expression_desc = Array_of_size e} *) - module L = Literals (* Invariant: this is relevant to how we encode string @@ -72301,10 +72293,9 @@ let is_null_undefined ?comment (x: t) : t = when Ext_ident.is_js id -> caml_true | Number _ | Array _ | Caml_block _ -> caml_false - | _ -> - + | _ -> { comment ; - expression_desc = Is_null_undefined_to_boolean x + expression_desc = Is_null_or_undefined x } let eq_null_undefined_boolean ?comment (a : t) (b : t) = @@ -84696,7 +84687,7 @@ and else action () ); cxt - | Is_null_undefined_to_boolean e -> + | Is_null_or_undefined e -> let action = (fun _ -> let cxt = expression 1 cxt f e in P.space f ; @@ -85165,7 +85156,7 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Dot _ | Cond _ | Bin _ - | Is_null_undefined_to_boolean _ + | Is_null_or_undefined _ | String_access _ | Access _ | Array_of_size _ @@ -88408,8 +88399,8 @@ class virtual map = let _x_i1 = o#length_object _x_i1 in Length (_x, _x_i1) | Char_of_int _x -> let _x = o#expression _x in Char_of_int _x | Char_to_int _x -> let _x = o#expression _x in Char_to_int _x - | Is_null_undefined_to_boolean _x -> - let _x = o#expression _x in Is_null_undefined_to_boolean _x + | Is_null_or_undefined _x -> + let _x = o#expression _x in Is_null_or_undefined _x | Array_of_size _x -> let _x = o#expression _x in Array_of_size _x | Array_copy _x -> let _x = o#expression _x in Array_copy _x | String_append (_x, _x_i1) -> From 2ed52813a8b7a61af263b32c77f9475418db4f44 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 11:43:44 +0800 Subject: [PATCH 06/11] continue --- jscomp/core/j.ml | 5 ----- jscomp/core/js_analyzer.ml | 2 -- jscomp/core/js_dump.ml | 11 ----------- jscomp/core/js_fold.ml | 5 ----- jscomp/core/js_map.ml | 5 ----- lib/whole_compiler.ml | 28 ---------------------------- 6 files changed, 56 deletions(-) diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index e96e9b47b5..a221a4e447 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -104,11 +104,6 @@ and expression_desc = | Char_to_int of expression | Is_null_or_undefined of expression (** where we use a trick [== null ] *) - | Array_of_size of expression - (* used in [#create_array] primitive, note having - uninitilized array is not as bad as in ocaml, - since GC does not rely on it - *) | Array_copy of expression (* shallow copy, like [x.slice] *) | String_append of expression * expression diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index 72390c8f69..683e746f82 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -113,7 +113,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Bin (op, a, b) -> op <> Eq && no_side_effect a && no_side_effect b | Math _ - | Array_of_size _ | Array_copy _ | Js_not _ | String_of_small_int_array _ @@ -252,7 +251,6 @@ let rec eq_expression | Char_of_int _ | Char_to_int _ | Is_null_or_undefined _ - | Array_of_size _ | Array_copy _ | String_append _ | Typeof _ diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 92515a1090..53e0e5e8b9 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -874,16 +874,6 @@ and in if l > 15 then P.paren_group f 1 action else action () - | Array_of_size e -> - let action () = - P.group f 1 @@ fun _ -> - P.string f L.new_; - P.space f; - P.string f L.array; - P.paren_group f 1 @@ fun _ -> expression 0 cxt f e - in - if l > 15 then P.paren_group f 1 action else action () - | Cond (e, e1, e2) -> let action () = (* P.group f 1 @@ fun _ -> *) @@ -1075,7 +1065,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Is_null_or_undefined _ | String_access _ | Access _ - | Array_of_size _ | String_append _ | Char_of_int _ | Char_to_int _ diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index 87868836f6..7e0deb068b 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -125,10 +125,6 @@ class virtual fold = ]} *) (** where we use a trick [== null ] *) - (* used in [#create_array] primitive, note having - uninitilized array is not as bad as in ocaml, - since GC does not rely on it - *) (* shallow copy, like [x.slice] *) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator @@ -332,7 +328,6 @@ class virtual fold = | Char_of_int _x -> let o = o#expression _x in o | Char_to_int _x -> let o = o#expression _x in o | Is_null_or_undefined _x -> let o = o#expression _x in o - | Array_of_size _x -> let o = o#expression _x in o | Array_copy _x -> let o = o#expression _x in o | String_append (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o diff --git a/jscomp/core/js_map.ml b/jscomp/core/js_map.ml index 22bcb32dff..5f414855f6 100644 --- a/jscomp/core/js_map.ml +++ b/jscomp/core/js_map.ml @@ -138,10 +138,6 @@ class virtual map = ]} *) (** where we use a trick [== null ] *) - (* used in [#create_array] primitive, note having - uninitilized array is not as bad as in ocaml, - since GC does not rely on it - *) (* shallow copy, like [x.slice] *) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator @@ -358,7 +354,6 @@ class virtual map = | Char_to_int _x -> let _x = o#expression _x in Char_to_int _x | Is_null_or_undefined _x -> let _x = o#expression _x in Is_null_or_undefined _x - | Array_of_size _x -> let _x = o#expression _x in Array_of_size _x | Array_copy _x -> let _x = o#expression _x in Array_copy _x | String_append (_x, _x_i1) -> let _x = o#expression _x in diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index c35638e243..dc4626952e 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -63303,11 +63303,6 @@ and expression_desc = | Char_to_int of expression | Is_null_or_undefined of expression (** where we use a trick [== null ] *) - | Array_of_size of expression - (* used in [#create_array] primitive, note having - uninitilized array is not as bad as in ocaml, - since GC does not rely on it - *) | Array_copy of expression (* shallow copy, like [x.slice] *) | String_append of expression * expression @@ -69575,10 +69570,6 @@ class virtual fold = ]} *) (** where we use a trick [== null ] *) - (* used in [#create_array] primitive, note having - uninitilized array is not as bad as in ocaml, - since GC does not rely on it - *) (* shallow copy, like [x.slice] *) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator @@ -69782,7 +69773,6 @@ class virtual fold = | Char_of_int _x -> let o = o#expression _x in o | Char_to_int _x -> let o = o#expression _x in o | Is_null_or_undefined _x -> let o = o#expression _x in o - | Array_of_size _x -> let o = o#expression _x in o | Array_copy _x -> let o = o#expression _x in o | String_append (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o @@ -70085,7 +70075,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Bin (op, a, b) -> op <> Eq && no_side_effect a && no_side_effect b | Math _ - | Array_of_size _ | Array_copy _ | Js_not _ | String_of_small_int_array _ @@ -70224,7 +70213,6 @@ let rec eq_expression | Char_of_int _ | Char_to_int _ | Is_null_or_undefined _ - | Array_of_size _ | Array_copy _ | String_append _ | Typeof _ @@ -84958,16 +84946,6 @@ and in if l > 15 then P.paren_group f 1 action else action () - | Array_of_size e -> - let action () = - P.group f 1 @@ fun _ -> - P.string f L.new_; - P.space f; - P.string f L.array; - P.paren_group f 1 @@ fun _ -> expression 0 cxt f e - in - if l > 15 then P.paren_group f 1 action else action () - | Cond (e, e1, e2) -> let action () = (* P.group f 1 @@ fun _ -> *) @@ -85159,7 +85137,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Is_null_or_undefined _ | String_access _ | Access _ - | Array_of_size _ | String_append _ | Char_of_int _ | Char_to_int _ @@ -88181,10 +88158,6 @@ class virtual map = ]} *) (** where we use a trick [== null ] *) - (* used in [#create_array] primitive, note having - uninitilized array is not as bad as in ocaml, - since GC does not rely on it - *) (* shallow copy, like [x.slice] *) (* js true/false*) (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator @@ -88401,7 +88374,6 @@ class virtual map = | Char_to_int _x -> let _x = o#expression _x in Char_to_int _x | Is_null_or_undefined _x -> let _x = o#expression _x in Is_null_or_undefined _x - | Array_of_size _x -> let _x = o#expression _x in Array_of_size _x | Array_copy _x -> let _x = o#expression _x in Array_copy _x | String_append (_x, _x_i1) -> let _x = o#expression _x in From 25008642005144b6c81296bfc876f6d6917c133d Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 11:48:06 +0800 Subject: [PATCH 07/11] continue --- jscomp/bin/all_ounit_tests.ml | 2 ++ jscomp/core/js_exp_make.ml | 1 + jscomp/ext/literals.ml | 1 + jscomp/ext/literals.mli | 1 + lib/bsb.ml | 2 ++ lib/bsb_helper.ml | 2 ++ lib/bsdep.ml | 2 ++ lib/bsppx.ml | 2 ++ lib/whole_compiler.ml | 3 +++ 9 files changed, 16 insertions(+) diff --git a/jscomp/bin/all_ounit_tests.ml b/jscomp/bin/all_ounit_tests.ml index 84362790af..2fbd4c0091 100644 --- a/jscomp/bin/all_ounit_tests.ml +++ b/jscomp/bin/all_ounit_tests.ml @@ -3529,6 +3529,7 @@ val js_array_ctor : string val js_type_number : string val js_type_string : string val js_type_object : string +val js_type_boolean : string val js_undefined : string val js_prop_length : string @@ -3663,6 +3664,7 @@ let js_array_ctor = "Array" let js_type_number = "number" let js_type_string = "string" let js_type_object = "object" +let js_type_boolean = "boolean" let js_undefined = "undefined" let js_prop_length = "length" diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index 57f1b2ab7f..662ca28392 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -186,6 +186,7 @@ let typeof ?comment (e : t) : t = -> str ?comment L.js_type_string | Array _ -> str ?comment L.js_type_object + | Bool _ -> str ?comment L.js_type_boolean | _ -> {expression_desc = Typeof e ; comment } diff --git a/jscomp/ext/literals.ml b/jscomp/ext/literals.ml index 47ce84cccd..09e01835eb 100644 --- a/jscomp/ext/literals.ml +++ b/jscomp/ext/literals.ml @@ -32,6 +32,7 @@ let js_array_ctor = "Array" let js_type_number = "number" let js_type_string = "string" let js_type_object = "object" +let js_type_boolean = "boolean" let js_undefined = "undefined" let js_prop_length = "length" diff --git a/jscomp/ext/literals.mli b/jscomp/ext/literals.mli index d3e3de6980..c298775c0c 100644 --- a/jscomp/ext/literals.mli +++ b/jscomp/ext/literals.mli @@ -31,6 +31,7 @@ val js_array_ctor : string val js_type_number : string val js_type_string : string val js_type_object : string +val js_type_boolean : string val js_undefined : string val js_prop_length : string diff --git a/lib/bsb.ml b/lib/bsb.ml index a5a83c7bac..be71463476 100644 --- a/lib/bsb.ml +++ b/lib/bsb.ml @@ -3376,6 +3376,7 @@ val js_array_ctor : string val js_type_number : string val js_type_string : string val js_type_object : string +val js_type_boolean : string val js_undefined : string val js_prop_length : string @@ -3510,6 +3511,7 @@ let js_array_ctor = "Array" let js_type_number = "number" let js_type_string = "string" let js_type_object = "object" +let js_type_boolean = "boolean" let js_undefined = "undefined" let js_prop_length = "length" diff --git a/lib/bsb_helper.ml b/lib/bsb_helper.ml index ef1c0a26b8..9ac1a2792c 100644 --- a/lib/bsb_helper.ml +++ b/lib/bsb_helper.ml @@ -3360,6 +3360,7 @@ val js_array_ctor : string val js_type_number : string val js_type_string : string val js_type_object : string +val js_type_boolean : string val js_undefined : string val js_prop_length : string @@ -3494,6 +3495,7 @@ let js_array_ctor = "Array" let js_type_number = "number" let js_type_string = "string" let js_type_object = "object" +let js_type_boolean = "boolean" let js_undefined = "undefined" let js_prop_length = "length" diff --git a/lib/bsdep.ml b/lib/bsdep.ml index 3fca73d989..150034bf45 100644 --- a/lib/bsdep.ml +++ b/lib/bsdep.ml @@ -27873,6 +27873,7 @@ val js_array_ctor : string val js_type_number : string val js_type_string : string val js_type_object : string +val js_type_boolean : string val js_undefined : string val js_prop_length : string @@ -28007,6 +28008,7 @@ let js_array_ctor = "Array" let js_type_number = "number" let js_type_string = "string" let js_type_object = "object" +let js_type_boolean = "boolean" let js_undefined = "undefined" let js_prop_length = "length" diff --git a/lib/bsppx.ml b/lib/bsppx.ml index 277b4109f7..d736f918a1 100644 --- a/lib/bsppx.ml +++ b/lib/bsppx.ml @@ -9815,6 +9815,7 @@ val js_array_ctor : string val js_type_number : string val js_type_string : string val js_type_object : string +val js_type_boolean : string val js_undefined : string val js_prop_length : string @@ -9949,6 +9950,7 @@ let js_array_ctor = "Array" let js_type_number = "number" let js_type_string = "string" let js_type_object = "object" +let js_type_boolean = "boolean" let js_undefined = "undefined" let js_prop_length = "length" diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index dc4626952e..d583d63db8 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -24857,6 +24857,7 @@ val js_array_ctor : string val js_type_number : string val js_type_string : string val js_type_object : string +val js_type_boolean : string val js_undefined : string val js_prop_length : string @@ -24991,6 +24992,7 @@ let js_array_ctor = "Array" let js_type_number = "number" let js_type_string = "string" let js_type_object = "object" +let js_type_boolean = "boolean" let js_undefined = "undefined" let js_prop_length = "length" @@ -71229,6 +71231,7 @@ let typeof ?comment (e : t) : t = -> str ?comment L.js_type_string | Array _ -> str ?comment L.js_type_object + | Bool _ -> str ?comment L.js_type_boolean | _ -> {expression_desc = Typeof e ; comment } From a90c314135df391a67d6872f61444a7431a3abcf Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 12:02:55 +0800 Subject: [PATCH 08/11] continue --- jscomp/core/j.ml | 13 ---- jscomp/core/js_analyzer.ml | 2 - jscomp/core/js_dump.ml | 22 ------- jscomp/core/js_exp_make.ml | 5 -- jscomp/core/js_exp_make.mli | 2 - jscomp/core/js_fold.ml | 11 +--- jscomp/core/js_map.ml | 12 +--- jscomp/core/lam.ml | 9 --- jscomp/core/lam.mli | 3 - jscomp/core/lam_analysis.ml | 3 +- jscomp/core/lam_compile_primitive.ml | 12 ---- jscomp/core/lam_print.ml | 2 - jscomp/runtime/bs_string.ml | 13 +++- jscomp/test/bang_primitive.js | 2 +- jscomp/test/bang_primitive.ml | 7 +- lib/js/caml_string.js | 4 +- lib/whole_compiler.ml | 96 +--------------------------- 17 files changed, 26 insertions(+), 192 deletions(-) diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index a221a4e447..4a79f46352 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -113,19 +113,6 @@ and expression_desc = *) | Typeof of expression | Js_not of expression (* !v *) - | String_of_small_int_array of expression - (* String.fromCharCode.apply(null, args) *) - (* Convert JS boolean into OCaml boolean - like [+true], note this ast talks using js - terminnology unless explicity stated - *) - - (* TODO: - add - {[ Assert of bool * expression ]} - *) - - (* TODO: Add some primitives so that [js inliner] can do a better job *) | Seq of expression * expression | Cond of expression * expression * expression diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index 683e746f82..abc7c8c5b4 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -115,7 +115,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Math _ | Array_copy _ | Js_not _ - | String_of_small_int_array _ | Cond _ | FlatCall _ @@ -255,7 +254,6 @@ let rec eq_expression | String_append _ | Typeof _ | Js_not _ - | String_of_small_int_array _ | Cond _ | FlatCall _ | String_access _ diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 53e0e5e8b9..1092171fff 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -489,27 +489,6 @@ and expression 1 cxt f el ) ) - | String_of_small_int_array ({expression_desc = desc } as e) -> - let action () = - P.group f 1 (fun _ -> - P.string f L.string_cap; - P.string f L.dot ; - P.string f L.fromCharcode; - begin match desc with - | Array (el, _mutable) - -> - P.paren_group f 1 (fun _ -> arguments cxt f el) - | _ -> - P.string f L.dot ; - P.string f L.apply; - P.paren_group f 1 (fun _ -> - P.string f L.null; - P.string f L.comma; - expression 1 cxt f e ) - end ) - in - if l > 15 then P.paren_group f 1 action - else action () | Array_copy e -> P.group f 1 (fun _ -> @@ -1054,7 +1033,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Caml_block_set_tag _ | Length _ | Caml_block_set_length _ - | String_of_small_int_array _ | Call _ | Array_copy _ | Caml_block_tag _ diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index 662ca28392..05623c46b3 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -752,11 +752,6 @@ let is_type_number ?comment (e : t) : t = string_equal ?comment (typeof e) (str "number") -let string_of_small_int_array ?comment xs : t = - {expression_desc = String_of_small_int_array xs; comment} - - - (* we are calling [Caml_primitive.primitive_name], since it's under our control, we should make it follow the javascript name convention, and call plain [dot] diff --git a/jscomp/core/js_exp_make.mli b/jscomp/core/js_exp_make.mli index dac87cc518..330cf21aff 100644 --- a/jscomp/core/js_exp_make.mli +++ b/jscomp/core/js_exp_make.mli @@ -139,8 +139,6 @@ val array_length : unary_op val string_length : unary_op -val string_of_small_int_array : unary_op - val bytes_length : unary_op val function_length : unary_op diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index 7e0deb068b..b6cdcb4451 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -129,15 +129,7 @@ class virtual fold = (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) - (* !v *) (* String.fromCharCode.apply(null, args) *) - (* Convert JS boolean into OCaml boolean - like [+true], note this ast talks using js - terminnology unless explicity stated - *) - (* TODO: - add - {[ Assert of bool * expression ]} - *) + (* !v *) (* TODO: Add some primitives so that [js inliner] can do a better job *) (* [int_op] will guarantee return [int32] bits https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators *) @@ -334,7 +326,6 @@ class virtual fold = | Bool _x -> let o = o#bool _x in o | Typeof _x -> let o = o#expression _x in o | Js_not _x -> let o = o#expression _x in o - | String_of_small_int_array _x -> let o = o#expression _x in o | Seq (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o | Cond (_x, _x_i1, _x_i2) -> diff --git a/jscomp/core/js_map.ml b/jscomp/core/js_map.ml index 5f414855f6..d0bf57bbc1 100644 --- a/jscomp/core/js_map.ml +++ b/jscomp/core/js_map.ml @@ -142,15 +142,7 @@ class virtual map = (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) - (* !v *) (* String.fromCharCode.apply(null, args) *) - (* Convert JS boolean into OCaml boolean - like [+true], note this ast talks using js - terminnology unless explicity stated - *) - (* TODO: - add - {[ Assert of bool * expression ]} - *) + (* !v *) (* TODO: Add some primitives so that [js inliner] can do a better job *) (* [int_op] will guarantee return [int32] bits https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators *) @@ -361,8 +353,6 @@ class virtual map = | Bool _x -> let _x = o#bool _x in Bool _x | Typeof _x -> let _x = o#expression _x in Typeof _x | Js_not _x -> let _x = o#expression _x in Js_not _x - | String_of_small_int_array _x -> - let _x = o#expression _x in String_of_small_int_array _x | Seq (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in Seq (_x, _x_i1) diff --git a/jscomp/core/lam.ml b/jscomp/core/lam.ml index c2ac867eb2..41d0551219 100644 --- a/jscomp/core/lam.ml +++ b/jscomp/core/lam.ml @@ -197,9 +197,6 @@ type primitive = | Pis_null_undefined | Pjs_typeof | Pjs_function_length - - | Pjs_string_of_small_array - (* | Pjs_is_instance_array *) | Pcaml_obj_length | Pcaml_obj_set_length | Pwrap_exn (* convert either JS exception or OCaml exception into OCaml format *) @@ -1866,12 +1863,6 @@ let convert exports lam : _ * _ = | "#null_to_opt" -> Pnull_to_opt | "#is_nil_undef" -> Pis_null_undefined | "#string_append" -> Pstringadd - - - | "#string_of_small_int_array" -> Pjs_string_of_small_array - (* {[String.fromCharCode.apply(null,x)]} - Note if we have better suport [@bs.splice], - we can get rid of it*) | "#obj_set_length" -> Pcaml_obj_set_length | "#obj_length" -> Pcaml_obj_length | "#function_length" -> Pjs_function_length diff --git a/jscomp/core/lam.mli b/jscomp/core/lam.mli index ffaceb1949..1ce9907502 100644 --- a/jscomp/core/lam.mli +++ b/jscomp/core/lam.mli @@ -192,9 +192,6 @@ type primitive = | Pjs_typeof | Pjs_function_length - - | Pjs_string_of_small_array - (* | Pjs_is_instance_array *) | Pcaml_obj_length | Pcaml_obj_set_length | Pwrap_exn (* convert either JS exception or OCaml exception into OCaml format *) diff --git a/jscomp/core/lam_analysis.ml b/jscomp/core/lam_analysis.ml index e0a6cb510a..6e422b940e 100644 --- a/jscomp/core/lam_analysis.ml +++ b/jscomp/core/lam_analysis.ml @@ -160,7 +160,6 @@ let rec no_side_effects (lam : Lam.t) : bool = (* | Pjs_is_instance_array *) | Pwrap_exn -> true - | Pjs_string_of_small_array | Pcaml_obj_set_length | Pjs_apply | Pjs_runtime_apply @@ -541,7 +540,7 @@ and eq_primitive ( lhs : Lam.primitive) (rhs : Lam.primitive) = | Pupdate_mod -> rhs = Pupdate_mod | Pbswap16 -> rhs = Pbswap16 | Pjs_function_length -> rhs = Pjs_function_length - | Pjs_string_of_small_array -> rhs = Pjs_string_of_small_array + (* | Pjs_string_of_small_array -> rhs = Pjs_string_of_small_array *) (* | Pjs_is_instance_array -> rhs = Pjs_is_instance_array *) | Pcaml_obj_length -> rhs = Pcaml_obj_length | Pcaml_obj_set_length -> rhs = Pcaml_obj_set_length diff --git a/jscomp/core/lam_compile_primitive.ml b/jscomp/core/lam_compile_primitive.ml index 3f724bf387..8bd89baea2 100644 --- a/jscomp/core/lam_compile_primitive.ml +++ b/jscomp/core/lam_compile_primitive.ml @@ -137,18 +137,6 @@ let translate loc | [a;b] -> E.set_length a b | _ -> assert false end - | Lam.Pjs_string_of_small_array -> - begin match args with - | [e] -> E.string_of_small_int_array e - | _ -> assert false - end - (* | Lam.Pjs_is_instance_array -> - begin match args with - | [e] -> E.is_instance_array e - | _ -> assert false - end *) - - | Pis_null -> begin match args with | [e] -> E.is_null e diff --git a/jscomp/core/lam_print.ml b/jscomp/core/lam_print.ml index caedd92592..f596576eb3 100644 --- a/jscomp/core/lam_print.ml +++ b/jscomp/core/lam_print.ml @@ -108,8 +108,6 @@ let primitive ppf (prim : Lam.primitive) = match prim with (* | Pcreate_exception s -> fprintf ppf "[exn-create]%S" s *) | Pcreate_extension s -> fprintf ppf "[ext-create]%S" s | Pwrap_exn -> fprintf ppf "#exn" - | Pjs_string_of_small_array -> fprintf ppf "#string_of_small_array" - (* | Pjs_is_instance_array -> fprintf ppf "#is_instance_array" *) | Pcaml_obj_length -> fprintf ppf "#obj_length" | Pcaml_obj_set_length -> fprintf ppf "#obj_set_length" | Pinit_mod -> fprintf ppf "init_mod!" diff --git a/jscomp/runtime/bs_string.ml b/jscomp/runtime/bs_string.ml index 69647a3f07..5be372dab5 100644 --- a/jscomp/runtime/bs_string.ml +++ b/jscomp/runtime/bs_string.ml @@ -45,8 +45,17 @@ external slice_rest : string -> int -> string = "slice" external index_of : string -> string -> int = "indexOf" [@@bs.send] external append : string -> string -> string = "#string_append" -external of_small_int_array : int array -> string = "#string_of_small_int_array" -external of_small_int32_array : int32 array -> string = "#string_of_small_int_array" +external of_small_int_array : + (_ [@bs.as {json|null|json}] ) -> + int array -> string = + "String.fromCharCode.apply" +[@@bs.val] + +external of_small_int32_array : + int32 array -> string = + "String.fromCharCode" +[@@bs.val] [@@bs.splice] + external lastIndexOf : string -> string -> int = "lastIndexOf" [@@bs.send] external of_any : 'a -> string = "String" diff --git a/jscomp/test/bang_primitive.js b/jscomp/test/bang_primitive.js index 02db9387b8..d9887fa2f7 100644 --- a/jscomp/test/bang_primitive.js +++ b/jscomp/test/bang_primitive.js @@ -14,7 +14,7 @@ function test(x, y) { function f(x, _) { return /* tuple */[ - String.fromCharCode.apply(null,x), + String.fromCharCode.apply((null), x), 0 ]; } diff --git a/jscomp/test/bang_primitive.ml b/jscomp/test/bang_primitive.ml index 447f214a7a..1c6ab6d23e 100644 --- a/jscomp/test/bang_primitive.ml +++ b/jscomp/test/bang_primitive.ml @@ -35,7 +35,12 @@ external append : 'a array -> 'a array -> 'a array = "#array_append" let f x y = append x y *) -external of_small_int_array : int array -> string = "#string_of_small_int_array" +external of_small_int_array : + (_ [@bs.as {json|null|json}] ) -> + int array -> string = + "String.fromCharCode.apply" +[@@bs.val] + (* external string_of_char : char -> string = "#string_of_char" *) (* string_of_char y *) diff --git a/lib/js/caml_string.js b/lib/js/caml_string.js index 993e015fbf..8a37c40b2e 100644 --- a/lib/js/caml_string.js +++ b/lib/js/caml_string.js @@ -129,14 +129,14 @@ function bytes_to_string(a) { var s = ""; var s_len = len; if (i === 0 && len <= 4096 && len === bytes.length) { - return String.fromCharCode.apply(null,bytes); + return String.fromCharCode.apply((null), bytes); } else { var offset = 0; while(s_len > 0) { var next = s_len < 1024 ? s_len : 1024; var tmp_bytes = new Array(next); caml_blit_bytes(bytes, offset, tmp_bytes, 0, next); - s = s + String.fromCharCode.apply(null,tmp_bytes); + s = s + String.fromCharCode.apply((null), tmp_bytes); s_len = s_len - next | 0; offset = offset + next | 0; }; diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index d583d63db8..a2a74d5d0e 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -63314,19 +63314,6 @@ and expression_desc = *) | Typeof of expression | Js_not of expression (* !v *) - | String_of_small_int_array of expression - (* String.fromCharCode.apply(null, args) *) - (* Convert JS boolean into OCaml boolean - like [+true], note this ast talks using js - terminnology unless explicity stated - *) - - (* TODO: - add - {[ Assert of bool * expression ]} - *) - - (* TODO: Add some primitives so that [js inliner] can do a better job *) | Seq of expression * expression | Cond of expression * expression * expression @@ -66706,9 +66693,6 @@ type primitive = | Pjs_typeof | Pjs_function_length - - | Pjs_string_of_small_array - (* | Pjs_is_instance_array *) | Pcaml_obj_length | Pcaml_obj_set_length | Pwrap_exn (* convert either JS exception or OCaml exception into OCaml format *) @@ -67069,9 +67053,6 @@ type primitive = | Pis_null_undefined | Pjs_typeof | Pjs_function_length - - | Pjs_string_of_small_array - (* | Pjs_is_instance_array *) | Pcaml_obj_length | Pcaml_obj_set_length | Pwrap_exn (* convert either JS exception or OCaml exception into OCaml format *) @@ -68738,12 +68719,6 @@ let convert exports lam : _ * _ = | "#null_to_opt" -> Pnull_to_opt | "#is_nil_undef" -> Pis_null_undefined | "#string_append" -> Pstringadd - - - | "#string_of_small_int_array" -> Pjs_string_of_small_array - (* {[String.fromCharCode.apply(null,x)]} - Note if we have better suport [@bs.splice], - we can get rid of it*) | "#obj_set_length" -> Pcaml_obj_set_length | "#obj_length" -> Pcaml_obj_length | "#function_length" -> Pjs_function_length @@ -69576,15 +69551,7 @@ class virtual fold = (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) - (* !v *) (* String.fromCharCode.apply(null, args) *) - (* Convert JS boolean into OCaml boolean - like [+true], note this ast talks using js - terminnology unless explicity stated - *) - (* TODO: - add - {[ Assert of bool * expression ]} - *) + (* !v *) (* TODO: Add some primitives so that [js inliner] can do a better job *) (* [int_op] will guarantee return [int32] bits https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators *) @@ -69781,7 +69748,6 @@ class virtual fold = | Bool _x -> let o = o#bool _x in o | Typeof _x -> let o = o#expression _x in o | Js_not _x -> let o = o#expression _x in o - | String_of_small_int_array _x -> let o = o#expression _x in o | Seq (_x, _x_i1) -> let o = o#expression _x in let o = o#expression _x_i1 in o | Cond (_x, _x_i1, _x_i2) -> @@ -70079,7 +70045,6 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = | Math _ | Array_copy _ | Js_not _ - | String_of_small_int_array _ | Cond _ | FlatCall _ @@ -70219,7 +70184,6 @@ let rec eq_expression | String_append _ | Typeof _ | Js_not _ - | String_of_small_int_array _ | Cond _ | FlatCall _ | String_access _ @@ -70851,8 +70815,6 @@ val array_length : unary_op val string_length : unary_op -val string_of_small_int_array : unary_op - val bytes_length : unary_op val function_length : unary_op @@ -71797,11 +71759,6 @@ let is_type_number ?comment (e : t) : t = string_equal ?comment (typeof e) (str "number") -let string_of_small_int_array ?comment xs : t = - {expression_desc = String_of_small_int_array xs; comment} - - - (* we are calling [Caml_primitive.primitive_name], since it's under our control, we should make it follow the javascript name convention, and call plain [dot] @@ -72497,8 +72454,6 @@ let primitive ppf (prim : Lam.primitive) = match prim with (* | Pcreate_exception s -> fprintf ppf "[exn-create]%S" s *) | Pcreate_extension s -> fprintf ppf "[ext-create]%S" s | Pwrap_exn -> fprintf ppf "#exn" - | Pjs_string_of_small_array -> fprintf ppf "#string_of_small_array" - (* | Pjs_is_instance_array -> fprintf ppf "#is_instance_array" *) | Pcaml_obj_length -> fprintf ppf "#obj_length" | Pcaml_obj_set_length -> fprintf ppf "#obj_set_length" | Pinit_mod -> fprintf ppf "init_mod!" @@ -84564,27 +84519,6 @@ and expression 1 cxt f el ) ) - | String_of_small_int_array ({expression_desc = desc } as e) -> - let action () = - P.group f 1 (fun _ -> - P.string f L.string_cap; - P.string f L.dot ; - P.string f L.fromCharcode; - begin match desc with - | Array (el, _mutable) - -> - P.paren_group f 1 (fun _ -> arguments cxt f el) - | _ -> - P.string f L.dot ; - P.string f L.apply; - P.paren_group f 1 (fun _ -> - P.string f L.null; - P.string f L.comma; - expression 1 cxt f e ) - end ) - in - if l > 15 then P.paren_group f 1 action - else action () | Array_copy e -> P.group f 1 (fun _ -> @@ -85129,7 +85063,6 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Caml_block_set_tag _ | Length _ | Caml_block_set_length _ - | String_of_small_int_array _ | Call _ | Array_copy _ | Caml_block_tag _ @@ -86468,7 +86401,6 @@ let rec no_side_effects (lam : Lam.t) : bool = (* | Pjs_is_instance_array *) | Pwrap_exn -> true - | Pjs_string_of_small_array | Pcaml_obj_set_length | Pjs_apply | Pjs_runtime_apply @@ -86849,7 +86781,7 @@ and eq_primitive ( lhs : Lam.primitive) (rhs : Lam.primitive) = | Pupdate_mod -> rhs = Pupdate_mod | Pbswap16 -> rhs = Pbswap16 | Pjs_function_length -> rhs = Pjs_function_length - | Pjs_string_of_small_array -> rhs = Pjs_string_of_small_array + (* | Pjs_string_of_small_array -> rhs = Pjs_string_of_small_array *) (* | Pjs_is_instance_array -> rhs = Pjs_is_instance_array *) | Pcaml_obj_length -> rhs = Pcaml_obj_length | Pcaml_obj_set_length -> rhs = Pcaml_obj_set_length @@ -88165,15 +88097,7 @@ class virtual map = (* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence [typeof] is an operator *) - (* !v *) (* String.fromCharCode.apply(null, args) *) - (* Convert JS boolean into OCaml boolean - like [+true], note this ast talks using js - terminnology unless explicity stated - *) - (* TODO: - add - {[ Assert of bool * expression ]} - *) + (* !v *) (* TODO: Add some primitives so that [js inliner] can do a better job *) (* [int_op] will guarantee return [int32] bits https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators *) @@ -88384,8 +88308,6 @@ class virtual map = | Bool _x -> let _x = o#bool _x in Bool _x | Typeof _x -> let _x = o#expression _x in Typeof _x | Js_not _x -> let _x = o#expression _x in Js_not _x - | String_of_small_int_array _x -> - let _x = o#expression _x in String_of_small_int_array _x | Seq (_x, _x_i1) -> let _x = o#expression _x in let _x_i1 = o#expression _x_i1 in Seq (_x, _x_i1) @@ -95698,18 +95620,6 @@ let translate loc | [a;b] -> E.set_length a b | _ -> assert false end - | Lam.Pjs_string_of_small_array -> - begin match args with - | [e] -> E.string_of_small_int_array e - | _ -> assert false - end - (* | Lam.Pjs_is_instance_array -> - begin match args with - | [e] -> E.is_instance_array e - | _ -> assert false - end *) - - | Pis_null -> begin match args with | [e] -> E.is_null e From 0c8a07feec1e930cbe98dc0ab0a4dd342972efdf Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 12:05:17 +0800 Subject: [PATCH 09/11] clean up --- jscomp/core/js_exp_make.ml | 2 +- jscomp/core/js_exp_make.mli | 2 +- jscomp/core/lam_dispatch_primitive.ml | 2 +- lib/whole_compiler.ml | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index 05623c46b3..d35cd99dfb 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -813,7 +813,7 @@ let public_method_call meth_name obj label cache args = (* [fn ; arr NA args ] *) (* ) *) -let set_tag ?comment e tag : t = +let block_set_tag ?comment e tag : t = seq {expression_desc = Caml_block_set_tag (e,tag); comment } unit diff --git a/jscomp/core/js_exp_make.mli b/jscomp/core/js_exp_make.mli index 330cf21aff..c8fc9b25f5 100644 --- a/jscomp/core/js_exp_make.mli +++ b/jscomp/core/js_exp_make.mli @@ -294,7 +294,7 @@ val is_caml_block : ?comment:string -> t -> t val tag : ?comment:string -> J.expression -> t -val set_tag : ?comment:string -> J.expression -> J.expression -> t +val block_set_tag : ?comment:string -> J.expression -> J.expression -> t (** Note that this is coupled with how we encode block, if we use the `Object.defineProperty(..)` since the array already hold the length, diff --git a/jscomp/core/lam_dispatch_primitive.ml b/jscomp/core/lam_dispatch_primitive.ml index 25132a2c57..ed743d3aea 100644 --- a/jscomp/core/lam_dispatch_primitive.ml +++ b/jscomp/core/lam_dispatch_primitive.ml @@ -746,7 +746,7 @@ let translate loc (prim_name : string) call Js_runtime_modules.obj_runtime | "caml_obj_set_tag" -> begin match args with - | [a;b] -> E.set_tag a b + | [a;b] -> E.block_set_tag a b | _ -> assert false end | "caml_obj_tag" -> (* Note that in ocaml, [int] has tag [1000] and [string] has tag [252] diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index a2a74d5d0e..19757941c2 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -70970,7 +70970,7 @@ val is_caml_block : ?comment:string -> t -> t val tag : ?comment:string -> J.expression -> t -val set_tag : ?comment:string -> J.expression -> J.expression -> t +val block_set_tag : ?comment:string -> J.expression -> J.expression -> t (** Note that this is coupled with how we encode block, if we use the `Object.defineProperty(..)` since the array already hold the length, @@ -71820,7 +71820,7 @@ let public_method_call meth_name obj label cache args = (* [fn ; arr NA args ] *) (* ) *) -let set_tag ?comment e tag : t = +let block_set_tag ?comment e tag : t = seq {expression_desc = Caml_block_set_tag (e,tag); comment } unit @@ -95311,7 +95311,7 @@ let translate loc (prim_name : string) call Js_runtime_modules.obj_runtime | "caml_obj_set_tag" -> begin match args with - | [a;b] -> E.set_tag a b + | [a;b] -> E.block_set_tag a b | _ -> assert false end | "caml_obj_tag" -> (* Note that in ocaml, [int] has tag [1000] and [string] has tag [252] From 6167bc97228198221696d94487a9b390ac55f7e7 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 13:18:09 +0800 Subject: [PATCH 10/11] continue --- jscomp/core/lam_compile_external_call.ml | 8 ++------ jscomp/others/js_null_undefined.ml | 4 ++-- jscomp/others/js_null_undefined.mli | 4 ++-- lib/whole_compiler.ml | 8 ++------ 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/jscomp/core/lam_compile_external_call.ml b/jscomp/core/lam_compile_external_call.ml index dee4fc6885..32b3e149d4 100644 --- a/jscomp/core/lam_compile_external_call.ml +++ b/jscomp/core/lam_compile_external_call.ml @@ -321,12 +321,8 @@ let translate_ffi 2. support [@@bs.scope "window"] we need know whether we should call [add_js_module] or not *) - begin match name, handle_external_opt external_module_name , scopes with - | "null", None, [] -> E.nil - | "undefined", None, [] -> E.undefined - | _, _, _ -> - translate_scoped_module_val external_module_name name scopes - end + translate_scoped_module_val external_module_name name scopes + | Js_send {splice = js_splice ; name ; pipe = false; js_send_scopes = scopes } -> begin match args with | self :: args -> diff --git a/jscomp/others/js_null_undefined.ml b/jscomp/others/js_null_undefined.ml index c4d10ad0d4..0d19d58017 100644 --- a/jscomp/others/js_null_undefined.ml +++ b/jscomp/others/js_null_undefined.ml @@ -29,8 +29,8 @@ external toOption : 'a t -> 'a option = "#null_undefined_to_opt" external to_opt : 'a t -> 'a option = "#null_undefined_to_opt" external return : 'a -> 'a t = "%identity" external test : 'a t -> bool = "#is_nil_undef" -external null : 'a t = "null" [@@bs.val] -external undefined : 'a t = "undefined" [@@bs.val] +external null : 'a t = "#null" +external undefined : 'a t = "#undefined" let bind x f = match to_opt x with diff --git a/jscomp/others/js_null_undefined.mli b/jscomp/others/js_null_undefined.mli index 6dd23b350c..56fadbff75 100644 --- a/jscomp/others/js_null_undefined.mli +++ b/jscomp/others/js_null_undefined.mli @@ -34,10 +34,10 @@ external return : 'a -> 'a t = "%identity" external test : 'a t -> bool = "#is_nil_undef" (** The [null] value of type ['a Js.null_undefined]*) -external null : 'a t = "null" [@@bs.val] +external null : 'a t = "#null" (** The [undefined] value of type ['a Js.null_undefined] *) -external undefined : 'a t = "undefined" [@@bs.val] +external undefined : 'a t = "#undefined" diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index 19757941c2..fdf2c03b06 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -93765,12 +93765,8 @@ let translate_ffi 2. support [@@bs.scope "window"] we need know whether we should call [add_js_module] or not *) - begin match name, handle_external_opt external_module_name , scopes with - | "null", None, [] -> E.nil - | "undefined", None, [] -> E.undefined - | _, _, _ -> - translate_scoped_module_val external_module_name name scopes - end + translate_scoped_module_val external_module_name name scopes + | Js_send {splice = js_splice ; name ; pipe = false; js_send_scopes = scopes } -> begin match args with | self :: args -> From 545b711798683405bb537b24ccc51ced70c10864 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 3 Apr 2018 14:41:23 +0800 Subject: [PATCH 11/11] remove "undefined"/"null" --- jscomp/bin/all_ounit_tests.ml | 7 +- jscomp/core/j.ml | 3 +- jscomp/core/js_analyzer.ml | 4 + jscomp/core/js_dump.ml | 7 ++ jscomp/core/js_dump_lit.ml | 1 + jscomp/core/js_exp_make.ml | 74 ++++++++--------- jscomp/core/js_fold.ml | 2 + jscomp/core/js_map.ml | 2 + jscomp/core/js_stmt_make.ml | 6 +- jscomp/core/lam_compile_const.ml | 2 +- jscomp/core/lam_compile_primitive.ml | 8 +- jscomp/ext/ext_ident.ml | 3 - jscomp/ext/ext_ident.mli | 4 +- jscomp/test/bang_primitive.js | 2 +- jscomp/test/gpr_1484.js | 2 +- jscomp/test/js_null_test.js | 4 +- jscomp/test/js_undefined_test.js | 2 +- lib/js/caml_string.js | 4 +- lib/whole_compiler.ml | 116 +++++++++++++++------------ 19 files changed, 135 insertions(+), 118 deletions(-) diff --git a/jscomp/bin/all_ounit_tests.ml b/jscomp/bin/all_ounit_tests.ml index 2fbd4c0091..6a8fcda6a7 100644 --- a/jscomp/bin/all_ounit_tests.ml +++ b/jscomp/bin/all_ounit_tests.ml @@ -6897,9 +6897,9 @@ val make_unused : unit -> Ident.t val convert : string -> string -val undefined : Ident.t + val is_js_or_global : Ident.t -> bool - val nil : Ident.t + val compare : Ident.t -> Ident.t -> int @@ -7221,9 +7221,6 @@ let reset () = String_hashtbl.clear js_module_table -let undefined = create_js "undefined" -let nil = create_js "null" - (* Has to be total order, [x < y] and [x > y] should be consistent flags are not relevant here diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index 4a79f46352..b580e3b5e1 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -194,7 +194,8 @@ and expression_desc = *) | Number of number | Object of property_map - + | Undefined + | Null and for_ident_expression = expression (* pure*) and finish_ident_expression = expression (* pure *) diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index abc7c8c5b4..949c132abd 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -83,6 +83,8 @@ let free_variables_of_expression used_idents defined_idents st = let rec no_side_effect_expression_desc (x : J.expression_desc) = match x with + | Undefined + | Null | Bool _ | Var _ | Unicode _ -> true @@ -175,6 +177,8 @@ let rec eq_expression ({expression_desc = x0} : J.expression) ({expression_desc = y0} : J.expression) = begin match x0 with + | Null -> y0 = Null + | Undefined -> y0 = Undefined | Number (Int i) -> begin match y0 with | Number (Int j) -> i = j diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 1092171fff..959b40ee9c 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -421,6 +421,11 @@ and expression l cxt f (exp : J.expression) : Ext_pp_scope.t = and expression_desc cxt (l:int) f x : Ext_pp_scope.t = match x with + | Null -> + P.string f L.null; cxt + | Undefined + -> + P.string f L.undefined; cxt | Var v -> vident cxt f v | Bool b -> @@ -1048,6 +1053,8 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Char_to_int _ | Math _ | Var _ + | Undefined + | Null | Str _ | Unicode _ | Array _ diff --git a/jscomp/core/js_dump_lit.ml b/jscomp/core/js_dump_lit.ml index 8d8d473a72..74da7ce126 100644 --- a/jscomp/core/js_dump_lit.ml +++ b/jscomp/core/js_dump_lit.ml @@ -73,6 +73,7 @@ let bind = "bind" let math = "Math" let apply = "apply" let null = "null" +let undefined = "undefined" let string_cap = "String" let fromCharcode = "fromCharCode" let eq = "=" diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index d35cd99dfb..55adc03e8f 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -87,10 +87,11 @@ let var ?comment id : t = let js_global ?comment (v : string) = var ?comment (Ext_ident.create_js v ) -let undefined = var Ext_ident.undefined - -let nil = var Ext_ident.nil +let undefined : t = + {expression_desc = Undefined ; comment = None} +let nil : t = + {expression_desc = Null ; comment = None} let call ?comment ~info e0 args : t = {expression_desc = Call(e0,args,info); comment } @@ -496,17 +497,17 @@ let float_mod ?comment e1 e2 : J.expression = *) let rec triple_equal ?comment (e0 : t) (e1 : t ) : t = match e0.expression_desc, e1.expression_desc with - | Var (Id ({name = "undefined"|"null"} as id)), + | (Null| Undefined), (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _ ) - when Ext_ident.is_js id && no_side_effect e1 -> + when no_side_effect e1 -> caml_false (* TODO: rename it as [caml_false] *) | (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ - | Fun _ | Array _ | Caml_block _ ), Var (Id ({name = "undefined"|"null"; } as id)) - when Ext_ident.is_js id && no_side_effect e0 -> + | Fun _ | Array _ | Caml_block _ ), (Null|Undefined) + when no_side_effect e0 -> caml_false | Str (_,x), Str (_,y) -> (* CF*) bool (Ext_string.equal x y) @@ -520,6 +521,10 @@ let rec triple_equal ?comment (e0 : t) (e1 : t ) : t = bool (i0 = i1) | Char_of_int a , Char_of_int b -> triple_equal ?comment a b + | Null, Undefined + | Undefined, Null -> caml_false + | Null, Null + | Undefined, Undefined -> caml_true | _ -> {expression_desc = Bin(EqEqEq, e0,e1); comment} @@ -528,14 +533,7 @@ let bin ?comment (op : J.binop) e0 e1 : t = | EqEqEq -> triple_equal ?comment e0 e1 | _ -> {expression_desc = Bin(op,e0,e1); comment} -(* | (Bin (NotEqEq, e1, *) -(* {expression_desc = Var (Id ({name = "undefined"; _} as id))}) *) -(* | Bin (NotEqEq, *) -(* {expression_desc = Var (Id ({name = "undefined"; _} as id))}, *) -(* e1) *) -(* ), *) -(* _ when Ext_ident.is_js id -> *) -(* and_ e1 e2 *) + (* TODO: Constant folding, Google Closure will do that?, Even if Google Clsoure can do that, we will see how it interact with other optimizations @@ -1217,21 +1215,21 @@ let of_block ?comment ?e block : t = , Js_fun_env.empty 0) } [] -let is_null ?comment x = triple_equal ?comment x nil +let is_null ?comment (x : t) = + triple_equal ?comment x nil let is_undef ?comment x = triple_equal ?comment x undefined let for_sure_js_null_undefined (x : t) = match x.expression_desc with - | Var (Id ({name = "undefined" | "null"} as id)) - -> Ext_ident.is_js id + | Null | Undefined + -> true | _ -> false let is_null_undefined ?comment (x: t) : t = match x.expression_desc with - | Var (Id ({name = "undefined" | "null"} as id)) - when Ext_ident.is_js id + | Null | Undefined -> caml_true | Number _ | Array _ | Caml_block _ -> caml_false | _ -> @@ -1241,22 +1239,23 @@ let is_null_undefined ?comment (x: t) : t = let eq_null_undefined_boolean ?comment (a : t) (b : t) = match a.expression_desc, b.expression_desc with - | Var (Id ({name = "null" | "undefined"} as id) ), + | (Null | Undefined), (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _ ) - when Ext_ident.is_js id -> + -> caml_false | (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _ ), - Var (Id ({name = "null" | "undefined"} as id) ) - when Ext_ident.is_js id -> + (Null | Undefined) + -> caml_false - | Var (Id ({name = "null" | "undefined" as n1 } as id1) ), - Var (Id ({name = "null" | "undefined" as n2 } as id2) ) - when Ext_ident.is_js id1 && Ext_ident.is_js id2 - -> bool (n1 = n2) + | (Null, Undefined) + | (Undefined, Null) -> caml_false + | (Null, Null) + | (Undefined, Undefined) + -> caml_true | _ -> {expression_desc = Bin(EqEqEq, a, b); comment} @@ -1264,23 +1263,24 @@ let eq_null_undefined_boolean ?comment (a : t) (b : t) = let neq_null_undefined_boolean ?comment (a : t) (b : t) = match a.expression_desc, b.expression_desc with - | Var (Id ({name = "null" | "undefined"} as id) ), + | (Null | Undefined), (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _ ) - when Ext_ident.is_js id -> + -> caml_true | (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _ ), - Var (Id ({name = "null" | "undefined"} as id) ) - when Ext_ident.is_js id -> + (Null | Undefined) + -> caml_true - | Var (Id ({name = "null" | "undefined" as n1 } as id1) ), - Var (Id ({name = "null" | "undefined" as n2 } as id2) ) - when Ext_ident.is_js id1 && Ext_ident.is_js id2 - -> - if n1 <> n2 then caml_true else caml_false + | (Null , Null ) + | (Undefined, Undefined) + -> caml_false + | (Null, Undefined) + | (Undefined, Null) + -> caml_true | _ -> {expression_desc = Bin(NotEqEq, a, b); comment} diff --git a/jscomp/core/js_fold.ml b/jscomp/core/js_fold.ml index b6cdcb4451..9540e705ac 100644 --- a/jscomp/core/js_fold.ml +++ b/jscomp/core/js_fold.ml @@ -374,6 +374,8 @@ class virtual fold = let o = o#expression _x in let o = o#expression _x_i1 in o | Number _x -> let o = o#number _x in o | Object _x -> let o = o#property_map _x in o + | Undefined -> o + | Null -> o method expression : expression -> 'self_type = fun { expression_desc = _x; comment = _x_i1 } -> let o = o#expression_desc _x in diff --git a/jscomp/core/js_map.ml b/jscomp/core/js_map.ml index d0bf57bbc1..dfb241d0e8 100644 --- a/jscomp/core/js_map.ml +++ b/jscomp/core/js_map.ml @@ -416,6 +416,8 @@ class virtual map = let _x_i1 = o#expression _x_i1 in Caml_block_set_length (_x, _x_i1) | Number _x -> let _x = o#number _x in Number _x | Object _x -> let _x = o#property_map _x in Object _x + | Undefined -> Undefined + | Null -> Null method expression : expression -> expression = fun { expression_desc = _x; comment = _x_i1 } -> let _x = o#expression_desc _x in diff --git a/jscomp/core/js_stmt_make.ml b/jscomp/core/js_stmt_make.ml index 67ce93164f..1fe3a70a9c 100644 --- a/jscomp/core/js_stmt_make.ml +++ b/jscomp/core/js_stmt_make.ml @@ -268,11 +268,7 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) | Bin (Bor , {expression_desc = Number (Int { i = 0l ; _})}, a), _, _ -> aux ?comment a then_ else_ acc - (* | Bin (NotEqEq, e1, *) - (* {expression_desc = Var (Id ({name = "undefined"; _} as id))}), *) - (* _, _ *) - (* when Ext_ident.is_js id -> *) - (* aux ?comment e1 then_ else_ acc *) + | ((Bin (Gt, ({expression_desc = diff --git a/jscomp/core/lam_compile_const.ml b/jscomp/core/lam_compile_const.ml index 1182d7f013..5880a0f5e8 100644 --- a/jscomp/core/lam_compile_const.ml +++ b/jscomp/core/lam_compile_const.ml @@ -107,7 +107,7 @@ let translate_arg_cst (cst : External_arg_spec.cst) = E.int (Int32.of_int i) | Arg_string_lit i -> E.str i - | Arg_js_null -> E.raw_js_code Exp "null" + | Arg_js_null -> E.nil | Arg_js_json s -> E.raw_js_code Exp s diff --git a/jscomp/core/lam_compile_primitive.ml b/jscomp/core/lam_compile_primitive.ml index 8bd89baea2..94d3b78636 100644 --- a/jscomp/core/lam_compile_primitive.ml +++ b/jscomp/core/lam_compile_primitive.ml @@ -75,8 +75,8 @@ let translate loc begin match args with | [e] -> begin match e.expression_desc with - | Var _ -> - E.econd (E.is_null e) Js_of_lam_option.none (Js_of_lam_option.some e) + | Var _ | Undefined | Null -> + E.econd (E.is_null e) Js_of_lam_option.none (Js_of_lam_option.some e) | _ -> E.runtime_call Js_runtime_modules.js_primitive "null_to_opt" args @@ -93,7 +93,7 @@ let translate loc begin match args with | [e] -> begin match e.expression_desc with - | Var _ -> + | Var _ | Undefined | Null -> E.econd (E.is_undef e) Js_of_lam_option.none (Js_of_lam_option.some e) | _ -> E.runtime_call Js_runtime_modules.js_primitive @@ -111,7 +111,7 @@ let translate loc begin match args with | [e] -> begin match e.expression_desc with - | Var _ -> + | Var _ | Undefined | Null -> E.econd (E.is_null_undefined e) Js_of_lam_option.none (Js_of_lam_option.some e) diff --git a/jscomp/ext/ext_ident.ml b/jscomp/ext/ext_ident.ml index ea98bdd93a..33c17448e0 100644 --- a/jscomp/ext/ext_ident.ml +++ b/jscomp/ext/ext_ident.ml @@ -312,9 +312,6 @@ let reset () = String_hashtbl.clear js_module_table -let undefined = create_js "undefined" -let nil = create_js "null" - (* Has to be total order, [x < y] and [x > y] should be consistent flags are not relevant here diff --git a/jscomp/ext/ext_ident.mli b/jscomp/ext/ext_ident.mli index adb286b2d7..90869c55a3 100644 --- a/jscomp/ext/ext_ident.mli +++ b/jscomp/ext/ext_ident.mli @@ -56,9 +56,9 @@ val make_unused : unit -> Ident.t val convert : string -> string -val undefined : Ident.t + val is_js_or_global : Ident.t -> bool - val nil : Ident.t + val compare : Ident.t -> Ident.t -> int diff --git a/jscomp/test/bang_primitive.js b/jscomp/test/bang_primitive.js index d9887fa2f7..1de53f634d 100644 --- a/jscomp/test/bang_primitive.js +++ b/jscomp/test/bang_primitive.js @@ -14,7 +14,7 @@ function test(x, y) { function f(x, _) { return /* tuple */[ - String.fromCharCode.apply((null), x), + String.fromCharCode.apply(null, x), 0 ]; } diff --git a/jscomp/test/gpr_1484.js b/jscomp/test/gpr_1484.js index c0e9fac5f8..27e856c65c 100644 --- a/jscomp/test/gpr_1484.js +++ b/jscomp/test/gpr_1484.js @@ -2,7 +2,7 @@ function test(x) { - x.nodeValue = (null); + x.nodeValue = null; return /* () */0; } diff --git a/jscomp/test/js_null_test.js b/jscomp/test/js_null_test.js index f24ae1a333..0ba403c824 100644 --- a/jscomp/test/js_null_test.js +++ b/jscomp/test/js_null_test.js @@ -10,7 +10,7 @@ var suites_000 = /* tuple */[ (function () { return /* Eq */Block.__(0, [ /* None */0, - null === null ? /* None */0 : [null] + /* None */0 ]); }) ]; @@ -31,7 +31,7 @@ var suites_001 = /* :: */[ (function () { return /* Eq */Block.__(0, [ /* None */0, - null === null ? /* None */0 : [null] + /* None */0 ]); }) ], diff --git a/jscomp/test/js_undefined_test.js b/jscomp/test/js_undefined_test.js index 826cee0da4..7f8e79b5d4 100644 --- a/jscomp/test/js_undefined_test.js +++ b/jscomp/test/js_undefined_test.js @@ -10,7 +10,7 @@ var suites_000 = /* tuple */[ (function () { return /* Eq */Block.__(0, [ /* None */0, - undefined === undefined ? /* None */0 : [undefined] + /* None */0 ]); }) ]; diff --git a/lib/js/caml_string.js b/lib/js/caml_string.js index 8a37c40b2e..a80b7e3143 100644 --- a/lib/js/caml_string.js +++ b/lib/js/caml_string.js @@ -129,14 +129,14 @@ function bytes_to_string(a) { var s = ""; var s_len = len; if (i === 0 && len <= 4096 && len === bytes.length) { - return String.fromCharCode.apply((null), bytes); + return String.fromCharCode.apply(null, bytes); } else { var offset = 0; while(s_len > 0) { var next = s_len < 1024 ? s_len : 1024; var tmp_bytes = new Array(next); caml_blit_bytes(bytes, offset, tmp_bytes, 0, next); - s = s + String.fromCharCode.apply((null), tmp_bytes); + s = s + String.fromCharCode.apply(null, tmp_bytes); s_len = s_len - next | 0; offset = offset + next | 0; }; diff --git a/lib/whole_compiler.ml b/lib/whole_compiler.ml index fdf2c03b06..4ba0f25a58 100644 --- a/lib/whole_compiler.ml +++ b/lib/whole_compiler.ml @@ -60342,9 +60342,9 @@ val make_unused : unit -> Ident.t val convert : string -> string -val undefined : Ident.t + val is_js_or_global : Ident.t -> bool - val nil : Ident.t + val compare : Ident.t -> Ident.t -> int @@ -60666,9 +60666,6 @@ let reset () = String_hashtbl.clear js_module_table -let undefined = create_js "undefined" -let nil = create_js "null" - (* Has to be total order, [x < y] and [x > y] should be consistent flags are not relevant here @@ -63395,7 +63392,8 @@ and expression_desc = *) | Number of number | Object of property_map - + | Undefined + | Null and for_ident_expression = expression (* pure*) and finish_ident_expression = expression (* pure *) @@ -69796,6 +69794,8 @@ class virtual fold = let o = o#expression _x in let o = o#expression _x_i1 in o | Number _x -> let o = o#number _x in o | Object _x -> let o = o#property_map _x in o + | Undefined -> o + | Null -> o method expression : expression -> 'self_type = fun { expression_desc = _x; comment = _x_i1 } -> let o = o#expression_desc _x in @@ -70013,6 +70013,8 @@ let free_variables_of_expression used_idents defined_idents st = let rec no_side_effect_expression_desc (x : J.expression_desc) = match x with + | Undefined + | Null | Bool _ | Var _ | Unicode _ -> true @@ -70105,6 +70107,8 @@ let rec eq_expression ({expression_desc = x0} : J.expression) ({expression_desc = y0} : J.expression) = begin match x0 with + | Null -> y0 = Null + | Undefined -> y0 = Undefined | Number (Int i) -> begin match y0 with | Number (Int j) -> i = j @@ -71094,10 +71098,11 @@ let var ?comment id : t = let js_global ?comment (v : string) = var ?comment (Ext_ident.create_js v ) -let undefined = var Ext_ident.undefined - -let nil = var Ext_ident.nil +let undefined : t = + {expression_desc = Undefined ; comment = None} +let nil : t = + {expression_desc = Null ; comment = None} let call ?comment ~info e0 args : t = {expression_desc = Call(e0,args,info); comment } @@ -71503,17 +71508,17 @@ let float_mod ?comment e1 e2 : J.expression = *) let rec triple_equal ?comment (e0 : t) (e1 : t ) : t = match e0.expression_desc, e1.expression_desc with - | Var (Id ({name = "undefined"|"null"} as id)), + | (Null| Undefined), (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _ ) - when Ext_ident.is_js id && no_side_effect e1 -> + when no_side_effect e1 -> caml_false (* TODO: rename it as [caml_false] *) | (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ - | Fun _ | Array _ | Caml_block _ ), Var (Id ({name = "undefined"|"null"; } as id)) - when Ext_ident.is_js id && no_side_effect e0 -> + | Fun _ | Array _ | Caml_block _ ), (Null|Undefined) + when no_side_effect e0 -> caml_false | Str (_,x), Str (_,y) -> (* CF*) bool (Ext_string.equal x y) @@ -71527,6 +71532,10 @@ let rec triple_equal ?comment (e0 : t) (e1 : t ) : t = bool (i0 = i1) | Char_of_int a , Char_of_int b -> triple_equal ?comment a b + | Null, Undefined + | Undefined, Null -> caml_false + | Null, Null + | Undefined, Undefined -> caml_true | _ -> {expression_desc = Bin(EqEqEq, e0,e1); comment} @@ -71535,14 +71544,7 @@ let bin ?comment (op : J.binop) e0 e1 : t = | EqEqEq -> triple_equal ?comment e0 e1 | _ -> {expression_desc = Bin(op,e0,e1); comment} -(* | (Bin (NotEqEq, e1, *) -(* {expression_desc = Var (Id ({name = "undefined"; _} as id))}) *) -(* | Bin (NotEqEq, *) -(* {expression_desc = Var (Id ({name = "undefined"; _} as id))}, *) -(* e1) *) -(* ), *) -(* _ when Ext_ident.is_js id -> *) -(* and_ e1 e2 *) + (* TODO: Constant folding, Google Closure will do that?, Even if Google Clsoure can do that, we will see how it interact with other optimizations @@ -72224,21 +72226,21 @@ let of_block ?comment ?e block : t = , Js_fun_env.empty 0) } [] -let is_null ?comment x = triple_equal ?comment x nil +let is_null ?comment (x : t) = + triple_equal ?comment x nil let is_undef ?comment x = triple_equal ?comment x undefined let for_sure_js_null_undefined (x : t) = match x.expression_desc with - | Var (Id ({name = "undefined" | "null"} as id)) - -> Ext_ident.is_js id + | Null | Undefined + -> true | _ -> false let is_null_undefined ?comment (x: t) : t = match x.expression_desc with - | Var (Id ({name = "undefined" | "null"} as id)) - when Ext_ident.is_js id + | Null | Undefined -> caml_true | Number _ | Array _ | Caml_block _ -> caml_false | _ -> @@ -72248,22 +72250,23 @@ let is_null_undefined ?comment (x: t) : t = let eq_null_undefined_boolean ?comment (a : t) (b : t) = match a.expression_desc, b.expression_desc with - | Var (Id ({name = "null" | "undefined"} as id) ), + | (Null | Undefined), (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _ ) - when Ext_ident.is_js id -> + -> caml_false | (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _ ), - Var (Id ({name = "null" | "undefined"} as id) ) - when Ext_ident.is_js id -> + (Null | Undefined) + -> caml_false - | Var (Id ({name = "null" | "undefined" as n1 } as id1) ), - Var (Id ({name = "null" | "undefined" as n2 } as id2) ) - when Ext_ident.is_js id1 && Ext_ident.is_js id2 - -> bool (n1 = n2) + | (Null, Undefined) + | (Undefined, Null) -> caml_false + | (Null, Null) + | (Undefined, Undefined) + -> caml_true | _ -> {expression_desc = Bin(EqEqEq, a, b); comment} @@ -72271,23 +72274,24 @@ let eq_null_undefined_boolean ?comment (a : t) (b : t) = let neq_null_undefined_boolean ?comment (a : t) (b : t) = match a.expression_desc, b.expression_desc with - | Var (Id ({name = "null" | "undefined"} as id) ), + | (Null | Undefined), (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _ ) - when Ext_ident.is_js id -> + -> caml_true | (Char_of_int _ | Char_to_int _ | Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _ ), - Var (Id ({name = "null" | "undefined"} as id) ) - when Ext_ident.is_js id -> + (Null | Undefined) + -> caml_true - | Var (Id ({name = "null" | "undefined" as n1 } as id1) ), - Var (Id ({name = "null" | "undefined" as n2 } as id2) ) - when Ext_ident.is_js id1 && Ext_ident.is_js id2 - -> - if n1 <> n2 then caml_true else caml_false + | (Null , Null ) + | (Undefined, Undefined) + -> caml_false + | (Null, Undefined) + | (Undefined, Null) + -> caml_true | _ -> {expression_desc = Bin(NotEqEq, a, b); comment} @@ -73419,11 +73423,7 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) | Bin (Bor , {expression_desc = Number (Int { i = 0l ; _})}, a), _, _ -> aux ?comment a then_ else_ acc - (* | Bin (NotEqEq, e1, *) - (* {expression_desc = Var (Id ({name = "undefined"; _} as id))}), *) - (* _, _ *) - (* when Ext_ident.is_js id -> *) - (* aux ?comment e1 then_ else_ acc *) + | ((Bin (Gt, ({expression_desc = @@ -83562,6 +83562,7 @@ let bind = "bind" let math = "Math" let apply = "apply" let null = "null" +let undefined = "undefined" let string_cap = "String" let fromCharcode = "fromCharCode" let eq = "=" @@ -84451,6 +84452,11 @@ and expression l cxt f (exp : J.expression) : Ext_pp_scope.t = and expression_desc cxt (l:int) f x : Ext_pp_scope.t = match x with + | Null -> + P.string f L.null; cxt + | Undefined + -> + P.string f L.undefined; cxt | Var v -> vident cxt f v | Bool b -> @@ -85078,6 +85084,8 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t = | Char_to_int _ | Math _ | Var _ + | Undefined + | Null | Str _ | Unicode _ | Array _ @@ -88371,6 +88379,8 @@ class virtual map = let _x_i1 = o#expression _x_i1 in Caml_block_set_length (_x, _x_i1) | Number _x -> let _x = o#number _x in Number _x | Object _x -> let _x = o#property_map _x in Object _x + | Undefined -> Undefined + | Null -> Null method expression : expression -> expression = fun { expression_desc = _x; comment = _x_i1 } -> let _x = o#expression_desc _x in @@ -93106,7 +93116,7 @@ let translate_arg_cst (cst : External_arg_spec.cst) = E.int (Int32.of_int i) | Arg_string_lit i -> E.str i - | Arg_js_null -> E.raw_js_code Exp "null" + | Arg_js_null -> E.nil | Arg_js_json s -> E.raw_js_code Exp s @@ -95554,8 +95564,8 @@ let translate loc begin match args with | [e] -> begin match e.expression_desc with - | Var _ -> - E.econd (E.is_null e) Js_of_lam_option.none (Js_of_lam_option.some e) + | Var _ | Undefined | Null -> + E.econd (E.is_null e) Js_of_lam_option.none (Js_of_lam_option.some e) | _ -> E.runtime_call Js_runtime_modules.js_primitive "null_to_opt" args @@ -95572,7 +95582,7 @@ let translate loc begin match args with | [e] -> begin match e.expression_desc with - | Var _ -> + | Var _ | Undefined | Null -> E.econd (E.is_undef e) Js_of_lam_option.none (Js_of_lam_option.some e) | _ -> E.runtime_call Js_runtime_modules.js_primitive @@ -95590,7 +95600,7 @@ let translate loc begin match args with | [e] -> begin match e.expression_desc with - | Var _ -> + | Var _ | Undefined | Null -> E.econd (E.is_null_undefined e) Js_of_lam_option.none (Js_of_lam_option.some e)