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
33 * This program is free software: you can redistribute it and/or modify
44 * it under the terms of the GNU Lesser General Public License as published by
55 * the Free Software Foundation, either version 3 of the License, or
2121 * You should have received a copy of the GNU Lesser General Public License
2222 * along with this program; if not, write to the Free Software
2323 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24-
24+ [ @@@ warning " +9 " ]
2525type module_bind_name =
2626 | Phint_name of string
2727 (* explicit hint name *)
28-
2928 | Phint_nothing
3029
3130
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+ }
3635
3736type pipe = bool
3837
@@ -52,9 +51,10 @@ type external_spec =
5251 scopes : string list
5352 }
5453 | 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+ }
5858 | Js_module_as_class of external_module_name
5959 | Js_call of {
6060 name : string ;
@@ -75,13 +75,14 @@ type external_spec =
7575 external_module_name : external_module_name option ;
7676 scopes : string list ;
7777 }
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+ }
8586 | Js_get_index of {
8687 js_get_index_scopes : string list
8788 }
@@ -137,6 +138,8 @@ type t =
137138
138139
139140
141+
142+
140143let valid_js_char =
141144 let a = Array. init 256 (fun i ->
142145 let c = Char. chr i in
@@ -155,14 +158,14 @@ let valid_first_js_char =
155158let valid_ident (s : string ) =
156159 let len = String. length s in
157160 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
159162 try
160163 for i = 1 to len - 1 do
161164 if not (valid_js_char (String. unsafe_get s i)) then
162- raise E. E
165+ raise_notrace E
163166 done ;
164167 true
165- with E. E -> false )
168+ with E -> false )
166169
167170let is_package_relative_path (x : string ) =
168171 Ext_string. starts_with x " ./" ||
@@ -192,7 +195,7 @@ let valid_method_name ?loc:_ _txt =
192195let check_external_module_name ?loc x =
193196 match x with
194197 | {bundle = " " ; _ }
195- | { module_bind_name = Phint_name "" } ->
198+ | { module_bind_name = Phint_name "" ; bundle = _ } ->
196199 Location. raise_errorf ?loc " empty name encountered"
197200 | _ -> ()
198201
@@ -203,14 +206,14 @@ let check_ffi ?loc ffi : bool =
203206 let upgrade bool =
204207 if not (! xrelative) then xrelative := bool in
205208 begin match ffi with
206- | Js_var {name; external_module_name} ->
209+ | Js_var {name; external_module_name; scopes = _ } ->
207210 upgrade (is_package_relative_path name);
208211 Ext_option. iter external_module_name (fun name ->
209212 upgrade (is_package_relative_path name.bundle));
210213 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 = _ }
214217 -> valid_method_name ?loc name
215218 | Js_get_index _ (* TODO: check scopes *)
216219 | Js_set_index _
@@ -222,7 +225,7 @@ let check_ffi ?loc ffi : bool =
222225 ->
223226 upgrade (is_package_relative_path external_module_name.bundle);
224227 check_external_module_name external_module_name
225- | Js_new {external_module_name ; name}
228+ | Js_new {external_module_name ; name; scopes = _ }
226229 | Js_call {external_module_name ; name ; splice = _; scopes = _ }
227230 ->
228231 Ext_option. iter external_module_name (fun external_module_name ->
@@ -268,7 +271,7 @@ let () = Oprint.map_primitive_name :=
268271 if is_bs_primitive s then " BS:external"
269272 else s )
270273#else
271- ( fun s -> String. escaped s) (* For debugging *)
274+ String. escaped
272275#end
273276
274277(* TODO: better error message when version mismatch *)
@@ -277,7 +280,36 @@ let from_string s : t =
277280 Ext_marshal. from_string_uncheck s
278281 else Ffi_normal
279282
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+ )
281313let inline_string_primitive (s : string ) (op : string option ) : string list =
282314 let lam : Lam_constant.t =
283315 match op with
0 commit comments