Skip to content

Commit

Permalink
improve code gen for bs.this when this is not used, add a raw example…
Browse files Browse the repository at this point in the history
… for bs object
  • Loading branch information
Hongbo Zhang committed Aug 8, 2016
1 parent 6647d7a commit 11fb926
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 11 deletions.
30 changes: 19 additions & 11 deletions jscomp/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
4 changes: 4 additions & 0 deletions jscomp/test/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions jscomp/test/test_bs_this.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
1 change: 1 addition & 0 deletions jscomp/test/test_bs_this.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ let f (x : x ) =
end
end

let u = fun [@bs.this] (_ : int) (x : int) -> x
19 changes: 19 additions & 0 deletions jscomp/test/unsafe_obj_external.js
Original file line number Diff line number Diff line change
@@ -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 */
34 changes: 34 additions & 0 deletions jscomp/test/unsafe_obj_external.ml
Original file line number Diff line number Diff line change
@@ -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: *)

0 comments on commit 11fb926

Please sign in to comment.