-
Notifications
You must be signed in to change notification settings - Fork 484
Add @res.hoistedFunction support for flat JS export
#8402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cknitt
wants to merge
12
commits into
master
Choose a base branch
from
hoisted-functions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3c50e90
Add `@res.hoistedFunction` support for flat JS export
cknitt a433fe2
Diagnose unsupported hoisted function patterns
cknitt ea70870
Diagnose hoists hidden by signatures
cknitt cb7e60c
Reject hoists inside private modules
cknitt 24763f1
Track hoisted functions by source path
cknitt 8e5d81c
Preserve hoisted binding identity
cknitt f52fd1a
Name hoisted function metadata fields
cknitt 15f9480
Tidy hoisted function metadata handling
cknitt b8b8c20
Bump compatibility marker for CMJ schema
cknitt 3f6fae3
Reuse unused attribute handling for hoisted functions
cknitt d1900ac
Rely on final hoisted binding validation
cknitt 4cc7110
Fix playground Lambda debug output
cknitt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -290,6 +290,39 @@ type initialization = J.block | |
| *) | ||
|
|
||
| let compile output_prefix = | ||
| (* When compiling a read from another module, a nested source path like | ||
| Other.A.B.make reaches this point as nested module-field reads: | ||
|
|
||
| Pfield "make" (Pfield "B" (Pfield "A" (Lglobal_module Other))) | ||
|
|
||
| Normal compilation does not look up the full path. It only queries the | ||
| first field, "A", and then emits the remaining fields as JS property | ||
| access: Other.A.B.make. The "A" lookup may include Submodule arity data, | ||
| but it does not say whether A.B.make has a separate root-level export. | ||
|
|
||
| Hoisted functions need that extra question. For them, query the separate | ||
| hoisted-values table with an unambiguous key for source path A.B.make. If | ||
| present, the table returns the root-level JS export name, for example | ||
| A$B$make. Normal export metadata still lives in the regular .cmj values | ||
| table. *) | ||
| let rec extract_field_path segments primitive args = | ||
| match (primitive, args) with | ||
| | ( Lam_primitive.Pfield (_, Fld_module {name}), | ||
| [Lam.Lprim {primitive; args; _}] ) -> | ||
| extract_field_path (name :: segments) primitive args | ||
| | ( Lam_primitive.Pfield (_, Fld_module {name}), | ||
| [Lam.Lglobal_module (id, dynamic_import)] ) -> | ||
| Some (id, dynamic_import, name :: segments) | ||
| | _ -> None | ||
| in | ||
| let hoisted_external_field_name primitive args = | ||
| match extract_field_path [] primitive args with | ||
| | Some (id, dynamic_import, (_ :: _ :: _ as segments)) -> | ||
| Ext_option.map | ||
| (Lam_compile_env.find_hoisted_external_export ~dynamic_import id | ||
| segments) (fun name -> (id, dynamic_import, name)) | ||
| | Some (_, _, ([] | [_])) | None -> None | ||
| in | ||
| let rec compile_external_field (* Like [List.empty]*) | ||
| ?(dynamic_import = false) (lamba_cxt : Lam_compile_context.t) | ||
| (id : Ident.t) name : Js_output.t = | ||
|
|
@@ -1718,17 +1751,47 @@ let compile output_prefix = | |
| fn_code args))) | ||
| and compile_prim (prim_info : Lam.prim_info) | ||
| (lambda_cxt : Lam_compile_context.t) = | ||
| let compile_primitive_default primitive args loc = | ||
| let args_block, args_expr = | ||
| if args = [] then ([], []) | ||
| else | ||
| let new_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in | ||
| Ext_list.split_map args (fun x -> | ||
| match compile_lambda new_cxt x with | ||
| | {block; value = Some b} -> (block, b) | ||
| | {value = None} -> assert false) | ||
| in | ||
| let args_code : J.block = List.concat args_block in | ||
| let exp = | ||
| (* TODO: all can be done in [compile_primitive] *) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. forgotten or is that one for me to continue?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part of the code was just moved, the TODO was there before. |
||
| Lam_compile_primitive.translate output_prefix loc lambda_cxt primitive | ||
| args_expr | ||
| in | ||
| Js_output.output_of_block_and_expression lambda_cxt.continuation args_code | ||
| (with_source_loc loc exp) | ||
| in | ||
| match prim_info with | ||
| | { | ||
| primitive = Pfield (_, fld_info); | ||
| args = [Lglobal_module (id, dynamic_import)]; | ||
| _; | ||
| } -> ( | ||
| (* should be before Lglobal_global *) | ||
| match fld_info with | ||
| | Fld_module {name = field} -> | ||
| compile_external_field ~dynamic_import lambda_cxt id field | ||
| | _ -> assert false) | ||
| | {primitive = Pfield (_, Fld_module _); _} -> ( | ||
| match hoisted_external_field_name prim_info.primitive prim_info.args with | ||
| | Some (id, dynamic_import, hoisted_name) -> | ||
| Js_output.output_of_expression lambda_cxt.continuation | ||
| ~no_effects:no_effects_const | ||
| (E.ml_var_dot ~dynamic_import id hoisted_name) | ||
| | None -> ( | ||
| match prim_info with | ||
| | { | ||
| primitive = Pfield (_, fld_info); | ||
| args = [Lglobal_module (id, dynamic_import)]; | ||
| _; | ||
| } -> ( | ||
| (* should be before Lglobal_global *) | ||
| match fld_info with | ||
| | Fld_module {name = field} -> | ||
| compile_external_field ~dynamic_import lambda_cxt id field | ||
| | _ -> assert false) | ||
| | _ -> | ||
| compile_primitive_default prim_info.primitive prim_info.args | ||
| prim_info.loc)) | ||
| | {primitive = Praise; args = [e]; loc} -> ( | ||
| match | ||
| compile_lambda {lambda_cxt with continuation = NeedValue Not_tail} e | ||
|
|
@@ -1898,24 +1961,7 @@ let compile output_prefix = | |
| Location.raise_errorf ~loc | ||
| "Invalid argument: unsupported argument to dynamic import. If you \ | ||
| believe this should be supported, please open an issue.") | ||
| | {primitive; args; loc} -> | ||
| let args_block, args_expr = | ||
| if args = [] then ([], []) | ||
| else | ||
| let new_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in | ||
| Ext_list.split_map args (fun x -> | ||
| match compile_lambda new_cxt x with | ||
| | {block; value = Some b} -> (block, b) | ||
| | {value = None} -> assert false) | ||
| in | ||
| let args_code : J.block = List.concat args_block in | ||
| let exp = | ||
| (* TODO: all can be done in [compile_primitive] *) | ||
| Lam_compile_primitive.translate output_prefix loc lambda_cxt primitive | ||
| args_expr | ||
| in | ||
| Js_output.output_of_block_and_expression lambda_cxt.continuation args_code | ||
| (with_source_loc loc exp) | ||
| | {primitive; args; loc} -> compile_primitive_default primitive args loc | ||
| and collect_dup_overrides (copy_id : Ident.t) (lam : Lam.t) | ||
| (acc : (Lam_compat.set_field_dbg_info * Lam.t) list) : | ||
| (Lam_compat.set_field_dbg_info * Lam.t) list option = | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.