Files
bun.sh/src/codegen/helpers.ts
taylor.fish f14f3b03bb Add new bindings generator; port SSLConfig (#23169)
Add a new generator for JS → Zig bindings. The bulk of the conversion is
done in C++, after which the data is transformed into an FFI-safe
representation, passed to Zig, and then finally transformed into
idiomatic Zig types.

In its current form, the new bindings generator supports:

* Signed and unsigned integers
* Floats (plus a “finite” variant that disallows NaN and infinities)
* Strings
* ArrayBuffer (accepts ArrayBuffer, TypedArray, or DataView)
* Blob
* Optional types
* Nullable types (allows null, whereas Optional only allows undefined)
* Arrays
* User-defined string enumerations
* User-defined unions (fields can optionally be named to provide a
better experience in Zig)
* Null and undefined, for use in unions (can more efficiently represent
optional/nullable unions than wrapping a union in an optional)
* User-defined dictionaries (arbitrary key-value pairs; expects a JS
object and parses it into a struct)
* Default values for dictionary members
* Alternative names for dictionary members (e.g., to support both
`serverName` and `servername` without taking up twice the space)
* Descriptive error messages
* Automatic `fromJS` functions in Zig for dictionaries
* Automatic `deinit` functions for the generated Zig types

Although this bindings generator has many features not present in
`bindgen.ts`, it does not yet implement all of `bindgen.ts`'s
functionality, so for the time being, it has been named `bindgenv2`, and
its configuration is specified in `.bindv2.ts` files. Once all
`bindgen.ts`'s functionality has been incorporated, it will be renamed.

This PR ports `SSLConfig` to use the new bindings generator; see
`SSLConfig.bindv2.ts`.

(For internal tracking: fixes STAB-1319, STAB-1322, STAB-1323,
STAB-1324)

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Alistair Smith <hi@alistair.sh>
2025-10-03 17:10:28 -07:00

161 lines
4.4 KiB
TypeScript

import { isAscii } from "buffer";
import fs from "fs";
import path from "path";
// MSVC has a max of 16k characters per string literal
// Combining string literals didn't support constexpr apparently
// so we have to do this the gigantic array way
export function fmtCPPCharArray(str: string, nullTerminated: boolean = true) {
const normalized = str + "\n";
var remain = normalized;
const chars =
"{" +
remain
.split("")
.map(a => a.charCodeAt(0))
.join(",") +
(nullTerminated ? ",0" : "") +
"}";
return [chars, normalized.length + (nullTerminated ? 1 : 0)] as const;
}
export function addCPPCharArray(str: string, nullTerminated: boolean = true) {
const normalized = str.trim() + "\n";
return (
normalized
.split("")
.map(a => a.charCodeAt(0))
.join(",") + (nullTerminated ? ",0" : "")
);
}
export function declareASCIILiteral(name: string, value: string) {
const [chars, count] = fmtCPPCharArray(value, true);
return `static constexpr const char ${name}Bytes[${count}] = ${chars};
static constexpr ASCIILiteral ${name} = ASCIILiteral::fromLiteralUnsafe(${name}Bytes);`;
}
export function cap(str: string) {
return str[0].toUpperCase() + str.slice(1);
}
export function low(str: string) {
if (str.startsWith("JS")) {
return "js" + str.slice(2);
}
return str[0].toLowerCase() + str.slice(1);
}
export function readdirRecursive(root: string): string[] {
const files = fs.readdirSync(root, { withFileTypes: true });
return files.flatMap(file => {
const fullPath = path.join(root, file.name);
return file.isDirectory() ? readdirRecursive(fullPath) : fullPath;
});
}
export function resolveSyncOrNull(specifier: string, from: string) {
try {
return Bun.resolveSync(specifier, from);
} catch {
return null;
}
}
export function checkAscii(str: string) {
if (!isAscii(Buffer.from(str))) {
throw new Error(`non-ascii character in string "${str}". this will not be a valid ASCIILiteral`);
}
return str;
}
export function writeIfNotChanged(file: string, contents: string) {
if (Array.isArray(contents)) contents = contents.join("");
contents = contents.replaceAll("\r\n", "\n").trim() + "\n";
try {
const oldContents = fs.readFileSync(file, "utf8");
if (oldContents === contents) {
return;
}
} catch (e) {}
try {
fs.writeFileSync(file, contents);
} catch (error) {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, contents);
}
if (fs.readFileSync(file, "utf8") !== contents) {
throw new Error(`Failed to write file ${file}`);
}
}
export function readdirRecursiveWithExclusionsAndExtensionsSync(
dir: string,
exclusions: string[],
exts: string[],
): string[] {
const entries = fs.readdirSync(dir, { withFileTypes: true });
return entries.flatMap(entry => {
if (exclusions.includes(entry.name)) return [];
const fullPath = path.join(dir, entry.name);
return entry.isDirectory()
? readdirRecursiveWithExclusionsAndExtensionsSync(fullPath, exclusions, exts)
: exts.some(ext => fullPath.endsWith(ext))
? fullPath
: [];
});
}
export function pathToUpperSnakeCase(filepath: string) {
return filepath
.replace(/^.*?:/, "")
.split(/[-_./\\]/g)
.join("_")
.toUpperCase();
}
export function camelCase(string: string) {
return string
.split(/[\s_]/)
.map((e, i) => (i ? e.charAt(0).toUpperCase() + e.slice(1).toLowerCase() : e.toLowerCase()));
}
export function pascalCase(string: string) {
return string.split(/[\s_]/).map((e, i) => (i ? e.charAt(0).toUpperCase() + e.slice(1) : e.toLowerCase()));
}
export function argParse(keys: string[]): any {
const options: { [key: string]: boolean | string } = {};
for (const arg of process.argv.slice(2)) {
if (!arg.startsWith("--")) {
console.error("error: unknown argument: " + arg);
process.exit(1);
}
const splitPos = arg.indexOf("=");
let name = arg;
let value: boolean | string = true;
if (splitPos !== -1) {
name = arg.slice(0, splitPos);
value = arg.slice(splitPos + 1);
}
options[name.slice(2)] = value;
}
const unknown = new Set(Object.keys(options));
for (const key of keys) {
unknown.delete(key);
}
for (const key of unknown) {
console.error("error: unknown argument: --" + key);
}
if (unknown.size > 0) process.exit(1);
return options;
}