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
1 change: 1 addition & 0 deletions changes/inline-unloadable-module.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- When inlining functions from other modules (until now only with `-bs-cross-module-opt`), call a function from another package instead of inlining it when its body names a module of a package the caller doesn't depend on. The build used to fail with "not found, it means either the module does not exist or it is a namespace".
41 changes: 39 additions & 2 deletions compiler/backend/lam_compile_env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ let cached_tbl_key =

let cached_tbl () = Domain.DLS.get cached_tbl_key

(* Whether this compile found a module's .cmj on the load path, by module
name, so each one is looked up once. *)
let loadable_tbl_key =
Request_local.new_key (fun () : (string, bool) Hashtbl.t -> Hashtbl.create 7)

let loadable_tbl () = Domain.DLS.get loadable_tbl_key

let ( +> ) id value = Lam_module_ident.Hash.add (cached_tbl ()) id value

(* For each compilation we need reset to make it re-entrant *)
Expand All @@ -56,7 +63,8 @@ let reset () =
(* This is needed in the playground since one no_export can make it true
In the payground, it seems we need reset more states
*)
Lam_module_ident.Hash.clear (cached_tbl ())
Lam_module_ident.Hash.clear (cached_tbl ());
Hashtbl.clear (loadable_tbl ())

(** We should not provide "#moduleid" as output
since when we print it in the end, it will
Expand Down Expand Up @@ -101,9 +109,38 @@ let cmj_table_of_module_id ~dynamic_import (module_id : Ident.t) =
let register_ml_module ~dynamic_import (module_id : Ident.t) : unit =
ignore (cmj_table_of_module_id ~dynamic_import module_id)

(* Whether this compile can load a module that a stored body names: it is
cached already, or its .cmj is on the load path. Looking it up must not
register it, since registered modules with side effects get imported. *)
let can_load (id : Ident.t) =
Lam_module_ident.Hash.mem (cached_tbl ())
(Lam_module_ident.of_ml ~dynamic_import:false id)
||
match Hashtbl.find_opt (loadable_tbl ()) id.name with
| Some loadable -> loadable
| None ->
let loadable =
Config_util.find_opt (id.name ^ Literals.suffix_cmj) <> None
in
Hashtbl.replace (loadable_tbl ()) id.name loadable;
loadable

let rec names_unloadable_module (lam : Lambda.t) =
(match lam with
| Lglobal_module id -> not (can_load id)
| _ -> false)
|| Lambda_traverse.shallow_exists names_unloadable_module lam

(* A stored body can name modules of packages this one does not depend on,
which are not on its load path. Inlining it would fail, so the caller
calls the value instead, as without a stored body. *)
let query_external_id_info (module_id : Ident.t) (name : string) : ident_info =
let cmj_table = cmj_table_of_module_id ~dynamic_import:false module_id in
Js_cmj_format.query_by_name cmj_table name
let info = Js_cmj_format.query_by_name cmj_table name in
match info.persistent_closed_lambda with
| Some lam when names_unloadable_module lam ->
{info with persistent_closed_lambda = None}
| Some _ | None -> info

let get_package_info_from_cmj (id : Lam_module_ident.t) :
Js_packages_info.t * Ext_js_file_kind.case =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lib
**/*.mjs
!node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app",
"version": "0.0.1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "app",
"sources": { "dir": "src" },
"dependencies": ["p"],
"package-specs": { "module": "esmodule", "in-source": true },
"suffix": ".mjs",
"compiler-flags": ["-bs-cross-module-opt"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// PMod.f names QMod, from a package app does not depend on: called.
let viaQ = PMod.f(2)
// PMod.k names no other module: inlined.
let local = PMod.k(2)
28 changes: 28 additions & 0 deletions tests/build_tests/projects/cross_module_unloadable_module/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check

// With -bs-cross-module-opt, a stored body that names a module of a package
// the caller does not depend on cannot be inlined there: that module is not
// on the caller's load path. The caller calls the value instead.

import * as assert from "node:assert";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { setup } from "#dev/process";

const { execBuildOrThrow, execClean } = setup(
path.join(import.meta.dirname, "app"),
);

await execClean();
await execBuildOrThrow();

const app = await fs.readFile(
path.join(import.meta.dirname, "app", "src", "App.mjs"),
"utf8",
);

assert.match(app, /PMod\.f\(2\)/, "a body naming QMod is called");
assert.doesNotMatch(app, /QMod/, "App does not refer to QMod");
assert.match(app, /let local = 4;/, "a body naming no module is inlined");

await execClean();

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading