Skip to content

Commit

Permalink
fix(compiler): Provide named function export for recursive functions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Sep 1, 2021
1 parent 4a201cf commit e7d2ff4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
12 changes: 11 additions & 1 deletion compiler/src/middle_end/linearize.re
Original file line number Diff line number Diff line change
Expand Up @@ -1323,15 +1323,25 @@ let rec transl_anf_statement =
};
(Some(exp_setup @ setup @ rest_setup), rest_imp);
| TTopLet(export_flag, Recursive, mut_flag, binds) =>
let exported = export_flag == Exported;
let (binds, new_binds_setup) =
List.split(
List.map(
({vb_pat, vb_expr}) => {
let name =
if (exported) {
switch (vb_pat.pat_desc) {
| TPatVar(bind, _) => Some(Ident.name(bind))
| _ => None
};
} else {
None;
};
let vb_expr = {
...vb_expr,
exp_attributes: attributes @ vb_expr.exp_attributes,
};
(vb_pat, transl_comp_expression(vb_expr));
(vb_pat, transl_comp_expression(~name?, vb_expr));
},
binds,
),
Expand Down
87 changes: 87 additions & 0 deletions compiler/test/__snapshots__/exports.a2013f43.0.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
exports › let_rec_export
(module
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $none_=>_none (func))
(type $none_=>_i32 (func (result i32)))
(type $i32_=>_i32 (func (param i32) (result i32)))
(import \"_grainEnv\" \"mem\" (memory $0 0))
(import \"_grainEnv\" \"tbl\" (table $tbl 0 funcref))
(elem $elem (global.get $import__grainEnv_0_relocBase_0) $foo_1145)
(import \"_grainEnv\" \"relocBase\" (global $import__grainEnv_0_relocBase_0 i32))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$malloc_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$decRefIgnoreZeros\" (global $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $import_GRAIN$MODULE$runtime/gc_0_malloc_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"decRefIgnoreZeros\" (func $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0 (param i32 i32) (result i32)))
(global $global_0 (mut i32) (i32.const 0))
(global $global_2 i32 (i32.const 1))
(export \"memory\" (memory $0))
(export \"GRAIN$EXPORT$foo\" (global $global_0))
(export \"foo\" (func $foo_1145))
(export \"_gmain\" (func $_gmain))
(export \"_start\" (func $_start))
(export \"GRAIN$TABLE_SIZE\" (global $global_2))
(func $foo_1145 (; has Stack IR ;) (param $0 i32) (result i32)
(i32.const 11)
)
(func $_gmain (; has Stack IR ;) (result i32)
(local $0 i32)
(tuple.extract 0
(tuple.make
(block (result i32)
(global.set $global_0
(tuple.extract 0
(tuple.make
(block (result i32)
(i32.store
(local.tee $0
(tuple.extract 0
(tuple.make
(call $import_GRAIN$MODULE$runtime/gc_0_malloc_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$malloc_0)
(i32.const 16)
)
(i32.const 0)
)
)
)
(i32.const 7)
)
(i32.store offset=4
(local.get $0)
(i32.const 1)
)
(i32.store offset=8
(local.get $0)
(global.get $import__grainEnv_0_relocBase_0)
)
(i32.store offset=12
(local.get $0)
(i32.const 0)
)
(local.get $0)
)
(call $import_GRAIN$MODULE$runtime/gc_0_decRefIgnoreZeros_0
(global.get $import_GRAIN$MODULE$runtime/gc_0_GRAIN$EXPORT$decRefIgnoreZeros_0)
(global.get $global_0)
)
)
)
)
(i32.const 1879048190)
)
(tuple.extract 0
(tuple.make
(global.get $global_0)
(local.get $0)
)
)
)
)
)
(func $_start (; has Stack IR ;)
(drop
(call $_gmain)
)
)
;; custom section \"cmi\", size 607
)
2 changes: 2 additions & 0 deletions compiler/test/suites/exports.re
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ describe("exports", ({test}) => {
"import * from \"exportStar\"; y(secret)",
"Unbound value secret",
);

assertSnapshot("let_rec_export", "export let rec foo = () => 5");
});

0 comments on commit e7d2ff4

Please sign in to comment.