Skip to content

Commit a39b48f

Browse files
authored
fix(compiler): Refactor WASI polyfill resolution (#1261)
1 parent f80ae2f commit a39b48f

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

compiler/src/linking/link.re

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,17 @@ let is_grain_module = mod_name => {
4747
};
4848

4949
let wasi_polyfill_module = () => {
50-
"GRAIN$MODULE$./"
51-
++ Filename.remove_extension(Option.get(Config.wasi_polyfill^));
50+
Filename.remove_extension(Option.get(Config.wasi_polyfill_path()))
51+
++ ".gr.wasm";
5252
};
5353

5454
let is_wasi_module = mod_name => {
5555
mod_name == "wasi_snapshot_preview1";
5656
};
5757

58-
let is_wasi_polyfill_module = mod_path =>
59-
mod_path == resolve(wasi_polyfill_module());
58+
let is_wasi_polyfill_module = mod_path => {
59+
mod_path == wasi_polyfill_module();
60+
};
6061

6162
let new_base_dir = Filename.dirname;
6263

@@ -99,15 +100,14 @@ let rec build_dependency_graph = (~base_dir, mod_path) => {
99100
// Perform any WASI polyfilling. Note that we skip this step if we are compiling the polyfill module itself.
100101
// If we are importing a foreign from WASI, then add a dependency to the polyfill instead.
101102
let imported_module = wasi_polyfill_module();
102-
let resolved_import = resolve(imported_module);
103-
if (!Hashtbl.mem(modules, resolved_import)) {
104-
Hashtbl.add(modules, resolved_import, load_module(resolved_import));
103+
if (!Hashtbl.mem(modules, imported_module)) {
104+
Hashtbl.add(modules, imported_module, load_module(imported_module));
105105
build_dependency_graph(
106-
new_base_dir(resolved_import),
107-
resolved_import,
106+
new_base_dir(imported_module),
107+
imported_module,
108108
);
109109
};
110-
G.add_edge(dependency_graph, mod_path, resolved_import);
110+
G.add_edge(dependency_graph, mod_path, imported_module);
111111
};
112112
};
113113
};
@@ -416,7 +416,7 @@ let link_all = (linked_mod, dependencies, signature) => {
416416
let wasi_polyfill = wasi_polyfill_module();
417417
let new_name =
418418
Hashtbl.find_opt(
419-
Hashtbl.find(exported_names, resolve(wasi_polyfill)),
419+
Hashtbl.find(exported_names, wasi_polyfill),
420420
imported_name,
421421
);
422422
let new_name =

compiler/src/utils/config.re

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,12 @@ let stdlib_directory = (): option(string) =>
579579
stdlib_dir^,
580580
);
581581

582+
let wasi_polyfill_path = (): option(string) =>
583+
Option.map(
584+
path => Filepath.(to_string(String.derelativize(path))),
585+
wasi_polyfill^,
586+
);
587+
582588
let module_search_path = () => {
583589
switch (stdlib_directory()) {
584590
| Some(x) => include_dirs^ @ [x] /* stdlib goes last */

compiler/src/utils/config.rei

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ type optimization_level =
77
/** The Grain stdlib directory, based on the current configuration */
88
let stdlib_directory: unit => option(string);
99

10+
/** The WASI polyfill path, based on the current configuration */
11+
let wasi_polyfill_path: unit => option(string);
12+
1013
/** The list of directories to search for modules in, based on the current configuration */
1114

1215
let module_search_path: unit => list(string);

0 commit comments

Comments
 (0)