Skip to content
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

Untagged variants powered by instanceof #6383

Merged
merged 8 commits into from
Sep 7, 2023
Merged

Untagged variants powered by instanceof #6383

merged 8 commits into from
Sep 7, 2023

Conversation

cristianoc
Copy link
Collaborator

No description provided.

@zth
Copy link
Collaborator

zth commented Sep 5, 2023

This implements support for using instanceof as discriminator in untagged variants. This opens up for "natively" supporting using a bunch of built in things together, which isn't possible right now because we mostly rely on typeof:

  • Promise
  • Date
  • RegExp
  • File
  • Blob
  • BigInt This is a typeof, add in separate PR
  • FormData I was sure I saw one, but right now can't find an API that returns FormData together with some other type. So waiting with this one.

Adding more to this list as we go.

One restriction here is that we need anything using instanceof to be backed by actual types defined in the compiler. promise<_>, Js.Re.t, Js.Date.t all fit the bill well here. We can add simple abstract types for the other relevant things like File and Blob. Libs can then reference those types for themselves, and be compatible with untagged variants.

@zth
Copy link
Collaborator

zth commented Sep 6, 2023

Follow up work from this:

  • Look into optimizing output when there are literal checks.
  • Dig through aliases so using aliased types work.

Copy link
Collaborator Author

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't approve as I figure as the author.
But it's approved.

Left some comments.

jscomp/core/lam_compile.ml Outdated Show resolved Hide resolved
S.string_switch ?default ?declaration (E.typeof e) clauses in
~else_:([build_if_chain rest])
| (Ast_untagged_variants.Untagged (InstanceType instanceType), {J.switch_body}) :: rest ->
S.if_ (E.instanceof e (E.js_global (Ast_untagged_variants.Instance.to_string instanceType)))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See js_exp_make: the array case emits different code.
Perhaps put the emission logic in one place shared so it's done only once for instances (and the array special cased only once in that logic)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristianoc can you check this commit, is this along the lines of what you had in mind? e687695

Logic is now only in emit_check.

jscomp/ml/ast_untagged_variants.ml Outdated Show resolved Hide resolved
jscomp/ml/ast_untagged_variants.ml Outdated Show resolved Hide resolved
@DZakh
Copy link
Contributor

DZakh commented Sep 6, 2023

Dig through aliases so using aliased types work.

Will it work even with opaque types? Sounds like a magic 🙌

@zth
Copy link
Collaborator

zth commented Sep 6, 2023

Dig through aliases so using aliased types work.

Will it work even with opaque types? Sounds like a magic 🙌

Depends on what you mean. It'll work with a few select built in opaque types, but not any opaque type. Aliases is about if you for example have type fn = unit => string and want to use fn as payload. Or the Dict.t case from RescriptCore, which should work because Dict.t = Js.Dict.t but right now doesn't because we don't follow aliases. I think anyway, I haven't investigated this deeply yet.

@zth zth changed the title Skeleton adapt untagged variants to several instanceof cases. Untagged variants powered by instanceof Sep 6, 2023
@zth zth merged commit 8ee328b into master Sep 7, 2023
7 checks passed
@zth zth deleted the instanceof branch September 7, 2023 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants