From 15c053117ceee42ca911818843fee7d932237374 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Fri, 11 Mar 2016 17:04:16 -0500 Subject: [PATCH] fix #96, now put console.log in a function and correct return value --- jscomp/lam_compile_group.ml | 12 ++++++++++-- jscomp/stdlib/pervasives.js | 10 ++++++++-- jscomp/test/.depend | 4 ++++ jscomp/test/io_test.d.ts | 2 ++ jscomp/test/io_test.js | 13 +++++++++++++ jscomp/test/io_test.ml | 7 +++++++ jscomp/test/test.mllib | 3 ++- 7 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 jscomp/test/io_test.d.ts create mode 100644 jscomp/test/io_test.js create mode 100644 jscomp/test/io_test.ml diff --git a/jscomp/lam_compile_group.ml b/jscomp/lam_compile_group.ml index f727b8ef21..4923e41ee4 100644 --- a/jscomp/lam_compile_group.ml +++ b/jscomp/lam_compile_group.ml @@ -75,10 +75,18 @@ let compile_group ({filename = file_name; env;} as meta : Lam_stats.meta) *) | Single(_, ({name="print_endline";_} as id),_ ), "pervasives.ml" -> Js_output.of_stmt @@ S.alias_variable id - ~exp:(E.js_global "console.log") + ~exp:(let param = Ext_ident.create "param" in + E.fun_ [param] [S.return + (E.seq (E.call ~info:{arity=Full} + (E.js_global "console.log") [E.var param]) + E.zero_int_literal )] ) | Single(_, ({name="prerr_endline";_} as id),_ ), "pervasives.ml" -> Js_output.of_stmt @@ S.alias_variable id - ~exp:(E.js_global "console.error") + ~exp:(let param = Ext_ident.create "param" in + E.fun_ [param] [S.return + (E.seq (E.call ~info:{arity=Full} + (E.js_global "console.error") [E.var param]) + E.zero_int_literal )] ) | Single(_, ({name="string_of_int";_} as id),_ ), "pervasives.ml" -> diff --git a/jscomp/stdlib/pervasives.js b/jscomp/stdlib/pervasives.js index c49886eb96..b1adb8b92d 100644 --- a/jscomp/stdlib/pervasives.js +++ b/jscomp/stdlib/pervasives.js @@ -444,7 +444,10 @@ function print_float(f) { return output_string(stdout, valid_float_lexem(Caml_format.caml_format_float("%.12g", f))); } -var print_endline = console.log; +function print_endline(param) { + console.log(param); + return 0; +} function print_newline() { Caml_io.caml_ml_output_char(stdout, /* "\n" */10); @@ -471,7 +474,10 @@ function prerr_float(f) { return output_string(stderr, valid_float_lexem(Caml_format.caml_format_float("%.12g", f))); } -var prerr_endline = console.error; +function prerr_endline(param) { + console.error(param); + return 0; +} function prerr_newline() { Caml_io.caml_ml_output_char(stderr, /* "\n" */10); diff --git a/jscomp/test/.depend b/jscomp/test/.depend index 363e935a7d..6fd430ce00 100644 --- a/jscomp/test/.depend +++ b/jscomp/test/.depend @@ -164,6 +164,8 @@ int_overflow_test.cmo : ../stdlib/string.cmi mt.cmi ../stdlib/int32.cmi \ ../stdlib/char.cmi int_overflow_test.cmx : ../stdlib/string.cmx mt.cmx ../stdlib/int32.cmx \ ../stdlib/char.cmx +io_test.cmo : ../lib/js.cmi +io_test.cmx : ../lib/js.cmx js_bool_test.cmo : mt.cmi ../lib/js.cmi js_bool_test.cmx : mt.cmx ../lib/js.cmx js_date_test.cmo : mt.cmi ../lib/js_date.cmo @@ -618,6 +620,8 @@ int_overflow_test.cmo : ../stdlib/string.cmi mt.cmi ../stdlib/int32.cmi \ ../stdlib/char.cmi int_overflow_test.cmj : ../stdlib/string.cmj mt.cmj ../stdlib/int32.cmj \ ../stdlib/char.cmj +io_test.cmo : ../lib/js.cmi +io_test.cmj : ../lib/js.cmj js_bool_test.cmo : mt.cmi ../lib/js.cmi js_bool_test.cmj : mt.cmj ../lib/js.cmj js_date_test.cmo : mt.cmi ../lib/js_date.cmo diff --git a/jscomp/test/io_test.d.ts b/jscomp/test/io_test.d.ts new file mode 100644 index 0000000000..7cc5d6fdbe --- /dev/null +++ b/jscomp/test/io_test.d.ts @@ -0,0 +1,2 @@ +export var f: (param : any) => any ; + diff --git a/jscomp/test/io_test.js b/jscomp/test/io_test.js new file mode 100644 index 0000000000..5ece82c04b --- /dev/null +++ b/jscomp/test/io_test.js @@ -0,0 +1,13 @@ +// Generated CODE, PLEASE EDIT WITH CARE +'use strict'; + + +function f() { + var v = (console.error("x"), /* () */0); + console.log(v); + console.log((console.log("hi"), /* () */0)); + return /* () */0; +} + +exports.f = f; +/* No side effect */ diff --git a/jscomp/test/io_test.ml b/jscomp/test/io_test.ml new file mode 100644 index 0000000000..10d32c2f98 --- /dev/null +++ b/jscomp/test/io_test.ml @@ -0,0 +1,7 @@ + + +let f () = + let v = prerr_endline "x" in + Js.log v ; + let u = print_endline "hi" in + Js.log u diff --git a/jscomp/test/test.mllib b/jscomp/test/test.mllib index f3532dcbc9..2a07194703 100644 --- a/jscomp/test/test.mllib +++ b/jscomp/test/test.mllib @@ -201,4 +201,5 @@ debugger_test caml_format_test js_val -epsilon_test \ No newline at end of file +epsilon_test +io_test \ No newline at end of file