Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
paperclover committed Jan 29, 2024
1 parent 4fd6f25 commit 5cc7542
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 52 deletions.
22 changes: 12 additions & 10 deletions src/codegen/bundle-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ async function processFileSplit(filename: string): Promise<{ functions: BundledB
// do not allow the bundler to rename a symbol to $
($);
$$capture_start$$(${fn.async ? "async " : ""}${useThis
? `function(${fn.params.join(",")})`
: `${fn.params.length === 1 ? fn.params[0] : `(${fn.params.join(",")})`}=>`
$$capture_start$$(${fn.async ? "async " : ""}${
useThis
? `function(${fn.params.join(",")})`
: `${fn.params.length === 1 ? fn.params[0] : `(${fn.params.join(",")})`}=>`
} {${fn.source}}).$$capture_end$$;
`,
);
Expand All @@ -198,11 +199,11 @@ $$capture_start$$(${fn.async ? "async " : ""}${useThis
(fn.directives.sloppy
? captured
: captured.replace(
/function\s*\(.*?\)\s*{/,
'$&"use strict";' +
(usesDebug ? createLogClientJS("BUILTINS", fn.name) : "") +
(usesAssert ? createAssertClientJS(fn.name) : ""),
)
/function\s*\(.*?\)\s*{/,
'$&"use strict";' +
(usesDebug ? createLogClientJS("BUILTINS", fn.name) : "") +
(usesAssert ? createAssertClientJS(fn.name) : ""),
)
)
.replace(/^\((async )?function\(/, "($1function (")
.replace(/__intrinsic__/g, "@")
Expand Down Expand Up @@ -282,8 +283,9 @@ for (const { basename, functions } of files) {
const name = `${lowerBasename}${cap(fn.name)}Code`;
bundledCPP += `// ${fn.name}
const JSC::ConstructAbility s_${name}ConstructAbility = JSC::ConstructAbility::${fn.constructAbility};
const JSC::InlineAttribute s_${name}InlineAttribute = JSC::InlineAttribute::${fn.directives.alwaysInline ? "Always" : "None"
};
const JSC::InlineAttribute s_${name}InlineAttribute = JSC::InlineAttribute::${
fn.directives.alwaysInline ? "Always" : "None"
};
const JSC::ConstructorKind s_${name}ConstructorKind = JSC::ConstructorKind::${fn.constructKind};
const JSC::ImplementationVisibility s_${name}ImplementationVisibility = JSC::ImplementationVisibility::${fn.visibility};
const int s_${name}Length = ${fn.source.length};
Expand Down
55 changes: 28 additions & 27 deletions src/codegen/bundle-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const {
// work, so i have lot of debug logs that blow up the console because not sure what is going on.
// that is also the reason for using `retry` when theoretically writing a file the first time
// should actually write the file.
const verbose = Bun.env.VERBOSE ? console.log : () => { };
const verbose = Bun.env.VERBOSE ? console.log : () => {};
async function retry(n, fn) {
var err;
while (n > 0) {
Expand Down Expand Up @@ -87,12 +87,12 @@ for (let i = 0; i < moduleList.length; i++) {

const processed = sliceSourceCode(
"{" +
input
.replace(
/\bimport(\s*type)?\s*(\{[^}]*\}|(\*\s*as)?\s[a-zA-Z0-9_$]+)\s*from\s*['"][^'"]+['"]/g,
stmt => (importStatements.push(stmt), ""),
)
.replace(/export\s*{\s*}\s*;/g, ""),
input
.replace(
/\bimport(\s*type)?\s*(\{[^}]*\}|(\*\s*as)?\s[a-zA-Z0-9_$]+)\s*from\s*['"][^'"]+['"]/g,
stmt => (importStatements.push(stmt), ""),
)
.replace(/export\s*{\s*}\s*;/g, ""),
true,
x => requireTransformer(x, moduleList[i]),
);
Expand Down Expand Up @@ -237,13 +237,13 @@ for (const entrypoint of bundledEntryPoints) {
captured = captured.replace(
/function\s*\(.*?\)\s*{/,
'$&"use strict";' +
(usesDebug
? createLogClientJS(
file_path.replace(".js", ""),
idToPublicSpecifierOrEnumName(file_path).replace(/^node:|^bun:/, ""),
)
: "") +
(usesAssert ? createAssertClientJS(idToPublicSpecifierOrEnumName(file_path).replace(/^node:|^bun:/, "")) : ""),
(usesDebug
? createLogClientJS(
file_path.replace(".js", ""),
idToPublicSpecifierOrEnumName(file_path).replace(/^node:|^bun:/, ""),
)
: "") +
(usesAssert ? createAssertClientJS(idToPublicSpecifierOrEnumName(file_path).replace(/^node:|^bun:/, "")) : ""),
);
const outputPath = path.join(JS_DIR, file_path);
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
Expand Down Expand Up @@ -286,11 +286,12 @@ writeIfNotChanged(
// actually use this enum but it's probably a good thing to include.
writeIfNotChanged(
path.join(CODEGEN_DIR, "InternalModuleRegistry+enum.h"),
`${moduleList
.map((id, n) => {
return `${idToEnumName(id)} = ${n},`;
})
.join("\n") + "\n"
`${
moduleList
.map((id, n) => {
return `${idToEnumName(id)} = ${n},`;
})
.join("\n") + "\n"
}
`,
);
Expand All @@ -304,16 +305,16 @@ JSValue InternalModuleRegistry::createInternalModuleById(JSGlobalObject* globalO
switch (id) {
// JS internal modules
${moduleList
.map((id, n) => {
return `case Field::${idToEnumName(id)}: {
.map((id, n) => {
return `case Field::${idToEnumName(id)}: {
INTERNAL_MODULE_REGISTRY_GENERATE(globalObject, vm, "${idToPublicSpecifierOrEnumName(id)}"_s, ${JSON.stringify(
id.replace(/\.[mc]?[tj]s$/, ".js"),
)}_s, InternalModuleRegistryConstants::${idToEnumName(id)}Code, "builtin://${id
.replace(/\.[mc]?[tj]s$/, "")
.replace(/[^a-zA-Z0-9]+/g, "/")}"_s);
}`;
})
.join("\n ")}
})
.join("\n ")}
default: {
__builtin_unreachable();
}
Expand Down Expand Up @@ -373,8 +374,8 @@ pub const ResolvedSourceTag = enum(u32) {
${moduleList.map((id, n) => ` @"${idToPublicSpecifierOrEnumName(id)}" = ${(1 << 9) | n},`).join("\n")}
// Native modules run through a different system using ESM registry.
${Object.entries(nativeModuleIds)
.map(([id, n]) => ` @"${id}" = ${(1 << 10) | n},`)
.join("\n")}
.map(([id, n]) => ` @"${id}" = ${(1 << 10) | n},`)
.join("\n")}
};
`,
);
Expand All @@ -400,8 +401,8 @@ ${moduleList.map((id, n) => ` ${idToEnumName(id)} = ${(1 << 9) | n},`).join("
// They also have bit 10 set to differentiate them from JS builtins.
NativeModuleFlag = (1 << 10) | (1 << 9),
${Object.entries(nativeModuleEnumToId)
.map(([id, n]) => ` ${id} = ${(1 << 10) | n},`)
.join("\n")}
.map(([id, n]) => ` ${id} = ${(1 << 10) | n},`)
.join("\n")}
};
`,
Expand Down
29 changes: 16 additions & 13 deletions src/codegen/replacements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export const globalsToPrefix = [
"undefined",
];

replacements.push({ from: new RegExp(`\\bextends\\s+(${globalsToPrefix.join("|")})`, "g"), to: "extends __no_intrinsic__%1" });
replacements.push({
from: new RegExp(`\\bextends\\s+(${globalsToPrefix.join("|")})`, "g"),
to: "extends __no_intrinsic__%1",
});

// These enums map to $<enum>IdToLabel and $<enum>LabelToId
// Make sure to define in ./builtins.d.ts
Expand Down Expand Up @@ -133,7 +136,7 @@ export function applyReplacements(src: string, length: number) {
let rest = src.slice(length);
slice = slice.replace(/([^a-zA-Z0-9_\$])\$([a-zA-Z0-9_]+\b)/gm, `$1__intrinsic__$2`);
for (const replacement of replacements) {
slice = slice.replace(replacement.from, replacement.to.replaceAll("$", "__intrinsic__").replaceAll('%', '$'));
slice = slice.replace(replacement.from, replacement.to.replaceAll("$", "__intrinsic__").replaceAll("%", "$"));
}
let match;
if ((match = slice.match(/__intrinsic__(debug|assert)$/)) && rest.startsWith("(")) {
Expand All @@ -156,17 +159,17 @@ export function applyReplacements(src: string, length: number) {
}
return [
slice.slice(0, match.index) +
"(IS_BUN_DEVELOPMENT?$assert(" +
checkSlice.result.slice(1, -1) +
"," +
JSON.stringify(
checkSlice.result
.slice(1, -1)
.replace(/__intrinsic__/g, "$")
.trim(),
) +
extraArgs +
"):void 0)",
"(IS_BUN_DEVELOPMENT?$assert(" +
checkSlice.result.slice(1, -1) +
"," +
JSON.stringify(
checkSlice.result
.slice(1, -1)
.replace(/__intrinsic__/g, "$")
.trim(),
) +
extraArgs +
"):void 0)",
rest2,
true,
];
Expand Down
4 changes: 2 additions & 2 deletions src/js/private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type BunFS = Omit<typeof import("node:fs") & typeof import("node:fs/promises"),
filename: string,
options:
| (WatchOptions & {
encoding: "buffer";
})
encoding: "buffer";
})
| "buffer",
listener?: BunWatchListener<Buffer>,
): BunFSWatcher;
Expand Down

0 comments on commit 5cc7542

Please sign in to comment.