Skip to content

Commit ae2dc2d

Browse files
authored
Merge pull request #613 from bloomberg/external_object
improve code gen for bs.this when this is not used, add a raw example…
2 parents 6647d7a + 11fb926 commit ae2dc2d

File tree

6 files changed

+82
-11
lines changed

6 files changed

+82
-11
lines changed

jscomp/js_dump.ml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -374,17 +374,25 @@ let rec pp_function method_
374374
formal_parameter_list inner_cxt f (List.tl l) env )
375375
in
376376
P.space f ;
377-
ignore @@ P.brace_vgroup f 1 (fun _ ->
378-
P.string f L.var ;
379-
P.space f;
380-
let cxt = ident cxt f (List.hd l) in
381-
P.space f ;
382-
P.string f L.eq ;
383-
P.space f ;
384-
P.string f L.this;
385-
P.space f ;
386-
semi f ;
387-
P.newline f ;
377+
ignore @@ P.brace_vgroup f 1 (fun _ ->
378+
let cxt =
379+
if not (Js_fun_env.get_unused env 0) then
380+
begin
381+
P.string f L.var ;
382+
P.space f;
383+
let cxt = ident cxt f (List.hd l) in
384+
P.space f ;
385+
P.string f L.eq ;
386+
P.space f ;
387+
P.string f L.this;
388+
P.space f ;
389+
semi f ;
390+
P.newline f ;
391+
cxt ;
392+
end
393+
else
394+
cxt
395+
in
388396
statement_list false cxt f b
389397
);
390398

jscomp/test/.depend

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,8 @@ undef_regression_test.cmj : ../runtime/js_obj.cmj ../runtime/js.cmj
774774
undef_regression_test.cmx : ../runtime/js_obj.cmx ../runtime/js.cmx
775775
unitest_string.cmj :
776776
unitest_string.cmx :
777+
unsafe_obj_external.cmj : ../runtime/js.cmj
778+
unsafe_obj_external.cmx : ../runtime/js.cmx
777779
unsafe_ppx_test.cmj : mt.cmi ffi_js.cmj
778780
unsafe_ppx_test.cmx : mt.cmx ffi_js.cmx
779781
unsafe_this.cmj : ../runtime/js.cmj unsafe_this.cmi
@@ -1520,6 +1522,8 @@ undef_regression_test.cmo : ../runtime/js_obj.cmo ../runtime/js.cmo
15201522
undef_regression_test.cmj : ../runtime/js_obj.cmj ../runtime/js.cmj
15211523
unitest_string.cmo :
15221524
unitest_string.cmj :
1525+
unsafe_obj_external.cmo : ../runtime/js.cmo
1526+
unsafe_obj_external.cmj : ../runtime/js.cmj
15231527
unsafe_ppx_test.cmo : mt.cmi ffi_js.cmo
15241528
unsafe_ppx_test.cmj : mt.cmj ffi_js.cmj
15251529
unsafe_this.cmo : ../runtime/js.cmo unsafe_this.cmi

jscomp/test/test_bs_this.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ function f(x) {
4646
});
4747
}
4848

49+
function u() {
50+
return x;
51+
}
52+
4953
exports.uux_this = uux_this;
5054
exports.even = even;
5155
exports.bark = bark;
5256
exports.js_obj = js_obj;
5357
exports.f = f;
58+
exports.u = u;
5459
/* uux_this Not a pure module */

jscomp/test/test_bs_this.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ let f (x : x ) =
3939
end
4040
end
4141

42+
let u = fun [@bs.this] (_ : int) (x : int) -> x

jscomp/test/unsafe_obj_external.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.0 , PLEASE EDIT WITH CARE
2+
'use strict';
3+
4+
5+
var v = {
6+
x: function () {
7+
return 3;
8+
},
9+
say: function (x) {
10+
var self = this ;
11+
return self.x() + x | 0;
12+
}
13+
};
14+
15+
var u = v.x() + v.say(3) | 0;
16+
17+
exports.v = v;
18+
exports.u = u;
19+
/* v Not a pure module */

jscomp/test/unsafe_obj_external.ml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
3+
4+
external config :
5+
x : ('self_type -> 'x [@bs.this]) ->
6+
say :('self_type -> 'x -> 'say [@bs.this]) ->
7+
unit ->
8+
(<
9+
x : unit -> 'x [@bs.meth];
10+
say : 'x -> 'say [@bs.meth]
11+
> Js.t as 'self_type) = "" [@@bs.obj]
12+
13+
14+
let v =
15+
let x = 3 in
16+
config
17+
~x:(fun [@bs.this] _ -> x )
18+
~say:(fun [@bs.this] self x -> self##x () + x)
19+
()
20+
21+
(**
22+
let x = 3 in
23+
object (self : 'self_type)
24+
method x = x
25+
method say x = self##x + x
26+
end [@bs]
27+
*)
28+
let u =
29+
v##x () + v##say 3
30+
31+
32+
(* local variables: *)
33+
(* compile-command: "bsc -I ../runtime -drawlambda unsafe_obj_external.ml" *)
34+
(* end: *)

0 commit comments

Comments
 (0)