Closed Bug 1101903 Opened 11 years ago Closed 11 years ago

Allow parsing and emitting strict mode code in smaller than script-sized units

Categories

(Core :: JavaScript Engine, defect)

x86_64
Linux
defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla39
Tracking Status
firefox39 --- fixed

People

(Reporter: efaust, Assigned: efaust)

References

Details

Attachments

(2 files)

Classes want to make everything parsed and emitted inside them to be "in strict mode". Unfortunately, all our understanding of strict mode everywhere is script-wide. Fixing this involves two stages: 1) Alter the bytecode and consumers to include a notion of strictness of individual operations. 2) Allow the frontend to consider strictness in terms of smaller units. (account for writes to the bit, and perhaps decouple the notion of strictness of the parser from the bce. The second steps are mostly simplifications and helper functions, and can go in this bug. The first part is a larger undertaking, and so I will file it as a separate blocker
Depends on: 1101905
Note that NOT ALL SITES of |sc->strict| change to |sc->strict()|! Some remain querying the script-level bool. Is there anywhere else we should be asserting?
Assignee: nobody → efaustbmo
Status: NEW → ASSIGNED
Attachment #8526423 - Flags: review?(jwalden+bmo)
The intended flow here is bool strictSave = setLocalStrictness(true); ... JS_ALWAYS_TRUE(setLocalStrictness(strictSave));
Attachment #8526424 - Flags: review?(jwalden+bmo)
Comment on attachment 8526423 [details] [diff] [review] Part 1: Convert SharedContext::strict to an accessor method. Review of attachment 8526423 [details] [diff] [review]: ----------------------------------------------------------------- I assume you've compiled with a compiler that will warn on boolean testing of a function reference, right? |if (bce->sc->strict)| will "work" just fine per C++ rules, so being sure to have taken the extra caution is a very good idea here. Unclear to me what else where we could assert. Nothing jumped out at me looking at this, exactly. If there's anything else, someone will notice it eventually, I expect. ::: js/src/frontend/BytecodeEmitter.cpp @@ +1522,1 @@ > break; Should be switch (op) { case JSOP_SETNAME: if (bce->sc->strict()) op = JSOP_STRICTSETNAME; break; case JSOP_SETGNAME: ... } @@ +5917,5 @@ > // "use strict" should warn for any "use strict" statements seen > // later in the script, because such statements are misleading. > const char *directive = nullptr; > if (atom == cx->names().useStrict) { > + if (!bce->sc->strictScript) Hmm. So I guess this is right with the current code. And we simply don't warn about a useless expression here, if the "use strict" redundantly restates the truth. From having written this, I remember there being some dumb reason we couldn't warn about useless expressions like this that aren't contrary but simply restate reality; too bad that has to be the way it is, I guess. ::: js/src/frontend/Parser.cpp @@ +2772,5 @@ > if (pc->sc->isFunctionBox()) { > // Request that this function be reparsed as strict. > pc->newDirectives->setStrict(); > return false; > } else { That looks like an else-after-return, violating our usual style rules. Probably worth a fix in a separate patch, rs=me. @@ +2775,5 @@ > return false; > } else { > // We don't reparse global scopes, so we keep track of the > // one possible strict violation that could occur in the > // directive prologue -- octal escapes -- and complain now. I remember implementing this nonsense, back in the day... ::: js/src/frontend/SharedContext.h @@ +202,5 @@ > > inline bool allLocalsAliased(); > > + bool strict() { > + // To be enahanced later in stack. A nice tactic for reviewers, when it's remembered early enough to not require absolute patch dismemberment to add.
Attachment #8526423 - Flags: review?(jwalden+bmo) → review+
Comment on attachment 8526424 [details] [diff] [review] Part 2: Add a notion of local strictness. Review of attachment 8526424 [details] [diff] [review]: ----------------------------------------------------------------- Your intended flow scares me a bit, but forward progress. I would prefer to get us to the point where classes introduce a third type of ParseContext/SharedContext/whatever, because classes are going to have their own weird rules in various ways, I expect. But forward motion for now. ::: js/src/frontend/Parser.h @@ +512,5 @@ > > virtual bool strictMode() { return pc->sc->strict(); } > + bool setLocalStrictMode(bool strict) { > + MOZ_ASSERT(tokenStream.debugNoLookahead()); > + return pc->sc->setLocalStrictMode(strict); We do this because if we had any lookahead, it might have been a string token that was "stupid octal: \007" or something? But please adjust to explain this: MOZ_ASSERT(tokenStream.debugHasNoLookahead(), "must not have lookahead (e.g. '\\007') that might have " "contained missed strict mode errors"); ::: js/src/frontend/SharedContext.h @@ +174,5 @@ > public: > ExclusiveContext *const context; > AnyContextFlags anyCxFlags; > bool strictScript; > + bool localStrict; Does this not need to be initialized by constructors? Seems a little odd. @@ +208,5 @@ > + } > + bool setLocalStrictMode(bool strict) { > + bool retVal = localStrict; > + localStrict = strict; > + return retVal; Could do bool setLocalStrictMode(bool strict) { mozilla::Swap(strict, localStrict); return strict; } Too cute? I dunno, I could go either way. I'd prefer |prev| or |previous| to |retVal|, tho -- capture the sense of the value, not its use. ::: js/src/frontend/TokenStream.h @@ +532,5 @@ > void seek(const Position &pos); > bool seek(const Position &pos, const TokenStream &other); > +#ifdef DEBUG > + inline bool debugNoLookahead() const { > + return !lookahead; debugHasNoLookahead(), for a little more clarity? And make that |lookahead == 0|, because the variable doesn't primarily have a boolean sense to it.
Attachment #8526424 - Flags: review?(jwalden+bmo) → review+
Status: ASSIGNED → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla39
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: