Skip to content

Commit

Permalink
Statically generate boilerplate for bitfield accessors (#16482)
Browse files Browse the repository at this point in the history
* Statically generate boilerplate for parser state accessors

* Also use in `@babel/traverse`

* Node 6 compat in the plugin

* Use public field
  • Loading branch information
nicolo-ribaudo committed May 9, 2024
1 parent 4bd1b2c commit 3ff20b9
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 174 deletions.
2 changes: 2 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ module.exports = function (api) {

convertESM ? "@babel/transform-export-namespace-from" : null,
env !== "standalone" ? "@babel/plugin-proposal-json-modules" : null,

require("./scripts/babel-plugin-bit-decorator/plugin.cjs"),
].filter(Boolean),
overrides: [
{
Expand Down
154 changes: 17 additions & 137 deletions packages/babel-parser/src/tokenizer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,18 @@ type TopicContextState = {
maxTopicIndex: null | 0;
};

const enum StateFlags {
None = 0,
Strict = 1 << 0,
maybeInArrowParameters = 1 << 1,
inType = 1 << 2,
noAnonFunctionType = 1 << 3,
hasFlowComment = 1 << 4,
isAmbientContext = 1 << 5,
inAbstractClass = 1 << 6,
inDisallowConditionalTypesContext = 1 << 7,
soloAwait = 1 << 8,
inFSharpPipelineDirectBody = 1 << 9,
canStartJSXElement = 1 << 10,
containsEsc = 1 << 11,
}

export const enum LoopLabelKind {
Loop = 1,
Switch = 2,
}

declare const bit: import("../../../../scripts/babel-plugin-bit-decorator/types.d.ts").BitDecorator<State>;

export default class State {
flags: number = StateFlags.canStartJSXElement;
@bit.storage flags: number;

@bit accessor strict = false;

get strict(): boolean {
return (this.flags & StateFlags.Strict) > 0;
}
set strict(value: boolean) {
if (value) {
this.flags |= StateFlags.Strict;
} else {
this.flags &= ~StateFlags.Strict;
}
}
curLine: number;
lineStart: number;

Expand Down Expand Up @@ -98,76 +76,13 @@ export default class State {
noArrowParamsConversionAt: number[] = [];

// Flags to track
get maybeInArrowParameters(): boolean {
return (this.flags & StateFlags.maybeInArrowParameters) > 0;
}
set maybeInArrowParameters(value: boolean) {
if (value) {
this.flags |= StateFlags.maybeInArrowParameters;
} else {
this.flags &= ~StateFlags.maybeInArrowParameters;
}
}
get inType(): boolean {
return (this.flags & StateFlags.inType) > 0;
}
set inType(value: boolean) {
if (value) {
this.flags |= StateFlags.inType;
} else {
this.flags &= ~StateFlags.inType;
}
}
get noAnonFunctionType(): boolean {
return (this.flags & StateFlags.noAnonFunctionType) > 0;
}
set noAnonFunctionType(value: boolean) {
if (value) {
this.flags |= StateFlags.noAnonFunctionType;
} else {
this.flags &= ~StateFlags.noAnonFunctionType;
}
}
get hasFlowComment(): boolean {
return (this.flags & StateFlags.hasFlowComment) > 0;
}
set hasFlowComment(value: boolean) {
if (value) {
this.flags |= StateFlags.hasFlowComment;
} else {
this.flags &= ~StateFlags.hasFlowComment;
}
}
get isAmbientContext(): boolean {
return (this.flags & StateFlags.isAmbientContext) > 0;
}
set isAmbientContext(value: boolean) {
if (value) {
this.flags |= StateFlags.isAmbientContext;
} else {
this.flags &= ~StateFlags.isAmbientContext;
}
}
get inAbstractClass(): boolean {
return (this.flags & StateFlags.inAbstractClass) > 0;
}
set inAbstractClass(value: boolean) {
if (value) {
this.flags |= StateFlags.inAbstractClass;
} else {
this.flags &= ~StateFlags.inAbstractClass;
}
}
get inDisallowConditionalTypesContext(): boolean {
return (this.flags & StateFlags.inDisallowConditionalTypesContext) > 0;
}
set inDisallowConditionalTypesContext(value: boolean) {
if (value) {
this.flags |= StateFlags.inDisallowConditionalTypesContext;
} else {
this.flags &= ~StateFlags.inDisallowConditionalTypesContext;
}
}
@bit accessor maybeInArrowParameters = false;
@bit accessor inType = false;
@bit accessor noAnonFunctionType = false;
@bit accessor hasFlowComment = false;
@bit accessor isAmbientContext = false;
@bit accessor inAbstractClass = false;
@bit accessor inDisallowConditionalTypesContext = false;

// For the Hack-style pipelines plugin
topicContext: TopicContextState = {
Expand All @@ -176,26 +91,8 @@ export default class State {
};

// For the F#-style pipelines plugin
get soloAwait(): boolean {
return (this.flags & StateFlags.soloAwait) > 0;
}
set soloAwait(value: boolean) {
if (value) {
this.flags |= StateFlags.soloAwait;
} else {
this.flags &= ~StateFlags.soloAwait;
}
}
get inFSharpPipelineDirectBody(): boolean {
return (this.flags & StateFlags.inFSharpPipelineDirectBody) > 0;
}
set inFSharpPipelineDirectBody(value: boolean) {
if (value) {
this.flags |= StateFlags.inFSharpPipelineDirectBody;
} else {
this.flags &= ~StateFlags.inFSharpPipelineDirectBody;
}
}
@bit accessor soloAwait = false;
@bit accessor inFSharpPipelineDirectBody = false;

// Labels in scope.
labels: Array<{
Expand Down Expand Up @@ -231,31 +128,14 @@ export default class State {
// The context stack is used to track whether the apostrophe "`" starts
// or ends a string template
context: Array<TokContext> = [ct.brace];

// Used to track whether a JSX element is allowed to form
get canStartJSXElement(): boolean {
return (this.flags & StateFlags.canStartJSXElement) > 0;
}
set canStartJSXElement(value: boolean) {
if (value) {
this.flags |= StateFlags.canStartJSXElement;
} else {
this.flags &= ~StateFlags.canStartJSXElement;
}
}
@bit accessor canStartJSXElement = true;

// Used to signal to callers of `readWord1` whether the word
// contained any escape sequences. This is needed because words with
// escape sequences must not be interpreted as keywords.
get containsEsc(): boolean {
return (this.flags & StateFlags.containsEsc) > 0;
}
set containsEsc(value: boolean) {
if (value) {
this.flags |= StateFlags.containsEsc;
} else {
this.flags &= ~StateFlags.containsEsc;
}
}
@bit accessor containsEsc = false;

// Used to track invalid escape sequences in template literals,
// that must be reported if the template is not tagged.
Expand Down
45 changes: 8 additions & 37 deletions packages/babel-traverse/src/path/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const REMOVED = 1 << 0;
export const SHOULD_STOP = 1 << 1;
export const SHOULD_SKIP = 1 << 2;

declare const bit: import("../../../../scripts/babel-plugin-bit-decorator/types.d.ts").BitDecorator<any>;

const NodePath_Final = class NodePath {
constructor(hub: HubInterface, parent: t.Node | null) {
this.parent = parent;
Expand All @@ -53,8 +55,12 @@ const NodePath_Final = class NodePath {
contexts: Array<TraversalContext> = [];
state: any = null;
opts: ExplodedTraverseOptions | null = null;
// this.shouldSkip = false; this.shouldStop = false; this.removed = false;
_traverseFlags: number = 0;

@bit.storage _traverseFlags: number;
@bit(REMOVED) accessor removed = false;
@bit(SHOULD_STOP) accessor shouldStop = false;
@bit(SHOULD_SKIP) accessor shouldSkip = false;

skipKeys: Record<string, boolean> | null = null;
parentPath: NodePath_Final | null = null;
container: t.Node | Array<t.Node> | null = null;
Expand Down Expand Up @@ -180,41 +186,6 @@ const NodePath_Final = class NodePath {
get parentKey(): string {
return (this.listKey || this.key) as string;
}

get shouldSkip() {
return !!(this._traverseFlags & SHOULD_SKIP);
}

set shouldSkip(v) {
if (v) {
this._traverseFlags |= SHOULD_SKIP;
} else {
this._traverseFlags &= ~SHOULD_SKIP;
}
}

get shouldStop() {
return !!(this._traverseFlags & SHOULD_STOP);
}

set shouldStop(v) {
if (v) {
this._traverseFlags |= SHOULD_STOP;
} else {
this._traverseFlags &= ~SHOULD_STOP;
}
}

get removed() {
return !!(this._traverseFlags & REMOVED);
}
set removed(v) {
if (v) {
this._traverseFlags |= REMOVED;
} else {
this._traverseFlags &= ~REMOVED;
}
}
};

Object.assign(
Expand Down
126 changes: 126 additions & 0 deletions scripts/babel-plugin-bit-decorator/plugin.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
module.exports = pluginBabelBitDecorator;

/** @param {{ types: import("@babel/types") }} api */
function pluginBabelBitDecorator({ types: t, template }) {
const bodyTemplate = template.statement({ allowReturnOutsideFunction: true });

return {
manipulateOptions({ parserOpts }) {
parserOpts.plugins.push("decorators", "decoratorAutoAccessors");
},
visitor: {
Class(path) {
let storageField;
let storageName;

for (const element of path.get("body.body")) {
if (
(element.isClassProperty() || element.isClassPrivateProperty()) &&
element.node.decorators &&
element.node.decorators.some(dec =>
t.matchesPattern(dec.expression, "bit.storage")
)
) {
element.node.decorators = element.node.decorators.filter(
dec => !t.matchesPattern(dec.expression, "bit.storage")
);
storageField = element;
storageName = element.node.key;
break;
}
}

let initial = 0;
let nextMask = 1;

for (const element of path.get("body.body")) {
let dec;
if (
element.isClassAccessorProperty() &&
(dec =
element.node.decorators &&
element
.get("decorators")
.find(
({ node: dec }) =>
t.isIdentifier(dec.expression, { name: "bit" }) ||
(t.isCallExpression(dec.expression) &&
t.isIdentifier(dec.expression.callee, { name: "bit" }))
))
) {
if (element.node.static || t.isPrivateName(element.node.key)) {
throw element.buildCodeFrameError(
"@bit cannot be used on static or private fields"
);
}
if (element.node.decorators.length > 1) {
throw element.buildCodeFrameError(
"@bit cannot be used with other decorators"
);
}
if (!t.isBooleanLiteral(element.node.value)) {
throw element.buildCodeFrameError(
"@bit fields must be initialized to a boolean literal"
);
}
if (!storageName) {
throw path.buildCodeFrameError(
"Cannot use @bit withuot also declaring a @bit.storage field"
);
}
if (nextMask === 0) {
// overflow
throw path.buildCodeFrameError(
"A class can contain at most 32 @bit decorators"
);
}

let val;
if (
t.isCallExpression(dec.node.expression) &&
dec.node.expression.arguments.length > 0 &&
(val = dec.get("expression.arguments.0").evaluate().value) !==
nextMask
) {
throw dec.buildCodeFrameError(
`Bit mask is ${nextMask.toString(2)}, but found ${val.toString(2)} (or couldn't evaluate)`
);
}

if (element.node.value.value) {
initial |= nextMask;
}

element.replaceWithMultiple([
t.classMethod(
"get",
element.node.key,
[],
bodyTemplate.ast`{
return (
this.${t.cloneNode(storageName)} & ${t.numericLiteral(nextMask)}
) > 0;
}`
),
t.classMethod(
"set",
element.node.key,
[t.identifier("v")],
bodyTemplate.ast`{
if (v) this.${t.cloneNode(storageName)} |= ${t.numericLiteral(nextMask)};
else this.${t.cloneNode(storageName)} &= ${t.valueToNode(~nextMask)};
}`
),
]);

nextMask <<= 1;
}
}

if (storageField) {
storageField.node.value = t.numericLiteral(initial);
}
},
},
};
}
Loading

0 comments on commit 3ff20b9

Please sign in to comment.