Skip to content

Commit 98aa244

Browse files
authored
Merge pull request #4583 from BuckleScript/ffi_cleanup_3
WIP: fix #4582, #4463
2 parents 2f3a44e + a1f14d3 commit 98aa244

26 files changed

+3165
-6305
lines changed

jscomp/core/js_dump.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ and expression_desc cxt ~(level:int) f x : cxt =
650650
| Exp exp_info ->
651651
let raw_paren =
652652
not (match exp_info with
653-
| Js_literal _ | Js_raw_json -> true
653+
| Js_literal _ -> true
654654
| Js_function _ | Js_exp_unknown -> false || raw_snippet_exp_simple_enough s) in
655655
if raw_paren then P.string f L.lparen;
656656
P.string f s ;

jscomp/core/js_raw_info.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ type exp =
4040
| RegExp of RegExp.t
4141
]}
4242
*)
43-
| Js_raw_json
4443
| Js_exp_unknown
4544

4645

jscomp/core/lam_compile_const.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,5 @@ let translate_arg_cst (cst : External_arg_spec.cst) =
130130
E.int (Int32.of_int i)
131131
| Arg_string_lit i ->
132132
E.str i
133-
| Arg_js_null -> E.nil
134-
| Arg_js_json s
135-
-> E.raw_js_code (Exp Js_raw_json) s
136-
137-
| Arg_js_true -> E.bool true
138-
| Arg_js_false -> E.bool false
133+
| Arg_js_literal s
134+
-> E.raw_js_code (Exp (Js_literal {comment = None})) s

jscomp/main/builtin_cmi_datasets.ml

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

jscomp/ounit_tests/ounit_cmd_tests.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ external ff :
148148
type t
149149
external mk : int -> (_ [@bs.as {json| { x : 3 } |json}]) -> t = "mk" [@@bs.val]
150150
|} in
151-
OUnit.assert_bool __LOC__ (Ext_string.contain_substring should_err.stderr "Invalid json literal")
151+
OUnit.assert_bool __LOC__ (Ext_string.is_empty should_err.stderr)
152152
end
153153
;
154154
__LOC__ >:: begin fun _ ->

jscomp/syntax/ast_attributes.ml

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ let iter_process_bs_int_as (attrs : t) =
265265
type as_const_payload =
266266
| Int of int
267267
| Str of string
268-
| Json_str of string
269-
268+
| Js_literal_str of string
270269
let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
271270
let st = ref None in
272271
Ext_list.iter attrs
@@ -279,16 +278,31 @@ let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
279278
(Bs_ast_invariant.mark_used_bs_attribute attr ;
280279
match Ast_payload.is_single_int payload with
281280
| None ->
282-
begin match Ast_payload.is_single_string payload with
283-
| Some (s,None) ->
284-
st := Some (Str (s))
285-
| Some (s, Some "json") ->
286-
st := Some (Json_str s )
287-
| None | Some (_, Some _) ->
281+
begin match payload with
282+
| PStr [ {
283+
pstr_desc =
284+
Pstr_eval (
285+
{pexp_desc =
286+
Pexp_constant
287+
(Pconst_string(s, (None | Some "json" as dec)))
288+
; pexp_loc ;
289+
_},_);
290+
_}] ->
291+
if dec = None then
292+
st := Some (Str (s))
293+
else
294+
begin
295+
(match Classify_function.classify ~check:(pexp_loc, Bs_flow_ast_utils.flow_deli_offset dec)
296+
s with
297+
| Js_literal _ -> ()
298+
| _ ->
299+
Location.raise_errorf ~loc:pexp_loc "an object literal expected");
300+
st := Some (Js_literal_str s )
301+
end
302+
| _ ->
288303
Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal
289-
290304
end
291-
| Some v->
305+
| Some v->
292306
st := (Some (Int v))
293307
)
294308
else

jscomp/syntax/ast_attributes.mli

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ val iter_process_bs_int_as :
8282
type as_const_payload =
8383
| Int of int
8484
| Str of string
85-
| Json_str of string
86-
85+
| Js_literal_str of string
8786

8887
val iter_process_bs_string_or_int_as :
8988
t ->

