Skip to content

Commit 695c5c3

Browse files
committed
Merge pull request #56 from bloomberg/any_to_string
add a primitive `js_anything_to_string` `cml_anything_to_string`
2 parents 1ffef4a + 872c185 commit 695c5c3

File tree

13 files changed

+130
-6
lines changed

13 files changed

+130
-6
lines changed

jscomp/j.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,27 @@ and expression_desc =
120120
which makes optimizations easier
121121
{[ JSON.stringify(value, replacer[, space]) ]}
122122
*)
123+
| Anything_to_string of expression
124+
(* for debugging utitlites,
125+
TODO: [Dump] is not necessary with this primitive
126+
Note that the semantics is slightly different from [JSON.stringify]
127+
{[
128+
JSON.stringify("x")
129+
]}
130+
{[
131+
""x""
132+
]}
133+
{[
134+
JSON.stringify(undefined)
135+
]}
136+
{[
137+
undefined
138+
]}
139+
{[ '' + undefined
140+
]}
141+
{[ 'undefined'
142+
]}
143+
*)
123144
| Dump of Js_op.level * expression list
124145
(* to support
125146
val log1 : 'a -> unit

jscomp/js_dump.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ and expression l cxt f (exp : J.expression) : Ext_pp_scope.t =
362362
expression_desc cxt l f exp.expression_desc
363363

364364
and
365-
expression_desc cxt (l:int) f expression_desc : Ext_pp_scope.t =
366-
match expression_desc with
365+
expression_desc cxt (l:int) f x : Ext_pp_scope.t =
366+
match x with
367367
| Var v ->
368368
vident cxt f v
369369

@@ -655,6 +655,11 @@ and
655655
P.space f ;
656656
expression 13 cxt f delta
657657
end
658+
| Anything_to_string e ->
659+
(* Note that we should not apply any smart construtor here,
660+
it's purely a convenice for pretty-printing
661+
*)
662+
expression_desc cxt l f (Bin (Plus, {expression_desc = Str (true,""); comment = None}, e))
658663

659664
| Bin (Minus, {expression_desc = Number (Int {i=0;_} | Float {f = "0."})}, e)
660665
(* TODO:
@@ -943,6 +948,7 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t =
943948
| Call ({expression_desc = Fun _; },_,_) -> true
944949

945950
| Fun _ | Object _ -> true
951+
| Anything_to_string _
946952
| String_of_small_int_array _
947953
| Call _
948954
| Array_append _

jscomp/js_fold.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ class virtual fold =
135135
(* TODO: in the future, it might make sense to group primitivie by type,
136136
which makes optimizations easier
137137
{[ JSON.stringify(value, replacer[, space]) ]}
138+
*)
139+
(* for debugging utitlites,
140+
TODO: [Dump] is not necessary with this primitive
141+
Note that the semantics is slightly different from [JSON.stringify]
142+
{[
143+
JSON.stringify("x")
144+
]}
145+
{[
146+
""x""
147+
]}
148+
{[
149+
JSON.stringify(undefined)
150+
]}
151+
{[
152+
undefined
153+
]}
154+
{[ '' + undefined
155+
]}
156+
{[ 'undefined'
157+
]}
138158
*)
139159
(* to support
140160
val log1 : 'a -> unit
@@ -329,6 +349,7 @@ class virtual fold =
329349
| Not _x -> let o = o#expression _x in o
330350
| String_of_small_int_array _x -> let o = o#expression _x in o
331351
| Json_stringify _x -> let o = o#expression _x in o
352+
| Anything_to_string _x -> let o = o#expression _x in o
332353
| Dump (_x, _x_i1) ->
333354
let o = o#unknown _x in
334355
let o = o#list (fun o -> o#expression) _x_i1 in o

jscomp/js_helper.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ module Exp = struct
142142

143143
let str ?(pure=true) ?comment s : t = {expression_desc = Str (pure,s); comment}
144144

145+
let any_to_string ?comment (e : t) : t =
146+
match e.expression_desc with
147+
| Str _ -> e
148+
| _ -> {expression_desc = Anything_to_string e ; comment}
149+
145150
(* Shared mutable state is evil
146151
[Js_fun_env.empty] is a mutable state ..
147152
*)

jscomp/js_helper.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ module Exp : sig
188188
val flat_call : binary_op
189189

190190
val dump : ?comment:string -> Js_op.level -> t list -> t
191+
192+
val any_to_string : unary_op
191193
val to_json_string : unary_op
192194

193195
val new_ : ?comment:string -> J.expression -> J.expression list -> t

jscomp/js_map.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,26 @@ class virtual map =
148148
(* TODO: in the future, it might make sense to group primitivie by type,
149149
which makes optimizations easier
150150
{[ JSON.stringify(value, replacer[, space]) ]}
151+
*)
152+
(* for debugging utitlites,
153+
TODO: [Dump] is not necessary with this primitive
154+
Note that the semantics is slightly different from [JSON.stringify]
155+
{[
156+
JSON.stringify("x")
157+
]}
158+
{[
159+
""x""
160+
]}
161+
{[
162+
JSON.stringify(undefined)
163+
]}
164+
{[
165+
undefined
166+
]}
167+
{[ '' + undefined
168+
]}
169+
{[ 'undefined'
170+
]}
151171
*)
152172
(* to support
153173
val log1 : 'a -> unit
@@ -363,6 +383,8 @@ class virtual map =
363383
| String_of_small_int_array _x ->
364384
let _x = o#expression _x in String_of_small_int_array _x
365385
| Json_stringify _x -> let _x = o#expression _x in Json_stringify _x
386+
| Anything_to_string _x ->
387+
let _x = o#expression _x in Anything_to_string _x
366388
| Dump (_x, _x_i1) ->
367389
let _x = o#unknown _x in
368390
let _x_i1 = o#list (fun o -> o#expression) _x_i1

jscomp/lam_dispatch_primitive.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,16 @@ let query (prim : Lam_compile_env.primitive_description)
655655
]}
656656
*)
657657
E.seq (E.dump Log args) (E.unit ())
658+
659+
| "caml_anything_to_string"
660+
(* patched to compiler to support for convenience *)
661+
| "js_anything_to_string"
662+
->
663+
begin match args with
664+
| [e] -> E.any_to_string e
665+
| _ -> assert false
666+
end
667+
658668
| "js_json_stringify"
659669
->
660670
begin match args with

jscomp/lib/js.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ external typeof : 'a -> string = "js_typeof"
2424
external to_json_string : 'a -> string = "js_json_stringify"
2525

2626
external log : 'a -> unit = "js_dump"
27+
28+
external anything_to_string : 'a -> string = "js_anything_to_string"

jscomp/lib/js.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ external to_json_string : 'a -> string = "js_json_stringify"
3434
the return value is [unit] instead of [undefined]
3535
*)
3636
external log : 'a -> unit = "js_dump"
37+
38+
39+
external anything_to_string : 'a -> string = "js_anything_to_string"

