Skip to content

Commit 47db883

Browse files
• Add platform-specific module support
1 parent 52e838b commit 47db883

61 files changed

Lines changed: 7194 additions & 182 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#### :rocket: New Feature
3030

31+
- Add opt-in platform modules such as `Button.android.res` and `Button.ios.res`, with platform-specific JavaScript outputs sharing one `Button.resi` interface. https://github.com/rescript-lang/rescript/issues/1436
3132
- Support UTF-16 surrogate-pair escapes such as `"\uD83D\uDE00"` in ordinary string literals. https://github.com/rescript-lang/rescript/pull/8606
3233
- Support dynamic imports of external bindings annotated with `@scope`; the generated import follows the complete property path. These imports were previously rejected. https://github.com/rescript-lang/rescript/pull/8582
3334
- Add `@res.hoistedFunction` for emitting nested module functions as flat JavaScript exports. https://github.com/rescript-lang/rescript/pull/8402

‎analysis/src/find_files.ml‎

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,27 @@ let get_namespace config =
116116
in
117117
from_name |> Option.map name_space_to_name
118118

119+
let get_platforms config =
120+
match
121+
config |> Yojson_helpers.get "platforms" |> bind Yojson_helpers.to_list_opt
122+
with
123+
| None -> []
124+
| Some platforms -> platforms |> List.filter_map Yojson_helpers.string_opt
125+
126+
let get_platform_implementation platforms file =
127+
if not (Filename.check_suffix file ".res") then None
128+
else
129+
let stem = Filename.basename file |> Filename.chop_extension in
130+
platforms
131+
|> List.find_map (fun platform ->
132+
let suffix = "." ^ platform in
133+
if Filename.check_suffix stem suffix then
134+
Some
135+
( platform,
136+
String.sub stem 0 (String.length stem - String.length suffix)
137+
|> String.capitalize_ascii )
138+
else None)
139+
119140
module String_set = Set.Make (String)
120141

121142
let get_public config =
@@ -180,7 +201,8 @@ let find_package_root ~base ~sourcedirs_package_roots name =
180201
| _ -> Module_resolution.resolve_node_module_path ~start_path:base name
181202

182203
(* returns a list of (absolute path to cmt(i), relative path from base to source file) *)
183-
let find_project_files ~public ~namespace ~path ~source_directories ~lib_bs =
204+
let find_project_files ~public ~namespace ~platforms ~path ~source_directories
205+
~lib_bs =
184206
let dirs =
185207
source_directories |> List.map (Filename.concat path) |> String_set.of_list
186208
in
@@ -205,47 +227,85 @@ let find_project_files ~public ~namespace ~path ~source_directories ~lib_bs =
205227

206228
let normals =
207229
files |> String_set.elements
208-
|> Utils.filter_map (fun file ->
230+
|> List.concat_map (fun file ->
209231
if is_implementation file then (
210-
let module_name = get_name file in
232+
let platform_implementation =
233+
get_platform_implementation platforms file
234+
in
235+
let physical_module_name = get_name file in
236+
let module_name =
237+
match platform_implementation with
238+
| Some (_, logical_module_name) -> logical_module_name
239+
| None -> physical_module_name
240+
in
241+
let is_primary_platform =
242+
match (platforms, platform_implementation) with
243+
| primary :: _, Some (platform, _) -> primary = platform
244+
| _ -> true
245+
in
211246
let resi = Hashtbl.find_opt interfaces module_name in
212-
Hashtbl.remove interfaces module_name;
247+
if is_primary_platform then Hashtbl.remove interfaces module_name;
213248
let base = compiled_base_name ~namespace (Files.relpath path file) in
214-
match resi with
215-
| Some resi ->
216-
let cmti = (lib_bs /+ base) ^ ".cmti" in
249+
match (platform_implementation, is_primary_platform, resi) with
250+
| Some _, false, _ ->
251+
let cmt = (lib_bs /+ base) ^ ".cmt" in
252+
if Files.exists cmt then
253+
[
254+
( physical_module_name,
255+
module_name,
256+
Shared_types.Impl {cmt; res = file} );
257+
]
258+
else (
259+
Log.log ("Bad platform source file (no cmt) " ^ (lib_bs /+ base));
260+
[])
261+
| _, _, Some resi ->
262+
let interface_base =
263+
compiled_base_name ~namespace (Files.relpath path resi)
264+
in
265+
let cmti = (lib_bs /+ interface_base) ^ ".cmti" in
217266
let cmt = (lib_bs /+ base) ^ ".cmt" in
218267
if Files.exists cmti then
219268
if Files.exists cmt then
220269
(* Log.log("Intf and impl " ++ cmti ++ " " ++ cmt) *)
221-
Some
270+
let logical_entry =
222271
( module_name,
272+
module_name,
223273
Shared_types.IntfAndImpl {cmti; resi; cmt; res = file} )
224-
else None
274+
in
275+
match platform_implementation with
276+
| Some _ ->
277+
[
278+
logical_entry;
279+
( physical_module_name,
280+
module_name,
281+
Shared_types.Impl {cmt; res = file} );
282+
]
283+
| None -> [logical_entry]
284+
else []
225285
else (
226286
(* Log.log("Just intf " ++ cmti) *)
227287
Log.log ("Bad source file (no cmt/cmti/cmi) " ^ (lib_bs /+ base));
228-
None)
229-
| None ->
288+
[])
289+
| _, _, None ->
230290
let cmt = (lib_bs /+ base) ^ ".cmt" in
231-
if Files.exists cmt then Some (module_name, Impl {cmt; res = file})
291+
if Files.exists cmt then
292+
[(module_name, module_name, Shared_types.Impl {cmt; res = file})]
232293
else (
233294
Log.log ("Bad source file (no cmt/cmi) " ^ (lib_bs /+ base));
234-
None))
235-
else None)
295+
[]))
296+
else [])
236297
in
237298
let result =
238299
normals
239-
|> List.filter_map (fun (name, paths) ->
240-
let original_name = name in
300+
|> List.filter_map (fun (name, public_name, paths) ->
241301
let name =
242302
match namespace with
243303
| None -> name
244304
| Some namespace -> name ^ "-" ^ namespace
245305
in
246306
match public with
247307
| Some public ->
248-
if public |> String_set.mem original_name then Some (name, paths)
308+
if public |> String_set.mem public_name then Some (name, paths)
249309
else None
250310
| None -> Some (name, paths))
251311
in
@@ -316,7 +376,8 @@ let find_dependency_files base config =
316376
in
317377
let project_files =
318378
find_project_files ~public:(get_public inner) ~namespace
319-
~path ~source_directories ~lib_bs
379+
~platforms:(get_platforms inner) ~path
380+
~source_directories ~lib_bs
320381
in
321382
Some (compiled_directories, project_files))
322383
| None -> None