jscomp/syntax/ast_external_process.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,9 @@ let refine_arg_type ~(nolabel:bool) (ptyp : Ast_core_type.t)
112112
(* This type is used in bs.obj only to construct obj type*)
113113
Arg_cst(External_arg_spec.cst_int i)
114114
| Some (Str i)->
115-
Arg_cst (External_arg_spec.cst_string i)
116-
| Some (Json_str s) ->
117-
(* FIXME: This seems to be wrong in bs.obj, we should disable such payload in bs.obj *)
118-
Arg_cst (External_arg_spec.cst_json ptyp.ptyp_loc s)
115+
Arg_cst (External_arg_spec.cst_string i)
116+
| Some (Js_literal_str s) ->
117+
Arg_cst (External_arg_spec.cst_obj_literal s)
119118
else (* ([`a|`b] [@bs.string]) *)
120119
spec_of_ptyp nolabel ptyp
121120
)
@@ -138,7 +137,7 @@ let refine_obj_arg_type ~(nolabel:bool) (ptyp : Ast_core_type.t)
138137
Ast_literal.type_int ~loc:ptyp.ptyp_loc (), Arg_cst(External_arg_spec.cst_int i)
139138
| Some (Str i)->
140139
Ast_literal.type_string ~loc:ptyp.ptyp_loc (), Arg_cst (External_arg_spec.cst_string i)
141-
| Some (Json_str _) ->
140+
| Some (Js_literal_str _ ) ->
142141
Location.raise_errorf ~loc:ptyp.ptyp_loc "json payload is not supported in bs.obj since its type can not be inferred"
143142
else (* ([`a|`b] [@bs.string]) *)
144143
ptyp, spec_of_ptyp nolabel ptyp

jscomp/syntax/ast_payload.ml

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,38 +66,9 @@ let is_single_int (x : t ) : int option =
6666
| _ -> None
6767

6868

69-
let offset_pos
70-
({pos_lnum; pos_bol; pos_cnum} as loc : Lexing.position)
71-
({line; column} : Loc.position) first_line_offset : Lexing.position =
72-
if line = 1 then
73-
{loc with pos_cnum = pos_cnum + column + first_line_offset }
74-
else {
75-
loc with
76-
pos_lnum = pos_lnum + line - 1;
77-
pos_cnum = pos_bol + column
78-
}
7969

80-
let flow_deli_off_set deli =
81-
(match deli with
82-
| None -> 1 (* length of '"'*)
83-
| Some deli ->
84-
String.length deli + 2 (* length of "{|"*)
85-
)
86-
(* Here the loc is the payload loc *)
87-
let check_flow_errors ~(loc : Location.t)
88-
~offset(errors : (Loc.t * Parse_error.t) list) =
89-
match errors with
90-
| [] -> ()
91-
| ({start ;
92-
_end },first_error) :: _ ->
93-
94-
Location.raise_errorf ~loc:{loc with
95-
loc_start = offset_pos loc.loc_start start
96-
offset ;
97-
loc_end = offset_pos loc.loc_start _end
98-
offset } "%s"
99-
(Parse_error.PP.error first_error)
100-
;;
70+
71+
10172
let raw_as_string_exp_exn
10273
~(kind: Js_raw_info.raw_kind)
10374
(x : t ) : _ option =
@@ -111,7 +82,7 @@ let raw_as_string_exp_exn
11182
;
11283
pexp_loc = loc} as e ,_);
11384
_}] ->
114-
check_flow_errors ~loc ~offset:(flow_deli_off_set deli) (match kind with
85+
Bs_flow_ast_utils.check_flow_errors ~loc ~offset:(Bs_flow_ast_utils.flow_deli_offset deli) (match kind with
11586
| Raw_re
11687
| Raw_exp ->
11788
let (_loc,e),errors = (Parser_flow.parse_expression (Parser_env.init_env None str) false) in

jscomp/syntax/bs_flow_ast_utils.ml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
(* Copyright (C) 2020 - Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
let offset_pos
27+
({pos_lnum; pos_bol; pos_cnum} as loc : Lexing.position)
28+
({line; column} : Loc.position)
29+
first_line_offset : Lexing.position =
30+
if line = 1 then
31+
{loc with pos_cnum = pos_cnum + column + first_line_offset }
32+
else {
33+
loc with
34+
pos_lnum = pos_lnum + line - 1;
35+
pos_cnum = pos_bol + column
36+
}
37+
38+
39+
let flow_deli_offset deli =
40+
(match deli with
41+
| None -> 1 (* length of '"'*)
42+
| Some deli ->
43+
String.length deli + 2 (* length of "{|"*)
44+
)
45+
46+
;;
47+
48+
49+
(* Here the loc is the payload loc *)
50+
let check_flow_errors ~(loc : Location.t)
51+
~offset
52+
(errors : (Loc.t * Parse_error.t) list) =
53+
match errors with
54+
| [] -> ()
55+
| ({start ;
56+
_end },first_error) :: _ ->
57+
let loc_start = loc.loc_start in
58+
Location.raise_errorf
59+
~loc:{loc with
60+
loc_start = offset_pos loc_start start
61+
offset ;
62+
loc_end = offset_pos loc_start _end
63+
offset } "%s"
64+
(Parse_error.PP.error first_error)

0 commit comments

Comments
 (0)