-
Notifications
You must be signed in to change notification settings - Fork 455
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
Support @as("foo") to customize the representation of tags. #6095
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
524650e
Emit tags as strings.
cristianoc 78827c5
Allow more general type for tags.
cristianoc 4d925d3
Do not special case variants with only 1 case with payload.
cristianoc 69a7e41
Support @as("foo") to customize the representation of tags.
cristianoc 80a4ee3
Cleanup: chatgpt's suggestion
cristianoc dbe3570
Add support for custom representation of the form `@as(12)`
cristianoc 5b28cb5
Add null, undefined, unboxed customization.
cristianoc 8d964e9
Add tagged unions example from chatgpt.
cristianoc b286212
Use custom tags.
cristianoc 4b9d87e
Remove unused set_tag from compiler internals.
cristianoc 273c4f4
Add support for @tag(...) to customize the property used for the tag.
cristianoc 935951e
Document places needing restriction.
cristianoc a02fdf7
Add support for @as(true) and @as(false)
cristianoc 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 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 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 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 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 |
---|---|---|
|
@@ -316,6 +316,13 @@ let small_int i : t = | |
| 248 -> obj_int_tag_literal | ||
| i -> int (Int32.of_int i) | ||
|
||
let as_value = function | ||
| Lambda.AsString s -> str s ~delim:DStarJ | ||
| AsInt i -> small_int i | ||
| AsNull -> nil | ||
| AsUndefined -> undefined | ||
| AsUnboxed -> assert false (* Should not emit tags for unboxed *) | ||
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. TODO: put restriction on the variant definitions allowed, to make sure this never happens. |
||
|
||
let array_index ?comment (e0 : t) (e1 : t) : t = | ||
match (e0.expression_desc, e1.expression_desc) with | ||
| Array (l, _), Number (Int { i; _ }) | ||
|
@@ -762,8 +769,26 @@ let string_equal ?comment (e0 : t) (e1 : t) : t = | |
let is_type_number ?comment (e : t) : t = | ||
string_equal ?comment (typeof e) (str "number") | ||
|
||
let is_tag (e : t) : t = | ||
{ expression_desc = Bin (NotEqEq, typeof e, str "object"); comment=None } | ||
let is_tag ?(has_null_undefined_other=(false, false, false)) (e : t) : t = | ||
let (has_null, has_undefined, has_other) = has_null_undefined_other in | ||
if has_null && (has_undefined = false) && (has_other = false) then (* null *) | ||
{ expression_desc = Bin (EqEqEq, e, nil); comment=None } | ||
else if has_null && has_undefined && has_other=false then (* null + undefined *) | ||
{ J.expression_desc = Bin | ||
(Or, | ||
{ expression_desc = Bin (EqEqEq, e, nil); comment=None }, | ||
{ expression_desc = Bin (EqEqEq, e, undefined); comment=None } | ||
); comment=None } | ||
else if has_null=false && has_undefined && has_other=false then (* undefined *) | ||
{ expression_desc = Bin (EqEqEq, e, undefined); comment=None } | ||
else if has_null then (* (null + undefined + other) || (null + other) *) | ||
{ J.expression_desc = Bin | ||
(Or, | ||
{ expression_desc = Bin (EqEqEq, e, nil); comment=None }, | ||
{ expression_desc = Bin (NotEqEq, typeof e, str "object"); comment=None } | ||
); comment=None } | ||
else (* (undefiled + other) || other *) | ||
{ expression_desc = Bin (NotEqEq, typeof e, str "object"); comment=None } | ||
|
||
let is_type_string ?comment (e : t) : t = | ||
string_equal ?comment (typeof e) (str "string") | ||
|
@@ -775,8 +800,8 @@ let is_type_object (e : t) : t = string_equal (typeof e) (str "object") | |
call plain [dot] | ||
*) | ||
|
||
let tag ?comment e : t = | ||
{ expression_desc = Caml_block_tag e; comment } | ||
let tag ?comment ?(name=Js_dump_lit.tag) e : t = | ||
{ expression_desc = Caml_block_tag (e, name); comment } | ||
|
||
(* according to the compiler, [Btype.hash_variant], | ||
it's reduced to 31 bits for hash | ||
|
This file contains 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 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 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 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 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 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 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: put restriction on the variant definitions allowed, to make sure this never happens.