1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
import { existsSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "fs";
import path from "path";
import { sliceSourceCode } from "./builtin-parser";
import { applyGlobalReplacements, enums, globalsToPrefix } from "./replacements";
import { cap, fmtCPPString, low } from "./helpers";
import { spawn } from "bun";
async function createStaticHashtables() {
const STATIC_HASH_TABLES = ["src/bun.js/bindings/Process.cpp"];
console.time("Creating static hash tables...");
const create_hash_table = path.join(import.meta.dir, "../../../../src/bun.js/scripts/create_hash_table");
if (!create_hash_table) {
console.warn(
"Could not find create_hash_table executable. Run `bun i` or clone webkit to build static hash tables",
);
return;
}
for (let cpp of STATIC_HASH_TABLES) {
cpp = path.join(import.meta.dir, "../../../../", cpp);
const { stdout, exited } = spawn({
cmd: [create_hash_table, cpp],
stdout: "pipe",
stderr: "inherit",
});
await exited;
let str = await new Response(stdout).text();
str = str.replaceAll(/^\/\/.*$/gm, "");
str = str.replaceAll(/^#include.*$/gm, "");
str = str.replaceAll(`namespace JSC {`, "");
str = str.replaceAll(`} // namespace JSC`, "");
str = "// File generated via `make generate-builtins`\n" + str.trim() + "\n";
await Bun.write(cpp.replace(/\.cpp$/, ".lut.h"), str);
}
console.timeEnd("Creating static hash tables...");
}
const staticHashTablePromise = createStaticHashtables();
console.log("Bundling Bun builtins...");
const MINIFY = process.argv.includes("--minify") || process.argv.includes("-m");
const PARALLEL = process.argv.includes("--parallel") || process.argv.includes("-p");
const KEEP_TMP = process.argv.includes("--keep-tmp") || process.argv.includes("-k") || true;
const SRC_DIR = path.join(import.meta.dir, "../");
const OUT_DIR = path.join(SRC_DIR, "../out");
const TMP_DIR = path.join(SRC_DIR, "../out/tmp");
if (existsSync(TMP_DIR)) rmSync(TMP_DIR, { recursive: true });
mkdirSync(TMP_DIR);
const define = {
"process.env.NODE_ENV": "development",
"process.platform": process.platform,
"process.arch": process.arch,
};
for (const name in enums) {
const value = enums[name];
if (typeof value !== "object") throw new Error("Invalid enum object " + name + " defined in " + import.meta.file);
if (typeof value === null) throw new Error("Invalid enum object " + name + " defined in " + import.meta.file);
const keys = Array.isArray(value) ? value : Object.keys(value).filter(k => !k.match(/^[0-9]+$/));
define[`__intrinsic__${name}IdToLabel`] = "[" + keys.map(k => `"${k}"`).join(", ") + "]";
define[`__intrinsic__${name}LabelToId`] = "{" + keys.map(k => `"${k}": ${keys.indexOf(k)}`).join(", ") + "}";
}
for (const name of globalsToPrefix) {
define[name] = "__intrinsic__" + name;
}
interface ParsedBuiltin {
name: string;
params: string[];
directives: Record<string, any>;
source: string;
async: boolean;
}
interface BundledBuiltin {
name: string;
directives: Record<string, any>;
isGetter: boolean;
isConstructor: boolean;
isLinkTimeConstant: boolean;
isNakedConstructor: boolean;
intrinsic: string;
overriddenName: string;
source: string;
params: string[];
visibility: string;
}
/**
* Source .ts file --> Array<bundled js function code>
*/
async function processFileSplit(filename: string): Promise<{ functions: BundledBuiltin[]; internal: boolean }> {
const basename = path.basename(filename, ".ts");
let contents = await Bun.file(filename).text();
contents = applyGlobalReplacements(contents);
// first approach doesnt work perfectly because we actually need to split each function declaration
// and then compile those separately
const consumeWhitespace = /^\s*/;
const consumeTopLevelContent = /^(\/\*|\/\/|type|import|interface|\$|export (?:async )?function|(?:async )?function)/;
const consumeEndOfType = /;|.(?=export|type|interface|\$|\/\/|\/\*|function)/;
const functions: ParsedBuiltin[] = [];
let directives: Record<string, any> = {};
const bundledFunctions: BundledBuiltin[] = [];
let internal = false;
while (contents.length) {
contents = contents.replace(consumeWhitespace, "");
if (!contents.length) break;
const match = contents.match(consumeTopLevelContent);
if (!match) {
throw new SyntaxError("Could not process input:\n" + contents.slice(0, contents.indexOf("\n")));
}
contents = contents.slice(match.index!);
if (match[1] === "import") {
// TODO: we may want to do stuff with these
const i = contents.indexOf(";");
contents = contents.slice(i + 1);
} else if (match[1] === "/*") {
const i = contents.indexOf("*/") + 2;
internal ||= contents.slice(0, i).includes("@internal");
contents = contents.slice(i);
} else if (match[1] === "//") {
const i = contents.indexOf("\n") + 1;
internal ||= contents.slice(0, i).includes("@internal");
contents = contents.slice(i);
} else if (match[1] === "type" || match[1] === "export type") {
const i = contents.search(consumeEndOfType);
contents = contents.slice(i + 1);
} else if (match[1] === "interface") {
contents = sliceSourceCode(contents, false).rest;
} else if (match[1] === "$") {
const directive = contents.match(/^\$([a-zA-Z0-9]+)(?:\s*=\s*([^\n]+?))?\s*;?\n/);
if (!directive) {
throw new SyntaxError("Could not parse directive:\n" + contents.slice(0, contents.indexOf("\n")));
}
const name = directive[1];
let value;
try {
value = directive[2] ? JSON.parse(directive[2]) : true;
} catch (error) {
throw new SyntaxError("Could not parse directive value " + directive[2] + " (must be JSON parsable)");
}
if (name === "constructor") {
throw new SyntaxError("$constructor not implemented");
}
if (name === "nakedConstructor") {
throw new SyntaxError("$nakedConstructor not implemented");
}
directives[name] = value;
contents = contents.slice(directive[0].length);
} else if (match[1] === "export function" || match[1] === "export async function") {
const declaration = contents.match(
/^export\s+(async\s+)?function\s+([a-zA-Z0-9]+)\s*\(([^)]*)\)(?:\s*:\s*([^{\n]+))?\s*{?/,
);
if (!declaration)
throw new SyntaxError("Could not parse function declaration:\n" + contents.slice(0, contents.indexOf("\n")));
const async = !!declaration[1];
const name = declaration[2];
const paramString = declaration[3];
const params =
paramString.trim().length === 0 ? [] : paramString.split(",").map(x => x.replace(/:.+$/, "").trim());
if (params[0] === "this") {
params.shift();
}
const { result, rest } = sliceSourceCode(contents.slice(declaration[0].length - 1), true);
functions.push({
name,
params,
directives,
source: result.trim().slice(1, -1),
async,
});
contents = rest;
directives = {};
} else if (match[1] === "function" || match[1] === "async function") {
const fnname = contents.match(/^function ([a-zA-Z0-9]+)\(([^)]*)\)(?:\s*:\s*([^{\n]+))?\s*{?/)![1];
throw new SyntaxError("All top level functions must be exported: " + fnname);
} else {
throw new Error("TODO: parse " + match[1]);
}
}
for (const fn of functions) {
const tmpFile = path.join(TMP_DIR, `${basename}.${fn.name}.ts`);
// not sure if this optimization works properly in jsc builtins
// const useThis = fn.usesThis;
const useThis = true;
// TODO: we should use format=IIFE so we could bundle imports and extra functions.
await Bun.write(
tmpFile,
`// @ts-nocheck
// GENERATED TEMP FILE - DO NOT EDIT
// Sourced from ${path.relative(TMP_DIR, filename)}
// do not allow the bundler to rename a symbol to $
($);
$$capture_start$$(${fn.async ? "async " : ""}${
useThis
? `function(${fn.params.join(",")})`
: `${fn.params.length === 1 ? fn.params[0] : `(${fn.params.join(",")})`}=>`
} {${fn.source}}).$$capture_end$$;
`,
);
await Bun.sleep(1);
const build = await Bun.build({
entrypoints: [tmpFile],
define,
minify: true,
});
if (!build.success) {
throw new AggregateError(build.logs, "Failed bundling builtin function " + fn.name + " from " + basename + ".ts");
}
if (build.outputs.length !== 1) {
throw new Error("expected one output");
}
const output = await build.outputs[0].text();
const captured = output.match(/\$\$capture_start\$\$([\s\S]+)\.\$\$capture_end\$\$/)![1];
const finalReplacement =
(fn.directives.sloppy ? captured : captured.replace(/function\s*\(.*?\)\s*{/, '$&"use strict";'))
.replace(/^\((async )?function\(/, "($1function (")
.replace(/__intrinsic__lazy\(/g, "globalThis[globalThis.Symbol.for('Bun.lazy')](")
.replace(/__intrinsic__/g, "@") + "\n";
bundledFunctions.push({
name: fn.name,
directives: fn.directives,
source: finalReplacement,
params: fn.params,
visibility: fn.directives.visibility ?? (fn.directives.linkTimeConstant ? "Private" : "Public"),
isGetter: !!fn.directives.getter,
isConstructor: !!fn.directives.constructor,
isLinkTimeConstant: !!fn.directives.linkTimeConstant,
isNakedConstructor: !!fn.directives.nakedConstructor,
intrinsic: fn.directives.intrinsic ?? "NoIntrinsic",
overriddenName: fn.directives.getter
? `"get ${fn.name}"_s`
: fn.directives.overriddenName
? `"${fn.directives.overriddenName}"_s`
: "ASCIILiteral()",
});
}
return {
functions: bundledFunctions,
internal,
};
}
const filesToProcess = readdirSync(SRC_DIR).filter(x => x.endsWith(".ts") && !x.endsWith(".d.ts"));
const files: Array<{ basename: string; functions: BundledBuiltin[]; internal: boolean }> = [];
async function processFile(x: string) {
const basename = path.basename(x, ".ts");
try {
files.push({
basename,
...(await processFileSplit(path.join(SRC_DIR, x))),
});
} catch (error) {
console.error("Failed to process file: " + basename + ".ts");
console.error(error);
process.exit(1);
}
}
// Bun seems to crash if this is parallelized, :(
if (PARALLEL) {
await Promise.all(filesToProcess.map(processFile));
} else {
for (const x of filesToProcess) {
await processFile(x);
}
}
// C++ codegen
let bundledCPP = `// Generated by \`bun src/js/builtins/codegen\`
// Do not edit by hand.
namespace Zig { class GlobalObject; }
#include "root.h"
#include "config.h"
#include "JSDOMGlobalObject.h"
#include "WebCoreJSClientData.h"
#include <JavaScriptCore/JSObjectInlines.h>
namespace WebCore {
`;
for (const { basename, functions } of files) {
bundledCPP += `/* ${basename}.ts */\n`;
const lowerBasename = low(basename);
for (const fn of functions) {
const name = `${lowerBasename}${cap(fn.name)}Code`;
bundledCPP += `// ${fn.name}
const JSC::ConstructAbility s_${name}ConstructAbility = JSC::ConstructAbility::CannotConstruct;
const JSC::ConstructorKind s_${name}ConstructorKind = JSC::ConstructorKind::None;
const JSC::ImplementationVisibility s_${name}ImplementationVisibility = JSC::ImplementationVisibility::${fn.visibility};
const int s_${name}Length = ${fn.source.length};
static const JSC::Intrinsic s_${name}Intrinsic = JSC::NoIntrinsic;
const char* const s_${name} = ${fmtCPPString(fn.source)};
`;
}
bundledCPP += `#define DEFINE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \\
JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \\
{\\
JSVMClientData* clientData = static_cast<JSVMClientData*>(vm.clientData); \\
return clientData->builtinFunctions().${lowerBasename}Builtins().codeName##Executable()->link(vm, nullptr, clientData->builtinFunctions().${lowerBasename}Builtins().codeName##Source(), std::nullopt, s_##codeName##Intrinsic); \\
}
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_CODE(DEFINE_BUILTIN_GENERATOR)
#undef DEFINE_BUILTIN_GENERATOR
`;
}
bundledCPP += `
JSBuiltinInternalFunctions::JSBuiltinInternalFunctions(JSC::VM& vm)
: m_vm(vm)
`;
for (const { basename, internal } of files) {
if (internal) {
bundledCPP += ` , m_${low(basename)}(vm)\n`;
}
}
bundledCPP += `
{
UNUSED_PARAM(vm);
}
template<typename Visitor>
void JSBuiltinInternalFunctions::visit(Visitor& visitor)
{
`;
for (const { basename, internal } of files) {
if (internal) bundledCPP += ` m_${low(basename)}.visit(visitor);\n`;
}
bundledCPP += `
UNUSED_PARAM(visitor);
}
template void JSBuiltinInternalFunctions::visit(AbstractSlotVisitor&);
template void JSBuiltinInternalFunctions::visit(SlotVisitor&);
SUPPRESS_ASAN void JSBuiltinInternalFunctions::initialize(Zig::GlobalObject& globalObject)
{
UNUSED_PARAM(globalObject);
`;
for (const { basename, internal } of files) {
if (internal) {
bundledCPP += ` m_${low(basename)}.init(globalObject);\n`;
}
}
bundledCPP += `
JSVMClientData& clientData = *static_cast<JSVMClientData*>(m_vm.clientData);
Zig::GlobalObject::GlobalPropertyInfo staticGlobals[] = {
`;
for (const { basename, internal } of files) {
if (internal) {
bundledCPP += `#define DECLARE_GLOBAL_STATIC(name) \\
Zig::GlobalObject::GlobalPropertyInfo( \\
clientData.builtinFunctions().${low(basename)}Builtins().name##PrivateName(), ${low(
basename,
)}().m_##name##Function.get() , JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly),
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_FUNCTION_NAME(DECLARE_GLOBAL_STATIC)
#undef DECLARE_GLOBAL_STATIC
`;
}
}
bundledCPP += `
};
globalObject.addStaticGlobals(staticGlobals, std::size(staticGlobals));
UNUSED_PARAM(clientData);
}
} // namespace WebCore
`;
// C++ Header codegen
let bundledHeader = `// Generated by \`bun src/js/builtins/codegen\`
// Do not edit by hand.
#pragma once
namespace Zig { class GlobalObject; }
#include "root.h"
#include <JavaScriptCore/BuiltinUtils.h>
#include <JavaScriptCore/Identifier.h>
#include <JavaScriptCore/JSFunction.h>
#include <JavaScriptCore/UnlinkedFunctionExecutable.h>
#include <JavaScriptCore/VM.h>
#include <JavaScriptCore/WeakInlines.h>
namespace JSC {
class FunctionExecutable;
}
namespace WebCore {
`;
for (const { basename, functions, internal } of files) {
bundledHeader += `/* ${basename}.ts */
`;
const lowerBasename = low(basename);
for (const fn of functions) {
const name = `${lowerBasename}${cap(fn.name)}Code`;
bundledHeader += `// ${fn.name}
#define WEBCORE_BUILTIN_${basename.toUpperCase()}_${fn.name.toUpperCase()} 1
extern const char* const s_${name};
extern const int s_${name}Length;
extern const JSC::ConstructAbility s_${name}ConstructAbility;
extern const JSC::ConstructorKind s_${name}ConstructorKind;
extern const JSC::ImplementationVisibility s_${name}ImplementationVisibility;
`;
}
bundledHeader += `#define WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_DATA(macro) \\\n`;
for (const fn of functions) {
bundledHeader += ` macro(${fn.name}, ${lowerBasename}${cap(fn.name)}, ${fn.params.length}) \\\n`;
}
bundledHeader += "\n";
bundledHeader += `#define WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_CODE(macro) \\\n`;
for (const fn of functions) {
const name = `${lowerBasename}${cap(fn.name)}Code`;
bundledHeader += ` macro(${name}, ${fn.name}, ${fn.overriddenName}, s_${name}Length) \\\n`;
}
bundledHeader += "\n";
bundledHeader += `#define WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_FUNCTION_NAME(macro) \\\n`;
for (const fn of functions) {
bundledHeader += ` macro(${fn.name}) \\\n`;
}
bundledHeader += `
#define DECLARE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \\
JSC::FunctionExecutable* codeName##Generator(JSC::VM&);
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_CODE(DECLARE_BUILTIN_GENERATOR)
#undef DECLARE_BUILTIN_GENERATOR
class ${basename}BuiltinsWrapper : private JSC::WeakHandleOwner {
public:
explicit ${basename}BuiltinsWrapper(JSC::VM& vm)
: m_vm(vm)
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_FUNCTION_NAME(INITIALIZE_BUILTIN_NAMES)
#define INITIALIZE_BUILTIN_SOURCE_MEMBERS(name, functionName, overriddenName, length) , m_##name##Source(JSC::makeSource(StringImpl::createWithoutCopying(s_##name, length), { }))
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_CODE(INITIALIZE_BUILTIN_SOURCE_MEMBERS)
#undef INITIALIZE_BUILTIN_SOURCE_MEMBERS
{
}
#define EXPOSE_BUILTIN_EXECUTABLES(name, functionName, overriddenName, length) \\
JSC::UnlinkedFunctionExecutable* name##Executable(); \\
const JSC::SourceCode& name##Source() const { return m_##name##Source; }
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_CODE(EXPOSE_BUILTIN_EXECUTABLES)
#undef EXPOSE_BUILTIN_EXECUTABLES
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_IDENTIFIER_ACCESSOR)
void exportNames();
private:
JSC::VM& m_vm;
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_NAMES)
#define DECLARE_BUILTIN_SOURCE_MEMBERS(name, functionName, overriddenName, length) \\
JSC::SourceCode m_##name##Source;\\
JSC::Weak<JSC::UnlinkedFunctionExecutable> m_##name##Executable;
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_CODE(DECLARE_BUILTIN_SOURCE_MEMBERS)
#undef DECLARE_BUILTIN_SOURCE_MEMBERS
};
#define DEFINE_BUILTIN_EXECUTABLES(name, functionName, overriddenName, length) \\
inline JSC::UnlinkedFunctionExecutable* ${basename}BuiltinsWrapper::name##Executable() \\
{\\
if (!m_##name##Executable) {\\
JSC::Identifier executableName = functionName##PublicName();\\
if (overriddenName)\\
executableName = JSC::Identifier::fromString(m_vm, overriddenName);\\
m_##name##Executable = JSC::Weak<JSC::UnlinkedFunctionExecutable>(JSC::createBuiltinExecutable(m_vm, m_##name##Source, executableName, s_##name##ImplementationVisibility, s_##name##ConstructorKind, s_##name##ConstructAbility), this, &m_##name##Executable);\\
}\\
return m_##name##Executable.get();\\
}
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_CODE(DEFINE_BUILTIN_EXECUTABLES)
#undef DEFINE_BUILTIN_EXECUTABLES
inline void ${basename}BuiltinsWrapper::exportNames()
{
#define EXPORT_FUNCTION_NAME(name) m_vm.propertyNames->appendExternalName(name##PublicName(), name##PrivateName());
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_FUNCTION_NAME(EXPORT_FUNCTION_NAME)
#undef EXPORT_FUNCTION_NAME
}
`;
if (internal) {
bundledHeader += `class ${basename}BuiltinFunctions {
public:
explicit ${basename}BuiltinFunctions(JSC::VM& vm) : m_vm(vm) { }
void init(JSC::JSGlobalObject&);
template<typename Visitor> void visit(Visitor&);
public:
JSC::VM& m_vm;
#define DECLARE_BUILTIN_SOURCE_MEMBERS(functionName) \\
JSC::WriteBarrier<JSC::JSFunction> m_##functionName##Function;
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_FUNCTION_NAME(DECLARE_BUILTIN_SOURCE_MEMBERS)
#undef DECLARE_BUILTIN_SOURCE_MEMBERS
};
inline void ${basename}BuiltinFunctions::init(JSC::JSGlobalObject& globalObject)
{
#define EXPORT_FUNCTION(codeName, functionName, overriddenName, length) \\
m_##functionName##Function.set(m_vm, &globalObject, JSC::JSFunction::create(m_vm, codeName##Generator(m_vm), &globalObject));
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_CODE(EXPORT_FUNCTION)
#undef EXPORT_FUNCTION
}
template<typename Visitor>
inline void ${basename}BuiltinFunctions::visit(Visitor& visitor)
{
#define VISIT_FUNCTION(name) visitor.append(m_##name##Function);
WEBCORE_FOREACH_${basename.toUpperCase()}_BUILTIN_FUNCTION_NAME(VISIT_FUNCTION)
#undef VISIT_FUNCTION
}
template void ${basename}BuiltinFunctions::visit(JSC::AbstractSlotVisitor&);
template void ${basename}BuiltinFunctions::visit(JSC::SlotVisitor&);
`;
}
}
bundledHeader += `class JSBuiltinFunctions {
public:
explicit JSBuiltinFunctions(JSC::VM& vm)
: m_vm(vm)
`;
for (const { basename } of files) {
bundledHeader += ` , m_${low(basename)}Builtins(m_vm)\n`;
}
bundledHeader += `
{
`;
for (const { basename, internal } of files) {
if (internal) {
bundledHeader += ` m_${low(basename)}Builtins.exportNames();\n`;
}
}
bundledHeader += ` }
`;
for (const { basename } of files) {
bundledHeader += ` ${basename}BuiltinsWrapper& ${low(basename)}Builtins() { return m_${low(
basename,
)}Builtins; }\n`;
}
bundledHeader += `
private:
JSC::VM& m_vm;
`;
for (const { basename } of files) {
bundledHeader += ` ${basename}BuiltinsWrapper m_${low(basename)}Builtins;\n`;
}
bundledHeader += `;
};
class JSBuiltinInternalFunctions {
public:
explicit JSBuiltinInternalFunctions(JSC::VM&);
template<typename Visitor> void visit(Visitor&);
void initialize(Zig::GlobalObject&);
`;
for (const { basename, internal } of files) {
if (internal) {
bundledHeader += ` ${basename}BuiltinFunctions& ${low(basename)}() { return m_${low(basename)}; }\n`;
}
}
bundledHeader += `
private:
JSC::VM& m_vm;
`;
for (const { basename, internal } of files) {
if (internal) {
bundledHeader += ` ${basename}BuiltinFunctions m_${low(basename)};\n`;
}
}
bundledHeader += `
};
} // namespace WebCore
`;
await Bun.write(path.join(OUT_DIR, "WebCoreJSBuiltins.h"), bundledHeader);
await Bun.write(path.join(OUT_DIR, "WebCoreJSBuiltins.cpp"), bundledCPP);
// Generate TS types
let dts = `// Generated by \`bun src/js/builtins/codegen\`
// Do not edit by hand.
type RemoveThis<F> = F extends (this: infer T, ...args: infer A) => infer R ? (...args: A) => R : F;
`;
for (const { basename, functions, internal } of files) {
if (internal) {
dts += `\n// ${basename}.ts\n`;
for (const fn of functions) {
dts += `declare const \$${fn.name}: RemoveThis<typeof import("${path.relative(
OUT_DIR,
path.join(SRC_DIR, basename),
)}")[${JSON.stringify(fn.name)}]>;\n`;
}
}
}
await Bun.write(path.join(OUT_DIR, "WebCoreJSBuiltins.d.ts"), dts);
const totalJSSize = files.reduce(
(acc, { functions }) => acc + functions.reduce((acc, fn) => acc + fn.source.length, 0),
0,
);
if (!KEEP_TMP) {
await rmSync(TMP_DIR, { recursive: true });
}
await staticHashTablePromise;
console.log(
`Embedded JS size: %s bytes (across %s functions, %s files)`,
totalJSSize,
files.reduce((acc, { functions }) => acc + functions.length, 0),
files.length,
);
console.log(`[${performance.now().toFixed(1)}ms]`);
|