Skip to content

Commit

Permalink
Also use in @babel/traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 7, 2024
1 parent 8010d3f commit a38dff4
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 171 deletions.
117 changes: 2 additions & 115 deletions babel.config.js
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 All @@ -197,7 +199,6 @@ module.exports = function (api) {
plugins: [
"babel-plugin-transform-charcodes",
pluginBabelParserTokenType,
pluginBabelParserBitField,
],
assumptions: parserAssumptions,
},
Expand Down Expand Up @@ -1009,120 +1010,6 @@ function pluginBabelParserTokenType({
};
}

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

return {
manipulateOptions({ parserOpts }) {
parserOpts.plugins.push("decorators", "decoratorAutoAccessors");
},
visitor: {
Class(path) {
let storageName;
let initial = 0;
let nextMask = 1;
for (const element of path.get("body.body")) {
if (
element.isClassAccessorProperty() &&
element.node.decorators?.some(dec =>
t.isIdentifier(dec.expression, { 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 (nextMask === 0) {
// overflow
throw path.buildCodeFrameError(
"A class can contain at most 32 @bit decorators"
);
}

storageName ??= t.privateName(
path.scope.generateUidIdentifier("flags")
);

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 (storageName) {
path
.get("body")
.unshiftContainer(
"body",
t.classPrivateProperty(storageName, t.numericLiteral(initial))
);

path.traverse({
CallExpression(path) {
if (path.get("callee").isIdentifier({ name: "cloneBits" })) {
if (path.node.arguments.length !== 2) {
throw path.buildCodeFrameError(
"cloneBits expects two arguments"
);
}
path.replaceWith(
t.assignmentExpression(
"=",
t.memberExpression(
path.node.arguments[0],
t.cloneNode(storageName)
),
t.memberExpression(
path.node.arguments[1],
t.cloneNode(storageName)
)
)
);
}
},
});
}
},
},
};
}

// Inject `0 && exports.foo = 0` hints for the specified exports,
// to help the Node.js CJS-ESM interop. This is only
// needed when compiling ESM re-exports to CJS in `lazy` mode.
Expand Down
23 changes: 4 additions & 19 deletions packages/babel-parser/src/tokenizer/state.ts
Expand Up @@ -28,26 +28,11 @@ export const enum LoopLabelKind {
Switch = 2,
}

// This is observably a no-op, but we use it to statically pack
// multiple booleans into a single bitfield.
declare function bit(
target: ClassAccessorDecoratorTarget<State, boolean>,
context: ClassAccessorDecoratorContext<State, boolean> & {
private: false;
static: false;
},
): void; /*{
((context.metadata.bits as any[]) ??= []).push(context.name);
} */
// This function copies all properties marked with the `bit` decorator from
// `from` to `to`.
declare function cloneBits<T>(to: T, from: T): void; /* {
from.constructor[Symbol.metadata].bits?.forEach((name) => {
to[name] = from[name];
});
} */
declare const bit: import("../../../../scripts/babel-plugin-bit-decorator/types.d.ts").BitDecorator<State>;

export default class State {
@bit.storage #flags: number;

@bit accessor strict = false;

curLine: number;
Expand Down Expand Up @@ -180,7 +165,7 @@ export default class State {

clone(): State {
const state = new State();
cloneBits(state, this);
state.#flags = this.#flags;
state.curLine = this.curLine;
state.lineStart = this.lineStart;
state.startLoc = this.startLoc;
Expand Down
45 changes: 8 additions & 37 deletions packages/babel-traverse/src/path/index.ts
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
123 changes: 123 additions & 0 deletions scripts/babel-plugin-bit-decorator/plugin.cjs
@@ -0,0 +1,123 @@
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?.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
.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);
}
},
},
};
}

0 comments on commit a38dff4

Please sign in to comment.