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

style(governance): lint for Jessie dialect (WIP) #3876

Closed
wants to merge 1 commit into from

Conversation

dckc
Copy link
Member

@dckc dckc commented Sep 22, 2021

I got surprisingly few lint errors when I added @jessie-check to the governance sources. The code is already remarkably close to Jessie. The biggest issue I see is:

  • dynamic property names in paramManager

For code such as...

typesAndValues[name] = { type, value };

Is that just irreconcilable with Jessie? Or is there some Object.defineProperty(...) idiom that we prefer in such cases?

The other issue is relatively minor:

  • use of new in new WeakMap() (1 occurrence)

I'd be tempted to put

export const makeWeakMap = () => new WeakMap();

in the same directory, named tablemaker.js or jessielib.js or some such and import from there. I lean against adding a new package dependency for a 1-liner (plus, I'm not sure how stable our jessie stdlib package is).

@@ -106,7 +107,7 @@ const makeBinaryVoteCounter = (questionSpec, threshold, instance) => {
if (choice < 0) {
spoiled += shares;
} else {
tally[choice] += shares;
tally[+choice] += shares;
Copy link
Contributor

Choose a reason for hiding this comment

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

What does a unary plus do as an array index? Is it coercing choice to a number?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think so.

I have only a vague understanding of why, but...

Jessie omits mutation through computed property names. Jessie has syntax for mutating only number-named properties, which include integers, floating point, NaN, Infinity, and -Infinity.
-- https://github.com/endojs/Jessie

Copy link
Member

@erights erights Sep 22, 2021

Choose a reason for hiding this comment

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

We want to make it easier to statically reason more reliably about Jessie programs, while avoiding undue syntactic burden. A typing practice common outside JS and approximated by TS is that records are heterogeneously typed -- each named field is separately typed, and arrays are uniformly typed. But JS fights us here, by making array indexes just be normal property names, and generalizing the [] syntax to apply to all property names. Computed indexing destroys simple static reasoning about heterogeneously typed record fields.

The more correct syntax for Jessie to insist on to ensure computed property names are array indexes would be obj[index|0] as done in asm.js. Perhaps we should do that instead. But we opted for a looser compromise to get a slightly less unpleasant syntax. Our looser compromise depends on normal records not having properties named for floating point values or NaN or Infinity or -Infinity.

(Btw, I think the obj[+index] syntactic compromise is originally due to @mikesamuel . Thanks!)

Copy link
Member

Choose a reason for hiding this comment

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

The

E(paramMgr)[`update${paramName}`]

below is a great example of something that is prohibited by Jessie, but is in this case a good idea. For this one, I think the correct approach is an appropriate eslint-ignore-like comment or ts-ignore-like comment, though I don't yet know how to do those for Jessie. It is the right approach because it is unusual for code like this to be a good idea, and its costs to static reasoning are real and significant. As with ts-ignore comments, the Jessie ignore comment should be accompanied by rationale.

Copy link
Member Author

Choose a reason for hiding this comment

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

For the sort of formal methods I hope to use, I don't expect them to cope well with ignore comments.
I'm inclined to extract the computed property lookup stuff to a function in a separate file, which we would reason about separately.

Copy link
Member

@erights erights Sep 22, 2021

Choose a reason for hiding this comment

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

Yes. Along the lines of your fromEntries suggestion at #3876 (comment) , for each of these accesses, there is already adequate reflective API. For property reading by itself, for example, there is

Reflect.get(tally, choice)

Copy link
Contributor

@katelynsills katelynsills Sep 23, 2021

Choose a reason for hiding this comment

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

I had the opposite opinion earlier, but now I don't see many gains from E(paramMgr)[`update${paramName}`]. What makes this a good idea?

Copy link
Member Author

Choose a reason for hiding this comment

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

The earlier answer was:

each updateFoo() message is a separable capability.

I mentioned "my preference for static typing", which argues for your E(paramMgr).update(paramName, proposedValue) suggestion; it looks like Jessie suggests a similar preference.

I suppose the separate capability could always be expressed using currying: proposedValue => E(paramMgr).update("knob1", proposedValue).

I can see it both ways.

@dckc
Copy link
Member Author

dckc commented Sep 22, 2021

Oh... the typesAndValues[name] = { type, value }; is in a loop that constructs typesAndValues. Rewriting to use Object.fromEntries(paramDescriptions.map(...)) seems straightforward.

But typesAndValues is still mutated in the updateFn. Should it be a map rather than an object?

@michaelfig
Copy link
Member

typesAndValues[name] = { type, value };

Is that just irreconcilable with Jessie? Or is there some Object.defineProperty(...) idiom that we prefer in such cases?

Objects-as-maps is the antipattern we're trying to avoid. Just use Map if possible. If it's impossible, then unfortunately you're out of luck.

I lean against adding a new package dependency for a 1-liner (plus, I'm not sure how stable our jessie stdlib package is).

Please just import { makeWeakMap } from '@jessie.js'. If you've already bought into @jessie-check, there's not any compelling reason to avoid it.

@jessie.js will remain backward-compatible, and we expect to hang other useful things there (maybe even E or a new Far-alike).

Copy link
Member

@erights erights left a comment

Choose a reason for hiding this comment

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

Wonderful to see! We need everyone to do a lot more of this! Thanks for getting us started!

@@ -106,7 +107,7 @@ const makeBinaryVoteCounter = (questionSpec, threshold, instance) => {
if (choice < 0) {
spoiled += shares;
} else {
tally[choice] += shares;
tally[+choice] += shares;
Copy link
Member

@erights erights Sep 22, 2021

Choose a reason for hiding this comment

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

We want to make it easier to statically reason more reliably about Jessie programs, while avoiding undue syntactic burden. A typing practice common outside JS and approximated by TS is that records are heterogeneously typed -- each named field is separately typed, and arrays are uniformly typed. But JS fights us here, by making array indexes just be normal property names, and generalizing the [] syntax to apply to all property names. Computed indexing destroys simple static reasoning about heterogeneously typed record fields.

The more correct syntax for Jessie to insist on to ensure computed property names are array indexes would be obj[index|0] as done in asm.js. Perhaps we should do that instead. But we opted for a looser compromise to get a slightly less unpleasant syntax. Our looser compromise depends on normal records not having properties named for floating point values or NaN or Infinity or -Infinity.

(Btw, I think the obj[+index] syntactic compromise is originally due to @mikesamuel . Thanks!)

@@ -106,7 +107,7 @@ const makeBinaryVoteCounter = (questionSpec, threshold, instance) => {
if (choice < 0) {
spoiled += shares;
} else {
tally[choice] += shares;
tally[+choice] += shares;
Copy link
Member

Choose a reason for hiding this comment

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

The

E(paramMgr)[`update${paramName}`]

below is a great example of something that is prohibited by Jessie, but is in this case a good idea. For this one, I think the correct approach is an appropriate eslint-ignore-like comment or ts-ignore-like comment, though I don't yet know how to do those for Jessie. It is the right approach because it is unusual for code like this to be a good idea, and its costs to static reasoning are real and significant. As with ts-ignore comments, the Jessie ignore comment should be accompanied by rationale.

Copy link
Contributor

@katelynsills katelynsills left a comment

Choose a reason for hiding this comment

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

This is cool to see! I added comments separately.

One question for @dckc, what's the benefit of export const makeWeakMap = () => new WeakMap();? Maybe I don't understand the reasoning behind wanting to reduce usage of new

@dckc
Copy link
Member Author

dckc commented Sep 23, 2021

... for @dckc, what's the benefit of export const makeWeakMap = () => new WeakMap();? Maybe I don't understand the reasoning behind wanting to reduce usage of new

The stated justification is not all that clear to me either:

Arrow functions and concise methods have a [[Call]] behavior and no [[Construct]] behavior, preventing them from being called as a constructor, such as with new. However, Jessie function functions can be called by SES code with new. Without this it is hard to see how this could confuse a function function, but I am not yet confident that this does not produce a hazard for the Jessie code.
-- https://github.com/endojs/Jessie#remaining-hazards

I guess there's a hazard of calling functions as constructors when they weren't meant to be? I'm fuzzy on what that hazzard is @erights maybe you want to beef up the justification a bit? Add a link to some popular explanation of this hazard?

also...

TinySES [and hence Jessie] is not intended to run legacy code or code that uses inheritance.

Jessie doesn't have this nor class and I think it doesn't have prototypical inheritance; I suppose without those, the only use for new is to construct a WeakMap, Map, or Set.. From the perspective of someone writing a Jessie interpreter, adding makeWeakMap library function is in some ways simpler than including the new language construct.

@Chris-Hibbert Chris-Hibbert force-pushed the manageTreasury-3189 branch 7 times, most recently from 97f1d53 to 6ce7f17 Compare October 7, 2021 16:40
Base automatically changed from manageTreasury-3189 to master October 8, 2021 16:32
@dckc
Copy link
Member Author

dckc commented Oct 12, 2021

@mhofman had a suggestion in the area of types for parameterized governance:

For this to work, GetParams would have to be a generic on the ParamDescription and getLoadParams: GetParams<LoadParamDescriptions>
The getLoanParamValue can then accept a @param {keyof LoadParamDescriptions} key, and typing should flow through

The code is remarkably close to Jessie. Remaining issues include:

 - new WeakMap()
 - dynamic property names in paramManager
@dckc
Copy link
Member Author

dckc commented May 6, 2023

thanks landing a bunch of @jessie-check stuff @erights

@dckc dckc closed this May 6, 2023
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.

5 participants