From 11fb926864a7fd72313c7b2c1e67f2acc493c61c Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Mon, 8 Aug 2016 15:10:17 -0400 Subject: [PATCH] improve code gen for bs.this when this is not used, add a raw example for bs object --- jscomp/js_dump.ml | 30 ++++++++++++++++---------- jscomp/test/.depend | 4 ++++ jscomp/test/test_bs_this.js | 5 +++++ jscomp/test/test_bs_this.ml | 1 + jscomp/test/unsafe_obj_external.js | 19 +++++++++++++++++ jscomp/test/unsafe_obj_external.ml | 34 ++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 jscomp/test/unsafe_obj_external.js create mode 100644 jscomp/test/unsafe_obj_external.ml diff --git a/jscomp/js_dump.ml b/jscomp/js_dump.ml index 8e33410099..6c11a7208a 100644 --- a/jscomp/js_dump.ml +++ b/jscomp/js_dump.ml @@ -374,17 +374,25 @@ let rec pp_function method_ formal_parameter_list inner_cxt f (List.tl l) env ) in P.space f ; - ignore @@ P.brace_vgroup f 1 (fun _ -> - P.string f L.var ; - P.space f; - let cxt = ident cxt f (List.hd l) in - P.space f ; - P.string f L.eq ; - P.space f ; - P.string f L.this; - P.space f ; - semi f ; - P.newline f ; + ignore @@ P.brace_vgroup f 1 (fun _ -> + let cxt = + if not (Js_fun_env.get_unused env 0) then + begin + P.string f L.var ; + P.space f; + let cxt = ident cxt f (List.hd l) in + P.space f ; + P.string f L.eq ; + P.space f ; + P.string f L.this; + P.space f ; + semi f ; + P.newline f ; + cxt ; + end + else + cxt + in statement_list false cxt f b ); diff --git a/jscomp/test/.depend b/jscomp/test/.depend index 9381656e1b..ddb1511ba9 100644 --- a/jscomp/test/.depend +++ b/jscomp/test/.depend @@ -774,6 +774,8 @@ undef_regression_test.cmj : ../runtime/js_obj.cmj ../runtime/js.cmj undef_regression_test.cmx : ../runtime/js_obj.cmx ../runtime/js.cmx unitest_string.cmj : unitest_string.cmx : +unsafe_obj_external.cmj : ../runtime/js.cmj +unsafe_obj_external.cmx : ../runtime/js.cmx unsafe_ppx_test.cmj : mt.cmi ffi_js.cmj unsafe_ppx_test.cmx : mt.cmx ffi_js.cmx unsafe_this.cmj : ../runtime/js.cmj unsafe_this.cmi @@ -1520,6 +1522,8 @@ undef_regression_test.cmo : ../runtime/js_obj.cmo ../runtime/js.cmo undef_regression_test.cmj : ../runtime/js_obj.cmj ../runtime/js.cmj unitest_string.cmo : unitest_string.cmj : +unsafe_obj_external.cmo : ../runtime/js.cmo +unsafe_obj_external.cmj : ../runtime/js.cmj unsafe_ppx_test.cmo : mt.cmi ffi_js.cmo unsafe_ppx_test.cmj : mt.cmj ffi_js.cmj unsafe_this.cmo : ../runtime/js.cmo unsafe_this.cmi diff --git a/jscomp/test/test_bs_this.js b/jscomp/test/test_bs_this.js index a9e88495cd..f5f850f5d0 100644 --- a/jscomp/test/test_bs_this.js +++ b/jscomp/test/test_bs_this.js @@ -46,9 +46,14 @@ function f(x) { }); } +function u() { + return x; +} + exports.uux_this = uux_this; exports.even = even; exports.bark = bark; exports.js_obj = js_obj; exports.f = f; +exports.u = u; /* uux_this Not a pure module */ diff --git a/jscomp/test/test_bs_this.ml b/jscomp/test/test_bs_this.ml index 9ee218e05d..fae4f71a2d 100644 --- a/jscomp/test/test_bs_this.ml +++ b/jscomp/test/test_bs_this.ml @@ -39,3 +39,4 @@ let f (x : x ) = end end +let u = fun [@bs.this] (_ : int) (x : int) -> x diff --git a/jscomp/test/unsafe_obj_external.js b/jscomp/test/unsafe_obj_external.js new file mode 100644 index 0000000000..c48bf05109 --- /dev/null +++ b/jscomp/test/unsafe_obj_external.js @@ -0,0 +1,19 @@ +// GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.0 , PLEASE EDIT WITH CARE +'use strict'; + + +var v = { + x: function () { + return 3; + }, + say: function (x) { + var self = this ; + return self.x() + x | 0; + } +}; + +var u = v.x() + v.say(3) | 0; + +exports.v = v; +exports.u = u; +/* v Not a pure module */ diff --git a/jscomp/test/unsafe_obj_external.ml b/jscomp/test/unsafe_obj_external.ml new file mode 100644 index 0000000000..010823b358 --- /dev/null +++ b/jscomp/test/unsafe_obj_external.ml @@ -0,0 +1,34 @@ + + + +external config : + x : ('self_type -> 'x [@bs.this]) -> + say :('self_type -> 'x -> 'say [@bs.this]) -> + unit -> + (< + x : unit -> 'x [@bs.meth]; + say : 'x -> 'say [@bs.meth] + > Js.t as 'self_type) = "" [@@bs.obj] + + +let v = + let x = 3 in + config + ~x:(fun [@bs.this] _ -> x ) + ~say:(fun [@bs.this] self x -> self##x () + x) + () + +(** +let x = 3 in +object (self : 'self_type) + method x = x + method say x = self##x + x +end [@bs] +*) +let u = + v##x () + v##say 3 + + +(* local variables: *) +(* compile-command: "bsc -I ../runtime -drawlambda unsafe_obj_external.ml" *) +(* end: *)