mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
module pr 2 (#18266)
This commit is contained in:
@@ -52,7 +52,7 @@ function markVerbose(log: string) {
|
||||
|
||||
const mark = silent ? (log: string) => {} : markVerbose;
|
||||
|
||||
const { moduleList, nativeModuleIds, nativeModuleEnumToId, nativeModuleEnums, requireTransformer } =
|
||||
const { moduleList, nativeModuleIds, nativeModuleEnumToId, nativeModuleEnums, requireTransformer, nativeStartIndex } =
|
||||
createInternalModuleRegistry(BASE);
|
||||
globalThis.requireTransformer = requireTransformer;
|
||||
|
||||
@@ -78,7 +78,7 @@ async function retry(n, fn) {
|
||||
|
||||
// Preprocess builtins
|
||||
const bundledEntryPoints: string[] = [];
|
||||
for (let i = 0; i < moduleList.length; i++) {
|
||||
for (let i = 0; i < nativeStartIndex; i++) {
|
||||
try {
|
||||
let input = fs.readFileSync(path.join(BASE, moduleList[i]), "utf8");
|
||||
|
||||
@@ -295,7 +295,9 @@ mark("Bundle Functions");
|
||||
// This is a file with a single macro that is used in defining InternalModuleRegistry.h
|
||||
writeIfNotChanged(
|
||||
path.join(CODEGEN_DIR, "InternalModuleRegistry+numberOfModules.h"),
|
||||
`#define BUN_INTERNAL_MODULE_COUNT ${moduleList.length}\n`,
|
||||
`#define BUN_INTERNAL_MODULE_COUNT ${moduleList.length}
|
||||
#define BUN_NATIVE_MODULE_START_INDEX ${nativeStartIndex}
|
||||
`,
|
||||
);
|
||||
|
||||
// This code slice is used in InternalModuleRegistry.h for inlining the enum. I dont think we
|
||||
@@ -322,12 +324,15 @@ JSValue InternalModuleRegistry::createInternalModuleById(JSGlobalObject* globalO
|
||||
// JS internal modules
|
||||
${moduleList
|
||||
.map((id, n) => {
|
||||
return `case Field::${idToEnumName(id)}: {
|
||||
INTERNAL_MODULE_REGISTRY_GENERATE(globalObject, vm, "${idToPublicSpecifierOrEnumName(id)}"_s, ${JSON.stringify(
|
||||
const inner = n >= nativeStartIndex
|
||||
? `return generateNativeModule(globalObject, vm, generateNativeModule_${nativeModuleEnums[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);
|
||||
.replace(/[^a-zA-Z0-9]+/g, "/")}"_s);`
|
||||
return `case Field::${idToEnumName(id)}: {
|
||||
${inner}
|
||||
}`;
|
||||
})
|
||||
.join("\n ")}
|
||||
@@ -335,6 +340,7 @@ JSValue InternalModuleRegistry::createInternalModuleById(JSGlobalObject* globalO
|
||||
__builtin_unreachable();
|
||||
}
|
||||
}
|
||||
__builtin_unreachable();
|
||||
}
|
||||
`,
|
||||
);
|
||||
@@ -353,6 +359,7 @@ if (!debug) {
|
||||
namespace Bun {
|
||||
namespace InternalModuleRegistryConstants {
|
||||
${moduleList
|
||||
.slice(0, nativeStartIndex)
|
||||
.map((id, n) => {
|
||||
const out = outputs.get(id.slice(0, -3).replaceAll("/", path.sep));
|
||||
if (!out) {
|
||||
@@ -373,7 +380,7 @@ namespace InternalModuleRegistryConstants {
|
||||
|
||||
namespace Bun {
|
||||
namespace InternalModuleRegistryConstants {
|
||||
${moduleList.map((id, n) => `${declareASCIILiteral(`${idToEnumName(id)}Code`, "")}`).join("\n")}
|
||||
${moduleList.slice(0, nativeStartIndex).map((id, n) => `${declareASCIILiteral(`${idToEnumName(id)}Code`, "")}`).join("\n")}
|
||||
}
|
||||
}`,
|
||||
);
|
||||
@@ -392,7 +399,7 @@ pub const ResolvedSourceTag = enum(u32) {
|
||||
file = 4,
|
||||
esm = 5,
|
||||
json_for_object_loader = 6,
|
||||
/// Generate an object with "default" set to all the exports, including a "default" propert
|
||||
/// Generate an object with "default" set to all the exports, including a "default" property
|
||||
exports_object = 7,
|
||||
|
||||
/// Generate a module that only exports default the input JSValue
|
||||
@@ -400,10 +407,10 @@ pub const ResolvedSourceTag = enum(u32) {
|
||||
|
||||
// Built in modules are loaded through InternalModuleRegistry by numerical ID.
|
||||
// In this enum are represented as \`(1 << 9) & id\`
|
||||
${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},`)
|
||||
${moduleList.slice(0, nativeStartIndex).map((id, n) => ` @"${idToPublicSpecifierOrEnumName(id)}" = ${(1 << 9) | n},`).join("\n")}
|
||||
// Native modules come after the JS modules
|
||||
${Object.entries(nativeModuleEnumToId)
|
||||
.map(([id, n], i) => ` @"${moduleList[nativeStartIndex + i]}" = ${(1 << 9) | (n + nativeStartIndex)},`)
|
||||
.join("\n")}
|
||||
};
|
||||
`,
|
||||
@@ -425,13 +432,10 @@ writeIfNotChanged(
|
||||
// Built in modules are loaded through InternalModuleRegistry by numerical ID.
|
||||
// In this enum are represented as \`(1 << 9) & id\`
|
||||
InternalModuleRegistryFlag = 1 << 9,
|
||||
${moduleList.map((id, n) => ` ${idToEnumName(id)} = ${(1 << 9) | n},`).join("\n")}
|
||||
|
||||
// Native modules run through the same system, but with different underlying initializers.
|
||||
// They also have bit 10 set to differentiate them from JS builtins.
|
||||
NativeModuleFlag = (1 << 10) | (1 << 9),
|
||||
${moduleList.slice(0, nativeStartIndex).map((id, n) => ` ${idToEnumName(id)} = ${(1 << 9) | n},`).join("\n")}
|
||||
// Native modules come after the JS modules
|
||||
${Object.entries(nativeModuleEnumToId)
|
||||
.map(([id, n]) => ` ${id} = ${(1 << 10) | n},`)
|
||||
.map(([id, n], i) => ` ${id} = ${(1 << 9) | (i + nativeStartIndex)},`)
|
||||
.join("\n")}
|
||||
};
|
||||
|
||||
@@ -476,7 +480,7 @@ declare module "module" {
|
||||
|
||||
`;
|
||||
|
||||
for (let i = 0; i < moduleList.length; i++) {
|
||||
for (let i = 0; i < nativeStartIndex; i++) {
|
||||
const id = moduleList[i];
|
||||
const out = outputs.get(id.slice(0, -3).replaceAll("/", path.sep));
|
||||
if (!out) {
|
||||
@@ -512,12 +516,13 @@ if (!silent) {
|
||||
console.log(
|
||||
` %s kb`,
|
||||
Math.floor(
|
||||
(moduleList.reduce((a, b) => a + outputs.get(b.slice(0, -3).replaceAll("/", path.sep)).length, 0) +
|
||||
(moduleList.slice(0, nativeStartIndex).reduce((a, b) => a + outputs.get(b.slice(0, -3).replaceAll("/", path.sep)).length, 0) +
|
||||
globalThis.internalFunctionJSSize) /
|
||||
1000,
|
||||
),
|
||||
);
|
||||
console.log(` %s internal modules`, moduleList.length);
|
||||
console.log(` %s internal modules`, nativeStartIndex);
|
||||
console.log(` %s native modules`, Object.keys(nativeModuleIds).length);
|
||||
console.log(
|
||||
` %s internal functions across %s files`,
|
||||
globalThis.internalFunctionCount,
|
||||
|
||||
@@ -56,7 +56,7 @@ const std = @import("std");
|
||||
const bun = @import("root").bun;
|
||||
const JSC = bun.JSC;
|
||||
|
||||
fn ErrorBuilder(comptime code: Error, comptime fmt: [:0]const u8, Args: type) type {
|
||||
pub fn ErrorBuilder(comptime code: Error, comptime fmt: [:0]const u8, Args: type) type {
|
||||
return struct {
|
||||
globalThis: *JSC.JSGlobalObject,
|
||||
args: Args,
|
||||
@@ -88,6 +88,7 @@ for (let [code, constructor, name, ...other_constructors] of NodeErrors) {
|
||||
if (name == null) name = constructor.name;
|
||||
enumHeader += ` ${code} = ${i},\n`;
|
||||
listHeader += ` { JSC::ErrorType::${constructor.name}, "${name}"_s, "${code}"_s },\n`;
|
||||
zig += ` /// ${name}: ${code} (instanceof ${constructor.name})\n`;
|
||||
zig += ` ${code} = ${i},\n`;
|
||||
listForUsingNamespace += ` /// ${name}: ${code} (instanceof ${constructor.name})\n`;
|
||||
listForUsingNamespace += ` pub inline fn ${code}(globalThis: *JSC.JSGlobalObject, comptime fmt: [:0]const u8, args: anytype) ErrorBuilder(Error.${code}, fmt, @TypeOf(args)) {\n`;
|
||||
@@ -100,6 +101,7 @@ for (let [code, constructor, name, ...other_constructors] of NodeErrors) {
|
||||
if (name == null) name = con.name;
|
||||
enumHeader += ` ${code}_${con.name} = ${i},\n`;
|
||||
listHeader += ` { JSC::ErrorType::${con.name}, "${con.name}"_s, "${code}"_s },\n`;
|
||||
zig += ` /// ${name}: ${code} (instanceof ${con.name})\n`;
|
||||
zig += ` ${code}_${con.name} = ${i},\n`;
|
||||
listForUsingNamespace += ` /// ${name}: ${code} (instanceof ${con.name})\n`;
|
||||
listForUsingNamespace += ` pub inline fn ${code}_${con.name}(globalThis: *JSC.JSGlobalObject, comptime fmt: [:0]const u8, args: anytype) ErrorBuilder(Error.${code}_${con.name}, fmt, @TypeOf(args)) {\n`;
|
||||
|
||||
@@ -45,6 +45,13 @@ export function createInternalModuleRegistry(basedir: string) {
|
||||
nativeModuleEnumToId[processedEnumValue] = processedNumericId;
|
||||
}
|
||||
|
||||
const nativeStartIndex = moduleList.length;
|
||||
|
||||
for (const [id] of Object.entries(nativeModuleIds)) {
|
||||
moduleList.push(id);
|
||||
internalRegistry.set(id, moduleList.length - 1);
|
||||
}
|
||||
|
||||
if (nextNativeModuleId === 0) {
|
||||
throw new Error(
|
||||
"Could not find BUN_FOREACH_ESM_AND_CJS_NATIVE_MODULE in _NativeModule.h. Knowing native module IDs is a part of the codegen process.",
|
||||
@@ -55,18 +62,10 @@ export function createInternalModuleRegistry(basedir: string) {
|
||||
return `(__intrinsic__getInternalField(__intrinsic__internalModuleRegistry, ${id}) || __intrinsic__createInternalModuleById(${id}))`;
|
||||
}
|
||||
|
||||
function codegenRequireNativeModule(id: string) {
|
||||
return `(__intrinsic__requireNativeModule(${id.replace(/node:/, "")}))`;
|
||||
}
|
||||
|
||||
const requireTransformer = (specifier: string, from: string) => {
|
||||
const directMatch = internalRegistry.get(specifier);
|
||||
if (directMatch) return codegenRequireId(`${directMatch}/*${specifier}*/`);
|
||||
|
||||
if (specifier in nativeModuleIds) {
|
||||
return codegenRequireNativeModule(JSON.stringify(specifier));
|
||||
}
|
||||
|
||||
const relativeMatch =
|
||||
resolveSyncOrNull(specifier, path.join(basedir, path.dirname(from))) ?? resolveSyncOrNull(specifier, basedir);
|
||||
|
||||
@@ -93,5 +92,6 @@ export function createInternalModuleRegistry(basedir: string) {
|
||||
nativeModuleEnumToId,
|
||||
internalRegistry,
|
||||
moduleList,
|
||||
nativeStartIndex,
|
||||
} as const;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user