From 534ca4d1fca57bfd8220ecbe790937ea5685243e Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Mon, 1 Feb 2016 14:55:58 -0500 Subject: [PATCH] refine let_kind in js ir, simplify [string_of_int] and other micro-optimizations --- jscomp/j.ml | 2 +- jscomp/js_dump.ml | 2 +- jscomp/js_fold.ml | 3 +- jscomp/js_helper.ml | 39 ++++++++++---------- jscomp/js_helper.mli | 9 ++--- jscomp/js_inline_and_eliminate.ml | 2 +- jscomp/js_map.ml | 3 +- jscomp/js_op.ml | 8 +++-- jscomp/js_pass_flatten_and_mark_dead.ml | 2 +- jscomp/js_pass_scope.ml | 8 ++--- jscomp/lam_compile.ml | 10 +++--- jscomp/lam_compile_global.ml | 3 ++ jscomp/lam_compile_group.ml | 47 +++++++++++++++---------- jscomp/lam_dispatch_primitive.ml | 6 ++-- jscomp/runtime/caml_primitive.js | 12 ------- jscomp/runtime/caml_primitive.ts | 24 +------------ jscomp/runtime/caml_string.d.ts | 1 - jscomp/runtime/caml_string.js | 5 --- jscomp/runtime/caml_string.ml | 2 -- jscomp/runtime/caml_string.mli | 3 -- jscomp/stdlib/bytes.js | 4 ++- jscomp/stdlib/camlinternalFormat.js | 8 ++--- jscomp/stdlib/format.js | 4 +-- jscomp/stdlib/pervasives.js | 8 +++-- jscomp/stdlib/random.js | 2 +- jscomp/test/a_scope_bug.js | 3 +- jscomp/test/buffer_test.js | 3 +- jscomp/test/complex_while_loop.js | 3 +- jscomp/test/hashtbl_test.js | 7 ++-- jscomp/test/map_test.js | 4 +-- jscomp/test/of_string_test.js | 2 +- jscomp/test/test_cps.js | 3 +- jscomp/test/test_google_closure.js | 5 ++- jscomp/test/test_side_effect_functor.js | 3 +- jscomp/test/test_string_map.js | 4 +-- jscomp/test/test_while_closure.js | 3 +- jscomp/test/test_while_side_effect.js | 7 ++-- 37 files changed, 116 insertions(+), 148 deletions(-) diff --git a/jscomp/j.ml b/jscomp/j.ml index 2d1ec5c135..de83a9742d 100644 --- a/jscomp/j.ml +++ b/jscomp/j.ml @@ -100,7 +100,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 list (* For [caml_array_append]*) + | Array_append of expression * expression (* For [caml_array_append]*) | Tag_ml_obj of expression | String_append of expression * expression | Int_of_boolean of expression diff --git a/jscomp/js_dump.ml b/jscomp/js_dump.ml index 1831233a8c..3412b948c1 100644 --- a/jscomp/js_dump.ml +++ b/jscomp/js_dump.ml @@ -432,7 +432,7 @@ and 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)) + P.paren_group f 1 (fun _ -> arguments cxt f [el])) | Array_copy e -> P.group f 1 (fun _ -> diff --git a/jscomp/js_fold.ml b/jscomp/js_fold.ml index f0fb34af23..8e3d1a8897 100644 --- a/jscomp/js_fold.ml +++ b/jscomp/js_fold.ml @@ -339,8 +339,7 @@ class virtual fold = | 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#list (fun o -> o#expression) _x_i1 in o + let o = o#expression _x in let o = o#expression _x_i1 in o | Tag_ml_obj _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/js_helper.ml b/jscomp/js_helper.ml index 22aa0d24cf..ca3b93f1ef 100644 --- a/jscomp/js_helper.ml +++ b/jscomp/js_helper.ml @@ -142,15 +142,26 @@ module Exp = struct let str ?(pure=true) ?comment s : t = {expression_desc = Str (pure,s); comment} - let any_to_string ?comment (e : t) : t = + let anything_to_string ?comment (e : t) : t = match e.expression_desc with | Str _ -> e | _ -> {expression_desc = Anything_to_string e ; comment} - + + (* we can do constant folding here, but need to make sure the result is consistent + {[ + let f x = string_of_int x + ;; f 3 + ]} + {[ + string_of_int 3 + ]} + *) + let int_to_string ?comment (e : t) : t = + anything_to_string ?comment e (* Shared mutable state is evil [Js_fun_env.empty] is a mutable state .. *) - let efun ?comment ?immutable_mask + let fun_ ?comment ?immutable_mask params block : t = let len = List.length params in { @@ -308,6 +319,8 @@ module Exp = struct 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)} @@ -911,12 +924,7 @@ module Stmt = struct { statement_desc = Exp e; comment} let declare_variable ?comment ?ident_info ~kind (v:Ident.t) : t= - let property : J.property = - match (kind : Lambda.let_kind ) with - | (Alias | Strict | StrictOpt ) - -> Immutable - | Variable -> Mutable - in + let property : J.property = kind in let ident_info : J.ident_info = match ident_info with | None -> {used_stats = NA} @@ -927,12 +935,7 @@ module Stmt = struct comment} let define ?comment ?ident_info ~kind (v:Ident.t) exp : t= - let property : J.property = - match (kind : Lambda.let_kind ) with - | (Alias | Strict | StrictOpt ) - -> Immutable - | Variable -> Mutable - in + let property : J.property = kind in let ident_info : J.ident_info = match ident_info with | None -> {used_stats = NA} @@ -1130,10 +1133,10 @@ module Stmt = struct - let const_variable ?comment ?exp (v:Ident.t) : t= + let alias_variable ?comment ?exp (v:Ident.t) : t= {statement_desc = Variable { - ident = v; value = exp; property = Immutable; + ident = v; value = exp; property = Alias; ident_info = {used_stats = NA } }; comment} @@ -1152,7 +1155,7 @@ module Stmt = struct statement_desc = J.Variable { ident = id; value = Some (Exp.unit ()) ; - property = Mutable; + property = Variable; ident_info = {used_stats = NA} }; comment diff --git a/jscomp/js_helper.mli b/jscomp/js_helper.mli index 337b84281f..bee4e525d5 100644 --- a/jscomp/js_helper.mli +++ b/jscomp/js_helper.mli @@ -96,7 +96,7 @@ module Exp : sig val str : ?pure:bool -> ?comment:string -> string -> t - val efun : ?comment:string -> + val fun_ : ?comment:string -> ?immutable_mask:bool array -> J.ident list -> J.block -> t val econd : ?comment:string -> t -> t -> t -> t @@ -126,7 +126,7 @@ module Exp : sig val char_to_int : unary_op - val array_append : ?comment:string -> t -> t list -> t + val array_append : binary_op val array_copy : unary_op val string_append : binary_op @@ -189,7 +189,8 @@ module Exp : sig val dump : ?comment:string -> Js_op.level -> t list -> t - val any_to_string : unary_op + val anything_to_string : unary_op + val int_to_string : unary_op val to_json_string : unary_op val new_ : ?comment:string -> J.expression -> J.expression list -> t @@ -285,7 +286,7 @@ module Stmt : sig ?ident_info:J.ident_info -> kind:Lambda.let_kind -> Ident.t -> J.expression -> t - val const_variable : + val alias_variable : ?comment:string -> ?exp:J.expression -> Ident.t -> t val assign : ?comment:string -> J.ident -> J.expression -> t diff --git a/jscomp/js_inline_and_eliminate.ml b/jscomp/js_inline_and_eliminate.ml index ca29ed7a38..b81232cca5 100644 --- a/jscomp/js_inline_and_eliminate.ml +++ b/jscomp/js_inline_and_eliminate.ml @@ -166,7 +166,7 @@ let subst name export_set stats = -> self#statement st :: self#block rest | { value = Some {expression_desc = Fun (params, block, _env) ; comment = _}; - property = Immutable; + property = (Alias | StrictOpt | Strict); ident_info = {used_stats = Once_pure }; ident = _ } as v diff --git a/jscomp/js_map.ml b/jscomp/js_map.ml index 7bd0ba0158..ca83ced193 100644 --- a/jscomp/js_map.ml +++ b/jscomp/js_map.ml @@ -371,8 +371,7 @@ class virtual map = | 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#list (fun o -> o#expression) _x_i1 - in Array_append (_x, _x_i1) + let _x_i1 = o#expression _x_i1 in Array_append (_x, _x_i1) | Tag_ml_obj _x -> let _x = o#expression _x in Tag_ml_obj _x | String_append (_x, _x_i1) -> let _x = o#expression _x in diff --git a/jscomp/js_op.ml b/jscomp/js_op.ml index 7587728a13..026a265f7d 100644 --- a/jscomp/js_op.ml +++ b/jscomp/js_op.ml @@ -127,9 +127,11 @@ type kind = | Runtime | External of string -type property = - | Mutable - | Immutable +type property = Lambda.let_kind = + | Strict + | Alias + | StrictOpt + | Variable type int_or_char = { i : int; diff --git a/jscomp/js_pass_flatten_and_mark_dead.ml b/jscomp/js_pass_flatten_and_mark_dead.ml index a7bae8c6c8..840680f62d 100644 --- a/jscomp/js_pass_flatten_and_mark_dead.ml +++ b/jscomp/js_pass_flatten_and_mark_dead.ml @@ -172,7 +172,7 @@ let subst_map name = object (self) {v with statement_desc = (Exp x)} | Variable ({ ident ; - property = Immutable; + property = (Strict | StrictOpt | Alias); value = Some ({expression_desc = (Array ( _:: _ :: _ as ls, Immutable))} as array) } as variable) -> (** If we do this, we should prevent incorrect inlning to inline it into an array :) diff --git a/jscomp/js_pass_scope.ml b/jscomp/js_pass_scope.ml index e23b31f616..19892177b1 100644 --- a/jscomp/js_pass_scope.ml +++ b/jscomp/js_pass_scope.ml @@ -196,10 +196,10 @@ let scope_pass = | { ident ; value; property } -> let obj = (match self#get_in_loop, property with - | true, Mutable + | true, Variable -> self#add_loop_mutable_variable ident - | true, Immutable + | true, (Strict | StrictOpt | Alias) (* Not real true immutable in javascript since it's in the loop @@ -235,10 +235,10 @@ let scope_pass = (* else *) self#add_loop_mutable_variable ident end - | false, Mutable + | false, Variable -> self#add_mutable_variable ident - | false, Immutable + | false, (Strict | StrictOpt | Alias) -> self )#add_defined_ident ident in diff --git a/jscomp/lam_compile.ml b/jscomp/lam_compile.ml index 535db02162..9823def135 100644 --- a/jscomp/lam_compile.ml +++ b/jscomp/lam_compile.ml @@ -90,7 +90,7 @@ and compile_recursive_let (cxt : Lam_compile_defs.cxt) (id : Ident.t) (arg : Lam jmp_table = Lam_compile_defs.empty_handler_map} body in if ret.triggered then let body_block = Js_output.to_block output in - E.efun (* TODO: save computation of length several times *) + E.fun_ (* TODO: save computation of length several times *) ~immutable_mask:ret.immutable_mask (List.map (fun x -> try Ident_map.find x ret.new_params with Not_found -> x) @@ -107,7 +107,7 @@ and compile_recursive_let (cxt : Lam_compile_defs.cxt) (id : Ident.t) (arg : Lam ] else (* TODO: save computation of length several times *) - E.efun params (Js_output.to_block output ) + E.fun_ params (Js_output.to_block output ) ), [] | (Lprim(Pmakeblock _ , _) ) -> (* Lconst should not appear here if we do [scc] @@ -261,7 +261,7 @@ and match lam with | Lfunction(kind, params, body) -> Js_output.handle_name_tail st should_return lam - (E.efun + (E.fun_ params (* Invariant: jmp_table can not across function boundary, here we share env @@ -918,11 +918,11 @@ and (* | String_length e *) (* -> *) (* let len = Ext_ident.create "_length" in *) - (* b2 @ [ S.const_variable len ~exp:e2 ], J.Finish (Id len ) *) + (* b2 @ [ S.alias_variable len ~exp:e2 ], J.Finish (Id len ) *) (* | _ -> *) (* (\* TODO: guess a better name when possible*\) *) (* let len = Ext_ident.create "_finish" in *) - (* b2 @ [S.const_variable len ~exp:e2], J.Finish (Id len) *) + (* b2 @ [S.alias_variable len ~exp:e2], J.Finish (Id len) *) (* in *) b1 @ (S.define ~kind:Variable id e1 :: b2 ) @ ([ diff --git a/jscomp/lam_compile_global.ml b/jscomp/lam_compile_global.ml index 2fc6c2d61f..3e4e9f7ba3 100644 --- a/jscomp/lam_compile_global.ml +++ b/jscomp/lam_compile_global.ml @@ -115,10 +115,13 @@ let get_exp_with_args (id : Ident.t) (pos : int) env (args : J.expression list) id.flags pos )) + ~found:(fun {id; name;arity; _} -> match id, name, args with | {name = "Pervasives"; _}, "^", [ e0 ; e1] -> E.string_append e0 e1 + | {name = "Pervasives"; _}, "string_of_int", [e] + -> E.int_to_string e | {name = "Pervasives"; _}, "print_endline", ([ _ ] as args) -> E.seq (E.dump Log args) (E.unit ()) | {name = "Pervasives"; _}, "prerr_endline", ([ _ ] as args) -> diff --git a/jscomp/lam_compile_group.ml b/jscomp/lam_compile_group.ml index c2bb344ea3..6f04bf1d8c 100644 --- a/jscomp/lam_compile_group.ml +++ b/jscomp/lam_compile_group.ml @@ -36,10 +36,11 @@ let compile_group ({filename = file_name; env;} as meta : Lam_stats.meta) 2. [E.builtin_dot] for javascript builtin 3. [E.mldot] *) + (* ATTENTION: check {!Lam_compile_global} for consistency *) (** Special handling for values in [Pervasives] *) | Single(_, ({name="stdout"|"stderr"|"stdin";_} as id),_ ), "pervasives.ml" -> - Js_output.of_stmt @@ S.const_variable id + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.runtime_ref Js_helper.io id.name) (* we delegate [stdout, stderr, and stdin] into [caml_io] module, @@ -49,11 +50,11 @@ let compile_group ({filename = file_name; env;} as meta : Lam_stats.meta) *) | Single(_, ({name="infinity";_} as id),_ ), "pervasives.ml" -> (* TODO: check relative path to compiler*) - Js_output.of_stmt @@ S.const_variable id ~exp:(E.js_global "Infinity") + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "Infinity") | Single(_, ({name="neg_infinity";_} as id),_ ), "pervasives.ml" -> - Js_output.of_stmt @@ S.const_variable id ~exp:(E.js_global "-Infinity") + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "-Infinity") | Single(_, ({name="nan";_} as id),_ ), "pervasives.ml" -> - Js_output.of_stmt @@ S.const_variable id ~exp:(E.js_global "NaN") + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "NaN") (* TODO: Make it more safe, we should rewrite the last one... @@ -62,39 +63,47 @@ let compile_group ({filename = file_name; env;} as meta : Lam_stats.meta) [Lam_dispatch_primitive], here it makes an exception since this function is not a primitive *) | Single(_, ({name="^";_} as id),_ ), "pervasives.ml" -> - Js_output.of_stmt @@ S.const_variable id - ~exp:(E.runtime_ref Js_helper.string "add") + Js_output.of_stmt @@ S.alias_variable id + ~exp:(let a = Ext_ident.create "a" in + let b = Ext_ident.create "b" in + E.fun_ [a;b] [S.return (E.string_append (E.var a) (E.var b))] + ) (* QUICK hack to make hello world example nicer, Note the arity of [print_endline] is already analyzed before, so it should be safe *) | Single(_, ({name="print_endline";_} as id),_ ), "pervasives.ml" -> - Js_output.of_stmt @@ S.const_variable id + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "console.log") | Single(_, ({name="prerr_endline";_} as id),_ ), "pervasives.ml" -> - Js_output.of_stmt @@ S.const_variable id + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "console.error") | Single(_, ({name="string_of_int";_} as id),_ ), "pervasives.ml" -> - Js_output.of_stmt @@ S.const_variable id ~exp:(E.runtime_ref - Js_helper.prim "string_of_int") + Js_output.of_stmt @@ S.alias_variable id + ~exp:( + let arg = Ext_ident.create "param" in + E.fun_ [arg] [S.return (E.anything_to_string (E.var arg))] + ) | Single(_, ({name="max_float";_} as id),_ ), "pervasives.ml" -> - Js_output.of_stmt @@ S.const_variable id + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global_dot "Number" "MAX_VALUE") | Single(_, ({name="min_float";_} as id) ,_ ), "pervasives.ml" -> - Js_output.of_stmt @@ S.const_variable id + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global_dot "Number" "MIN_VALUE") | Single(_, ({name="epsilon_float";_} as id) ,_ ), "pervasives.ml" -> - Js_output.of_stmt @@ S.const_variable id + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global_dot "Number" "EPSILON") | Single(_, ({name="cat";_} as id) ,_ ), "bytes.ml" -> - Js_output.of_stmt @@ S.const_variable id - ~exp:(E.runtime_ref - Js_helper.string "bytes_cat") + Js_output.of_stmt @@ S.alias_variable id + ~exp:(let a = Ext_ident.create "a" in + let b = Ext_ident.create "b" in + E.fun_ [a;b] [S.return (E.array_append (E.var a) (E.var b))] + ) (** Special handling for values in [Sys] *) | Single(_, ({name="max_array_length" | "max_string_length";_} as id) ,_ ), "sys.ml" -> @@ -102,15 +111,15 @@ let compile_group ({filename = file_name; env;} as meta : Lam_stats.meta) note that casual handling of {!Sys.max_string_length} could result into negative value which could cause wrong behavior of {!Buffer.create} *) - Js_output.of_stmt @@ S.const_variable id ~exp:(E.float "4_294_967_295.") + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.float "4_294_967_295.") | Single(_, ({name="max_int";_} as id) ,_ ), ("sys.ml" | "nativeint.ml") -> (* See [js_knowledge] Max int section, (2. ** 53. -. 1.;;) can not be expressed by OCaml int *) - Js_output.of_stmt @@ S.const_variable id ~exp:(E.float "9007199254740991.") + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.float "9007199254740991.") | Single(_, ({name="min_int";_} as id) ,_ ), ("sys.ml" | "nativeint.ml") -> (* See [js_knowledge] Max int section, -. (2. ** 53. -. 1.);; can not be expressed by OCaml int *) - Js_output.of_stmt @@ S.const_variable id ~exp:(E.float ("-9007199254740991.")) + Js_output.of_stmt @@ S.alias_variable id ~exp:(E.float ("-9007199254740991.")) | Single (kind, id, lam), _ -> (* let lam = Optimizer.simplify_lets [] lam in *) diff --git a/jscomp/lam_dispatch_primitive.ml b/jscomp/lam_dispatch_primitive.ml index c760f72ee2..17a8cbb958 100644 --- a/jscomp/lam_dispatch_primitive.ml +++ b/jscomp/lam_dispatch_primitive.ml @@ -149,7 +149,7 @@ let query (prim : Lam_compile_env.primitive_description) | "caml_array_append" -> begin match args with - | [e0;e1] -> E.array_append e0 [e1] + | [e0;e1] -> E.array_append e0 e1 | _ -> assert false end @@ -617,7 +617,7 @@ let query (prim : Lam_compile_env.primitive_description) -> begin match args with | [a;b] -> - E.array_append a [b] + E.array_append a b | _ -> assert false end | "js_string_append" @@ -661,7 +661,7 @@ let query (prim : Lam_compile_env.primitive_description) | "js_anything_to_string" -> begin match args with - | [e] -> E.any_to_string e + | [e] -> E.anything_to_string e | _ -> assert false end diff --git a/jscomp/runtime/caml_primitive.js b/jscomp/runtime/caml_primitive.js index 55c4e6d220..f903f3f2d0 100644 --- a/jscomp/runtime/caml_primitive.js +++ b/jscomp/runtime/caml_primitive.js @@ -297,15 +297,3 @@ function caml_hash(count, limit, seed, o) { return hashCode(JSON.stringify(o)); } exports.caml_hash = caml_hash; -/** - * @see {Caml_format.caml_format_int} - * Note that in {Pervasives.string_of_int = format_int "%d"}, we do the inline - * manually here - * We would expect more robust code specialization in the future - * @param {number} x - * @returns {string} - */ -function string_of_int(x) { - return "" + x; -} -exports.string_of_int = string_of_int; diff --git a/jscomp/runtime/caml_primitive.ts b/jscomp/runtime/caml_primitive.ts index 91efcb76cd..80d4f86adf 100644 --- a/jscomp/runtime/caml_primitive.ts +++ b/jscomp/runtime/caml_primitive.ts @@ -296,46 +296,24 @@ function caml_hash(count,limit, seed, o) { } - - - - -/** - * @see {Caml_format.caml_format_int} - * Note that in {Pervasives.string_of_int = format_int "%d"}, we do the inline - * manually here - * We would expect more robust code specialization in the future - * @param {number} x - * @returns {string} - */ -function string_of_int( x : number) : string{ - return "" + x; -} - export { - caml_update_dummy, caml_compare, caml_hash, caml_int_compare, // Inline? caml_int_compare as caml_int32_compare, caml_int_compare as caml_nativeint_compare, - caml_equal, caml_notequal, caml_greaterequal, caml_greaterthan, caml_lessequal, caml_lessthan, - - caml_convert_raw_backtrace_slot, - caml_bswap16, caml_int32_bswap, caml_int32_bswap as caml_nativeint_bswap, - caml_int64_bswap, - string_of_int + caml_int64_bswap } diff --git a/jscomp/runtime/caml_string.d.ts b/jscomp/runtime/caml_string.d.ts index a0a47be208..6df994715b 100644 --- a/jscomp/runtime/caml_string.d.ts +++ b/jscomp/runtime/caml_string.d.ts @@ -1,5 +1,4 @@ export var add: (prim : any, prim : any) => any ; -export var bytes_cat: (a : any, b : any) => any ; export var bytes_of_string: (s : any) => any ; export var bytes_to_string: (a : any) => any ; export var caml_is_printable: (c : any) => any ; diff --git a/jscomp/runtime/caml_string.js b/jscomp/runtime/caml_string.js index c6dfa58e85..63cc9a7156 100644 --- a/jscomp/runtime/caml_string.js +++ b/jscomp/runtime/caml_string.js @@ -7,10 +7,6 @@ function add(prim, prim$1) { return prim + prim$1; } -function bytes_cat(a, b) { - return a.concat(b); -} - function caml_string_get(s, i) { if (i >= s.length || i < 0) { throw [ @@ -157,7 +153,6 @@ function caml_is_printable(c) { } exports.add = add; -exports.bytes_cat = bytes_cat; exports.bytes_of_string = bytes_of_string; exports.bytes_to_string = bytes_to_string; exports.caml_is_printable = caml_is_printable; diff --git a/jscomp/runtime/caml_string.ml b/jscomp/runtime/caml_string.ml index d3a1cf3e12..6c2464165b 100644 --- a/jscomp/runtime/caml_string.ml +++ b/jscomp/runtime/caml_string.ml @@ -31,8 +31,6 @@ external bytes_of_int_array : int array -> bytes = "%identity" external new_uninitialized_bytes : int -> bytes = "js_create_array" let add = append -let bytes_cat a b = - bytes_of_int_array (array_append (bytes_to_int_array a) (bytes_to_int_array b)) let caml_string_get s i= if i >= String.length s || i < 0 then diff --git a/jscomp/runtime/caml_string.mli b/jscomp/runtime/caml_string.mli index b3858efb34..3e4c987494 100644 --- a/jscomp/runtime/caml_string.mli +++ b/jscomp/runtime/caml_string.mli @@ -21,9 +21,6 @@ val add : string -> string -> string - - -val bytes_cat : bytes -> bytes -> bytes val bytes_of_string : string -> bytes val bytes_to_string : bytes -> string diff --git a/jscomp/stdlib/bytes.js b/jscomp/stdlib/bytes.js index 0f35d7452d..0594e5bc44 100644 --- a/jscomp/stdlib/bytes.js +++ b/jscomp/stdlib/bytes.js @@ -152,7 +152,9 @@ function concat(sep, l) { } } -var cat = Caml_string.bytes_cat; +function cat(a, b) { + return a.concat(b); +} function is_space(param) { var switcher = param - 9; diff --git a/jscomp/stdlib/camlinternalFormat.js b/jscomp/stdlib/camlinternalFormat.js index 229c929d5a..04ccf49e2d 100644 --- a/jscomp/stdlib/camlinternalFormat.js +++ b/jscomp/stdlib/camlinternalFormat.js @@ -491,7 +491,7 @@ function bprint_ignored_flag(buf, ign_flag) { function bprint_pad_opt(buf, pad_opt) { if (pad_opt) { - return buffer_add_string(buf, Pervasives.string_of_int(pad_opt[1])); + return buffer_add_string(buf, "" + pad_opt[1]); } else { return /* () */0; @@ -508,7 +508,7 @@ function bprint_padding(buf, pad) { return buffer_add_char(buf, /* "*" */42); } else { - return buffer_add_string(buf, Pervasives.string_of_int(pad[2])); + return buffer_add_string(buf, "" + pad[2]); } } } @@ -524,7 +524,7 @@ function bprint_precision(buf, prec) { } else { buffer_add_char(buf, /* "." */46); - return buffer_add_string(buf, Pervasives.string_of_int(prec[1])); + return buffer_add_string(buf, "" + prec[1]); } } @@ -3608,7 +3608,7 @@ function format_of_fconv(fconv, prec) { buffer_add_char(buf, /* "%" */37); bprint_fconv_flag(buf, fconv); buffer_add_char(buf, /* "." */46); - buffer_add_string(buf, Pervasives.string_of_int(prec$1)); + buffer_add_string(buf, "" + prec$1); buffer_add_char(buf, symb); return buffer_contents(buf); } diff --git a/jscomp/stdlib/format.js b/jscomp/stdlib/format.js index d70fe4f9f5..4e8d5f1eb7 100644 --- a/jscomp/stdlib/format.js +++ b/jscomp/stdlib/format.js @@ -666,7 +666,7 @@ function pp_print_string(state, s) { } function pp_print_int(state, i) { - return pp_print_string(state, Pervasives.string_of_int(i)); + return pp_print_string(state, "" + i); } function pp_print_float(state, f) { @@ -1252,7 +1252,7 @@ function print_string(param) { } function print_int(param) { - return pp_print_string(std_formatter, Pervasives.string_of_int(param)); + return pp_print_string(std_formatter, "" + param); } function print_float(param) { diff --git a/jscomp/stdlib/pervasives.js b/jscomp/stdlib/pervasives.js index 36b94e80ca..afaa50982e 100644 --- a/jscomp/stdlib/pervasives.js +++ b/jscomp/stdlib/pervasives.js @@ -77,7 +77,9 @@ var min_float = Number.MIN_VALUE; var epsilon_float = Number.EPSILON; -var $caret = Caml_string.add; +function $caret(a, b) { + return a + b; +} function char_of_int(n) { if (n < 0 || n > 255) { @@ -116,7 +118,9 @@ function bool_of_string(param) { } } -var string_of_int = Caml_primitive.string_of_int; +function string_of_int(param) { + return "" + param; +} function valid_float_lexem(s) { var l = s.length; diff --git a/jscomp/stdlib/random.js b/jscomp/stdlib/random.js index 985dbd5422..fc173de03d 100644 --- a/jscomp/stdlib/random.js +++ b/jscomp/stdlib/random.js @@ -26,7 +26,7 @@ function assign(st1, st2) { function full_init(s, seed) { var combine = function (accu, x) { - return Digest.string(accu + Pervasives.string_of_int(x)); + return Digest.string(accu + x); }; var extract = function (d) { return d.charCodeAt(0) + (d.charCodeAt(1) << 8) + (d.charCodeAt(2) << 16) + (d.charCodeAt(3) << 24); diff --git a/jscomp/test/a_scope_bug.js b/jscomp/test/a_scope_bug.js index 8049ff493c..c26771902b 100644 --- a/jscomp/test/a_scope_bug.js +++ b/jscomp/test/a_scope_bug.js @@ -1,14 +1,13 @@ // Generated CODE, PLEASE EDIT WITH CARE "use strict"; -var Pervasives = require("../stdlib/pervasives"); function odd(_z) { while(true) { var z = _z; var even = z * z; var a = even + 4 + even; - console.log(Pervasives.string_of_int(a)); + console.log("" + a); _z = 32; }; } diff --git a/jscomp/test/buffer_test.js b/jscomp/test/buffer_test.js index 04a135f646..058d1a3253 100644 --- a/jscomp/test/buffer_test.js +++ b/jscomp/test/buffer_test.js @@ -3,7 +3,6 @@ var Bytes = require("../stdlib/bytes"); var Caml_exceptions = require("../runtime/caml_exceptions"); -var Pervasives = require("../stdlib/pervasives"); var Mt = require("./mt"); var Buffer = require("../stdlib/buffer"); @@ -79,7 +78,7 @@ var suites_002 = [ function () { var v = Buffer.create(30); for(var i = 0; i<= 10; ++i){ - Buffer.add_string(v, Pervasives.string_of_int(i)); + Buffer.add_string(v, "" + i); } return Mt.assert_equal(Buffer.contents(v), "012345678910"); } diff --git a/jscomp/test/complex_while_loop.js b/jscomp/test/complex_while_loop.js index f959fb2359..fc874695e5 100644 --- a/jscomp/test/complex_while_loop.js +++ b/jscomp/test/complex_while_loop.js @@ -1,7 +1,6 @@ // Generated CODE, PLEASE EDIT WITH CARE "use strict"; -var Pervasives = require("../stdlib/pervasives"); function f() { var n = 0; @@ -16,7 +15,7 @@ function f() { }; return +(fib(n) > 10); }()) { - console.log(Pervasives.string_of_int(n)); + console.log("" + n); ++ n; }; return /* () */0; diff --git a/jscomp/test/hashtbl_test.js b/jscomp/test/hashtbl_test.js index 68158737b9..fb4597b2df 100644 --- a/jscomp/test/hashtbl_test.js +++ b/jscomp/test/hashtbl_test.js @@ -2,7 +2,6 @@ "use strict"; var Hashtbl = require("../stdlib/hashtbl"); -var Pervasives = require("../stdlib/pervasives"); var Mt = require("./mt"); var Caml_primitive = require("../runtime/caml_primitive"); var $$Array = require("../stdlib/array"); @@ -34,10 +33,10 @@ function f() { function g(count) { var tbl = Hashtbl.create(/* None */0, 17); for(var i = 0; i<= count; ++i){ - Hashtbl.replace(tbl, i * 2, Pervasives.string_of_int(i)); + Hashtbl.replace(tbl, i * 2, "" + i); } for(var i$1 = 0; i$1<= count; ++i$1){ - Hashtbl.replace(tbl, i$1 * 2, Pervasives.string_of_int(i$1)); + Hashtbl.replace(tbl, i$1 * 2, "" + i$1); } var v = to_list(tbl); return $$Array.of_list(List.sort(function (param, param$1) { @@ -86,7 +85,7 @@ var suites_002 = [ return [ /* tuple */0, 2 * i, - Pervasives.string_of_int(i) + "" + i ]; }), g(count) diff --git a/jscomp/test/map_test.js b/jscomp/test/map_test.js index e87df03ed6..07faeb2792 100644 --- a/jscomp/test/map_test.js +++ b/jscomp/test/map_test.js @@ -491,10 +491,10 @@ var int_map_suites_002 = [ var m = /* Empty */0; var count = 10000; for(var i = 0; i<= count; ++i){ - m = add$1(Pervasives.string_of_int(i), Pervasives.string_of_int(i), m); + m = add$1("" + i, "" + i, m); } for(var i$1 = 0; i$1<= count; ++i$1){ - Mt.assert_equal(find(Pervasives.string_of_int(i$1), m), Pervasives.string_of_int(i$1)); + Mt.assert_equal(find("" + i$1, m), "" + i$1); } return /* () */0; } diff --git a/jscomp/test/of_string_test.js b/jscomp/test/of_string_test.js index f7c91f7180..b5e05c9224 100644 --- a/jscomp/test/of_string_test.js +++ b/jscomp/test/of_string_test.js @@ -25,7 +25,7 @@ var suites_002 = [ return [ /* Eq */0, "10", - Pervasives.string_of_int(10) + "" + 10 ]; } ], diff --git a/jscomp/test/test_cps.js b/jscomp/test/test_cps.js index 7c7326d1a7..9f256dfb29 100644 --- a/jscomp/test/test_cps.js +++ b/jscomp/test/test_cps.js @@ -1,7 +1,6 @@ // Generated CODE, PLEASE EDIT WITH CARE "use strict"; -var Pervasives = require("../stdlib/pervasives"); var Caml_array = require("../runtime/caml_array"); function f(_n, _acc) { @@ -11,7 +10,7 @@ function f(_n, _acc) { if (n) { _acc = (function(n,acc){ return function () { - console.log(Pervasives.string_of_int(n)); + console.log("" + n); return acc(/* () */0); } }(n,acc)); diff --git a/jscomp/test/test_google_closure.js b/jscomp/test/test_google_closure.js index 9c2936ed4d..6930301997 100644 --- a/jscomp/test/test_google_closure.js +++ b/jscomp/test/test_google_closure.js @@ -1,8 +1,7 @@ // Generated CODE, PLEASE EDIT WITH CARE "use strict"; -var Pervasives = require("../stdlib/pervasives"); -var $$Array = require("../stdlib/array"); +var $$Array = require("../stdlib/array"); function f(a, b, _) { return a + b; @@ -22,7 +21,7 @@ for(var i = 0; i<= 2; ++i){ arr[i] = i + 1; } -var match_001 = Pervasives.string_of_int(3); +var match_001 = "" + 3; var a = match_001; diff --git a/jscomp/test/test_side_effect_functor.js b/jscomp/test/test_side_effect_functor.js index 2e8397683b..4f90cb342a 100644 --- a/jscomp/test/test_side_effect_functor.js +++ b/jscomp/test/test_side_effect_functor.js @@ -1,13 +1,12 @@ // Generated CODE, PLEASE EDIT WITH CARE "use strict"; -var Pervasives = require("../stdlib/pervasives"); var v = 0; ++ v; -console.log(Pervasives.string_of_int(v)); +console.log("" + v); function unuse_v() { return 35; diff --git a/jscomp/test/test_string_map.js b/jscomp/test/test_string_map.js index 78833b0c6d..2eb92bc2b9 100644 --- a/jscomp/test/test_string_map.js +++ b/jscomp/test/test_string_map.js @@ -153,13 +153,13 @@ function assertion_test() { var count = 1000000; timing("building", function () { for(var i = 0; i<= count; ++i){ - m[1] = add(Pervasives.string_of_int(i), Pervasives.string_of_int(i), m[1]); + m[1] = add("" + i, "" + i, m[1]); } return /* () */0; }); return timing("querying", function () { for(var i = 0; i<= count; ++i){ - find(Pervasives.string_of_int(i), m[1]); + find("" + i, m[1]); } return /* () */0; }); diff --git a/jscomp/test/test_while_closure.js b/jscomp/test/test_while_closure.js index 87d3df6e10..cba00075d6 100644 --- a/jscomp/test/test_while_closure.js +++ b/jscomp/test/test_while_closure.js @@ -1,7 +1,6 @@ // Generated CODE, PLEASE EDIT WITH CARE "use strict"; -var Pervasives = require("../stdlib/pervasives"); var Caml_exceptions = require("../runtime/caml_exceptions"); var Caml_array = require("../runtime/caml_array"); var $$Array = require("../stdlib/array"); @@ -38,7 +37,7 @@ $$Array.iter(function (x) { return x(/* () */0); }, arr); -console.log(Pervasives.string_of_int(v[1])); +console.log("" + v[1]); if (v[1] !== 45) { throw [ diff --git a/jscomp/test/test_while_side_effect.js b/jscomp/test/test_while_side_effect.js index 64101b1f97..08592a9645 100644 --- a/jscomp/test/test_while_side_effect.js +++ b/jscomp/test/test_while_side_effect.js @@ -1,14 +1,13 @@ // Generated CODE, PLEASE EDIT WITH CARE "use strict"; -var Pervasives = require("../stdlib/pervasives"); var v = [ 0, 0 ]; -while(console.log(Pervasives.string_of_int(v[1])), ++ v[1], +(v[1] < 10)) { +while(console.log("" + v[1]), ++ v[1], +(v[1] < 10)) { }; @@ -28,12 +27,12 @@ var x = [ while(function () { var y = 3; - console.log(Pervasives.string_of_int(x[1])); + console.log("" + x[1]); ++ y; ++ x[1]; return +(fib(x[1]) + fib(x[1]) < 20); }()) { - console.log(Pervasives.string_of_int(3)); + console.log("" + 3); }; exports.v = v;