From b0c5f42419f49f6677c1632ed89f26170c641a13 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 9 Aug 2026 16:30:58 +0200 Subject: [PATCH] Replace CPPO browser branches with platform modules Replace BROWSER conditionals with Dune-selected ordinary modules. The browser profile selects playground implementations while every other profile selects native implementations, keeping dev and release builds functionally identical and documenting the convention where it is applied. Keep the platform boundary narrow: native builds use C primitives and artifact persistence, while the playground uses JavaScript-compatible primitives and no-op persistence. Keep shared Cmt_format behavior in an ordinary module and use generated source rules instead of virtual libraries, which trigger a Dune internal error with the compiler's unwrapped libraries and break @check and ocaml-lsp. Remove CPPO from compiler sources that no longer need preprocessing, remove the unused hash_small_int test hook, and retain CPPO only for genuine type-specialization templates. Update architecture guidance and the error-variant catalog for the new source layout. Signed-off-by: Cristiano Calcagno --- AGENTS.md | 9 + CHANGELOG.md | 1 + CONTRIBUTING.md | 20 +- compiler/core/build_artifact_stubs.c | 33 ++ compiler/core/dune | 50 +- compiler/core/js_name_of_module_id.cppo.ml | 167 ------ compiler/core/lam_compile_main.cppo.ml | 399 ------------- compiler/core/lam_compile_main.ml | 348 ++++++++++++ compiler/core/lam_module_ident.ml | 4 +- .../core/platform/native/build_artifact.ml | 2 + .../platform/native/js_name_of_module_id.ml | 139 +++++ .../platform/playground/build_artifact.ml | 2 + .../playground/js_name_of_module_id.ml | 6 + compiler/dune | 11 +- compiler/ext/bs_hash_stubs.cppo.ml | 40 -- compiler/ext/dune | 64 +-- ...tubs.c => ext_platform_primitives_stubs.c} | 94 +--- compiler/ext/ext_string.cppo.ml | 530 ------------------ compiler/ext/ext_string.ml | 469 ++++++++++++++++ .../{ext_string.cppo.mli => ext_string.mli} | 133 ++--- compiler/ext/{ext_sys.cppo.ml => ext_sys.ml} | 12 +- compiler/ext/hash.cppo.ml | 7 +- compiler/ext/hash_set.cppo.ml | 7 +- compiler/ext/hash_set_ident_mask.ml | 3 +- ...eserved_map.cppo.ml => js_reserved_map.ml} | 305 +++++----- compiler/ext/ordered_hash_map.cppo.ml | 3 +- .../native/ext_platform_primitives.ml | 19 + .../playground/ext_platform_primitives.ml | 13 + compiler/ext/vec.cppo.ml | 2 +- compiler/ml/cmt_format.ml | 6 + compiler/ml/cmt_format.mli | 2 +- ...mt_format.cppo.ml => cmt_format_common.ml} | 181 +++--- compiler/ml/dune | 32 +- .../platform/native/cmt_format_persistence.ml | 52 ++ .../playground/cmt_format_persistence.ml | 2 + tests/ERROR_VARIANTS.md | 4 +- tests/ounit_tests/ounit_hash_stubs_test.ml | 25 +- 37 files changed, 1489 insertions(+), 1707 deletions(-) create mode 100644 compiler/core/build_artifact_stubs.c delete mode 100644 compiler/core/js_name_of_module_id.cppo.ml delete mode 100644 compiler/core/lam_compile_main.cppo.ml create mode 100644 compiler/core/lam_compile_main.ml create mode 100644 compiler/core/platform/native/build_artifact.ml create mode 100644 compiler/core/platform/native/js_name_of_module_id.ml create mode 100644 compiler/core/platform/playground/build_artifact.ml create mode 100644 compiler/core/platform/playground/js_name_of_module_id.ml delete mode 100644 compiler/ext/bs_hash_stubs.cppo.ml rename compiler/ext/{ext_basic_hash_stubs.c => ext_platform_primitives_stubs.c} (59%) delete mode 100644 compiler/ext/ext_string.cppo.ml create mode 100644 compiler/ext/ext_string.ml rename compiler/ext/{ext_string.cppo.mli => ext_string.mli} (71%) rename compiler/ext/{ext_sys.cppo.ml => ext_sys.ml} (85%) rename compiler/ext/{js_reserved_map.cppo.ml => js_reserved_map.ml} (52%) create mode 100644 compiler/ext/platform/native/ext_platform_primitives.ml create mode 100644 compiler/ext/platform/playground/ext_platform_primitives.ml create mode 100644 compiler/ml/cmt_format.ml rename compiler/ml/{cmt_format.cppo.ml => cmt_format_common.ml} (52%) create mode 100644 compiler/ml/platform/native/cmt_format_persistence.ml create mode 100644 compiler/ml/platform/playground/cmt_format_persistence.ml diff --git a/AGENTS.md b/AGENTS.md index a1a7f2ce9c8..9b5974bc0ff 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -72,6 +72,15 @@ JS IR JavaScript Code ``` +### Platform-specific compiler modules + +The Dune `browser` profile builds the playground compiler. Platform-dependent +modules are stored below `platform/native/` and `platform/playground/` in their +owning compiler directory. Rules in that directory's `dune` file copy the +selected implementation into the build directory as an ordinary `.ml` module; +all other profiles select the native source. Generated module paths in errors +or stack traces therefore map back to one of those two source directories. + ### Key Directory Structure ``` diff --git a/CHANGELOG.md b/CHANGELOG.md index 76551587577..eee2c3a2e70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ - Add the `-check-lam` compiler option, enable Lambda invariant checking in compiler tests, and remove build-profile-dependent checking. https://github.com/rescript-lang/rescript/pull/8534 - Replace `-bs-diagnose` with `-debug-ir` and make IR diagnostic artifacts deterministic, compilation-local, and easy to clean. https://github.com/rescript-lang/rescript/pull/8535 +- Replace CPPO-based browser conditionals with Dune-selected native and playground compiler implementations. https://github.com/rescript-lang/rescript/pull/8541 # 13.0.0-alpha.5 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2802119dedb..46078febc0b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -297,6 +297,20 @@ The "Playground bundle" is a JS version of the ReScript compiler; including all The ReScript source code is compiled with a tool called [JSOO (js_of_ocaml)](https://ocsigen.org/js_of_ocaml/latest/manual/overview), which uses OCaml bytecode to compile to JavaScript and is part of the bigger OCaml ecosystem. +### Platform-specific compiler modules + +Within `compiler/`, the Dune `browser` profile specifically means the +playground compiler. A few modules have implementations under +`platform/native/` and `platform/playground/`; mutually exclusive rules in the +owning `dune` file copy the appropriate implementation into the build directory +as an ordinary `.ml` module. Other profiles select the native implementation. + +Consequently, a generated filename such as `ext_platform_primitives.ml` may +appear in a compiler stack trace even though it is not present in the source +tree. Its source is the corresponding file below `platform/native/` or +`platform/playground/` in the same compiler directory. Keep both +implementations API-compatible when changing one of these modules. + ### Building the Bundle The entry point of the JSOO bundle is located in `compiler/jsoo/jsoo_playground_main.ml`, the compiler and its relevant runtime cmij files can be built via make: @@ -310,9 +324,9 @@ Note that building the cmijs is based on the dependencies defined in `packages/p After a successful compilation, you will find following files in your project: -- `playground/compiler.js` -> This is the ReScript compiler, which binds the ReScript API to the `window` object. -- `playground/packages/compiler-builtins` -> The compiler base cmij containing all the relevant core modules (`Js`, `Belt`, `Pervasives`, etc.) -- `playground/packages/*` -> Contains third party deps with cmij.js files (as defined in `packages/playground/rescript.json`) +- `packages/playground/compiler.js` -> This is the ReScript compiler, which binds the ReScript API to the `window` object. +- `packages/playground/packages/compiler-builtins` -> The compiler base cmij containing all the relevant core modules (`Js`, `Belt`, `Pervasives`, etc.) +- `packages/playground/packages/*` -> Contains third party deps with cmij.js files (as defined in `packages/playground/rescript.json`) You can now use the `compiler.js` file either directly by using a `