mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
[bindings] Add custom field to enable writing one-off C++ in the zig class
This commit is contained in:
@@ -447,7 +447,7 @@ function generateConstructorHeader(typeName) {
|
||||
`;
|
||||
}
|
||||
|
||||
function generateConstructorImpl(typeName, obj) {
|
||||
function generateConstructorImpl(typeName, obj: ClassDefinition) {
|
||||
const name = constructorName(typeName);
|
||||
const { klass: fields } = obj;
|
||||
const hashTable =
|
||||
@@ -890,6 +890,7 @@ JSC_DEFINE_CUSTOM_SETTER(${symbolName(
|
||||
return rows.join("\n");
|
||||
}
|
||||
|
||||
var extraIncludes = [];
|
||||
function generateClassHeader(typeName, obj: ClassDefinition) {
|
||||
var { klass, proto, JSType = "ObjectType", values = [] } = obj;
|
||||
const name = className(typeName);
|
||||
@@ -1003,6 +1004,21 @@ function generateClassHeader(typeName, obj: ClassDefinition) {
|
||||
|
||||
void finishCreation(JSC::VM&);
|
||||
|
||||
${Object.entries(obj.custom ?? {})
|
||||
.map(([fieldName, field]) => {
|
||||
if (field.extraHeaderIncludes?.length ?? 0) {
|
||||
extraIncludes.push(...field.extraHeaderIncludes);
|
||||
}
|
||||
|
||||
var str = "";
|
||||
if (field.header) {
|
||||
str += `#include "${field.header}";` + "\n";
|
||||
}
|
||||
str += `${field.type} ${fieldName};`;
|
||||
return str;
|
||||
})
|
||||
.join("\n")}
|
||||
|
||||
${weakOwner}
|
||||
|
||||
${DECLARE_VISIT_CHILDREN}
|
||||
@@ -1089,11 +1105,19 @@ void ${name}::visitOutputConstraintsImpl(JSCell *cell, Visitor& visitor)
|
||||
|
||||
DEFINE_VISIT_OUTPUT_CONSTRAINTS(${name});
|
||||
|
||||
|
||||
|
||||
`.trim();
|
||||
}
|
||||
|
||||
var output = ``;
|
||||
|
||||
for (let { impl } of Object.values(obj.custom ?? {})) {
|
||||
if (impl) {
|
||||
output += `#include "${impl}";` + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (hasPendingActivity) {
|
||||
output += `
|
||||
extern "C" bool ${symbolName(typeName, "hasPendingActivity")}(void* ptr);
|
||||
@@ -1291,7 +1315,6 @@ function generateZig(
|
||||
symbolName(typeName, "hasPendingActivity"),
|
||||
);
|
||||
}
|
||||
|
||||
Object.values(klass).map((a) =>
|
||||
appendSymbols(exports, (name) => classSymbolName(typeName, name), a),
|
||||
);
|
||||
@@ -1649,7 +1672,8 @@ function generateLazyClassStructureImpl(
|
||||
`.trim();
|
||||
}
|
||||
|
||||
const GENERATED_CLASSES_HEADER = `
|
||||
const GENERATED_CLASSES_HEADER = [
|
||||
`
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// Generated by make codegen
|
||||
#pragma once
|
||||
@@ -1661,12 +1685,16 @@ namespace Zig {
|
||||
|
||||
#include "JSDOMWrapper.h"
|
||||
#include <wtf/NeverDestroyed.h>
|
||||
`,
|
||||
|
||||
`
|
||||
|
||||
namespace WebCore {
|
||||
using namespace Zig;
|
||||
using namespace JSC;
|
||||
|
||||
`;
|
||||
`,
|
||||
];
|
||||
|
||||
const GENERATED_CLASSES_FOOTER = `
|
||||
}
|
||||
@@ -1746,6 +1774,21 @@ void GlobalObject::visitGeneratedLazyClasses(GlobalObject *thisObject, Visitor&
|
||||
}
|
||||
|
||||
const ZIG_GENERATED_CLASSES_HEADER = `
|
||||
/// Generated code! To regenerate, run:
|
||||
///
|
||||
/// make codegen
|
||||
///
|
||||
/// This file is generated by:
|
||||
/// 1. \`bun src/bun.js/scripts/generate-classes.ts\`
|
||||
/// 2. Scan for **/*.classes.ts files in src/bun.js/src
|
||||
/// 3. Generate a JS wrapper for each class in:
|
||||
/// - Zig: generated_classes.zig
|
||||
/// - C++: ZigGeneratedClasses.h, ZigGeneratedClasses.cpp
|
||||
/// 4. For the Zig code to successfully compile:
|
||||
/// - Add it to generated_classes_list.zig
|
||||
/// - pub usingnamespace JSC.Codegen.JSMyClassName;
|
||||
/// 5. make clean-bindings && make bindings -j10
|
||||
///
|
||||
const JSC = @import("bun").JSC;
|
||||
const Classes = @import("./generated_classes_list.zig").Classes;
|
||||
const Environment = @import("../../env.zig");
|
||||
@@ -1799,9 +1842,12 @@ comptime {
|
||||
|
||||
`,
|
||||
]);
|
||||
const allHeaders = classes.map((a) => generateHeader(a.name, a));
|
||||
await writeAndUnlink(`${import.meta.dir}/../bindings/ZigGeneratedClasses.h`, [
|
||||
GENERATED_CLASSES_HEADER,
|
||||
...classes.map((a) => generateHeader(a.name, a)),
|
||||
GENERATED_CLASSES_HEADER[0],
|
||||
...[...new Set(extraIncludes.map((a) => `#include "${a}";` + "\n"))],
|
||||
GENERATED_CLASSES_HEADER[1],
|
||||
...allHeaders,
|
||||
GENERATED_CLASSES_FOOTER,
|
||||
]);
|
||||
await writeAndUnlink(`${import.meta.dir}/../bindings/ZigGeneratedClasses.cpp`, [
|
||||
|
||||
Reference in New Issue
Block a user