Skip to content

Commit bea0b32

Browse files
committed
emit str
1 parent 3525326 commit bea0b32

File tree

10 files changed

+38
-30
lines changed

10 files changed

+38
-30
lines changed

jscomp/core/j.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,13 @@ and expression_desc =
147147
it will be true when it's a method
148148
The last pararemter [true] return unit
149149
*)
150-
| Str of bool * string
151-
(* A string is UTF-8 encoded, the string may contain
150+
| Str of bool * string * bool
151+
(* A string is UTF-8 encoded, and may contain
152152
escape sequences.
153-
The first argument is used to mark it is non-pure, please
154-
don't optimize it, since it does have side effec,
155-
examples like "use asm;" and our compiler may generate "error;..."
156-
which is better to leave it alone
157-
The last argument is passed from as `j` from `{j||j}`
153+
First argument: used to mark it as non-pure.
154+
Please treat it carefully as it affects optimization.
155+
Second argument: the string "j" in `{j||j}`.
156+
The last argument: whether it should be escaped.
158157
*)
159158
| Unicode of string
160159
(* It is escaped string, print delimited by '"'*)

jscomp/core/js_analyzer.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
9090
| String_index (a, b) | Array_index (a, b) ->
9191
no_side_effect a && no_side_effect b
9292
| Is_null_or_undefined b -> no_side_effect b
93-
| Str (b, _) -> b
93+
| Str (b, _, _) -> b
9494
| Array (xs, _mutable_flag) | Caml_block (xs, _mutable_flag, _, _) ->
9595
(* create [immutable] block,
9696
does not really mean that this opreation itself is [pure].
@@ -181,8 +181,8 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
181181
| Bin (op1, a1, b1) ->
182182
op0 = op1 && eq_expression a0 a1 && eq_expression b0 b1
183183
| _ -> false)
184-
| Str (a0, b0) -> (
185-
match y0 with Str (a1, b1) -> a0 = a1 && b0 = b1 | _ -> false)
184+
| Str (a0, b0, c0) -> (
185+
match y0 with Str (a1, b1, c1) -> a0 = a1 && b0 = b1 && c0 = c1 | _ -> false)
186186
| Static_index (e0, p0, off0) -> (
187187
match y0 with
188188
| Static_index (e1, p1, off1) ->

jscomp/core/js_dump.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
591591
P.string f s;
592592
P.string f "\"";
593593
cxt
594-
| Str (_, s) ->
594+
| Str (_, s, _) ->
595595
(*TODO --
596596
when utf8-> it will not escape '\\' which is definitely not we want
597597
*)

jscomp/core/js_dump_string.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ let escape_to_string s =
101101
escape_to_buffer buf s;
102102
Ext_buffer.contents buf
103103

104+
(*
105+
let pp_string ?(escape=true) f s =
106+
if escape then
107+
P.string f (escape_to_string s)
108+
else
109+
P.string f ("\"" ^ s ^ "\"")
110+
111+
*)
104112
let pp_string f s = P.string f (escape_to_string s)
105113
(* let _best_string_quote s =
106114
let simple = ref 0 in

jscomp/core/js_dump_string.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@
2727
*)
2828
val escape_to_string : string -> string
2929

30+
(* val pp_string : ?escape:bool -> Ext_pp.t -> string -> unit *)
3031
val pp_string : Ext_pp.t -> string -> unit

jscomp/core/js_exp_make.ml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ let pure_runtime_call module_name fn_name args =
125125

126126
let runtime_ref module_name fn_name = runtime_var_dot module_name fn_name
127127

128-
let str ?(pure = true) ?comment s : t =
129-
{ expression_desc = Str (pure, s); comment }
128+
let str ?(pure = true) ?comment ?(escape=true) s : t =
129+
{ expression_desc = Str (pure, s, escape); comment }
130130

131131
let unicode ?comment s : t = { expression_desc = Unicode s; comment }
132132

@@ -398,7 +398,7 @@ let extension_access (e : t) name (pos : int32) : t =
398398

399399
let string_index ?comment (e0 : t) (e1 : t) : t =
400400
match (e0.expression_desc, e1.expression_desc) with
401-
| Str (_, s), Number (Int { i; _ }) ->
401+
| Str (_, s, _), Number (Int { i; _ }) ->
402402
(* Don't optimize {j||j} *)
403403
let i = Int32.to_int i in
404404
if i >= 0 && i < String.length s then
@@ -477,7 +477,7 @@ let array_length ?comment (e : t) : t =
477477

478478
let string_length ?comment (e : t) : t =
479479
match e.expression_desc with
480-
| Str (_, v) -> int ?comment (Int32.of_int (String.length v))
480+
| Str (_, v, _) -> int ?comment (Int32.of_int (String.length v))
481481
(* No optimization for {j||j}*)
482482
| _ -> { expression_desc = Length (e, String); comment }
483483

@@ -502,14 +502,14 @@ let function_length ?comment (e : t) : t =
502502

503503
let rec string_append ?comment (e : t) (el : t) : t =
504504
match (e.expression_desc, el.expression_desc) with
505-
| Str (_, a), String_append ({ expression_desc = Str (_, b) }, c) ->
505+
| Str (_, a, _), String_append ({ expression_desc = Str (_, b, _) }, c) ->
506506
string_append ?comment (str (a ^ b)) c
507-
| String_append (c, { expression_desc = Str (_, b) }), Str (_, a) ->
507+
| String_append (c, { expression_desc = Str (_, b, _) }), Str (_, a, _) ->
508508
string_append ?comment c (str (b ^ a))
509-
| ( String_append (a, { expression_desc = Str (_, b) }),
510-
String_append ({ expression_desc = Str (_, c) }, d) ) ->
509+
| ( String_append (a, { expression_desc = Str (_, b, _) }),
510+
String_append ({ expression_desc = Str (_, c, _) }, d) ) ->
511511
string_append ?comment (string_append a (str (b ^ c))) d
512-
| Str (_, a), Str (_, b) -> str ?comment (a ^ b)
512+
| Str (_, a, _), Str (_, b, _) -> str ?comment (a ^ b)
513513
| _, _ -> { comment; expression_desc = String_append (e, el) }
514514

515515
let obj ?comment properties : t =
@@ -548,7 +548,7 @@ let rec triple_equal ?comment (e0 : t) (e1 : t) : t =
548548
(Null | Undefined) )
549549
when no_side_effect e0 ->
550550
false_
551-
| Str (_, x), Str (_, y) ->
551+
| Str (_, x, _), Str (_, y, _) ->
552552
(* CF*)
553553
bool (Ext_string.equal x y)
554554
| Number (Int { i = i0; _ }), Number (Int { i = i1; _ }) -> bool (i0 = i1)
@@ -726,7 +726,7 @@ let int_equal = float_equal
726726

727727
let string_equal ?comment (e0 : t) (e1 : t) : t =
728728
match (e0.expression_desc, e1.expression_desc) with
729-
| Str (_, a0), Str (_, b0) -> bool (Ext_string.equal a0 b0)
729+
| Str (_, a0, _), Str (_, b0, _) -> bool (Ext_string.equal a0 b0)
730730
| Unicode a0, Unicode b0 -> bool (Ext_string.equal a0 b0)
731731
| _, _ -> { expression_desc = Bin (EqEqEq, e0, e1); comment }
732732

@@ -810,7 +810,7 @@ let uint32 ?comment n : J.expression =
810810

811811
let string_comp (cmp : J.binop) ?comment (e0 : t) (e1 : t) =
812812
match (e0.expression_desc, e1.expression_desc) with
813-
| Str (_, a0), Str (_, b0) -> (
813+
| Str (_, a0, _), Str (_, b0, _) -> (
814814
match cmp with
815815
| EqEqEq -> bool (a0 = b0)
816816
| NotEqEq -> bool (a0 <> b0)

jscomp/core/js_exp_make.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ val pure_runtime_call :
8282

8383
val runtime_ref : string -> string -> t
8484

85-
val str : ?pure:bool -> ?comment:string -> string -> t
85+
val str : ?pure:bool -> ?comment:string -> ?escape:bool -> string -> t
8686

8787
val unicode : ?comment:string -> string -> t
8888

jscomp/core/js_of_lam_variant.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ let eval (arg : J.expression) (dispatches : (string * string) list) : E.t =
3232
if arg == E.undefined then E.undefined
3333
else
3434
match arg.expression_desc with
35-
| Str (_, s) ->
35+
| Str (_, s, _) ->
3636
let s = Ext_list.assoc_by_string dispatches s None in
37-
E.str s
37+
E.str (* ~escape:false *) s
3838
| _ ->
3939
E.of_block
4040
[
@@ -63,7 +63,7 @@ let eval (arg : J.expression) (dispatches : (string * string) list) : E.t =
6363
let eval_as_event (arg : J.expression)
6464
(dispatches : (string * string) list option) =
6565
match arg.expression_desc with
66-
| Caml_block ([ { expression_desc = Str (_, s) }; cb ], _, _, Blk_poly_var _)
66+
| Caml_block ([ { expression_desc = Str (_, s, _) }; cb ], _, _, Blk_poly_var _)
6767
when Js_analyzer.no_side_effect_expression cb ->
6868
let v =
6969
match dispatches with
@@ -101,7 +101,7 @@ let eval_as_int (arg : J.expression) (dispatches : (string * int) list) : E.t =
101101
if arg == E.undefined then E.undefined
102102
else
103103
match arg.expression_desc with
104-
| Str (_, i) ->
104+
| Str (_, i, _) ->
105105
E.int (Int32.of_int (Ext_list.assoc_by_string dispatches i None))
106106
| _ ->
107107
E.of_block

jscomp/core/js_stmt_make.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ let string_switch ?(comment : string option)
131131
?(declaration : (J.property * Ident.t) option) ?(default : J.block option)
132132
(e : J.expression) (clauses : (string * J.case_clause) list) : t =
133133
match e.expression_desc with
134-
| Str (_, s) -> (
134+
| Str (_, s, _) -> (
135135
let continuation =
136136
match
137137
Ext_list.find_opt clauses (fun (switch_case, x) ->

jscomp/frontend/ast_derive_js_mapper.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ let init () =
282282
PStr
283283
[
284284
Str.eval
285-
(Exp.constant (Const.string revData));
285+
(Exp.constant (Const.string revData (* XXX *)));
286286
] )
287287
else expMap);
288288
toJsBody

0 commit comments

Comments
 (0)