bun.ptr.Shared.Lazy fixes cppbind change (#22753)

shared lazy:

- cloneWeak didn't incrementWeak. fixed
- exposes a public Optional so you can do `bun.ptr.Shared(*T).Optional`
- the doc comment for 'take' said it set self to null. but it did not.
fixed.
- upgrading a weak to a strong incremented the weak instead of
decrementing it. fixed.
- adds a new method unsafeGetStrongFromPointer. this is currently unused
but used in pfg/describe-2:

a690faa60a/src/bun.js/api/Timer/EventLoopTimer.zig (L220-L223)

cppbind:

- moves the bindings to the root of the file at the top and puts raw at
the bottom
- fixes false_is_throw to return void instead of bool
- updates the help message

---------

Co-authored-by: taylor.fish <contact@taylor.fish>
This commit is contained in:
pfg
2025-09-23 17:10:08 -07:00
committed by GitHub
parent 85271f9dd9
commit 144c45229e
2 changed files with 32 additions and 32 deletions

View File

@@ -471,7 +471,7 @@ function generateZigParameterList(parameters: CppParameter[], globalThisArg?: Cp
function generateZigSourceComment(cfg: Cfg, resultSourceLinks: string[], fn: CppFn): string {
const fileName = relative(cfg.dstDir, fn.position.file);
resultSourceLinks.push(`${fn.name}:${fileName}:${fn.position.start.line}:${fn.position.start.column}`);
return ` /// Source: ${fn.name}`;
return `/// Source: ${fn.name}`;
}
function closest(node: SyntaxNode | null, type: string): SyntaxNode | null {
@@ -644,7 +644,7 @@ function generateZigFn(
resultBindings.push(generateZigSourceComment(cfg, resultSourceLinks, fn));
if (fn.tag === "nothrow") {
resultBindings.push(
` pub extern fn ${formatZigName(fn.name)}(${generateZigParameterList(fn.parameters)}) ${returnType};`,
`pub extern fn ${formatZigName(fn.name)}(${generateZigParameterList(fn.parameters)}) ${returnType};`,
);
return;
}
@@ -667,21 +667,21 @@ function generateZigFn(
);
}
resultBindings.push(
` pub inline fn ${formatZigName(fn.name)}(${generateZigParameterList(fn.parameters, globalThisArg)}) bun.JSError!${returnType} {`,
` if (comptime Environment.ci_assert) {`,
` var scope: jsc.CatchScope = undefined;`,
` scope.init(${formatZigName(globalThisArg.name)}, @src());`,
` defer scope.deinit();`,
`pub inline fn ${formatZigName(fn.name)}(${generateZigParameterList(fn.parameters, globalThisArg)}) bun.JSError!${returnType} {`,
` if (comptime Environment.ci_assert) {`,
` var scope: jsc.CatchScope = undefined;`,
` scope.init(${formatZigName(globalThisArg.name)}, @src());`,
` defer scope.deinit();`,
``,
` const result = raw.${formatZigName(fn.name)}(${fn.parameters.map(p => formatZigName(p.name)).join(", ")});`,
` try scope.returnIfException();`,
` return result;`,
` } else {`,
` const result = raw.${formatZigName(fn.name)}(${fn.parameters.map(p => formatZigName(p.name)).join(", ")});`,
` if (Bun__RETURN_IF_EXCEPTION(${formatZigName(globalThisArg.name)})) return error.JSError;`,
` return result;`,
` }`,
` const result = raw.${formatZigName(fn.name)}(${fn.parameters.map(p => formatZigName(p.name)).join(", ")});`,
` try scope.returnIfException();`,
` return result;`,
` } else {`,
` const result = raw.${formatZigName(fn.name)}(${fn.parameters.map(p => formatZigName(p.name)).join(", ")});`,
` if (Bun__RETURN_IF_EXCEPTION(${formatZigName(globalThisArg.name)})) return error.JSError;`,
` return result;`,
` }`,
`}`,
);
return;
}
@@ -699,21 +699,21 @@ function generateZigFn(
}
} else assertNever(fn.tag);
resultBindings.push(
` pub inline fn ${formatZigName(fn.name)}(${generateZigParameterList(fn.parameters, globalThisArg)}) bun.JSError!${returnType} {`,
` if (comptime Environment.ci_assert) {`,
` var scope: jsc.ExceptionValidationScope = undefined;`,
` scope.init(${formatZigName(globalThisArg.name)}, @src());`,
` defer scope.deinit();`,
`pub inline fn ${formatZigName(fn.name)}(${generateZigParameterList(fn.parameters, globalThisArg)}) bun.JSError!${fn.tag === "false_is_throw" ? "void" : returnType} {`,
` if (comptime Environment.ci_assert) {`,
` var scope: jsc.ExceptionValidationScope = undefined;`,
` scope.init(${formatZigName(globalThisArg.name)}, @src());`,
` defer scope.deinit();`,
``,
` const value = raw.${formatZigName(fn.name)}(${fn.parameters.map(p => formatZigName(p.name)).join(", ")});`,
` scope.assertExceptionPresenceMatches(value == ${equalsValue});`,
` return if (value == ${equalsValue}) error.JSError else value;`,
` } else {`,
` const value = raw.${formatZigName(fn.name)}(${fn.parameters.map(p => formatZigName(p.name)).join(", ")});`,
` if (value == ${equalsValue}) return error.JSError;`,
` return value;`,
` }`,
` const value = raw.${formatZigName(fn.name)}(${fn.parameters.map(p => formatZigName(p.name)).join(", ")});`,
` scope.assertExceptionPresenceMatches(value == ${equalsValue});`,
` return if (value == ${equalsValue}) error.JSError ${fn.tag === "false_is_throw" ? "" : "else value"};`,
` } else {`,
` const value = raw.${formatZigName(fn.name)}(${fn.parameters.map(p => formatZigName(p.name)).join(", ")});`,
` if (value == ${equalsValue}) return error.JSError;`,
...(fn.tag === "false_is_throw" ? [] : [` return value;`]),
` }`,
`}`,
);
return;
}
@@ -783,10 +783,10 @@ async function main() {
const resultFilePath = join(dstDir, "cpp.zig");
const resultContents =
typeDeclarations +
"\nconst raw = struct {\n" +
resultRaw.join("\n") +
"\n};\n\npub const bindings = struct {\n" +
"\n" +
resultBindings.join("\n") +
"\n\nconst raw = struct {\n" +
resultRaw.join("\n") +
"\n};\n";
if ((await readFileOrEmpty(resultFilePath)) !== resultContents) {
await Bun.write(resultFilePath, resultContents);