1
- (* Copyright (C) 2015- 2016 Bloomberg Finance L.P.
2
- *
1
+ (* Copyright (C) 2015 - 2016 Bloomberg Finance L.P.
2
+ * Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
3
3
* This program is free software: you can redistribute it and/or modify
4
4
* it under the terms of the GNU Lesser General Public License as published by
5
5
* the Free Software Foundation, either version 3 of the License, or
21
21
* You should have received a copy of the GNU Lesser General Public License
22
22
* along with this program; if not, write to the Free Software
23
23
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24
-
24
+ [ @@@ warning " +9 " ]
25
25
type module_bind_name =
26
26
| Phint_name of string
27
27
(* explicit hint name *)
28
-
29
28
| Phint_nothing
30
29
31
30
32
- type external_module_name =
33
- { bundle : string ;
34
- module_bind_name : module_bind_name
35
- }
31
+ type external_module_name = {
32
+ bundle : string ;
33
+ module_bind_name : module_bind_name
34
+ }
36
35
37
36
type pipe = bool
38
37
@@ -52,9 +51,10 @@ type external_spec =
52
51
scopes : string list
53
52
}
54
53
| Js_module_as_var of external_module_name
55
- | Js_module_as_fn of { external_module_name : external_module_name ;
56
- splice : bool
57
- }
54
+ | Js_module_as_fn of {
55
+ external_module_name : external_module_name ;
56
+ splice : bool
57
+ }
58
58
| Js_module_as_class of external_module_name
59
59
| Js_call of {
60
60
name : string ;
@@ -75,13 +75,14 @@ type external_spec =
75
75
external_module_name : external_module_name option ;
76
76
scopes : string list ;
77
77
}
78
- | Js_set of
79
- { js_set_name : string ;
80
- js_set_scopes : string list
81
- }
82
- | Js_get of { js_get_name : string ;
83
- js_get_scopes : string list ;
84
- }
78
+ | Js_set of {
79
+ js_set_name : string ;
80
+ js_set_scopes : string list
81
+ }
82
+ | Js_get of {
83
+ js_get_name : string ;
84
+ js_get_scopes : string list ;
85
+ }
85
86
| Js_get_index of {
86
87
js_get_index_scopes : string list
87
88
}
@@ -137,6 +138,8 @@ type t =
137
138
138
139
139
140
141
+
142
+
140
143
let valid_js_char =
141
144
let a = Array. init 256 (fun i ->
142
145
let c = Char. chr i in
@@ -155,14 +158,14 @@ let valid_first_js_char =
155
158
let valid_ident (s : string ) =
156
159
let len = String. length s in
157
160
len > 0 && valid_js_char s.[0 ] && valid_first_js_char s.[0 ] &&
158
- (let module E = struct exception E end in
161
+ (let exception E in
159
162
try
160
163
for i = 1 to len - 1 do
161
164
if not (valid_js_char (String. unsafe_get s i)) then
162
- raise E. E
165
+ raise_notrace E
163
166
done ;
164
167
true
165
- with E. E -> false )
168
+ with E -> false )
166
169
167
170
let is_package_relative_path (x : string ) =
168
171
Ext_string. starts_with x " ./" ||
@@ -192,7 +195,7 @@ let valid_method_name ?loc:_ _txt =
192
195
let check_external_module_name ?loc x =
193
196
match x with
194
197
| {bundle = " " ; _ }
195
- | { module_bind_name = Phint_name "" } ->
198
+ | { module_bind_name = Phint_name "" ; bundle = _ } ->
196
199
Location. raise_errorf ?loc " empty name encountered"
197
200
| _ -> ()
198
201
@@ -203,14 +206,14 @@ let check_ffi ?loc ffi : bool =
203
206
let upgrade bool =
204
207
if not (! xrelative) then xrelative := bool in
205
208
begin match ffi with
206
- | Js_var {name; external_module_name} ->
209
+ | Js_var {name; external_module_name; scopes = _ } ->
207
210
upgrade (is_package_relative_path name);
208
211
Ext_option. iter external_module_name (fun name ->
209
212
upgrade (is_package_relative_path name.bundle));
210
213
valid_global_name ?loc name
211
- | Js_send {name }
212
- | Js_set {js_set_name = name}
213
- | Js_get { js_get_name = name}
214
+ | Js_send {name ; pipe = _; splice = _; js_send_scopes = _ }
215
+ | Js_set {js_set_name = name; js_set_scopes = _ }
216
+ | Js_get { js_get_name = name; js_get_scopes = _ }
214
217
-> valid_method_name ?loc name
215
218
| Js_get_index _ (* TODO: check scopes *)
216
219
| Js_set_index _
@@ -222,7 +225,7 @@ let check_ffi ?loc ffi : bool =
222
225
->
223
226
upgrade (is_package_relative_path external_module_name.bundle);
224
227
check_external_module_name external_module_name
225
- | Js_new {external_module_name ; name}
228
+ | Js_new {external_module_name ; name; scopes = _ }
226
229
| Js_call {external_module_name ; name ; splice = _; scopes = _ }
227
230
->
228
231
Ext_option. iter external_module_name (fun external_module_name ->
@@ -268,7 +271,7 @@ let () = Oprint.map_primitive_name :=
268
271
if is_bs_primitive s then " BS:external"
269
272
else s )
270
273
#else
271
- ( fun s -> String. escaped s) (* For debugging *)
274
+ String. escaped
272
275
#end
273
276
274
277
(* TODO: better error message when version mismatch *)
@@ -277,7 +280,36 @@ let from_string s : t =
277
280
Ext_marshal. from_string_uncheck s
278
281
else Ffi_normal
279
282
280
-
283
+ let () =
284
+ Primitive. coerce :=
285
+ (fun
286
+ ({prim_name; prim_arity; prim_native_name;
287
+ prim_alloc = _ ;
288
+ prim_native_repr_args = _ ;
289
+ prim_native_repr_res = _ } : Primitive. description )
290
+ (p2 : Primitive.description ) ->
291
+ let p2_native = p2.prim_native_name in
292
+ prim_name = p2.prim_name &&
293
+ prim_arity = p2.prim_arity &&
294
+ prim_native_name = p2_native || (
295
+ match from_string prim_native_name, from_string p2_native with
296
+ | Ffi_obj_create obj_parms , Ffi_obj_create obj_parms2 ->
297
+ Ext_list. for_all2_no_exn obj_parms obj_parms2 (fun {obj_arg_type; obj_arg_label} b ->
298
+ let b_obj_arg_label = b.obj_arg_label in
299
+ obj_arg_type = b.obj_arg_type &&
300
+ (obj_arg_label = b_obj_arg_label ||
301
+ match obj_arg_label, b_obj_arg_label with
302
+ | Obj_optional {name; for_sure_no_nested_option}, Obj_optional p
303
+ ->
304
+ name = p.name &&
305
+ ((Obj. magic for_sure_no_nested_option : int ) < = (Obj. magic p.for_sure_no_nested_option))
306
+ | _ -> false
307
+ )
308
+ )
309
+ | Ffi_bs _ , Ffi_bs _ -> false
310
+ | _ -> false
311
+ )
312
+ )
281
313
let inline_string_primitive (s : string ) (op : string option ) : string list =
282
314
let lam : Lam_constant.t =
283
315
match op with
0 commit comments