‎analysis/src/packages.ml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ let new_bs_package ~root_path =
106106
let project_files_and_paths =
107107
Find_files.find_project_files
108108
~public:(Find_files.get_public config)
109-
~namespace ~path:root_path ~source_directories ~lib_bs
109+
~namespace
110+
~platforms:(Find_files.get_platforms config)
111+
~path:root_path ~source_directories ~lib_bs
110112
in
111113
let paths_for_module =
112114
make_paths_for_module ~project_files_and_paths

‎compiler/bsc/rescript_compiler_main.ml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ let command_line_flags : (string * Bsc_args.spec * string) array =
249249
( "-bs-read-cmi",
250250
unit_call (fun _ -> Clflags.assume_no_mli := Mli_exists),
251251
"*internal* Assume mli always exist " );
252+
( "-bs-platform-interface",
253+
set Js_config.platform_interface,
254+
"*internal* Emit platform-independent cmj metadata" );
252255
( "-ppx",
253256
string_list_add Clflags.all_ppx,
254257
"*internal* <command> Pipe abstract syntax trees through preprocessor \

‎compiler/common/js_config.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ let no_version_header = ref false
3232

3333
let directives = ref []
3434
let cross_module_inline = ref false
35+
let platform_interface = ref false
3536
let debug_ir = ref false
3637
let check_lam = ref false
3738

‎compiler/common/js_config.mli‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ val directives : string list ref
4949
val cross_module_inline : bool ref
5050
(** cross module inline option *)
5151

52+
val platform_interface : bool ref
53+
(** emit conservative [.cmj] metadata for a platform implementation *)
54+
5255
val debug_ir : bool ref
5356
(** dump intermediate representations and related diagnostics *)
5457

‎compiler/core/js_packages_info.ml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ let iter (x : t) cb = Ext_list.iter x.module_systems cb
6868

6969
let map (x : t) cb = Ext_list.map x.module_systems cb
7070

71+
let with_suffix suffix (x : t) =
72+
{
73+
x with
74+
module_systems =
75+
Ext_list.map x.module_systems (fun package -> {package with suffix});
76+
}
77+
7178
(* let equal (x : t) ({name; module_systems}) =
7279
x.name = name &&
7380
Ext_list.for_all2_no_exn

‎compiler/core/js_packages_info.mli‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ val iter : t -> (package_info -> unit) -> unit
4242

4343
val map : t -> (package_info -> 'a) -> 'a list
4444

45+
val with_suffix : string -> t -> t
46+
4547
val empty : t
4648

4749
val from_name : string -> t

‎compiler/core/lam_stats_export.ml‎

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,49 +50,51 @@ let values_of_export (meta : Lam_stats.t) (export_map : Lambda.t Map_ident.t) :
5050
in
5151
let persistent_closed_lambda =
5252
let optlam = Map_ident.find_opt export_map x in
53-
match optlam with
54-
| Some
55-
(Lconst
56-
( Const_js_null | Const_js_undefined _ | Const_js_true
57-
| Const_js_false ))
58-
| None ->
59-
optlam
60-
| Some lambda ->
61-
if not !Js_config.cross_module_inline then None
62-
else if
63-
Lam_analysis.safe_to_inline lambda
64-
(* when inlning a non function, we have to be very careful,
53+
if !Js_config.platform_interface then None
54+
else
55+
match optlam with
56+
| Some
57+
(Lconst
58+
( Const_js_null | Const_js_undefined _ | Const_js_true
59+
| Const_js_false ))
60+
| None ->
61+
optlam
62+
| Some lambda ->
63+
if not !Js_config.cross_module_inline then None
64+
else if
65+
Lam_analysis.safe_to_inline lambda
66+
(* when inlning a non function, we have to be very careful,
6567
only truly immutable values can be inlined
6668
*)
67-
then
68-
match lambda with
69-
| Lfunction {attr = {inline = Always_inline}}
70-
(* FIXME: is_closed lambda is too restrictive
69+
then
70+
match lambda with
71+
| Lfunction {attr = {inline = Always_inline}}
72+
(* FIXME: is_closed lambda is too restrictive
7173
It precludes ues cases
7274
- inline forEach but not forEachU
7375
*)
74-
| Lfunction {attr = {is_a_functor = true}} ->
75-
if Lam_closure.is_closed lambda (* TODO: seriealize more*) then
76-
optlam
77-
else None
78-
| _ ->
79-
let lam_size = Lam_analysis.size lambda in
80-
(* TODO:
76+
| Lfunction {attr = {is_a_functor = true}} ->
77+
if Lam_closure.is_closed lambda (* TODO: seriealize more*) then
78+
optlam
79+
else None
80+
| _ ->
81+
let lam_size = Lam_analysis.size lambda in
82+
(* TODO:
8183
1. global need re-assocate when do the beta reduction
8284
2. [lambda_exports] is not precise
8385
*)
84-
let free_variables =
85-
Lam_closure.free_variables Set_ident.empty Map_ident.empty
86-
lambda
87-
in
88-
if
89-
lam_size < Lam_analysis.small_inline_size
90-
&& Map_ident.is_empty free_variables
91-
then (
92-
Ext_log.dwarn ~__POS__ "%s recorded for inlining @." x.name;
93-
optlam)
94-
else None
95-
else None
86+
let free_variables =
87+
Lam_closure.free_variables Set_ident.empty Map_ident.empty
88+
lambda
89+
in
90+
if
91+
lam_size < Lam_analysis.small_inline_size
92+
&& Map_ident.is_empty free_variables
93+
then (
94+
Ext_log.dwarn ~__POS__ "%s recorded for inlining @." x.name;
95+
optlam)
96+
else None
97+
else None
9698
in
9799
match (arity, persistent_closed_lambda) with
98100
| Single Arity_na, (None | Some (Lconst Const_module_alias)) -> acc
@@ -129,10 +131,17 @@ let get_dependent_module_effect (maybe_pure : string option)
129131
let export_to_cmj (meta : Lam_stats.t) effect_ export_map hoisted_exports case :
130132
Js_cmj_format.t =
131133
let values = values_of_export meta export_map in
132-
133-
Js_cmj_format.make ~values ~hoisted_exports ~effect_
134-
~package_spec:(Js_packages_state.get_packages_info ())
135-
~case
134+
let values, hoisted_exports, effect_, package_spec =
135+
if !Js_config.platform_interface then
136+
( Map_string.empty,
137+
[],
138+
Some "platform implementation",
139+
Js_packages_state.get_packages_info ()
140+
|> Js_packages_info.with_suffix "" )
141+
else
142+
(values, hoisted_exports, effect_, Js_packages_state.get_packages_info ())
143+
in
144+
Js_cmj_format.make ~values ~hoisted_exports ~effect_ ~package_spec ~case
136145
(* FIXME: make sure [-o] would not change its case
137146
add test for ns/non-ns
138147
*)

‎docs/docson/build-schema.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,16 @@
493493
"suffix": {
494494
"$ref": "#/definitions/suffix-spec"
495495
},
496+
"platforms": {
497+
"type": "array",
498+
"minItems": 1,
499+
"uniqueItems": true,
500+
"items": {
501+
"type": "string",
502+
"pattern": "^[a-z][a-z0-9_]*$"
503+
},
504+
"description": "Platform suffixes for shared-interface modules such as Button.android.res and Button.ios.res."
505+
},
496506
"reanalyze": {
497507
"$ref": "#/definitions/reanalyze",
498508
"description": "Configure reanalyze, a static code analysis tool for ReScript."

0 commit comments

Comments
 (0)