Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 17 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 `<script src="/path/to/compiler.js"/>` and `<script src="/path/to/packages/compilerCmij.js"/>` inside a html file, use a browser bundler infrastructure to optimize it, or use `nodejs` to run it on a command line:

Expand Down
33 changes: 33 additions & 0 deletions compiler/core/build_artifact_stubs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "caml/memory.h"
#include "caml/mlvalues.h"
#include "caml/osdeps.h"
#include "caml/signals.h"

#ifdef _WIN32
#include <sys/utime.h>

CAMLprim value caml_stale_file(value path)
{
CAMLparam1(path);
struct _utimbuf times;
char *os_path = caml_stat_strdup(String_val(path));
times.modtime = 0;
caml_enter_blocking_section();
_utime(os_path, &times);
caml_leave_blocking_section();
caml_stat_free(os_path);
CAMLreturn(Val_unit);
}
#else
#include <sys/time.h>

CAMLprim value caml_stale_file(value path)
{
CAMLparam1(path);
struct timeval times[2] = {{0, 0}, {0, 0}};
char *os_path = caml_stat_strdup_to_os(String_val(path));
utimes(os_path, times);
caml_stat_free(os_path);
CAMLreturn(Val_unit);
}
#endif
50 changes: 37 additions & 13 deletions compiler/core/dune
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
(library
(name core)
(wrapped false)
(instrumentation
(backend bisect_ppx))
(flags
(:standard -w +a-4-9-27-30-40-41-42-48-70))
(libraries depends ext flow_parser frontend gentype yojson))
(env
(_
(flags
(:standard -w +a-4-9-27-30-40-41-42-48-70))))

; The browser profile builds the playground compiler; these rules generate modules from platform/{native,playground}.

(rule
(target build_artifact.ml)
(enabled_if
(= %{profile} browser))
(action
(copy platform/playground/build_artifact.ml build_artifact.ml)))

(rule
(target build_artifact.ml)
(enabled_if
(<> %{profile} browser))
(action
(copy platform/native/build_artifact.ml build_artifact.ml)))

(rule
(target js_name_of_module_id.ml)
(deps js_name_of_module_id.cppo.ml)
(enabled_if
(= %{profile} browser))
(action
(run %{bin:cppo} %{env:CPPO_FLAGS=} %{deps} -o %{target})))
(copy platform/playground/js_name_of_module_id.ml js_name_of_module_id.ml)))

(rule
(target lam_compile_main.ml)
(deps lam_compile_main.cppo.ml)
(target js_name_of_module_id.ml)
(enabled_if
(<> %{profile} browser))
(action
(run %{bin:cppo} %{env:CPPO_FLAGS=} %{deps} -o %{target})))
(copy platform/native/js_name_of_module_id.ml js_name_of_module_id.ml)))

(library
(name core)
(wrapped false)
(instrumentation
(backend bisect_ppx))
(foreign_stubs
(language c)
(names build_artifact_stubs))
(libraries depends ext flow_parser frontend gentype yojson))
167 changes: 0 additions & 167 deletions compiler/core/js_name_of_module_id.cppo.ml

This file was deleted.

Loading
Loading