Files
bun.sh/src/js/builtins
Jarred Sumner 04b4157232 Fixes #3629
2023-07-13 02:57:25 -07:00
..
2023-07-05 01:21:35 -07:00
2023-06-27 18:23:56 -07:00
2023-07-13 02:57:25 -07:00

JavaScript Builtins

TLDR — When files in this directory change, run:

# Delete the built files
$ make regenerate-bindings
# Re-link the binary without compiling zig (so it's faster)
$ make bun-link-lld-debug

TypeScript files in ./ts are bundled into C++ Headers that can access JavaScriptCore intrinsics. These files use special globals that are prefixed with $.

$getter
export function foo() {
    return $getByIdDirectPrivate(this, "superSecret");
}

It looks kind of like decorators but they're not. They let you directly call engine intrinsics and help with avoiding prototype pollution issues.

V8 has a similar feature (they use % instead of @)

They usually are accompanied by a C++ file.

We use a custom code generator located in ./codegen which contains a regex-based parser that separates each function into it's own bundling context, so syntax like top level variables / functions will not work.

You can also use process.platform and process.arch in these files. The values are inlined and DCE'd.

Generating builtins

To regenerate the builtins, run this from Bun's project root (where the Makefile is)

$ make builtins

You'll want to also rebuild all the C++ bindings or you will get strange crashes on start

$ make clean-bindings

The make regenerate-bindings command will clean and rebuild the bindings.

Also, you can run the code generator manually.

$ bun ./codegen/index.ts
# pass --minify to minify (make passes this by default)
# pass --keep-tmp to keep the temporary ./tmp folder, which contains processed pre-bundled .ts files