Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions scripts/build/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,9 @@ function emitErrorCode({ n, cfg, o, dirStamp }: Ctx): void {
resolve(cfg.cwd, "src", "jsc", "bindings", "ErrorCode.h"),
];

const outputs = [resolve(cfg.codegenDir, "ErrorCode+List.h"), resolve(cfg.codegenDir, "ErrorCode+Data.h")];
const cppOutputs = [resolve(cfg.codegenDir, "ErrorCode+List.h"), resolve(cfg.codegenDir, "ErrorCode+Data.h")];
const rustOutput = resolve(cfg.codegenDir, "ErrorCode.generated.rs");
const outputs = [...cppOutputs, rustOutput];

n.build({
outputs,
Expand All @@ -586,7 +588,7 @@ function emitErrorCode({ n, cfg, o, dirStamp }: Ctx): void {

o.all.push(...outputs);
o.rustInputs.push(...outputs);
o.cppHeaders.push(outputs[0]!, outputs[1]!);
o.cppHeaders.push(...cppOutputs);
}

function emitGeneratedClasses({ n, cfg, sources, o, dirStamp }: Ctx): void {
Expand Down
38 changes: 38 additions & 0 deletions src/codegen/generate-node-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ if (count > 1 << 16) {

let enumHeader = ``;
let listHeader = ``;
let rustConsts = ``;
let rustAliases = ``;
let rustCodeStr = ``;

enumHeader = `
// clang-format off
Expand Down Expand Up @@ -57,13 +60,22 @@ for (let [code, constructor, name, ...other_constructors] of NodeErrors) {

enumHeader += ` ${code} = ${i},\n`;
listHeader += ` { JSC::ErrorType::${constructor.name}, "${name}"_s, "${code}"_s },\n`;
rustConsts += ` /// \`${code}\` (instanceof ${constructor.name})\n`;
rustConsts += ` pub const ${codeWithoutPrefix}: ErrorCode = ErrorCode(${i});\n`;
if (codeWithoutPrefix !== code) {
rustAliases += ` pub const ${code}: ErrorCode = ErrorCode::${codeWithoutPrefix};\n`;
}
rustCodeStr += ` "${code}",\n`;
i++;

for (const con of other_constructors) {
if (con == null) continue;
if (name == null) name = con.name;
enumHeader += ` ${code}_${con.name} = ${i},\n`;
listHeader += ` { JSC::ErrorType::${con.name}, "${con.name}"_s, "${code}"_s },\n`;
rustConsts += ` /// \`${code}\` (instanceof ${con.name})\n`;
rustConsts += ` pub const ${codeWithoutPrefix}_${con.name}: ErrorCode = ErrorCode(${i});\n`;
rustCodeStr += ` "${code}",\n`;
i++;
}
}
Expand All @@ -77,6 +89,31 @@ listHeader += `
};
`;

const rustSource = `\
// Generated by: src/codegen/generate-node-errors.ts
// Input: src/jsc/bindings/ErrorCode.ts
//
// Mirrors C++ \`Bun::ErrorCode\` in \`ErrorCode+List.h\`. Discriminants are
// index-aligned with the C++ \`errors[]\` table so \`Bun__createErrorWithCode\`
// picks the correct ctor / name / code triple.

impl ErrorCode {
${rustConsts}
/// == C++ \`NODE_ERROR_COUNT\`.
pub const COUNT: u16 = ${count};
}

// \`ERR_\`-prefixed aliases — some callers spell the full Node code string,
// some use the stripped name. Both resolve to the same discriminant.
impl ErrorCode {
${rustAliases}}

/// \`error.code\` string table — index-aligned with the consts above and with
/// C++ \`errors[].code\` in \`ErrorCode+Data.h\`.
static CODE_STR: [&str; ErrorCode::COUNT as usize] = [
${rustCodeStr}];
`;

let builtindtsPath = path.join(import.meta.dir, "..", "..", "src", "js", "builtins.d.ts");
let builtindts = await Bun.file(builtindtsPath).text();

Expand Down Expand Up @@ -108,3 +145,4 @@ declare function $${code}(message: string): ${namedError};\n`;
writeIfNotChanged(path.join(outputDir, "ErrorCode+List.h"), enumHeader);
writeIfNotChanged(path.join(outputDir, "ErrorCode+Data.h"), listHeader);
writeIfNotChanged(path.join(outputDir, "ErrorCode.d.ts"), dts);
writeIfNotChanged(path.join(outputDir, "ErrorCode.generated.rs"), rustSource);
Loading
Loading