jscomp/test/.depend

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ lexer_test.cmo : number_lexer.cmo mt.cmo ../stdlib/list.cmi \
106106
../stdlib/lexing.cmi arith_syntax.cmo arith_parser.cmo arith_lexer.cmo
107107
lexer_test.cmx : number_lexer.cmx mt.cmx ../stdlib/list.cmx \
108108
../stdlib/lexing.cmx arith_syntax.cmx arith_parser.cmx arith_lexer.cmx
109-
lib_js_test.cmo : ../lib/js.cmi
110-
lib_js_test.cmx : ../lib/js.cmx
109+
lib_js_test.cmo : mt.cmo ../lib/js.cmi
110+
lib_js_test.cmx : mt.cmx ../lib/js.cmx
111111
list_test.cmo : ../stdlib/pervasives.cmi mt.cmo ../stdlib/list.cmi \
112112
../stdlib/array.cmi
113113
list_test.cmx : ../stdlib/pervasives.cmx mt.cmx ../stdlib/list.cmx \
@@ -462,8 +462,8 @@ lexer_test.cmo : number_lexer.cmo mt.cmo ../stdlib/list.cmi \
462462
../stdlib/lexing.cmi arith_syntax.cmo arith_parser.cmo arith_lexer.cmo
463463
lexer_test.cmj : number_lexer.cmj mt.cmj ../stdlib/list.cmj \
464464
../stdlib/lexing.cmj arith_syntax.cmj arith_parser.cmj arith_lexer.cmj
465-
lib_js_test.cmo : ../lib/js.cmi
466-
lib_js_test.cmj : ../lib/js.cmj
465+
lib_js_test.cmo : mt.cmo ../lib/js.cmi
466+
lib_js_test.cmj : mt.cmj ../lib/js.cmj
467467
list_test.cmo : ../stdlib/pervasives.cmi mt.cmo ../stdlib/list.cmi \
468468
../stdlib/array.cmi
469469
list_test.cmj : ../stdlib/pervasives.cmj mt.cmj ../stdlib/list.cmj \

0 commit comments

Comments
 (0)