fix more node:stream (#16385)

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
Meghan Denny
2025-01-16 22:40:39 -08:00
committed by GitHub
parent 6cdcb1c867
commit 2d481e7bcb
143 changed files with 14248 additions and 6394 deletions

View File

@@ -19,6 +19,7 @@ import { getJS2NativeCPP, getJS2NativeZig } from "./generate-js2native";
import { cap, declareASCIILiteral, writeIfNotChanged } from "./helpers";
import { createInternalModuleRegistry } from "./internal-module-registry-scanner";
import { define } from "./replacements";
import jsclasses from "./../bun.js/bindings/js_classes";
const BASE = path.join(import.meta.dir, "../js");
const debug = process.argv[2] === "--debug=ON";
@@ -457,16 +458,33 @@ writeIfNotChanged(
`;
for (let i = 0; i < ErrorCode.length; i++) {
const [code, _, name] = ErrorCode[i];
const [code, constructor, name, ...other_constructors] = ErrorCode[i];
dts += `
/**
* Generate a ${name} error with the \`code\` property set to ${code}.
* Generate a ${name ?? constructor.name} error with the \`code\` property set to ${code}.
*
* @param msg The error message
* @param args Additional arguments
*/
declare function $${code}(msg: string, ...args: any[]): ${name};
`;
for (const con of other_constructors) {
if (con == null) continue;
dts += `
/**
* Generate a ${con.name} error with the \`code\` property set to ${code}.
*
* @param msg The error message
* @param args Additional arguments
*/
declare function $${code}_${con.name}(msg: string, ...args: any[]): ${name};
`;
}
}
for (const [name] of jsclasses) {
dts += `\ndeclare function $inherits${name}(value: any): boolean;`;
}
return dts;

View File

@@ -2,6 +2,7 @@
import path from "path";
import type { ClassDefinition, Field } from "./class-definitions";
import { camelCase, pascalCase, writeIfNotChanged } from "./helpers";
import jsclasses from "./../bun.js/bindings/js_classes";
if (process.env.BUN_SILENT === "1") {
console.log = () => {};
@@ -910,6 +911,7 @@ function renderStaticDecls(symbolName, typeName, fields, supportsObjectCreate =
return rows.join("\n");
}
function writeBarrier(symbolName, typeName, name, cacheName) {
return `
@@ -928,6 +930,7 @@ extern JSC_CALLCONV JSC::EncodedJSValue ${symbolName(typeName, name)}GetCachedVa
`.trim();
}
function renderFieldsImpl(
symbolName: (typeName: string, name: string) => string,
typeName: string,
@@ -1176,6 +1179,7 @@ JSC_DEFINE_HOST_FUNCTION(${symbolName(typeName, name)}Callback, (JSGlobalObject
return rows.map(a => a.trim()).join("\n");
}
function allCachedValues(obj: ClassDefinition) {
let values = (obj.values ?? []).slice().map(name => [name, `m_${name}`]);
for (const name in obj.proto) {
@@ -1193,6 +1197,7 @@ function allCachedValues(obj: ClassDefinition) {
return values;
}
var extraIncludes = [];
function generateClassHeader(typeName, obj: ClassDefinition) {
var { klass, proto, JSType = "ObjectType", values = [], callbacks = {}, zigOnly = false } = obj;
@@ -2101,6 +2106,9 @@ const GENERATED_CLASSES_HEADER = [
#include "root.h"
namespace Zig {
JSC_DECLARE_HOST_FUNCTION(jsFunctionInherits);
}
#include "JSDOMWrapper.h"
@@ -2168,6 +2176,30 @@ const GENERATED_CLASSES_IMPL_FOOTER = `
`;
function jsInheritsCppImpl() {
return `
${jsclasses
.map(v => v[1])
.filter(v => v?.length > 0)
.map((v, i) => `#include "${v}"`)
.join("\n")}
JSC_DEFINE_HOST_FUNCTION(Zig::jsFunctionInherits, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
auto id = callFrame->argument(0).toInt32(globalObject);
auto value = callFrame->argument(1);
if (!value.isCell()) return JSValue::encode(jsBoolean(false));
auto cell = value.asCell();
switch (id) {
${jsclasses
.map(v => v[0])
.map((v, i) => ` case ${i}: return JSValue::encode(jsBoolean(cell->inherits<WebCore::JS${v}>()));`)
.join("\n")}
}
return JSValue::encode(jsBoolean(false));
}`;
}
function initLazyClasses(initLaterFunctions) {
return `
@@ -2342,6 +2374,7 @@ comptime {
`,
]);
if (!process.env.ONLY_ZIG) {
const allHeaders = classes.map(a => generateHeader(a.name, a));
await writeIfNotChanged(`${outBase}/ZigGeneratedClasses.h`, [
@@ -2360,7 +2393,9 @@ if (!process.env.ONLY_ZIG) {
allImpls.join("\n"),
writeCppSerializers(classes),
GENERATED_CLASSES_IMPL_FOOTER,
jsInheritsCppImpl(),
]);
await writeIfNotChanged(
`${outBase}/ZigGeneratedClasses+lazyStructureHeader.h`,
classes.map(a => generateLazyClassStructureHeader(a.name, a)).join("\n"),

View File

@@ -6,6 +6,18 @@ if (!outputDir) {
throw new Error("Missing output directory");
}
const extra_count = NodeErrors.map(x => x.slice(3))
.filter(x => x.length > 0)
.reduce((ac, cv) => ac + cv.length, 0);
const count = NodeErrors.length + extra_count;
if (count > 256) {
// increase size of enum's to have more tags
// src/bun.js/node/types.zig#Encoding
// src/bun.js/bindings/BufferEncodingType.h
throw new Error("NodeError count exceeds u8");
}
let enumHeader = ``;
let listHeader = ``;
let zig = ``;
@@ -18,7 +30,7 @@ enumHeader = `
#include <cstdint>
namespace Bun {
static constexpr size_t NODE_ERROR_COUNT = ${NodeErrors.length};
static constexpr size_t NODE_ERROR_COUNT = ${count};
enum class ErrorCode : uint8_t {
`;
@@ -34,7 +46,7 @@ struct ErrorCodeData {
WTF::ASCIILiteral name;
WTF::ASCIILiteral code;
};
static constexpr ErrorCodeData errors[${NodeErrors.length}] = {
static constexpr ErrorCodeData errors[${count}] = {
`;
zig = `
@@ -71,7 +83,7 @@ pub const Error = enum(u8) {
let i = 0;
let listForUsingNamespace = "";
for (let [code, constructor, name] of NodeErrors) {
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`;
@@ -81,6 +93,19 @@ for (let [code, constructor, name] of NodeErrors) {
listForUsingNamespace += ` return .{ .globalThis = globalThis, .args = args };\n`;
listForUsingNamespace += ` }\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`;
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`;
listForUsingNamespace += ` return .{ .globalThis = globalThis, .args = args };\n`;
listForUsingNamespace += ` }\n`;
i++;
}
}
enumHeader += `

View File

@@ -2,6 +2,7 @@ import { LoaderKeys } from "../api/schema";
import NodeErrors from "../bun.js/bindings/ErrorCode.ts";
import { sliceSourceCode } from "./builtin-parser";
import { registerNativeCall } from "./generate-js2native";
import jsclasses from "./../bun.js/bindings/js_classes";
// This is a list of extra syntax replacements to do. Kind of like macros
// These are only run on code itself, not string contents or comments.
@@ -13,11 +14,29 @@ export const replacements: ReplacementRule[] = [
{ from: /\bexport\s*default/g, to: "$exports =" },
];
let error_i = 0;
for (let i = 0; i < NodeErrors.length; i++) {
const [code] = NodeErrors[i];
const [code, _constructor, _name, ...other_constructors] = NodeErrors[i];
replacements.push({
from: new RegExp(`\\b\\__intrinsic__${code}\\(`, "g"),
to: `$makeErrorWithCode(${i}, `,
to: `$makeErrorWithCode(${error_i}, `,
});
error_i += 1;
for (const con of other_constructors) {
if (con == null) continue;
replacements.push({
from: new RegExp(`\\b\\__intrinsic__${code}_${con.name}\\(`, "g"),
to: `$makeErrorWithCode(${error_i}, `,
});
error_i += 1;
}
}
for (let id = 0; id < jsclasses.length; id++) {
const name = jsclasses[id][0];
replacements.push({
from: new RegExp(`\\b\\__intrinsic__inherits${name}\\(`, "g"),
to: `$inherits(${id}, `,
});
}