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

Switch IsCallable and IsConstructor to return boolean #218

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

rbuckton
Copy link
Contributor

@rbuckton rbuckton commented Jul 5, 2023

This changes the behavior of IsCallable and IsConstructor to return boolean instead of BooleanValue so that they can be turned into type guards in a future type-safety PR.

@Jack-Works
Copy link
Contributor

I like this idea, but it is ambiguous when we should use BooleanValue and when we should use a boolean. Also, the test failed.

@rbuckton
Copy link
Contributor Author

rbuckton commented Jul 6, 2023

I like this idea, but it is ambiguous when we should use BooleanValue and when we should use a boolean.

There are a few other functions that return boolean rather than BooleanValue, so it's not unheard of. Unfortunately, using boolean is the only way to enforce a user-defined type guard in TypeScript.

Also, the test failed.

I missed a usage of IsCallable in test/test262.js, which has now been fixed. That does, apparently, make this a breaking change that may require a new major version, assuming engine262 follows semver.

@Jack-Works
Copy link
Contributor

do you feel ok with this change? cc @devsnek

@devsnek
Copy link
Member

devsnek commented Jul 31, 2023

there used to be a util to call runtime/abstract ops from inside js by prefixing them with %, like %IsCallable(f), in which case it was useful for them to return the lang values. additionally it matches the spec better. but ultimately i don't think either of those things matter too much for simple operations like this, so 🤷

my biggest curiosity is around the "future type safety". The spec does not statically care whether a value is callable or a constructor, in fact that's why these test functions exist in the first place, so why would engine262 care? if this work is generally going in the direction of what happened to completion.mts i would suggest not doing it.

That does, apparently, make this a breaking change that may require a new major version, assuming engine262 follows semver.

this package doesn't follow semver, but you should probably double check that https://engine262.js.org keeps working: https://github.com/engine262/engine262.github.io/blob/master/src/worker.js

@Jack-Works
Copy link
Contributor

For me, the type of safety that is worth adding is if we can avoid casting things. (Cast is somehow a foot gun that might get unaligned with the actual status of the code)

For example, if there is code like below, I think this is worth adding:

if (IsGenerator(value) === Value.true) {
    const brand = (value as GeneratorObjectValue).GeneratorBrand
}
// to
if (IsGenerator(value)) {
    const brand = value.GeneratorBrand
    // do not need cast, value is already GeneratorObjectValue
}
Assert(IsGenerator(value) === Value.true)
const brand = (value as GeneratorObjectValue).GeneratorBrand

// to
Assert(IsGenerator(value))
const brand = value.GeneratorBrand
// do not need cast, value is already GeneratorObjectValue

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