Skip to content

Commit 651977d

Browse files
authored
Merge pull request #5107 from rescript-lang/set_instance
2 parents 3134392 + e53e7e0 commit 651977d

15 files changed

+157
-58
lines changed

Diff for: jscomp/common/bs_version.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* You should have received a copy of the GNU Lesser General Public License
2323
* along with this program; if not, write to the Free Software
2424
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
25-
let version = "9.1.2"
25+
let version = "10.0.0"
2626
let header =
2727
"// Generated by ReScript, PLEASE EDIT WITH CARE"
2828
let package_name = ref "rescript"

Diff for: jscomp/common/js_config.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,6 @@ let as_ppx = ref false
9292

9393
let customize_runtime = ref None
9494

95-
let as_pp = ref false
95+
let as_pp = ref false
96+
97+
let self_stack : string Stack.t = Stack.create ()

Diff for: jscomp/common/js_config.mli

+3-1
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,6 @@ val as_ppx : bool ref
9999

100100

101101
val customize_runtime : string option ref
102-
val as_pp: bool ref
102+
val as_pp: bool ref
103+
104+
val self_stack : string Stack.t

Diff for: jscomp/frontend/ast_pat.ml

+8-5
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ let rec labels_of_fun (e : Parsetree.expression) =
6060

6161
let rec is_single_variable_pattern_conservative (p : t ) =
6262
match p.ppat_desc with
63-
| Parsetree.Ppat_any
64-
| Parsetree.Ppat_var _ -> true
65-
| Parsetree.Ppat_alias (p,_)
63+
| Parsetree.Ppat_any -> Some ""
64+
| Parsetree.Ppat_var s -> Some s.txt
65+
| Parsetree.Ppat_alias (p, s) ->
66+
(* Check more complex patterns is needed or not*)
67+
if is_single_variable_pattern_conservative p <> None then
68+
Some s.txt
69+
else None
6670
| Parsetree.Ppat_constraint (p, _) ->
6771
is_single_variable_pattern_conservative p
68-
69-
| _ -> false
72+
| _ -> None

Diff for: jscomp/frontend/ast_pat.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ val labels_of_fun :
3535
Parsetree.expression ->
3636
Asttypes.arg_label list
3737

38-
val is_single_variable_pattern_conservative : t -> bool
38+
val is_single_variable_pattern_conservative :
39+
t -> string option

Diff for: jscomp/frontend/ast_uncurry_gen.ml

+8-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ open Ast_helper
2626

2727
(* Handling `fun [@this]` used in `object [@bs] end` *)
2828
let to_method_callback loc (self : Bs_ast_mapper.mapper)
29-
label pat body : Parsetree.expression_desc
29+
label (self_pat : Parsetree.pattern) body : Parsetree.expression_desc
3030
=
31+
let self_pat = self.pat self self_pat in
32+
(match (Ast_pat.is_single_variable_pattern_conservative self_pat) with
33+
| None ->
34+
Bs_syntaxerr.err self_pat.ppat_loc Bs_this_simple_pattern
35+
| Some self -> Stack.push self Js_config.self_stack);
3136
Bs_syntaxerr.optional_err loc label;
3237
let rec aux acc (body : Parsetree.expression) =
3338
match Ast_attributes.process_attributes_rev body.pexp_attributes with
@@ -41,15 +46,13 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper)
4146
end
4247
| _, _ -> self.expr self body, acc
4348
in
44-
let first_arg = self.pat self pat in
45-
(if not (Ast_pat.is_single_variable_pattern_conservative first_arg) then
46-
Bs_syntaxerr.err first_arg.ppat_loc Bs_this_simple_pattern);
47-
let result, rev_extra_args = aux [label,first_arg] body in
49+
let result, rev_extra_args = aux [label,self_pat] body in
4850
let body =
4951
Ext_list.fold_left rev_extra_args result (fun e (label,p) -> Ast_helper.Exp.fun_ ~loc label None p e )
5052
in
5153
let arity = List.length rev_extra_args in
5254
let arity_s = string_of_int arity in
55+
Stack.pop Js_config.self_stack |> ignore;
5356
Parsetree.Pexp_apply
5457
(Exp.ident ~loc {loc ; txt = Ldot(Ast_literal.Lid.js_oo,"unsafe_to_method")},
5558
[Nolabel,

Diff for: jscomp/frontend/bs_builtin_ppx.ml

+17
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ let expr_mapper (self : mapper) (e : Parsetree.expression) =
8181
(** Its output should not be rewritten anymore *)
8282
| Pexp_extension extension ->
8383
Ast_exp_extension.handle_extension e self extension
84+
| Pexp_setinstvar ({txt;loc},expr) ->
85+
if Stack.is_empty Js_config.self_stack then
86+
Location.raise_errorf ~loc:e.pexp_loc "This assignment can only happen in object context";
87+
let name = Stack.top Js_config.self_stack in
88+
if name = "" then
89+
Location.raise_errorf ~loc:e.pexp_loc
90+
"The current object does not assign a name";
91+
let open Ast_helper in
92+
self.expr self
93+
(Exp.apply ~loc:e.pexp_loc
94+
(Exp.ident ~loc {loc; txt = Lident "#="})
95+
[Nolabel,
96+
(Exp.send ~loc
97+
(Exp.ident ~loc {loc; txt = Lident name})
98+
{loc ; txt});
99+
Nolabel, expr
100+
])
84101
| Pexp_constant (
85102
Pconst_string
86103
(s, (Some delim)))

Diff for: jscomp/main/rescript_compiler_main.ml

-3
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,6 @@ let buckle_script_flags : (string * Bsc_args.spec * string) array =
448448
"-nolabels", set Clflags.classic,
449449
"*internal* Ignore non-optional labels in types";
450450

451-
"-principal", set Clflags.principal,
452-
"*internal* Check principality of type inference";
453-
454451
"-short-paths", clear Clflags.real_paths,
455452
"*internal* Shorten paths in types";
456453

Diff for: jscomp/test/ppx_this_obj_field.js

+10
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ eq("File \"ppx_this_obj_field.ml\", line 103, characters 5-12", [
213213
]
214214
]);
215215

216+
var f = {
217+
x: 3,
218+
hei: (function () {
219+
var y = this ;
220+
y.x = y.x + 3 | 0;
221+
222+
})
223+
};
224+
216225
Mt.from_pair_suites("Ppx_this_obj_field", suites.contents);
217226

218227
exports.suites = suites;
@@ -227,4 +236,5 @@ exports.eventObj = eventObj;
227236
exports.test__ = test__;
228237
exports.zz = zz;
229238
exports.test_type2 = test_type2;
239+
exports.f = f;
230240
/* v5 Not a pure module */

Diff for: jscomp/test/ppx_this_obj_field.ml

+19-12
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ let v =
2727
val x = 3
2828
val mutable y = 0
2929
method private reset () =
30-
self##y#= 0
30+
y <- 0
3131
method incr () =
32-
self##y #= (self##y + 1)
32+
y <- self#y + 1
3333
(* TODO: the error message is confusing
3434
{[
3535
self##y #= self##y + 1
@@ -42,10 +42,10 @@ let v =
4242
it will not work if `self##y` is indeed a reference
4343
we need document this behavior
4444
*)
45-
method getY () = self##y
46-
method say () = self##x + self##y
45+
method getY () = self#y
46+
method say () = self#x + self#y
4747
end
48-
48+
4949

5050
let u =
5151
object (self)
@@ -59,16 +59,16 @@ let test_type = [u ; v]
5959
let z : < getX : (unit -> int [@bs.meth]); setX : (int -> unit [@bs.meth]) > =
6060
object (self)
6161
val x = ref 3
62-
method setX x = self##x := x
62+
method setX x = self#x := x
6363
method getX () = ! (self##x)
6464
end
6565

6666
let eventObj : <
6767
empty : (unit -> unit [@bs.meth]);
68-
needRebuild : (unit -> bool [@bs.meth]);
69-
push : string * string -> unit [@bs.meth]
70-
>
71-
68+
needRebuild : (unit -> bool [@bs.meth]);
69+
push : string * string -> unit [@bs.meth]
70+
>
71+
7272
=
7373
object (self)
7474
val events : (string * string) array = [||]
@@ -82,8 +82,8 @@ let test__ x = eventObj##push x
8282
let zz : < getX : (unit -> int [@bs.meth]); setX : (int -> unit [@bs.meth]) > =
8383
object (self)
8484
val mutable x = 3
85-
method setX x = self##x #= x
86-
method getX () = (self##x)
85+
method setX x = x <- x
86+
method getX () = self#x
8787
end
8888

8989
let test_type2 = [z;zz]
@@ -102,6 +102,13 @@ let () =
102102
let bb = z##getX () in
103103
eq __LOC__ ((3, 32), (aa,bb))
104104

105+
106+
let f = object (_ as y)
107+
val mutable x = 3
108+
method hei () =
109+
x <- y#x + 3
110+
111+
end
105112
let () =
106113
Mt.from_pair_suites __MODULE__ !suites
107114

Diff for: lib/4.06.1/bsb.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ end = struct
5555
* You should have received a copy of the GNU Lesser General Public License
5656
* along with this program; if not, write to the Free Software
5757
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
58-
let version = "9.1.2"
58+
let version = "10.0.0"
5959
let header =
6060
"// Generated by ReScript, PLEASE EDIT WITH CARE"
6161
let package_name = ref "rescript"

Diff for: lib/4.06.1/rescript.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5940,7 +5940,7 @@ end = struct
59405940
* You should have received a copy of the GNU Lesser General Public License
59415941
* along with this program; if not, write to the Free Software
59425942
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
5943-
let version = "9.1.2"
5943+
let version = "10.0.0"
59445944
let header =
59455945
"// Generated by ReScript, PLEASE EDIT WITH CARE"
59465946
let package_name = ref "rescript"

Diff for: lib/4.06.1/unstable/bspack.ml

+4
Original file line numberDiff line numberDiff line change
@@ -11969,6 +11969,8 @@ val as_ppx : bool ref
1196911969

1197011970
val customize_runtime : string option ref
1197111971
val as_pp: bool ref
11972+
11973+
val self_stack : string Stack.t
1197211974
end = struct
1197311975
#1 "js_config.ml"
1197411976
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -12064,6 +12066,8 @@ let as_ppx = ref false
1206412066
let customize_runtime = ref None
1206512067

1206612068
let as_pp = ref false
12069+
12070+
let self_stack : string Stack.t = Stack.create ()
1206712071
end
1206812072
module Map_gen : sig
1206912073
#1 "map_gen.mli"

Diff for: lib/4.06.1/unstable/js_compiler.ml

+40-12
Original file line numberDiff line numberDiff line change
@@ -17779,6 +17779,8 @@ val as_ppx : bool ref
1777917779

1778017780
val customize_runtime : string option ref
1778117781
val as_pp: bool ref
17782+
17783+
val self_stack : string Stack.t
1778217784
end = struct
1778317785
#1 "js_config.ml"
1778417786
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -17874,6 +17876,8 @@ let as_ppx = ref false
1787417876
let customize_runtime = ref None
1787517877

1787617878
let as_pp = ref false
17879+
17880+
let self_stack : string Stack.t = Stack.create ()
1787717881
end
1787817882
module Bs_cmi_load
1787917883
= struct
@@ -18004,7 +18008,7 @@ end = struct
1800418008
* You should have received a copy of the GNU Lesser General Public License
1800518009
* along with this program; if not, write to the Free Software
1800618010
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
18007-
let version = "9.1.2"
18011+
let version = "10.0.0"
1800818012
let header =
1800918013
"// Generated by ReScript, PLEASE EDIT WITH CARE"
1801018014
let package_name = ref "rescript"
@@ -399821,7 +399825,8 @@ val labels_of_fun :
399821399825
Parsetree.expression ->
399822399826
Asttypes.arg_label list
399823399827

399824-
val is_single_variable_pattern_conservative : t -> bool
399828+
val is_single_variable_pattern_conservative :
399829+
t -> string option
399825399830

399826399831
end = struct
399827399832
#1 "ast_pat.ml"
@@ -399887,13 +399892,16 @@ let rec labels_of_fun (e : Parsetree.expression) =
399887399892

399888399893
let rec is_single_variable_pattern_conservative (p : t ) =
399889399894
match p.ppat_desc with
399890-
| Parsetree.Ppat_any
399891-
| Parsetree.Ppat_var _ -> true
399892-
| Parsetree.Ppat_alias (p,_)
399895+
| Parsetree.Ppat_any -> Some ""
399896+
| Parsetree.Ppat_var s -> Some s.txt
399897+
| Parsetree.Ppat_alias (p, s) ->
399898+
(* Check more complex patterns is needed or not*)
399899+
if is_single_variable_pattern_conservative p <> None then
399900+
Some s.txt
399901+
else None
399893399902
| Parsetree.Ppat_constraint (p, _) ->
399894399903
is_single_variable_pattern_conservative p
399895-
399896-
| _ -> false
399904+
| _ -> None
399897399905

399898399906
end
399899399907
module Ast_typ_uncurry : sig
@@ -403597,8 +403605,13 @@ open Ast_helper
403597403605

403598403606
(* Handling `fun [@this]` used in `object [@bs] end` *)
403599403607
let to_method_callback loc (self : Bs_ast_mapper.mapper)
403600-
label pat body : Parsetree.expression_desc
403608+
label (self_pat : Parsetree.pattern) body : Parsetree.expression_desc
403601403609
=
403610+
let self_pat = self.pat self self_pat in
403611+
(match (Ast_pat.is_single_variable_pattern_conservative self_pat) with
403612+
| None ->
403613+
Bs_syntaxerr.err self_pat.ppat_loc Bs_this_simple_pattern
403614+
| Some self -> Stack.push self Js_config.self_stack);
403602403615
Bs_syntaxerr.optional_err loc label;
403603403616
let rec aux acc (body : Parsetree.expression) =
403604403617
match Ast_attributes.process_attributes_rev body.pexp_attributes with
@@ -403612,15 +403625,13 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper)
403612403625
end
403613403626
| _, _ -> self.expr self body, acc
403614403627
in
403615-
let first_arg = self.pat self pat in
403616-
(if not (Ast_pat.is_single_variable_pattern_conservative first_arg) then
403617-
Bs_syntaxerr.err first_arg.ppat_loc Bs_this_simple_pattern);
403618-
let result, rev_extra_args = aux [label,first_arg] body in
403628+
let result, rev_extra_args = aux [label,self_pat] body in
403619403629
let body =
403620403630
Ext_list.fold_left rev_extra_args result (fun e (label,p) -> Ast_helper.Exp.fun_ ~loc label None p e )
403621403631
in
403622403632
let arity = List.length rev_extra_args in
403623403633
let arity_s = string_of_int arity in
403634+
Stack.pop Js_config.self_stack |> ignore;
403624403635
Parsetree.Pexp_apply
403625403636
(Exp.ident ~loc {loc ; txt = Ldot(Ast_literal.Lid.js_oo,"unsafe_to_method")},
403626403637
[Nolabel,
@@ -405591,6 +405602,23 @@ let expr_mapper (self : mapper) (e : Parsetree.expression) =
405591405602
(** Its output should not be rewritten anymore *)
405592405603
| Pexp_extension extension ->
405593405604
Ast_exp_extension.handle_extension e self extension
405605+
| Pexp_setinstvar ({txt;loc},expr) ->
405606+
if Stack.is_empty Js_config.self_stack then
405607+
Location.raise_errorf ~loc:e.pexp_loc "This assignment can only happen in object context";
405608+
let name = Stack.top Js_config.self_stack in
405609+
if name = "" then
405610+
Location.raise_errorf ~loc:e.pexp_loc
405611+
"The current object does not assign a name";
405612+
let open Ast_helper in
405613+
self.expr self
405614+
(Exp.apply ~loc:e.pexp_loc
405615+
(Exp.ident ~loc {loc; txt = Lident "#="})
405616+
[Nolabel,
405617+
(Exp.send ~loc
405618+
(Exp.ident ~loc {loc; txt = Lident name})
405619+
{loc ; txt});
405620+
Nolabel, expr
405621+
])
405594405622
| Pexp_constant (
405595405623
Pconst_string
405596405624
(s, (Some delim)))

0 commit comments

Comments
 (0)