Skip to content

add a primitive js_anything_to_string cml_anything_to_string #56

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions jscomp/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ 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
(* to support
val log1 : 'a -> unit
Expand Down
10 changes: 8 additions & 2 deletions jscomp/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ and expression l cxt f (exp : J.expression) : Ext_pp_scope.t =
expression_desc cxt l f exp.expression_desc

and
expression_desc cxt (l:int) f expression_desc : Ext_pp_scope.t =
match expression_desc with
expression_desc cxt (l:int) f x : Ext_pp_scope.t =
match x with
| Var v ->
vident cxt f v

Expand Down Expand Up @@ -655,6 +655,11 @@ 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, {expression_desc = Str (true,""); comment = None}, e))

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

| Fun _ | Object _ -> true
| Anything_to_string _
| String_of_small_int_array _
| Call _
| Array_append _
Expand Down
21 changes: 21 additions & 0 deletions jscomp/js_fold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ 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'
]}
*)
(* to support
val log1 : 'a -> unit
Expand Down Expand Up @@ -329,6 +349,7 @@ class virtual fold =
| 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
Expand Down
5 changes: 5 additions & 0 deletions jscomp/js_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ module Exp = struct

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

let any_to_string ?comment (e : t) : t =
match e.expression_desc with
| Str _ -> e
| _ -> {expression_desc = Anything_to_string e ; comment}

(* Shared mutable state is evil
[Js_fun_env.empty] is a mutable state ..
*)
Expand Down
2 changes: 2 additions & 0 deletions jscomp/js_helper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ module Exp : sig
val flat_call : binary_op

val dump : ?comment:string -> Js_op.level -> t list -> t

val any_to_string : unary_op
val to_json_string : unary_op

val new_ : ?comment:string -> J.expression -> J.expression list -> t
Expand Down
22 changes: 22 additions & 0 deletions jscomp/js_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,26 @@ 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'
]}
*)
(* to support
val log1 : 'a -> unit
Expand Down Expand Up @@ -363,6 +383,8 @@ 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
Expand Down
10 changes: 10 additions & 0 deletions jscomp/lam_dispatch_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,16 @@ let query (prim : Lam_compile_env.primitive_description)
]}
*)
E.seq (E.dump Log args) (E.unit ())

| "caml_anything_to_string"
(* patched to compiler to support for convenience *)
| "js_anything_to_string"
->
begin match args with
| [e] -> E.any_to_string e
| _ -> assert false
end

| "js_json_stringify"
->
begin match args with
Expand Down
2 changes: 2 additions & 0 deletions jscomp/lib/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ external typeof : 'a -> string = "js_typeof"
external to_json_string : 'a -> string = "js_json_stringify"

external log : 'a -> unit = "js_dump"

external anything_to_string : 'a -> string = "js_anything_to_string"
3 changes: 3 additions & 0 deletions jscomp/lib/js.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ external to_json_string : 'a -> string = "js_json_stringify"
the return value is [unit] instead of [undefined]
*)
external log : 'a -> unit = "js_dump"


external anything_to_string : 'a -> string = "js_anything_to_string"
8 changes: 4 additions & 4 deletions jscomp/test/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ lexer_test.cmo : number_lexer.cmo mt.cmo ../stdlib/list.cmi \
../stdlib/lexing.cmi arith_syntax.cmo arith_parser.cmo arith_lexer.cmo
lexer_test.cmx : number_lexer.cmx mt.cmx ../stdlib/list.cmx \
../stdlib/lexing.cmx arith_syntax.cmx arith_parser.cmx arith_lexer.cmx
lib_js_test.cmo : ../lib/js.cmi
lib_js_test.cmx : ../lib/js.cmx
lib_js_test.cmo : mt.cmo ../lib/js.cmi
lib_js_test.cmx : mt.cmx ../lib/js.cmx
list_test.cmo : ../stdlib/pervasives.cmi mt.cmo ../stdlib/list.cmi \
../stdlib/array.cmi
list_test.cmx : ../stdlib/pervasives.cmx mt.cmx ../stdlib/list.cmx \
Expand Down Expand Up @@ -462,8 +462,8 @@ lexer_test.cmo : number_lexer.cmo mt.cmo ../stdlib/list.cmi \
../stdlib/lexing.cmi arith_syntax.cmo arith_parser.cmo arith_lexer.cmo
lexer_test.cmj : number_lexer.cmj mt.cmj ../stdlib/list.cmj \
../stdlib/lexing.cmj arith_syntax.cmj arith_parser.cmj arith_lexer.cmj
lib_js_test.cmo : ../lib/js.cmi
lib_js_test.cmj : ../lib/js.cmj
lib_js_test.cmo : mt.cmo ../lib/js.cmi
lib_js_test.cmj : mt.cmj ../lib/js.cmj
list_test.cmo : ../stdlib/pervasives.cmi mt.cmo ../stdlib/list.cmi \
../stdlib/array.cmi
list_test.cmj : ../stdlib/pervasives.cmj mt.cmj ../stdlib/list.cmj \
Expand Down
1 change: 1 addition & 0 deletions jscomp/test/lib_js_test.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export var suites: any ;

22 changes: 22 additions & 0 deletions jscomp/test/lib_js_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Generated CODE, PLEASE EDIT WITH CARE
"use strict";

var Mt = require("./mt");

console.log(JSON.stringify([
/* :: */0,
Expand All @@ -18,4 +19,25 @@ console.log(JSON.stringify([

console.log("hey");

var suites_001 = [
/* tuple */0,
"anything_to_string",
function () {
return [
/* Eq */0,
"3",
"" + 3
];
}
];

var suites = [
/* :: */0,
suites_001,
/* [] */0
];

Mt.from_pair_suites("lib_js_test.ml", suites);

exports.suites = suites;
/* Not a pure module */
9 changes: 9 additions & 0 deletions jscomp/test/lib_js_test.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@



let () =
begin

Js.log @@ Js.to_json_string [1;2;3];
Js.log "hey";
end

let suites = Mt.[
"anything_to_string", (fun _ -> Eq("3", Js.anything_to_string 3 ))
]

;; Mt.from_pair_suites __FILE__ suites