mirror of
https://github.com/oven-sh/bun
synced 2026-02-04 07:58:54 +00:00
Compare commits
68 Commits
claude/tom
...
dylan/byte
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ad6973fe4 | ||
|
|
caef7fceea | ||
|
|
d919a76dd6 | ||
|
|
973fa98796 | ||
|
|
b7a6087d71 | ||
|
|
55230c16e6 | ||
|
|
e2161e7e13 | ||
|
|
d5431fcfe6 | ||
|
|
b04f98885f | ||
|
|
1779ee807c | ||
|
|
42cec2f0e2 | ||
|
|
5b7fd9ed0e | ||
|
|
ed9353f95e | ||
|
|
4573b5b844 | ||
|
|
5a75bcde13 | ||
|
|
afc5f50237 | ||
|
|
ca8d8065ec | ||
|
|
0bcb3137d3 | ||
|
|
b79bbfe289 | ||
|
|
72490281e5 | ||
|
|
60ab798991 | ||
|
|
e1de7563e1 | ||
|
|
3d361c8b49 | ||
|
|
0759da233f | ||
|
|
9978424177 | ||
|
|
d42f536a74 | ||
|
|
f78d197523 | ||
|
|
80fb7c7375 | ||
|
|
e2bfeefc9d | ||
|
|
cff2c2690b | ||
|
|
d0272d4a98 | ||
|
|
48ebc15e63 | ||
|
|
2e8e7a000c | ||
|
|
c1584b8a35 | ||
|
|
a0f13ea5bb | ||
|
|
c2bd4095eb | ||
|
|
0a7313e66c | ||
|
|
83293ea50c | ||
|
|
de7c947161 | ||
|
|
033c977fea | ||
|
|
d957a81c0a | ||
|
|
0b98086c3d | ||
|
|
f6c5318560 | ||
|
|
ad1fa514ed | ||
|
|
24c43c8f4d | ||
|
|
d69eb3ca00 | ||
|
|
3f53add5f1 | ||
|
|
fcaff77ed7 | ||
|
|
25c61fcd5a | ||
|
|
d0b5f9b587 | ||
|
|
1400e05e11 | ||
|
|
c8e3a91602 | ||
|
|
2e5f7f10ae | ||
|
|
559c95ee2c | ||
|
|
262f8863cb | ||
|
|
05f5ea0070 | ||
|
|
46ce975175 | ||
|
|
fa4822f8b8 | ||
|
|
8881e671d4 | ||
|
|
97d55411de | ||
|
|
f247277375 | ||
|
|
b93468ca48 | ||
|
|
35e9f3d4a2 | ||
|
|
9142cdcb1a | ||
|
|
822445d922 | ||
|
|
a34e10db53 | ||
|
|
684f7ecd09 | ||
|
|
d189759576 |
8
.github/workflows/format.yml
vendored
8
.github/workflows/format.yml
vendored
@@ -8,10 +8,8 @@ on:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
merge_group:
|
||||
push:
|
||||
branches: ["main"]
|
||||
env:
|
||||
BUN_VERSION: "1.2.11"
|
||||
BUN_VERSION: "1.2.20"
|
||||
LLVM_VERSION: "19.1.7"
|
||||
LLVM_VERSION_MAJOR: "19"
|
||||
|
||||
@@ -37,6 +35,7 @@ jobs:
|
||||
- name: Setup Dependencies
|
||||
run: |
|
||||
bun install
|
||||
bun scripts/glob-sources.mjs
|
||||
- name: Format Code
|
||||
run: |
|
||||
# Start prettier in background with prefixed output
|
||||
@@ -62,7 +61,7 @@ jobs:
|
||||
(
|
||||
ZIG_TEMP=$(mktemp -d)
|
||||
echo "[zig] Downloading Zig (musl build)..."
|
||||
wget -q -O "$ZIG_TEMP/zig.zip" https://github.com/oven-sh/zig/releases/download/autobuild-ebe0cdac3170b0462a39caff4d1d7252b608e98d/bootstrap-x86_64-linux-musl.zip
|
||||
wget -q -O "$ZIG_TEMP/zig.zip" https://github.com/oven-sh/zig/releases/download/autobuild-e0b7c318f318196c5f81fdf3423816a7b5bb3112/bootstrap-x86_64-linux-musl.zip
|
||||
unzip -q -d "$ZIG_TEMP" "$ZIG_TEMP/zig.zip"
|
||||
export PATH="$ZIG_TEMP/bootstrap-x86_64-linux-musl:$PATH"
|
||||
echo "[zig] Running zig fmt..."
|
||||
@@ -106,4 +105,5 @@ jobs:
|
||||
- name: Ban Words
|
||||
run: |
|
||||
bun ./test/internal/ban-words.test.ts
|
||||
git rm -f cmake/sources/*.txt || true
|
||||
- uses: autofix-ci/action@635ffb0c9798bd160680f18fd73371e355b85f27
|
||||
|
||||
41
.github/workflows/glob-sources.yml
vendored
41
.github/workflows/glob-sources.yml
vendored
@@ -1,41 +0,0 @@
|
||||
name: Glob Sources
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
on:
|
||||
workflow_call:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
|
||||
env:
|
||||
BUN_VERSION: "1.2.11"
|
||||
|
||||
jobs:
|
||||
glob-sources:
|
||||
name: Glob Sources
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Configure Git
|
||||
run: |
|
||||
git config --global core.autocrlf true
|
||||
git config --global core.ignorecase true
|
||||
git config --global core.precomposeUnicode true
|
||||
- name: Setup Bun
|
||||
uses: ./.github/actions/setup-bun
|
||||
with:
|
||||
bun-version: ${{ env.BUN_VERSION }}
|
||||
- name: Setup Dependencies
|
||||
run: |
|
||||
bun install
|
||||
- name: Glob sources
|
||||
run: bun scripts/glob-sources.mjs
|
||||
- name: Commit
|
||||
uses: stefanzweifel/git-auto-commit-action@v5
|
||||
with:
|
||||
commit_message: "`bun scripts/glob-sources.mjs`"
|
||||
|
||||
41
.github/workflows/labeled.yml
vendored
41
.github/workflows/labeled.yml
vendored
@@ -5,6 +5,8 @@ env:
|
||||
on:
|
||||
issues:
|
||||
types: [labeled]
|
||||
pull_request_target:
|
||||
types: [labeled, opened, reopened, synchronize, unlabeled]
|
||||
|
||||
jobs:
|
||||
# on-bug:
|
||||
@@ -43,9 +45,46 @@ jobs:
|
||||
# token: ${{ secrets.GITHUB_TOKEN }}
|
||||
# issue-number: ${{ github.event.issue.number }}
|
||||
# labels: ${{ steps.add-labels.outputs.labels }}
|
||||
on-slop:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'slop')
|
||||
permissions:
|
||||
issues: write
|
||||
pull-requests: write
|
||||
contents: write
|
||||
steps:
|
||||
- name: Update PR title and body for slop and close
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const pr = await github.rest.pulls.get({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
pull_number: context.issue.number
|
||||
});
|
||||
|
||||
await github.rest.pulls.update({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
pull_number: context.issue.number,
|
||||
title: 'ai slop',
|
||||
body: 'This PR has been marked as AI slop and the description has been updated to avoid confusion or misleading reviewers.\n\nMany AI PRs are fine, but sometimes they submit a PR too early, fail to test if the problem is real, fail to reproduce the problem, or fail to test that the problem is fixed. If you think this PR is not AI slop, please leave a comment.',
|
||||
state: 'closed'
|
||||
});
|
||||
|
||||
// Delete the branch if it's from a fork or if it's not a protected branch
|
||||
try {
|
||||
await github.rest.git.deleteRef({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
ref: `heads/${pr.data.head.ref}`
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('Could not delete branch:', error.message);
|
||||
}
|
||||
on-labeled:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event.label.name == 'crash' || github.event.label.name == 'needs repro'
|
||||
if: github.event_name == 'issues' && (github.event.label.name == 'crash' || github.event.label.name == 'needs repro')
|
||||
permissions:
|
||||
issues: write
|
||||
steps:
|
||||
|
||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -186,4 +186,7 @@ scratch*.{js,ts,tsx,cjs,mjs}
|
||||
|
||||
*.bun-build
|
||||
|
||||
scripts/lldb-inline
|
||||
scripts/lldb-inline
|
||||
|
||||
# We regenerate these in all the build scripts
|
||||
cmake/sources/*.txt
|
||||
11
CLAUDE.md
11
CLAUDE.md
@@ -43,16 +43,11 @@ Tests use Bun's Jest-compatible test runner with proper test fixtures:
|
||||
|
||||
```typescript
|
||||
import { test, expect } from "bun:test";
|
||||
import {
|
||||
bunEnv,
|
||||
bunExe,
|
||||
normalizeBunSnapshot,
|
||||
tempDirWithFiles,
|
||||
} from "harness";
|
||||
import { bunEnv, bunExe, normalizeBunSnapshot, tempDir } from "harness";
|
||||
|
||||
test("my feature", async () => {
|
||||
// Create temp directory with test files
|
||||
const dir = tempDirWithFiles("test-prefix", {
|
||||
using dir = tempDir("test-prefix", {
|
||||
"index.js": `console.log("hello");`,
|
||||
});
|
||||
|
||||
@@ -60,7 +55,7 @@ test("my feature", async () => {
|
||||
await using proc = Bun.spawn({
|
||||
cmd: [bunExe(), "index.js"],
|
||||
env: bunEnv,
|
||||
cwd: dir,
|
||||
cwd: String(dir),
|
||||
stderr: "pipe",
|
||||
});
|
||||
|
||||
|
||||
116
bench/postMessage/postMessage-object.mjs
Normal file
116
bench/postMessage/postMessage-object.mjs
Normal file
@@ -0,0 +1,116 @@
|
||||
// Benchmark for object fast path optimization in postMessage with Workers
|
||||
|
||||
import { bench, run } from "mitata";
|
||||
import { Worker } from "node:worker_threads";
|
||||
|
||||
const extraProperties = {
|
||||
a: "a!",
|
||||
b: "b!",
|
||||
"second": "c!",
|
||||
bool: true,
|
||||
nully: null,
|
||||
undef: undefined,
|
||||
int: 0,
|
||||
double: 1.234,
|
||||
falsy: false,
|
||||
};
|
||||
|
||||
const objects = {
|
||||
small: { property: "Hello world", ...extraProperties },
|
||||
medium: {
|
||||
property: Buffer.alloc("Hello World!!!".length * 1024, "Hello World!!!").toString(),
|
||||
...extraProperties,
|
||||
},
|
||||
large: {
|
||||
property: Buffer.alloc("Hello World!!!".length * 1024 * 256, "Hello World!!!").toString(),
|
||||
...extraProperties,
|
||||
},
|
||||
};
|
||||
|
||||
let worker;
|
||||
let receivedCount = new Int32Array(new SharedArrayBuffer(4));
|
||||
let sentCount = 0;
|
||||
|
||||
function createWorker() {
|
||||
const workerCode = `
|
||||
import { parentPort, workerData } from "node:worker_threads";
|
||||
|
||||
let int = workerData;
|
||||
|
||||
parentPort?.on("message", data => {
|
||||
switch (data.property.length) {
|
||||
case ${objects.small.property.length}:
|
||||
case ${objects.medium.property.length}:
|
||||
case ${objects.large.property.length}: {
|
||||
if (
|
||||
data.a === "a!" &&
|
||||
data.b === "b!" &&
|
||||
data.second === "c!" &&
|
||||
data.bool === true &&
|
||||
data.nully === null &&
|
||||
data.undef === undefined &&
|
||||
data.int === 0 &&
|
||||
data.double === 1.234 &&
|
||||
data.falsy === false) {
|
||||
Atomics.add(int, 0, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
default: {
|
||||
throw new Error("Invalid data object: " + JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
`;
|
||||
|
||||
worker = new Worker(workerCode, { eval: true, workerData: receivedCount });
|
||||
|
||||
worker.on("message", confirmationId => {});
|
||||
|
||||
worker.on("error", error => {
|
||||
console.error("Worker error:", error);
|
||||
});
|
||||
}
|
||||
|
||||
// Initialize worker before running benchmarks
|
||||
createWorker();
|
||||
|
||||
function fmt(int) {
|
||||
if (int < 1000) {
|
||||
return `${int} chars`;
|
||||
}
|
||||
|
||||
if (int < 100000) {
|
||||
return `${(int / 1024) | 0} KB`;
|
||||
}
|
||||
|
||||
return `${(int / 1024 / 1024) | 0} MB`;
|
||||
}
|
||||
|
||||
// Benchmark postMessage with pure strings (uses fast path)
|
||||
bench("postMessage({ prop: " + fmt(objects.small.property.length) + " string, ...9 more props })", async () => {
|
||||
sentCount++;
|
||||
worker.postMessage(objects.small);
|
||||
});
|
||||
|
||||
bench("postMessage({ prop: " + fmt(objects.medium.property.length) + " string, ...9 more props })", async () => {
|
||||
sentCount++;
|
||||
worker.postMessage(objects.medium);
|
||||
});
|
||||
|
||||
bench("postMessage({ prop: " + fmt(objects.large.property.length) + " string, ...9 more props })", async () => {
|
||||
sentCount++;
|
||||
worker.postMessage(objects.large);
|
||||
});
|
||||
|
||||
await run();
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 5000));
|
||||
|
||||
if (receivedCount[0] !== sentCount) {
|
||||
throw new Error("Expected " + receivedCount[0] + " to equal " + sentCount);
|
||||
}
|
||||
|
||||
// Cleanup worker
|
||||
worker?.terminate();
|
||||
@@ -12,6 +12,9 @@ const scenarios = [
|
||||
{ alg: "sha1", digest: "base64" },
|
||||
{ alg: "sha256", digest: "hex" },
|
||||
{ alg: "sha256", digest: "base64" },
|
||||
{ alg: "blake2b512", digest: "hex" },
|
||||
{ alg: "sha512-224", digest: "hex" },
|
||||
{ alg: "sha512-256", digest: "hex" },
|
||||
];
|
||||
|
||||
for (const { alg, digest } of scenarios) {
|
||||
@@ -23,6 +26,10 @@ for (const { alg, digest } of scenarios) {
|
||||
bench(`${alg}-${digest} (Bun.CryptoHasher)`, () => {
|
||||
new Bun.CryptoHasher(alg).update(data).digest(digest);
|
||||
});
|
||||
|
||||
bench(`${alg}-${digest} (Bun.CryptoHasher.hash)`, () => {
|
||||
return Bun.CryptoHasher.hash(alg, data, digest);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
407
bench/yaml/yaml-stringify.mjs
Normal file
407
bench/yaml/yaml-stringify.mjs
Normal file
@@ -0,0 +1,407 @@
|
||||
import { bench, group, run } from "../runner.mjs";
|
||||
import jsYaml from "js-yaml";
|
||||
import yaml from "yaml";
|
||||
|
||||
// Small object
|
||||
const smallObject = {
|
||||
name: "John Doe",
|
||||
age: 30,
|
||||
email: "john@example.com",
|
||||
active: true,
|
||||
};
|
||||
|
||||
// Medium object with nested structures
|
||||
const mediumObject = {
|
||||
company: "Acme Corp",
|
||||
employees: [
|
||||
{
|
||||
name: "John Doe",
|
||||
age: 30,
|
||||
position: "Developer",
|
||||
skills: ["JavaScript", "TypeScript", "Node.js"],
|
||||
},
|
||||
{
|
||||
name: "Jane Smith",
|
||||
age: 28,
|
||||
position: "Designer",
|
||||
skills: ["Figma", "Photoshop", "Illustrator"],
|
||||
},
|
||||
{
|
||||
name: "Bob Johnson",
|
||||
age: 35,
|
||||
position: "Manager",
|
||||
skills: ["Leadership", "Communication", "Planning"],
|
||||
},
|
||||
],
|
||||
settings: {
|
||||
database: {
|
||||
host: "localhost",
|
||||
port: 5432,
|
||||
name: "mydb",
|
||||
},
|
||||
cache: {
|
||||
enabled: true,
|
||||
ttl: 3600,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// Large object with complex structures
|
||||
const largeObject = {
|
||||
apiVersion: "apps/v1",
|
||||
kind: "Deployment",
|
||||
metadata: {
|
||||
name: "nginx-deployment",
|
||||
labels: {
|
||||
app: "nginx",
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
replicas: 3,
|
||||
selector: {
|
||||
matchLabels: {
|
||||
app: "nginx",
|
||||
},
|
||||
},
|
||||
template: {
|
||||
metadata: {
|
||||
labels: {
|
||||
app: "nginx",
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
containers: [
|
||||
{
|
||||
name: "nginx",
|
||||
image: "nginx:1.14.2",
|
||||
ports: [
|
||||
{
|
||||
containerPort: 80,
|
||||
},
|
||||
],
|
||||
env: [
|
||||
{
|
||||
name: "ENV_VAR_1",
|
||||
value: "value1",
|
||||
},
|
||||
{
|
||||
name: "ENV_VAR_2",
|
||||
value: "value2",
|
||||
},
|
||||
],
|
||||
volumeMounts: [
|
||||
{
|
||||
name: "config",
|
||||
mountPath: "/etc/nginx",
|
||||
},
|
||||
],
|
||||
resources: {
|
||||
limits: {
|
||||
cpu: "1",
|
||||
memory: "1Gi",
|
||||
},
|
||||
requests: {
|
||||
cpu: "0.5",
|
||||
memory: "512Mi",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
volumes: [
|
||||
{
|
||||
name: "config",
|
||||
configMap: {
|
||||
name: "nginx-config",
|
||||
items: [
|
||||
{
|
||||
key: "nginx.conf",
|
||||
path: "nginx.conf",
|
||||
},
|
||||
{
|
||||
key: "mime.types",
|
||||
path: "mime.types",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
nodeSelector: {
|
||||
disktype: "ssd",
|
||||
},
|
||||
tolerations: [
|
||||
{
|
||||
key: "key1",
|
||||
operator: "Equal",
|
||||
value: "value1",
|
||||
effect: "NoSchedule",
|
||||
},
|
||||
{
|
||||
key: "key2",
|
||||
operator: "Exists",
|
||||
effect: "NoExecute",
|
||||
},
|
||||
],
|
||||
affinity: {
|
||||
nodeAffinity: {
|
||||
requiredDuringSchedulingIgnoredDuringExecution: {
|
||||
nodeSelectorTerms: [
|
||||
{
|
||||
matchExpressions: [
|
||||
{
|
||||
key: "kubernetes.io/e2e-az-name",
|
||||
operator: "In",
|
||||
values: ["e2e-az1", "e2e-az2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
podAntiAffinity: {
|
||||
preferredDuringSchedulingIgnoredDuringExecution: [
|
||||
{
|
||||
weight: 100,
|
||||
podAffinityTerm: {
|
||||
labelSelector: {
|
||||
matchExpressions: [
|
||||
{
|
||||
key: "app",
|
||||
operator: "In",
|
||||
values: ["web-store"],
|
||||
},
|
||||
],
|
||||
},
|
||||
topologyKey: "kubernetes.io/hostname",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// Object with anchors and references (after resolution)
|
||||
const objectWithAnchors = {
|
||||
defaults: {
|
||||
adapter: "postgresql",
|
||||
host: "localhost",
|
||||
port: 5432,
|
||||
},
|
||||
development: {
|
||||
adapter: "postgresql",
|
||||
host: "localhost",
|
||||
port: 5432,
|
||||
database: "dev_db",
|
||||
},
|
||||
test: {
|
||||
adapter: "postgresql",
|
||||
host: "localhost",
|
||||
port: 5432,
|
||||
database: "test_db",
|
||||
},
|
||||
production: {
|
||||
adapter: "postgresql",
|
||||
host: "prod.example.com",
|
||||
port: 5432,
|
||||
database: "prod_db",
|
||||
},
|
||||
};
|
||||
|
||||
// Array of items
|
||||
const arrayObject = [
|
||||
{
|
||||
id: 1,
|
||||
name: "Item 1",
|
||||
price: 10.99,
|
||||
tags: ["electronics", "gadgets"],
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Item 2",
|
||||
price: 25.5,
|
||||
tags: ["books", "education"],
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "Item 3",
|
||||
price: 5.0,
|
||||
tags: ["food", "snacks"],
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
name: "Item 4",
|
||||
price: 100.0,
|
||||
tags: ["electronics", "computers"],
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
name: "Item 5",
|
||||
price: 15.75,
|
||||
tags: ["clothing", "accessories"],
|
||||
},
|
||||
];
|
||||
|
||||
// Multiline strings
|
||||
const multilineObject = {
|
||||
description:
|
||||
"This is a multiline string\nthat preserves line breaks\nand indentation.\n\nIt can contain multiple paragraphs\nand special characters: !@#$%^&*()\n",
|
||||
folded: "This is a folded string where line breaks are converted to spaces unless there are\nempty lines like above.",
|
||||
plain: "This is a plain string",
|
||||
quoted: 'This is a quoted string with "escapes"',
|
||||
literal: "This is a literal string with 'quotes'",
|
||||
};
|
||||
|
||||
// Numbers and special values
|
||||
const numbersObject = {
|
||||
integer: 42,
|
||||
negative: -17,
|
||||
float: 3.14159,
|
||||
scientific: 0.000123,
|
||||
infinity: Infinity,
|
||||
negativeInfinity: -Infinity,
|
||||
notANumber: NaN,
|
||||
octal: 493, // 0o755
|
||||
hex: 255, // 0xFF
|
||||
binary: 10, // 0b1010
|
||||
};
|
||||
|
||||
// Dates and timestamps
|
||||
const datesObject = {
|
||||
date: new Date("2024-01-15"),
|
||||
datetime: new Date("2024-01-15T10:30:00Z"),
|
||||
timestamp: new Date("2024-01-15T15:30:00.123456789Z"), // Adjusted for UTC-5
|
||||
canonical: new Date("2024-01-15T10:30:00.123456789Z"),
|
||||
};
|
||||
|
||||
// Stringify benchmarks
|
||||
group("stringify small object", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(smallObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(smallObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(smallObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify medium object", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(mediumObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(mediumObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(mediumObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify large object", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(largeObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(largeObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(largeObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify object with anchors", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(objectWithAnchors);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(objectWithAnchors);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(objectWithAnchors);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify array", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(arrayObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(arrayObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(arrayObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify object with multiline strings", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(multilineObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(multilineObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(multilineObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify object with numbers", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(numbersObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(numbersObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(numbersObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify object with dates", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(datesObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(datesObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(datesObject);
|
||||
});
|
||||
});
|
||||
|
||||
await run();
|
||||
2
bun.lock
2
bun.lock
@@ -40,8 +40,8 @@
|
||||
},
|
||||
},
|
||||
"overrides": {
|
||||
"bun-types": "workspace:packages/bun-types",
|
||||
"@types/bun": "workspace:packages/@types/bun",
|
||||
"bun-types": "workspace:packages/bun-types",
|
||||
},
|
||||
"packages": {
|
||||
"@esbuild/aix-ppc64": ["@esbuild/aix-ppc64@0.21.5", "", { "os": "aix", "cpu": "ppc64" }, "sha512-1SDgH6ZSPTlggy1yI6+Dbkiz8xzpHJEVAlF/AM1tHPLsf5STom9rwtjE4hKAF20FfXXNTFqEYXyJNWh1GiZedQ=="],
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
src/bake/bake.d.ts
|
||||
src/bake/bake.private.d.ts
|
||||
src/bake/bun-framework-react/index.ts
|
||||
src/bake/client/css-reloader.ts
|
||||
src/bake/client/data-view.ts
|
||||
src/bake/client/error-serialization.ts
|
||||
src/bake/client/inspect.ts
|
||||
src/bake/client/JavaScriptSyntaxHighlighter.css
|
||||
src/bake/client/JavaScriptSyntaxHighlighter.ts
|
||||
src/bake/client/overlay.css
|
||||
src/bake/client/overlay.ts
|
||||
src/bake/client/stack-trace.ts
|
||||
src/bake/client/websocket.ts
|
||||
src/bake/debug.ts
|
||||
src/bake/DevServer.bind.ts
|
||||
src/bake/enums.ts
|
||||
src/bake/hmr-module.ts
|
||||
src/bake/hmr-runtime-client.ts
|
||||
src/bake/hmr-runtime-error.ts
|
||||
src/bake/hmr-runtime-server.ts
|
||||
src/bake/server/stack-trace-stub.ts
|
||||
src/bake/shared.ts
|
||||
@@ -1,7 +0,0 @@
|
||||
src/bake.bind.ts
|
||||
src/bake/DevServer.bind.ts
|
||||
src/bun.js/api/BunObject.bind.ts
|
||||
src/bun.js/bindgen_test.bind.ts
|
||||
src/bun.js/bindings/NodeModuleModule.bind.ts
|
||||
src/bun.js/node/node_os.bind.ts
|
||||
src/fmt.bind.ts
|
||||
@@ -1,12 +0,0 @@
|
||||
packages/bun-error/bun-error.css
|
||||
packages/bun-error/img/close.png
|
||||
packages/bun-error/img/error.png
|
||||
packages/bun-error/img/powered-by.png
|
||||
packages/bun-error/img/powered-by.webp
|
||||
packages/bun-error/index.tsx
|
||||
packages/bun-error/markdown.ts
|
||||
packages/bun-error/package.json
|
||||
packages/bun-error/runtime-error.ts
|
||||
packages/bun-error/sourcemap.ts
|
||||
packages/bun-error/stack-trace-parser.ts
|
||||
packages/bun-error/tsconfig.json
|
||||
@@ -1,15 +0,0 @@
|
||||
packages/bun-usockets/src/bsd.c
|
||||
packages/bun-usockets/src/context.c
|
||||
packages/bun-usockets/src/crypto/openssl.c
|
||||
packages/bun-usockets/src/eventing/epoll_kqueue.c
|
||||
packages/bun-usockets/src/eventing/libuv.c
|
||||
packages/bun-usockets/src/loop.c
|
||||
packages/bun-usockets/src/quic.c
|
||||
packages/bun-usockets/src/socket.c
|
||||
packages/bun-usockets/src/udp.c
|
||||
src/asan-config.c
|
||||
src/bun.js/bindings/node/http/llhttp/api.c
|
||||
src/bun.js/bindings/node/http/llhttp/http.c
|
||||
src/bun.js/bindings/node/http/llhttp/llhttp.c
|
||||
src/bun.js/bindings/uv-posix-polyfills.c
|
||||
src/bun.js/bindings/uv-posix-stubs.c
|
||||
@@ -198,6 +198,7 @@ src/bun.js/bindings/ServerRouteList.cpp
|
||||
src/bun.js/bindings/spawn.cpp
|
||||
src/bun.js/bindings/SQLClient.cpp
|
||||
src/bun.js/bindings/sqlite/JSSQLStatement.cpp
|
||||
src/bun.js/bindings/StringBuilderBinding.cpp
|
||||
src/bun.js/bindings/stripANSI.cpp
|
||||
src/bun.js/bindings/Strong.cpp
|
||||
src/bun.js/bindings/TextCodec.cpp
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
src/codegen/bake-codegen.ts
|
||||
src/codegen/bindgen-lib-internal.ts
|
||||
src/codegen/bindgen-lib.ts
|
||||
src/codegen/bindgen.ts
|
||||
src/codegen/buildTypeFlag.ts
|
||||
src/codegen/builtin-parser.ts
|
||||
src/codegen/bundle-functions.ts
|
||||
src/codegen/bundle-modules.ts
|
||||
src/codegen/class-definitions.ts
|
||||
src/codegen/client-js.ts
|
||||
src/codegen/cppbind.ts
|
||||
src/codegen/create-hash-table.ts
|
||||
src/codegen/generate-classes.ts
|
||||
src/codegen/generate-compact-string-table.ts
|
||||
src/codegen/generate-js2native.ts
|
||||
src/codegen/generate-jssink.ts
|
||||
src/codegen/generate-node-errors.ts
|
||||
src/codegen/helpers.ts
|
||||
src/codegen/internal-module-registry-scanner.ts
|
||||
src/codegen/replacements.ts
|
||||
src/codegen/shared-types.ts
|
||||
@@ -1,172 +0,0 @@
|
||||
src/js/builtins.d.ts
|
||||
src/js/builtins/Bake.ts
|
||||
src/js/builtins/BundlerPlugin.ts
|
||||
src/js/builtins/ByteLengthQueuingStrategy.ts
|
||||
src/js/builtins/CommonJS.ts
|
||||
src/js/builtins/ConsoleObject.ts
|
||||
src/js/builtins/CountQueuingStrategy.ts
|
||||
src/js/builtins/Glob.ts
|
||||
src/js/builtins/ImportMetaObject.ts
|
||||
src/js/builtins/Ipc.ts
|
||||
src/js/builtins/JSBufferConstructor.ts
|
||||
src/js/builtins/JSBufferPrototype.ts
|
||||
src/js/builtins/NodeModuleObject.ts
|
||||
src/js/builtins/Peek.ts
|
||||
src/js/builtins/ProcessObjectInternals.ts
|
||||
src/js/builtins/ReadableByteStreamController.ts
|
||||
src/js/builtins/ReadableByteStreamInternals.ts
|
||||
src/js/builtins/ReadableStream.ts
|
||||
src/js/builtins/ReadableStreamBYOBReader.ts
|
||||
src/js/builtins/ReadableStreamBYOBRequest.ts
|
||||
src/js/builtins/ReadableStreamDefaultController.ts
|
||||
src/js/builtins/ReadableStreamDefaultReader.ts
|
||||
src/js/builtins/ReadableStreamInternals.ts
|
||||
src/js/builtins/shell.ts
|
||||
src/js/builtins/StreamInternals.ts
|
||||
src/js/builtins/TextDecoderStream.ts
|
||||
src/js/builtins/TextEncoderStream.ts
|
||||
src/js/builtins/TransformStream.ts
|
||||
src/js/builtins/TransformStreamDefaultController.ts
|
||||
src/js/builtins/TransformStreamInternals.ts
|
||||
src/js/builtins/UtilInspect.ts
|
||||
src/js/builtins/WasmStreaming.ts
|
||||
src/js/builtins/WritableStreamDefaultController.ts
|
||||
src/js/builtins/WritableStreamDefaultWriter.ts
|
||||
src/js/builtins/WritableStreamInternals.ts
|
||||
src/js/bun/ffi.ts
|
||||
src/js/bun/sql.ts
|
||||
src/js/bun/sqlite.ts
|
||||
src/js/internal-for-testing.ts
|
||||
src/js/internal/abort_listener.ts
|
||||
src/js/internal/assert/assertion_error.ts
|
||||
src/js/internal/assert/calltracker.ts
|
||||
src/js/internal/assert/myers_diff.ts
|
||||
src/js/internal/assert/utils.ts
|
||||
src/js/internal/buffer.ts
|
||||
src/js/internal/cluster/child.ts
|
||||
src/js/internal/cluster/isPrimary.ts
|
||||
src/js/internal/cluster/primary.ts
|
||||
src/js/internal/cluster/RoundRobinHandle.ts
|
||||
src/js/internal/cluster/Worker.ts
|
||||
src/js/internal/crypto/x509.ts
|
||||
src/js/internal/debugger.ts
|
||||
src/js/internal/errors.ts
|
||||
src/js/internal/fifo.ts
|
||||
src/js/internal/fixed_queue.ts
|
||||
src/js/internal/freelist.ts
|
||||
src/js/internal/fs/cp-sync.ts
|
||||
src/js/internal/fs/cp.ts
|
||||
src/js/internal/fs/glob.ts
|
||||
src/js/internal/fs/streams.ts
|
||||
src/js/internal/html.ts
|
||||
src/js/internal/http.ts
|
||||
src/js/internal/http/FakeSocket.ts
|
||||
src/js/internal/linkedlist.ts
|
||||
src/js/internal/primordials.js
|
||||
src/js/internal/promisify.ts
|
||||
src/js/internal/shared.ts
|
||||
src/js/internal/sql/errors.ts
|
||||
src/js/internal/sql/mysql.ts
|
||||
src/js/internal/sql/postgres.ts
|
||||
src/js/internal/sql/query.ts
|
||||
src/js/internal/sql/shared.ts
|
||||
src/js/internal/sql/sqlite.ts
|
||||
src/js/internal/stream.promises.ts
|
||||
src/js/internal/stream.ts
|
||||
src/js/internal/streams/add-abort-signal.ts
|
||||
src/js/internal/streams/compose.ts
|
||||
src/js/internal/streams/destroy.ts
|
||||
src/js/internal/streams/duplex.ts
|
||||
src/js/internal/streams/duplexify.ts
|
||||
src/js/internal/streams/duplexpair.ts
|
||||
src/js/internal/streams/end-of-stream.ts
|
||||
src/js/internal/streams/from.ts
|
||||
src/js/internal/streams/lazy_transform.ts
|
||||
src/js/internal/streams/legacy.ts
|
||||
src/js/internal/streams/native-readable.ts
|
||||
src/js/internal/streams/operators.ts
|
||||
src/js/internal/streams/passthrough.ts
|
||||
src/js/internal/streams/pipeline.ts
|
||||
src/js/internal/streams/readable.ts
|
||||
src/js/internal/streams/state.ts
|
||||
src/js/internal/streams/transform.ts
|
||||
src/js/internal/streams/utils.ts
|
||||
src/js/internal/streams/writable.ts
|
||||
src/js/internal/timers.ts
|
||||
src/js/internal/tls.ts
|
||||
src/js/internal/tty.ts
|
||||
src/js/internal/url.ts
|
||||
src/js/internal/util/colors.ts
|
||||
src/js/internal/util/deprecate.ts
|
||||
src/js/internal/util/inspect.d.ts
|
||||
src/js/internal/util/inspect.js
|
||||
src/js/internal/util/mime.ts
|
||||
src/js/internal/validators.ts
|
||||
src/js/internal/webstreams_adapters.ts
|
||||
src/js/node/_http_agent.ts
|
||||
src/js/node/_http_client.ts
|
||||
src/js/node/_http_common.ts
|
||||
src/js/node/_http_incoming.ts
|
||||
src/js/node/_http_outgoing.ts
|
||||
src/js/node/_http_server.ts
|
||||
src/js/node/_stream_duplex.ts
|
||||
src/js/node/_stream_passthrough.ts
|
||||
src/js/node/_stream_readable.ts
|
||||
src/js/node/_stream_transform.ts
|
||||
src/js/node/_stream_wrap.ts
|
||||
src/js/node/_stream_writable.ts
|
||||
src/js/node/_tls_common.ts
|
||||
src/js/node/assert.strict.ts
|
||||
src/js/node/assert.ts
|
||||
src/js/node/async_hooks.ts
|
||||
src/js/node/child_process.ts
|
||||
src/js/node/cluster.ts
|
||||
src/js/node/console.ts
|
||||
src/js/node/crypto.ts
|
||||
src/js/node/dgram.ts
|
||||
src/js/node/diagnostics_channel.ts
|
||||
src/js/node/dns.promises.ts
|
||||
src/js/node/dns.ts
|
||||
src/js/node/domain.ts
|
||||
src/js/node/events.ts
|
||||
src/js/node/fs.promises.ts
|
||||
src/js/node/fs.ts
|
||||
src/js/node/http.ts
|
||||
src/js/node/http2.ts
|
||||
src/js/node/https.ts
|
||||
src/js/node/inspector.ts
|
||||
src/js/node/net.ts
|
||||
src/js/node/os.ts
|
||||
src/js/node/path.posix.ts
|
||||
src/js/node/path.ts
|
||||
src/js/node/path.win32.ts
|
||||
src/js/node/perf_hooks.ts
|
||||
src/js/node/punycode.ts
|
||||
src/js/node/querystring.ts
|
||||
src/js/node/readline.promises.ts
|
||||
src/js/node/readline.ts
|
||||
src/js/node/repl.ts
|
||||
src/js/node/stream.consumers.ts
|
||||
src/js/node/stream.promises.ts
|
||||
src/js/node/stream.ts
|
||||
src/js/node/stream.web.ts
|
||||
src/js/node/test.ts
|
||||
src/js/node/timers.promises.ts
|
||||
src/js/node/timers.ts
|
||||
src/js/node/tls.ts
|
||||
src/js/node/trace_events.ts
|
||||
src/js/node/tty.ts
|
||||
src/js/node/url.ts
|
||||
src/js/node/util.ts
|
||||
src/js/node/v8.ts
|
||||
src/js/node/vm.ts
|
||||
src/js/node/wasi.ts
|
||||
src/js/node/worker_threads.ts
|
||||
src/js/node/zlib.ts
|
||||
src/js/private.d.ts
|
||||
src/js/thirdparty/isomorphic-fetch.ts
|
||||
src/js/thirdparty/node-fetch.ts
|
||||
src/js/thirdparty/undici.js
|
||||
src/js/thirdparty/vercel_fetch.js
|
||||
src/js/thirdparty/ws.js
|
||||
src/js/wasi-runner.js
|
||||
@@ -1,24 +0,0 @@
|
||||
src/node-fallbacks/assert.js
|
||||
src/node-fallbacks/buffer.js
|
||||
src/node-fallbacks/console.js
|
||||
src/node-fallbacks/constants.js
|
||||
src/node-fallbacks/crypto.js
|
||||
src/node-fallbacks/domain.js
|
||||
src/node-fallbacks/events.js
|
||||
src/node-fallbacks/http.js
|
||||
src/node-fallbacks/https.js
|
||||
src/node-fallbacks/net.js
|
||||
src/node-fallbacks/os.js
|
||||
src/node-fallbacks/path.js
|
||||
src/node-fallbacks/process.js
|
||||
src/node-fallbacks/punycode.js
|
||||
src/node-fallbacks/querystring.js
|
||||
src/node-fallbacks/stream.js
|
||||
src/node-fallbacks/string_decoder.js
|
||||
src/node-fallbacks/sys.js
|
||||
src/node-fallbacks/timers.js
|
||||
src/node-fallbacks/timers.promises.js
|
||||
src/node-fallbacks/tty.js
|
||||
src/node-fallbacks/url.js
|
||||
src/node-fallbacks/util.js
|
||||
src/node-fallbacks/zlib.js
|
||||
@@ -1,25 +0,0 @@
|
||||
src/bun.js/api/BunObject.classes.ts
|
||||
src/bun.js/api/crypto.classes.ts
|
||||
src/bun.js/api/ffi.classes.ts
|
||||
src/bun.js/api/filesystem_router.classes.ts
|
||||
src/bun.js/api/Glob.classes.ts
|
||||
src/bun.js/api/h2.classes.ts
|
||||
src/bun.js/api/html_rewriter.classes.ts
|
||||
src/bun.js/api/JSBundler.classes.ts
|
||||
src/bun.js/api/ResumableSink.classes.ts
|
||||
src/bun.js/api/S3Client.classes.ts
|
||||
src/bun.js/api/S3Stat.classes.ts
|
||||
src/bun.js/api/server.classes.ts
|
||||
src/bun.js/api/Shell.classes.ts
|
||||
src/bun.js/api/ShellArgs.classes.ts
|
||||
src/bun.js/api/sockets.classes.ts
|
||||
src/bun.js/api/sourcemap.classes.ts
|
||||
src/bun.js/api/sql.classes.ts
|
||||
src/bun.js/api/streams.classes.ts
|
||||
src/bun.js/api/valkey.classes.ts
|
||||
src/bun.js/api/zlib.classes.ts
|
||||
src/bun.js/node/node.classes.ts
|
||||
src/bun.js/resolve_message.classes.ts
|
||||
src/bun.js/test/jest.classes.ts
|
||||
src/bun.js/webcore/encoding.classes.ts
|
||||
src/bun.js/webcore/response.classes.ts
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,7 @@ option(WEBKIT_VERSION "The version of WebKit to use")
|
||||
option(WEBKIT_LOCAL "If a local version of WebKit should be used instead of downloading")
|
||||
|
||||
if(NOT WEBKIT_VERSION)
|
||||
set(WEBKIT_VERSION f9e86fe8dc0aa2fc1f137cc94777cb10637c23a4)
|
||||
set(WEBKIT_VERSION f474428677de1fafaf13bb3b9a050fe3504dda25)
|
||||
endif()
|
||||
|
||||
string(SUBSTRING ${WEBKIT_VERSION} 0 16 WEBKIT_VERSION_PREFIX)
|
||||
|
||||
@@ -20,7 +20,7 @@ else()
|
||||
unsupported(CMAKE_SYSTEM_NAME)
|
||||
endif()
|
||||
|
||||
set(ZIG_COMMIT "ebe0cdac3170b0462a39caff4d1d7252b608e98d")
|
||||
set(ZIG_COMMIT "e0b7c318f318196c5f81fdf3423816a7b5bb3112")
|
||||
optionx(ZIG_TARGET STRING "The zig target to use" DEFAULT ${DEFAULT_ZIG_TARGET})
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
|
||||
367
docs/api/sql.md
367
docs/api/sql.md
@@ -1,4 +1,4 @@
|
||||
Bun provides native bindings for working with SQL databases through a unified Promise-based API that supports both PostgreSQL and SQLite. The interface is designed to be simple and performant, using tagged template literals for queries and offering features like connection pooling, transactions, and prepared statements.
|
||||
Bun provides native bindings for working with SQL databases through a unified Promise-based API that supports PostgreSQL, MySQL, and SQLite. The interface is designed to be simple and performant, using tagged template literals for queries and offering features like connection pooling, transactions, and prepared statements.
|
||||
|
||||
```ts
|
||||
import { sql, SQL } from "bun";
|
||||
@@ -10,9 +10,16 @@ const users = await sql`
|
||||
LIMIT ${10}
|
||||
`;
|
||||
|
||||
// With a a SQLite db
|
||||
// With MySQL
|
||||
const mysql = new SQL("mysql://user:pass@localhost:3306/mydb");
|
||||
const mysqlResults = await mysql`
|
||||
SELECT * FROM users
|
||||
WHERE active = ${true}
|
||||
`;
|
||||
|
||||
// With SQLite
|
||||
const sqlite = new SQL("sqlite://myapp.db");
|
||||
const results = await sqlite`
|
||||
const sqliteResults = await sqlite`
|
||||
SELECT * FROM users
|
||||
WHERE active = ${1}
|
||||
`;
|
||||
@@ -52,7 +59,7 @@ Bun.SQL provides a unified API for multiple database systems:
|
||||
|
||||
PostgreSQL is used when:
|
||||
|
||||
- The connection string doesn't match SQLite patterns (it's the fallback adapter)
|
||||
- The connection string doesn't match SQLite or MySQL patterns (it's the fallback adapter)
|
||||
- The connection string explicitly uses `postgres://` or `postgresql://` protocols
|
||||
- No connection string is provided and environment variables point to PostgreSQL
|
||||
|
||||
@@ -66,9 +73,82 @@ const pg = new SQL("postgres://user:pass@localhost:5432/mydb");
|
||||
await pg`SELECT ...`;
|
||||
```
|
||||
|
||||
### MySQL
|
||||
|
||||
MySQL support is built into Bun.SQL, providing the same tagged template literal interface with full compatibility for MySQL 5.7+ and MySQL 8.0+:
|
||||
|
||||
```ts
|
||||
import { SQL } from "bun";
|
||||
|
||||
// MySQL connection
|
||||
const mysql = new SQL("mysql://user:password@localhost:3306/database");
|
||||
const mysql2 = new SQL("mysql2://user:password@localhost:3306/database"); // mysql2 protocol also works
|
||||
|
||||
// Using options object
|
||||
const mysql3 = new SQL({
|
||||
adapter: "mysql",
|
||||
hostname: "localhost",
|
||||
port: 3306,
|
||||
database: "myapp",
|
||||
username: "dbuser",
|
||||
password: "secretpass",
|
||||
});
|
||||
|
||||
// Works with parameters - automatically uses prepared statements
|
||||
const users = await mysql`SELECT * FROM users WHERE id = ${userId}`;
|
||||
|
||||
// Transactions work the same as PostgreSQL
|
||||
await mysql.begin(async tx => {
|
||||
await tx`INSERT INTO users (name) VALUES (${"Alice"})`;
|
||||
await tx`UPDATE accounts SET balance = balance - 100 WHERE user_id = ${userId}`;
|
||||
});
|
||||
|
||||
// Bulk inserts
|
||||
const newUsers = [
|
||||
{ name: "Alice", email: "alice@example.com" },
|
||||
{ name: "Bob", email: "bob@example.com" },
|
||||
];
|
||||
await mysql`INSERT INTO users ${mysql(newUsers)}`;
|
||||
```
|
||||
|
||||
{% details summary="MySQL Connection String Formats" %}
|
||||
|
||||
MySQL accepts various URL formats for connection strings:
|
||||
|
||||
```ts
|
||||
// Standard mysql:// protocol
|
||||
new SQL("mysql://user:pass@localhost:3306/database");
|
||||
new SQL("mysql://user:pass@localhost/database"); // Default port 3306
|
||||
|
||||
// mysql2:// protocol (compatibility with mysql2 npm package)
|
||||
new SQL("mysql2://user:pass@localhost:3306/database");
|
||||
|
||||
// With query parameters
|
||||
new SQL("mysql://user:pass@localhost/db?ssl=true");
|
||||
|
||||
// Unix socket connection
|
||||
new SQL("mysql://user:pass@/database?socket=/var/run/mysqld/mysqld.sock");
|
||||
```
|
||||
|
||||
{% /details %}
|
||||
|
||||
{% details summary="MySQL-Specific Features" %}
|
||||
|
||||
MySQL databases support:
|
||||
|
||||
- **Prepared statements**: Automatically created for parameterized queries with statement caching
|
||||
- **Binary protocol**: For better performance with prepared statements and accurate type handling
|
||||
- **Multiple result sets**: Support for stored procedures returning multiple result sets
|
||||
- **Authentication plugins**: Support for mysql_native_password, caching_sha2_password (MySQL 8.0 default), and sha256_password
|
||||
- **SSL/TLS connections**: Configurable SSL modes similar to PostgreSQL
|
||||
- **Connection attributes**: Client information sent to server for monitoring
|
||||
- **Query pipelining**: Execute multiple prepared statements without waiting for responses
|
||||
|
||||
{% /details %}
|
||||
|
||||
### SQLite
|
||||
|
||||
SQLite support is now built into Bun.SQL, providing the same tagged template literal interface as PostgreSQL:
|
||||
SQLite support is built into Bun.SQL, providing the same tagged template literal interface:
|
||||
|
||||
```ts
|
||||
import { SQL } from "bun";
|
||||
@@ -362,7 +442,24 @@ await query;
|
||||
|
||||
### Automatic Database Detection
|
||||
|
||||
When using `Bun.sql()` without arguments or `new SQL()` with a connection string, the adapter is automatically detected based on the URL format. SQLite becomes the default adapter in these cases:
|
||||
When using `Bun.sql()` without arguments or `new SQL()` with a connection string, the adapter is automatically detected based on the URL format:
|
||||
|
||||
#### MySQL Auto-Detection
|
||||
|
||||
MySQL is automatically selected when the connection string matches these patterns:
|
||||
|
||||
- `mysql://...` - MySQL protocol URLs
|
||||
- `mysql2://...` - MySQL2 protocol URLs (compatibility alias)
|
||||
|
||||
```ts
|
||||
// These all use MySQL automatically (no adapter needed)
|
||||
const sql1 = new SQL("mysql://user:pass@localhost/mydb");
|
||||
const sql2 = new SQL("mysql2://user:pass@localhost:3306/mydb");
|
||||
|
||||
// Works with DATABASE_URL environment variable
|
||||
DATABASE_URL="mysql://user:pass@localhost/mydb" bun run app.js
|
||||
DATABASE_URL="mysql2://user:pass@localhost:3306/mydb" bun run app.js
|
||||
```
|
||||
|
||||
#### SQLite Auto-Detection
|
||||
|
||||
@@ -388,17 +485,42 @@ DATABASE_URL="file://./data/app.db" bun run app.js
|
||||
|
||||
#### PostgreSQL Auto-Detection
|
||||
|
||||
PostgreSQL is the default for all other connection strings:
|
||||
PostgreSQL is the default for connection strings that don't match MySQL or SQLite patterns:
|
||||
|
||||
```bash
|
||||
# PostgreSQL is detected for these patterns
|
||||
DATABASE_URL="postgres://user:pass@localhost:5432/mydb" bun run app.js
|
||||
DATABASE_URL="postgresql://user:pass@localhost:5432/mydb" bun run app.js
|
||||
|
||||
# Or any URL that doesn't match SQLite patterns
|
||||
# Or any URL that doesn't match MySQL or SQLite patterns
|
||||
DATABASE_URL="localhost:5432/mydb" bun run app.js
|
||||
```
|
||||
|
||||
### MySQL Environment Variables
|
||||
|
||||
MySQL connections can be configured via environment variables:
|
||||
|
||||
```bash
|
||||
# Primary connection URL (checked first)
|
||||
MYSQL_URL="mysql://user:pass@localhost:3306/mydb"
|
||||
|
||||
# Alternative: DATABASE_URL with MySQL protocol
|
||||
DATABASE_URL="mysql://user:pass@localhost:3306/mydb"
|
||||
DATABASE_URL="mysql2://user:pass@localhost:3306/mydb"
|
||||
```
|
||||
|
||||
If no connection URL is provided, MySQL checks these individual parameters:
|
||||
|
||||
| Environment Variable | Default Value | Description |
|
||||
| ------------------------ | ------------- | -------------------------------- |
|
||||
| `MYSQL_HOST` | `localhost` | Database host |
|
||||
| `MYSQL_PORT` | `3306` | Database port |
|
||||
| `MYSQL_USER` | `root` | Database user |
|
||||
| `MYSQL_PASSWORD` | (empty) | Database password |
|
||||
| `MYSQL_DATABASE` | `mysql` | Database name |
|
||||
| `MYSQL_URL` | (empty) | Primary connection URL for MySQL |
|
||||
| `TLS_MYSQL_DATABASE_URL` | (empty) | SSL/TLS-enabled connection URL |
|
||||
|
||||
### PostgreSQL Environment Variables
|
||||
|
||||
The following environment variables can be used to define the PostgreSQL connection:
|
||||
@@ -456,6 +578,54 @@ The `--sql-preconnect` flag will automatically establish a PostgreSQL connection
|
||||
|
||||
You can configure your database connection manually by passing options to the SQL constructor. Options vary depending on the database adapter:
|
||||
|
||||
### MySQL Options
|
||||
|
||||
```ts
|
||||
import { SQL } from "bun";
|
||||
|
||||
const db = new SQL({
|
||||
// Required for MySQL when using options object
|
||||
adapter: "mysql",
|
||||
|
||||
// Connection details
|
||||
hostname: "localhost",
|
||||
port: 3306,
|
||||
database: "myapp",
|
||||
username: "dbuser",
|
||||
password: "secretpass",
|
||||
|
||||
// Unix socket connection (alternative to hostname/port)
|
||||
// socket: "/var/run/mysqld/mysqld.sock",
|
||||
|
||||
// Connection pool settings
|
||||
max: 20, // Maximum connections in pool (default: 10)
|
||||
idleTimeout: 30, // Close idle connections after 30s
|
||||
maxLifetime: 0, // Connection lifetime in seconds (0 = forever)
|
||||
connectionTimeout: 30, // Timeout when establishing new connections
|
||||
|
||||
// SSL/TLS options
|
||||
ssl: "prefer", // or "disable", "require", "verify-ca", "verify-full"
|
||||
// tls: {
|
||||
// rejectUnauthorized: true,
|
||||
// ca: "path/to/ca.pem",
|
||||
// key: "path/to/key.pem",
|
||||
// cert: "path/to/cert.pem",
|
||||
// },
|
||||
|
||||
// Callbacks
|
||||
onconnect: client => {
|
||||
console.log("Connected to MySQL");
|
||||
},
|
||||
onclose: (client, err) => {
|
||||
if (err) {
|
||||
console.error("MySQL connection error:", err);
|
||||
} else {
|
||||
console.log("MySQL connection closed");
|
||||
}
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### PostgreSQL Options
|
||||
|
||||
```ts
|
||||
@@ -979,11 +1149,106 @@ console.log(typeof x, x); // "bigint" 9223372036854777n
|
||||
There's still some things we haven't finished yet.
|
||||
|
||||
- Connection preloading via `--db-preconnect` Bun CLI flag
|
||||
- MySQL support: [we're working on it](https://github.com/oven-sh/bun/pull/15274)
|
||||
- Column name transforms (e.g. `snake_case` to `camelCase`). This is mostly blocked on a unicode-aware implementation of changing the case in C++ using WebKit's `WTF::String`.
|
||||
- Column type transforms
|
||||
|
||||
### Postgres-specific features
|
||||
## Database-Specific Features
|
||||
|
||||
#### Authentication Methods
|
||||
|
||||
MySQL supports multiple authentication plugins that are automatically negotiated:
|
||||
|
||||
- **`mysql_native_password`** - Traditional MySQL authentication, widely compatible
|
||||
- **`caching_sha2_password`** - Default in MySQL 8.0+, more secure with RSA key exchange
|
||||
- **`sha256_password`** - SHA-256 based authentication
|
||||
|
||||
The client automatically handles authentication plugin switching when requested by the server, including secure password exchange over non-SSL connections.
|
||||
|
||||
#### Prepared Statements & Performance
|
||||
|
||||
MySQL uses server-side prepared statements for all parameterized queries:
|
||||
|
||||
```ts
|
||||
// This automatically creates a prepared statement on the server
|
||||
const user = await mysql`SELECT * FROM users WHERE id = ${userId}`;
|
||||
|
||||
// Prepared statements are cached and reused for identical queries
|
||||
for (const id of userIds) {
|
||||
// Same prepared statement is reused
|
||||
await mysql`SELECT * FROM users WHERE id = ${id}`;
|
||||
}
|
||||
|
||||
// Query pipelining - multiple statements sent without waiting
|
||||
const [users, orders, products] = await Promise.all([
|
||||
mysql`SELECT * FROM users WHERE active = ${true}`,
|
||||
mysql`SELECT * FROM orders WHERE status = ${"pending"}`,
|
||||
mysql`SELECT * FROM products WHERE in_stock = ${true}`,
|
||||
]);
|
||||
```
|
||||
|
||||
#### Multiple Result Sets
|
||||
|
||||
MySQL can return multiple result sets from multi-statement queries:
|
||||
|
||||
```ts
|
||||
const mysql = new SQL("mysql://user:pass@localhost/mydb");
|
||||
|
||||
// Multi-statement queries with simple() method
|
||||
const multiResults = await mysql`
|
||||
SELECT * FROM users WHERE id = 1;
|
||||
SELECT * FROM orders WHERE user_id = 1;
|
||||
`.simple();
|
||||
```
|
||||
|
||||
#### Character Sets & Collations
|
||||
|
||||
Bun.SQL automatically uses `utf8mb4` character set for MySQL connections, ensuring full Unicode support including emojis. This is the recommended character set for modern MySQL applications.
|
||||
|
||||
#### Connection Attributes
|
||||
|
||||
Bun automatically sends client information to MySQL for better monitoring:
|
||||
|
||||
```ts
|
||||
// These attributes are sent automatically:
|
||||
// _client_name: "Bun"
|
||||
// _client_version: <bun version>
|
||||
// You can see these in MySQL's performance_schema.session_connect_attrs
|
||||
```
|
||||
|
||||
#### Type Handling
|
||||
|
||||
MySQL types are automatically converted to JavaScript types:
|
||||
|
||||
| MySQL Type | JavaScript Type | Notes |
|
||||
| --------------------------------------- | ------------------------ | ---------------------------------------------------------------------------------------------------- |
|
||||
| INT, TINYINT, MEDIUMINT | number | Within safe integer range |
|
||||
| BIGINT | string, number or BigInt | If the value fits in i32/u32 size will be number otherwise string or BigInt Based on `bigint` option |
|
||||
| DECIMAL, NUMERIC | string | To preserve precision |
|
||||
| FLOAT, DOUBLE | number | |
|
||||
| DATE | Date | JavaScript Date object |
|
||||
| DATETIME, TIMESTAMP | Date | With timezone handling |
|
||||
| TIME | number | Total of microseconds |
|
||||
| YEAR | number | |
|
||||
| CHAR, VARCHAR, VARSTRING, STRING | string | |
|
||||
| TINY TEXT, MEDIUM TEXT, TEXT, LONG TEXT | string | |
|
||||
| TINY BLOB, MEDIUM BLOB, BLOG, LONG BLOB | string | BLOB Types are alias for TEXT types |
|
||||
| JSON | object/array | Automatically parsed |
|
||||
| BIT(1) | boolean | BIT(1) in MySQL |
|
||||
| GEOMETRY | string | Geometry data |
|
||||
|
||||
#### Differences from PostgreSQL
|
||||
|
||||
While the API is unified, there are some behavioral differences:
|
||||
|
||||
1. **Parameter placeholders**: MySQL uses `?` internally but Bun converts `$1, $2` style automatically
|
||||
2. **RETURNING clause**: MySQL doesn't support RETURNING; use `result.lastInsertRowid` or a separate SELECT
|
||||
3. **Array types**: MySQL doesn't have native array types like PostgreSQL
|
||||
|
||||
### MySQL-Specific Features
|
||||
|
||||
We haven't implemented `LOAD DATA INFILE` support yet
|
||||
|
||||
### PostgreSQL-Specific Features
|
||||
|
||||
We haven't implemented these yet:
|
||||
|
||||
@@ -998,6 +1263,88 @@ We also haven't implemented some of the more uncommon features like:
|
||||
- Point & PostGIS types
|
||||
- All the multi-dimensional integer array types (only a couple of the types are supported)
|
||||
|
||||
## Common Patterns & Best Practices
|
||||
|
||||
### Working with MySQL Result Sets
|
||||
|
||||
```ts
|
||||
// Getting insert ID after INSERT
|
||||
const result = await mysql`INSERT INTO users (name) VALUES (${"Alice"})`;
|
||||
console.log(result.lastInsertRowid); // MySQL's LAST_INSERT_ID()
|
||||
|
||||
// Handling affected rows
|
||||
const updated =
|
||||
await mysql`UPDATE users SET active = ${false} WHERE age < ${18}`;
|
||||
console.log(updated.affectedRows); // Number of rows updated
|
||||
|
||||
// Using MySQL-specific functions
|
||||
const now = await mysql`SELECT NOW() as current_time`;
|
||||
const uuid = await mysql`SELECT UUID() as id`;
|
||||
```
|
||||
|
||||
### MySQL Error Handling
|
||||
|
||||
```ts
|
||||
try {
|
||||
await mysql`INSERT INTO users (email) VALUES (${"duplicate@email.com"})`;
|
||||
} catch (error) {
|
||||
if (error.code === "ER_DUP_ENTRY") {
|
||||
console.log("Duplicate entry detected");
|
||||
} else if (error.code === "ER_ACCESS_DENIED_ERROR") {
|
||||
console.log("Access denied");
|
||||
} else if (error.code === "ER_BAD_DB_ERROR") {
|
||||
console.log("Database does not exist");
|
||||
}
|
||||
// MySQL error codes are compatible with mysql/mysql2 packages
|
||||
}
|
||||
```
|
||||
|
||||
### Performance Tips for MySQL
|
||||
|
||||
1. **Use connection pooling**: Set appropriate `max` pool size based on your workload
|
||||
2. **Enable prepared statements**: They're enabled by default and improve performance
|
||||
3. **Use transactions for bulk operations**: Group related queries in transactions
|
||||
4. **Index properly**: MySQL relies heavily on indexes for query performance
|
||||
5. **Use `utf8mb4` charset**: It's set by default and handles all Unicode characters
|
||||
|
||||
## Frequently Asked Questions
|
||||
|
||||
> Why is this `Bun.sql` and not `Bun.postgres`?
|
||||
|
||||
The plan was to add more database drivers in the future. Now with MySQL support added, this unified API supports PostgreSQL, MySQL, and SQLite.
|
||||
|
||||
> How do I know which database adapter is being used?
|
||||
|
||||
The adapter is automatically detected from the connection string:
|
||||
|
||||
- URLs starting with `mysql://` or `mysql2://` use MySQL
|
||||
- URLs matching SQLite patterns (`:memory:`, `sqlite://`, `file://`) use SQLite
|
||||
- Everything else defaults to PostgreSQL
|
||||
|
||||
> Are MySQL stored procedures supported?
|
||||
|
||||
Yes, stored procedures are fully supported including OUT parameters and multiple result sets:
|
||||
|
||||
```ts
|
||||
// Call stored procedure
|
||||
const results = await mysql`CALL GetUserStats(${userId}, @total_orders)`;
|
||||
|
||||
// Get OUT parameter
|
||||
const outParam = await mysql`SELECT @total_orders as total`;
|
||||
```
|
||||
|
||||
> Can I use MySQL-specific SQL syntax?
|
||||
|
||||
Yes, you can use any MySQL-specific syntax:
|
||||
|
||||
```ts
|
||||
// MySQL-specific syntax works fine
|
||||
await mysql`SET @user_id = ${userId}`;
|
||||
await mysql`SHOW TABLES`;
|
||||
await mysql`DESCRIBE users`;
|
||||
await mysql`EXPLAIN SELECT * FROM users WHERE id = ${id}`;
|
||||
```
|
||||
|
||||
## Why not just use an existing library?
|
||||
|
||||
npm packages like postgres.js, pg, and node-postgres can be used in Bun too. They're great options.
|
||||
|
||||
@@ -122,6 +122,59 @@ Messages are automatically enqueued until the worker is ready, so there is no ne
|
||||
|
||||
To send messages, use [`worker.postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage) and [`self.postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage). This leverages the [HTML Structured Clone Algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).
|
||||
|
||||
### Performance optimizations
|
||||
|
||||
Bun includes optimized fast paths for `postMessage` to dramatically improve performance for common data types:
|
||||
|
||||
**String fast path** - When posting pure string values, Bun bypasses the structured clone algorithm entirely, achieving significant performance gains with no serialization overhead.
|
||||
|
||||
**Simple object fast path** - For plain objects containing only primitive values (strings, numbers, booleans, null, undefined), Bun uses an optimized serialization path that stores properties directly without full structured cloning.
|
||||
|
||||
The simple object fast path activates when the object:
|
||||
|
||||
- Is a plain object with no prototype chain modifications
|
||||
- Contains only enumerable, configurable data properties
|
||||
- Has no indexed properties or getter/setter methods
|
||||
- All property values are primitives or strings
|
||||
|
||||
With these fast paths, Bun's `postMessage` performs **2-241x faster** because the message length no longer has a meaningful impact on performance.
|
||||
|
||||
**Bun (with fast paths):**
|
||||
|
||||
```
|
||||
postMessage({ prop: 11 chars string, ...9 more props }) - 648ns
|
||||
postMessage({ prop: 14 KB string, ...9 more props }) - 719ns
|
||||
postMessage({ prop: 3 MB string, ...9 more props }) - 1.26µs
|
||||
```
|
||||
|
||||
**Node.js v24.6.0 (for comparison):**
|
||||
|
||||
```
|
||||
postMessage({ prop: 11 chars string, ...9 more props }) - 1.19µs
|
||||
postMessage({ prop: 14 KB string, ...9 more props }) - 2.69µs
|
||||
postMessage({ prop: 3 MB string, ...9 more props }) - 304µs
|
||||
```
|
||||
|
||||
```js
|
||||
// String fast path - optimized
|
||||
postMessage("Hello, worker!");
|
||||
|
||||
// Simple object fast path - optimized
|
||||
postMessage({
|
||||
message: "Hello",
|
||||
count: 42,
|
||||
enabled: true,
|
||||
data: null,
|
||||
});
|
||||
|
||||
// Complex objects still work but use standard structured clone
|
||||
postMessage({
|
||||
nested: { deep: { object: true } },
|
||||
date: new Date(),
|
||||
buffer: new ArrayBuffer(8),
|
||||
});
|
||||
```
|
||||
|
||||
```js
|
||||
// On the worker thread, `postMessage` is automatically "routed" to the parent thread.
|
||||
postMessage({ hello: "world" });
|
||||
|
||||
@@ -245,8 +245,8 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot
|
||||
---
|
||||
|
||||
- `--jsx-side-effects`
|
||||
- n/a
|
||||
- JSX is always assumed to be side-effect-free
|
||||
- `--jsx-side-effects`
|
||||
- Controls whether JSX expressions are marked as `/* @__PURE__ */` for dead code elimination. Default is `false` (JSX marked as pure).
|
||||
|
||||
---
|
||||
|
||||
@@ -617,7 +617,7 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot
|
||||
|
||||
- `jsxSideEffects`
|
||||
- `jsxSideEffects`
|
||||
- Not supported in JS API, configure in `tsconfig.json`
|
||||
- Controls whether JSX expressions are marked as pure for dead code elimination
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -230,16 +230,15 @@ $ bun install --backend copyfile
|
||||
|
||||
**`symlink`** is typically only used for `file:` dependencies (and eventually `link:`) internally. To prevent infinite loops, it skips symlinking the `node_modules` folder.
|
||||
|
||||
If you install with `--backend=symlink`, Node.js won't resolve node_modules of dependencies unless each dependency has its own node_modules folder or you pass `--preserve-symlinks` to `node`. See [Node.js documentation on `--preserve-symlinks`](https://nodejs.org/api/cli.html#--preserve-symlinks).
|
||||
If you install with `--backend=symlink`, Node.js won't resolve node_modules of dependencies unless each dependency has its own node_modules folder or you pass `--preserve-symlinks` to `node` or `bun`. See [Node.js documentation on `--preserve-symlinks`](https://nodejs.org/api/cli.html#--preserve-symlinks).
|
||||
|
||||
```bash
|
||||
$ rm -rf node_modules
|
||||
$ bun install --backend symlink
|
||||
$ bun --preserve-symlinks ./my-file.js
|
||||
$ node --preserve-symlinks ./my-file.js # https://nodejs.org/api/cli.html#--preserve-symlinks
|
||||
```
|
||||
|
||||
Bun's runtime does not currently expose an equivalent of `--preserve-symlinks`, though the code for it does exist.
|
||||
|
||||
## npm registry metadata
|
||||
|
||||
bun uses a binary format for caching NPM registry responses. This loads much faster than JSON and tends to be smaller on disk.
|
||||
|
||||
@@ -8,6 +8,14 @@ The `bun` CLI contains a Node.js-compatible package manager designed to be a dra
|
||||
|
||||
{% /callout %}
|
||||
|
||||
{% callout %}
|
||||
|
||||
**💾 Disk efficient** — Bun install stores all packages in a global cache (`~/.bun/install/cache/`) and creates hardlinks (Linux) or copy-on-write clones (macOS) to `node_modules`. This means duplicate packages across projects point to the same underlying data, taking up virtually no extra disk space.
|
||||
|
||||
For more details, see [Package manager > Global cache](https://bun.com/docs/install/cache).
|
||||
|
||||
{% /callout %}
|
||||
|
||||
{% details summary="For Linux users" %}
|
||||
The recommended minimum Linux Kernel version is 5.6. If you're on Linux kernel 5.1 - 5.5, `bun install` will work, but HTTP requests will be slow due to a lack of support for io_uring's `connect()` operation.
|
||||
|
||||
@@ -207,6 +215,12 @@ Isolated installs create a central package store in `node_modules/.bun/` with sy
|
||||
|
||||
For complete documentation on isolated installs, refer to [Package manager > Isolated installs](https://bun.com/docs/install/isolated).
|
||||
|
||||
## Disk efficiency
|
||||
|
||||
Bun uses a global cache at `~/.bun/install/cache/` to minimize disk usage. Packages are stored once and linked to `node_modules` using hardlinks (Linux/Windows) or copy-on-write (macOS), so duplicate packages across projects don't consume additional disk space.
|
||||
|
||||
For complete documentation refer to [Package manager > Global cache](https://bun.com/docs/install/cache).
|
||||
|
||||
## Configuration
|
||||
|
||||
The default behavior of `bun install` can be configured in `bunfig.toml`. The default values are shown below.
|
||||
|
||||
@@ -48,12 +48,12 @@ This behavior is configurable with the `--backend` flag, which is respected by a
|
||||
- **`copyfile`**: The fallback used when any of the above fail. It is the slowest option. On macOS, it uses `fcopyfile()`; on Linux it uses `copy_file_range()`.
|
||||
- **`symlink`**: Currently used only `file:` (and eventually `link:`) dependencies. To prevent infinite loops, it skips symlinking the `node_modules` folder.
|
||||
|
||||
If you install with `--backend=symlink`, Node.js won't resolve node_modules of dependencies unless each dependency has its own `node_modules` folder or you pass `--preserve-symlinks` to `node`. See [Node.js documentation on `--preserve-symlinks`](https://nodejs.org/api/cli.html#--preserve-symlinks).
|
||||
If you install with `--backend=symlink`, Node.js won't resolve node_modules of dependencies unless each dependency has its own `node_modules` folder or you pass `--preserve-symlinks` to `node` or `bun`. See [Node.js documentation on `--preserve-symlinks`](https://nodejs.org/api/cli.html#--preserve-symlinks).
|
||||
|
||||
```bash
|
||||
$ bun install --backend symlink
|
||||
$ node --preserve-symlinks ./foo.js
|
||||
$ bun --preserve-symlinks ./foo.js
|
||||
```
|
||||
|
||||
Bun's runtime does not currently expose an equivalent of `--preserve-symlinks`.
|
||||
{% /details %}
|
||||
|
||||
@@ -246,6 +246,65 @@ The module from which the component factory function (`createElement`, `jsx`, `j
|
||||
|
||||
{% /table %}
|
||||
|
||||
### `jsxSideEffects`
|
||||
|
||||
By default, Bun marks JSX expressions as `/* @__PURE__ */` so they can be removed during bundling if they are unused (known as "dead code elimination" or "tree shaking"). Set `jsxSideEffects` to `true` to prevent this behavior.
|
||||
|
||||
{% table %}
|
||||
|
||||
- Compiler options
|
||||
- Transpiled output
|
||||
|
||||
---
|
||||
|
||||
- ```jsonc
|
||||
{
|
||||
"jsx": "react",
|
||||
// jsxSideEffects is false by default
|
||||
}
|
||||
```
|
||||
|
||||
- ```tsx
|
||||
// JSX expressions are marked as pure
|
||||
/* @__PURE__ */ React.createElement("div", null, "Hello");
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
- ```jsonc
|
||||
{
|
||||
"jsx": "react",
|
||||
"jsxSideEffects": true,
|
||||
}
|
||||
```
|
||||
|
||||
- ```tsx
|
||||
// JSX expressions are not marked as pure
|
||||
React.createElement("div", null, "Hello");
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
- ```jsonc
|
||||
{
|
||||
"jsx": "react-jsx",
|
||||
"jsxSideEffects": true,
|
||||
}
|
||||
```
|
||||
|
||||
- ```tsx
|
||||
// Automatic runtime also respects jsxSideEffects
|
||||
jsx("div", { children: "Hello" });
|
||||
```
|
||||
|
||||
{% /table %}
|
||||
|
||||
This option is also available as a CLI flag:
|
||||
|
||||
```bash
|
||||
$ bun build --jsx-side-effects
|
||||
```
|
||||
|
||||
### JSX pragma
|
||||
|
||||
All of these values can be set on a per-file basis using _pragmas_. A pragma is a special comment that sets a compiler option in a particular file.
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
"watch-windows": "bun run zig build check-windows --watch -fincremental --prominent-compile-errors --global-cache-dir build/debug/zig-check-cache --zig-lib-dir vendor/zig/lib",
|
||||
"bd:v": "(bun run --silent build:debug &> /tmp/bun.debug.build.log || (cat /tmp/bun.debug.build.log && rm -rf /tmp/bun.debug.build.log && exit 1)) && rm -f /tmp/bun.debug.build.log && ./build/debug/bun-debug",
|
||||
"bd": "BUN_DEBUG_QUIET_LOGS=1 bun --silent bd:v",
|
||||
"build:debug": "export COMSPEC=\"C:\\Windows\\System32\\cmd.exe\" && bun scripts/glob-sources.mjs > /dev/null && bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -B build/debug --log-level=NOTICE",
|
||||
"build:debug": "export COMSPEC=\"C:\\Windows\\System32\\cmd.exe\" && bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -B build/debug --log-level=NOTICE",
|
||||
"build:debug:asan": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -DENABLE_ASAN=ON -B build/debug-asan --log-level=NOTICE",
|
||||
"build:release": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -B build/release",
|
||||
"build:ci": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=ON -DCI=true -B build/release-ci --verbose --fresh",
|
||||
|
||||
115
packages/bun-types/bun.d.ts
vendored
115
packages/bun-types/bun.d.ts
vendored
@@ -617,45 +617,6 @@ declare module "bun" {
|
||||
* @returns A JavaScript object
|
||||
*/
|
||||
export function parse(input: string): object;
|
||||
|
||||
/**
|
||||
* Convert a JavaScript object to a TOML string.
|
||||
*
|
||||
* @category Utilities
|
||||
*
|
||||
* @param value The JavaScript object to stringify
|
||||
* @returns A TOML string
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { TOML } from "bun";
|
||||
*
|
||||
* const obj = {
|
||||
* title: "TOML Example",
|
||||
* database: {
|
||||
* server: "192.168.1.1",
|
||||
* ports: [8001, 8001, 8002],
|
||||
* connection_max: 5000,
|
||||
* enabled: true,
|
||||
* }
|
||||
* };
|
||||
*
|
||||
* console.log(TOML.stringify(obj));
|
||||
* // Output:
|
||||
* // title = "TOML Example"
|
||||
* //
|
||||
* // [database]
|
||||
* // server = "192.168.1.1"
|
||||
* // ports = [
|
||||
* // 8001,
|
||||
* // 8001,
|
||||
* // 8002
|
||||
* // ]
|
||||
* // connection_max = 5000
|
||||
* // enabled = true
|
||||
* ```
|
||||
*/
|
||||
export function stringify(value: any): string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -683,6 +644,38 @@ declare module "bun" {
|
||||
* ```
|
||||
*/
|
||||
export function parse(input: string): unknown;
|
||||
|
||||
/**
|
||||
* Convert a JavaScript value into a YAML string. Strings are double quoted if they contain keywords, non-printable or
|
||||
* escaped characters, or if a YAML parser would parse them as numbers. Anchors and aliases are inferred from objects, allowing cycles.
|
||||
*
|
||||
* @category Utilities
|
||||
*
|
||||
* @param input The JavaScript value to stringify.
|
||||
* @param replacer Currently not supported.
|
||||
* @param space A number for how many spaces each level of indentation gets, or a string used as indentation. The number is clamped between 0 and 10, and the first 10 characters of the string are used.
|
||||
* @returns A string containing the YAML document.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { YAML } from "bun";
|
||||
*
|
||||
* const input = {
|
||||
* abc: "def"
|
||||
* };
|
||||
* console.log(YAML.stringify(input));
|
||||
* // # output
|
||||
* // abc: def
|
||||
*
|
||||
* const cycle = {};
|
||||
* cycle.obj = cycle;
|
||||
* console.log(YAML.stringify(cycle));
|
||||
* // # output
|
||||
* // &root
|
||||
* // obj:
|
||||
* // *root
|
||||
*/
|
||||
export function stringify(input: unknown, replacer?: undefined | null, space?: string | number): string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1712,11 +1705,16 @@ declare module "bun" {
|
||||
* @see [Bun.build API docs](https://bun.com/docs/bundler#api)
|
||||
*/
|
||||
interface BuildConfigBase {
|
||||
entrypoints: string[]; // list of file path
|
||||
/**
|
||||
* List of entrypoints, usually file paths
|
||||
*/
|
||||
entrypoints: string[];
|
||||
|
||||
/**
|
||||
* @default "browser"
|
||||
*/
|
||||
target?: Target; // default: "browser"
|
||||
|
||||
/**
|
||||
* Output module format. Top-level await is only supported for `"esm"`.
|
||||
*
|
||||
@@ -1947,12 +1945,28 @@ declare module "bun" {
|
||||
* ```
|
||||
*/
|
||||
compile: boolean | Bun.Build.Target | CompileBuildOptions;
|
||||
|
||||
/**
|
||||
* Splitting is not currently supported with `.compile`
|
||||
*/
|
||||
splitting?: never;
|
||||
}
|
||||
|
||||
interface NormalBuildConfig extends BuildConfigBase {
|
||||
/**
|
||||
* Enable code splitting
|
||||
*
|
||||
* This does not currently work with {@link CompileBuildConfig.compile `compile`}
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
splitting?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see [Bun.build API docs](https://bun.com/docs/bundler#api)
|
||||
*/
|
||||
type BuildConfig = BuildConfigBase | CompileBuildConfig;
|
||||
type BuildConfig = CompileBuildConfig | NormalBuildConfig;
|
||||
|
||||
/**
|
||||
* Hash and verify passwords using argon2 or bcrypt
|
||||
@@ -3832,6 +3846,11 @@ declare module "bun" {
|
||||
* @category HTTP & Networking
|
||||
*/
|
||||
interface Server extends Disposable {
|
||||
/*
|
||||
* Closes all connections connected to this server which are not sending a request or waiting for a response. Does not close the listen socket.
|
||||
*/
|
||||
closeIdleConnections(): void;
|
||||
|
||||
/**
|
||||
* Stop listening to prevent new connections from being accepted.
|
||||
*
|
||||
@@ -5553,6 +5572,11 @@ declare module "bun" {
|
||||
type OnLoadCallback = (args: OnLoadArgs) => OnLoadResult | Promise<OnLoadResult>;
|
||||
type OnStartCallback = () => void | Promise<void>;
|
||||
type OnEndCallback = (result: BuildOutput) => void | Promise<void>;
|
||||
type OnBeforeParseCallback = {
|
||||
napiModule: unknown;
|
||||
symbol: string;
|
||||
external?: unknown | undefined;
|
||||
};
|
||||
|
||||
interface OnResolveArgs {
|
||||
/**
|
||||
@@ -5649,14 +5673,7 @@ declare module "bun" {
|
||||
* @returns `this` for method chaining
|
||||
*/
|
||||
onEnd(callback: OnEndCallback): this;
|
||||
onBeforeParse(
|
||||
constraints: PluginConstraints,
|
||||
callback: {
|
||||
napiModule: unknown;
|
||||
symbol: string;
|
||||
external?: unknown | undefined;
|
||||
},
|
||||
): this;
|
||||
onBeforeParse(constraints: PluginConstraints, callback: OnBeforeParseCallback): this;
|
||||
/**
|
||||
* Register a callback to load imports with a specific import specifier
|
||||
* @param constraints The constraints to apply the plugin to
|
||||
|
||||
19
packages/bun-types/ffi.d.ts
vendored
19
packages/bun-types/ffi.d.ts
vendored
@@ -219,44 +219,39 @@ declare module "bun:ffi" {
|
||||
|
||||
/**
|
||||
* int64 is a 64-bit signed integer
|
||||
*
|
||||
* This is not implemented yet!
|
||||
*/
|
||||
int64_t = 7,
|
||||
/**
|
||||
* i64 is a 64-bit signed integer
|
||||
*
|
||||
* This is not implemented yet!
|
||||
*/
|
||||
i64 = 7,
|
||||
|
||||
/**
|
||||
* 64-bit unsigned integer
|
||||
*
|
||||
* This is not implemented yet!
|
||||
*/
|
||||
uint64_t = 8,
|
||||
/**
|
||||
* 64-bit unsigned integer
|
||||
*
|
||||
* This is not implemented yet!
|
||||
*/
|
||||
u64 = 8,
|
||||
|
||||
/**
|
||||
* Doubles are not supported yet!
|
||||
* IEEE-754 double precision float
|
||||
*/
|
||||
double = 9,
|
||||
|
||||
/**
|
||||
* Doubles are not supported yet!
|
||||
* Alias of {@link FFIType.double}
|
||||
*/
|
||||
f64 = 9,
|
||||
|
||||
/**
|
||||
* Floats are not supported yet!
|
||||
* IEEE-754 single precision float
|
||||
*/
|
||||
float = 10,
|
||||
|
||||
/**
|
||||
* Floats are not supported yet!
|
||||
* Alias of {@link FFIType.float}
|
||||
*/
|
||||
f32 = 10,
|
||||
|
||||
|
||||
24
packages/bun-types/globals.d.ts
vendored
24
packages/bun-types/globals.d.ts
vendored
@@ -1564,6 +1564,12 @@ declare var AbortController: Bun.__internal.UseLibDomIfAvailable<
|
||||
}
|
||||
>;
|
||||
|
||||
interface AbortSignal extends EventTarget {
|
||||
readonly aborted: boolean;
|
||||
onabort: ((this: AbortSignal, ev: Event) => any) | null;
|
||||
readonly reason: any;
|
||||
throwIfAborted(): void;
|
||||
}
|
||||
declare var AbortSignal: Bun.__internal.UseLibDomIfAvailable<
|
||||
"AbortSignal",
|
||||
{
|
||||
@@ -1948,3 +1954,21 @@ declare namespace fetch {
|
||||
): void;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
interface RegExpConstructor {
|
||||
/**
|
||||
* Escapes any potential regex syntax characters in a string, and returns a
|
||||
* new string that can be safely used as a literal pattern for the RegExp()
|
||||
* constructor.
|
||||
*
|
||||
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/escape)
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const re = new RegExp(RegExp.escape("foo.bar"));
|
||||
* re.test("foo.bar"); // true
|
||||
* re.test("foo!bar"); // false
|
||||
* ```
|
||||
*/
|
||||
escape(string: string): string;
|
||||
}
|
||||
|
||||
30
packages/bun-types/test.d.ts
vendored
30
packages/bun-types/test.d.ts
vendored
@@ -152,11 +152,41 @@ declare module "bun:test" {
|
||||
type SpiedSetter<T> = JestMock.SpiedSetter<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a spy on an object property or method
|
||||
*/
|
||||
export function spyOn<T extends object, K extends keyof T>(
|
||||
obj: T,
|
||||
methodOrPropertyValue: K,
|
||||
): Mock<Extract<T[K], (...args: any[]) => any>>;
|
||||
|
||||
/**
|
||||
* Vitest-compatible mocking utilities
|
||||
* Provides Vitest-style mocking API for easier migration from Vitest to Bun
|
||||
*/
|
||||
export const vi: {
|
||||
/**
|
||||
* Create a mock function
|
||||
*/
|
||||
fn: typeof jest.fn;
|
||||
/**
|
||||
* Create a spy on an object property or method
|
||||
*/
|
||||
spyOn: typeof spyOn;
|
||||
/**
|
||||
* Mock a module
|
||||
*/
|
||||
module: typeof mock.module;
|
||||
/**
|
||||
* Restore all mocks to their original implementation
|
||||
*/
|
||||
restoreAllMocks: typeof jest.restoreAllMocks;
|
||||
/**
|
||||
* Clear all mock state (calls, results, etc.) without restoring original implementation
|
||||
*/
|
||||
clearAllMocks: typeof jest.clearAllMocks;
|
||||
};
|
||||
|
||||
interface FunctionLike {
|
||||
readonly name: string;
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ void us_internal_socket_context_unlink_connecting_socket(int ssl, struct us_sock
|
||||
}
|
||||
|
||||
/* We always add in the top, so we don't modify any s.next */
|
||||
void us_internal_socket_context_link_listen_socket(struct us_socket_context_t *context, struct us_listen_socket_t *ls) {
|
||||
void us_internal_socket_context_link_listen_socket(int ssl, struct us_socket_context_t *context, struct us_listen_socket_t *ls) {
|
||||
struct us_socket_t* s = &ls->s;
|
||||
s->context = context;
|
||||
s->next = (struct us_socket_t *) context->head_listen_sockets;
|
||||
@@ -162,7 +162,7 @@ void us_internal_socket_context_link_listen_socket(struct us_socket_context_t *c
|
||||
context->head_listen_sockets->s.prev = s;
|
||||
}
|
||||
context->head_listen_sockets = ls;
|
||||
us_socket_context_ref(0, context);
|
||||
us_socket_context_ref(ssl, context);
|
||||
}
|
||||
|
||||
void us_internal_socket_context_link_connecting_socket(int ssl, struct us_socket_context_t *context, struct us_connecting_socket_t *c) {
|
||||
@@ -179,7 +179,7 @@ void us_internal_socket_context_link_connecting_socket(int ssl, struct us_socket
|
||||
|
||||
|
||||
/* We always add in the top, so we don't modify any s.next */
|
||||
void us_internal_socket_context_link_socket(struct us_socket_context_t *context, struct us_socket_t *s) {
|
||||
void us_internal_socket_context_link_socket(int ssl, struct us_socket_context_t *context, struct us_socket_t *s) {
|
||||
s->context = context;
|
||||
s->next = context->head_sockets;
|
||||
s->prev = 0;
|
||||
@@ -187,7 +187,7 @@ void us_internal_socket_context_link_socket(struct us_socket_context_t *context,
|
||||
context->head_sockets->prev = s;
|
||||
}
|
||||
context->head_sockets = s;
|
||||
us_socket_context_ref(0, context);
|
||||
us_socket_context_ref(ssl, context);
|
||||
us_internal_enable_sweep_timer(context->loop);
|
||||
}
|
||||
|
||||
@@ -388,7 +388,7 @@ struct us_listen_socket_t *us_socket_context_listen(int ssl, struct us_socket_co
|
||||
s->flags.is_ipc = 0;
|
||||
s->next = 0;
|
||||
s->flags.allow_half_open = (options & LIBUS_SOCKET_ALLOW_HALF_OPEN);
|
||||
us_internal_socket_context_link_listen_socket(context, ls);
|
||||
us_internal_socket_context_link_listen_socket(ssl, context, ls);
|
||||
|
||||
ls->socket_ext_size = socket_ext_size;
|
||||
|
||||
@@ -423,7 +423,7 @@ struct us_listen_socket_t *us_socket_context_listen_unix(int ssl, struct us_sock
|
||||
s->flags.is_paused = 0;
|
||||
s->flags.is_ipc = 0;
|
||||
s->next = 0;
|
||||
us_internal_socket_context_link_listen_socket(context, ls);
|
||||
us_internal_socket_context_link_listen_socket(ssl, context, ls);
|
||||
|
||||
ls->socket_ext_size = socket_ext_size;
|
||||
|
||||
@@ -456,7 +456,7 @@ struct us_socket_t* us_socket_context_connect_resolved_dns(struct us_socket_cont
|
||||
socket->connect_state = NULL;
|
||||
socket->connect_next = NULL;
|
||||
|
||||
us_internal_socket_context_link_socket(context, socket);
|
||||
us_internal_socket_context_link_socket(0, context, socket);
|
||||
|
||||
return socket;
|
||||
}
|
||||
@@ -584,7 +584,7 @@ int start_connections(struct us_connecting_socket_t *c, int count) {
|
||||
flags->is_paused = 0;
|
||||
flags->is_ipc = 0;
|
||||
/* Link it into context so that timeout fires properly */
|
||||
us_internal_socket_context_link_socket(context, s);
|
||||
us_internal_socket_context_link_socket(0, context, s);
|
||||
|
||||
// TODO check this, specifically how it interacts with the SSL code
|
||||
// does this work when we create multiple sockets at once? will we need multiple SSL contexts?
|
||||
@@ -762,7 +762,7 @@ struct us_socket_t *us_socket_context_connect_unix(int ssl, struct us_socket_con
|
||||
connect_socket->flags.is_ipc = 0;
|
||||
connect_socket->connect_state = NULL;
|
||||
connect_socket->connect_next = NULL;
|
||||
us_internal_socket_context_link_socket(context, connect_socket);
|
||||
us_internal_socket_context_link_socket(ssl, context, connect_socket);
|
||||
|
||||
return connect_socket;
|
||||
}
|
||||
@@ -804,12 +804,9 @@ struct us_socket_t *us_socket_context_adopt_socket(int ssl, struct us_socket_con
|
||||
}
|
||||
|
||||
struct us_connecting_socket_t *c = s->connect_state;
|
||||
|
||||
struct us_socket_t *new_s = s;
|
||||
|
||||
if (ext_size != -1) {
|
||||
struct us_poll_t *pool_ref = &s->p;
|
||||
|
||||
new_s = (struct us_socket_t *) us_poll_resize(pool_ref, loop, sizeof(struct us_socket_t) + ext_size);
|
||||
if (c) {
|
||||
c->connecting_head = new_s;
|
||||
@@ -831,7 +828,7 @@ struct us_socket_t *us_socket_context_adopt_socket(int ssl, struct us_socket_con
|
||||
/* We manually ref/unref context to handle context life cycle with low-priority queue */
|
||||
us_socket_context_ref(ssl, context);
|
||||
} else {
|
||||
us_internal_socket_context_link_socket(context, new_s);
|
||||
us_internal_socket_context_link_socket(ssl, context, new_s);
|
||||
}
|
||||
/* We can safely unref the old context here with can potentially be freed */
|
||||
us_socket_context_unref(ssl, old_context);
|
||||
|
||||
@@ -150,16 +150,12 @@ void us_internal_init_loop_ssl_data(us_loop_r loop);
|
||||
void us_internal_free_loop_ssl_data(us_loop_r loop);
|
||||
|
||||
/* Socket context related */
|
||||
void us_internal_socket_context_link_socket(us_socket_context_r context,
|
||||
us_socket_r s);
|
||||
void us_internal_socket_context_unlink_socket(int ssl,
|
||||
us_socket_context_r context, us_socket_r s);
|
||||
void us_internal_socket_context_link_socket(int ssl, us_socket_context_r context, us_socket_r s);
|
||||
void us_internal_socket_context_unlink_socket(int ssl, us_socket_context_r context, us_socket_r s);
|
||||
|
||||
void us_internal_socket_after_resolve(struct us_connecting_socket_t *s);
|
||||
void us_internal_socket_after_open(us_socket_r s, int error);
|
||||
struct us_internal_ssl_socket_t *
|
||||
us_internal_ssl_socket_close(us_internal_ssl_socket_r s, int code,
|
||||
void *reason);
|
||||
struct us_internal_ssl_socket_t *us_internal_ssl_socket_close(us_internal_ssl_socket_r s, int code, void *reason);
|
||||
|
||||
int us_internal_handle_dns_results(us_loop_r loop);
|
||||
|
||||
@@ -271,7 +267,7 @@ struct us_listen_socket_t {
|
||||
};
|
||||
|
||||
/* Listen sockets are keps in their own list */
|
||||
void us_internal_socket_context_link_listen_socket(
|
||||
void us_internal_socket_context_link_listen_socket(int ssl,
|
||||
us_socket_context_r context, struct us_listen_socket_t *s);
|
||||
void us_internal_socket_context_unlink_listen_socket(int ssl,
|
||||
us_socket_context_r context, struct us_listen_socket_t *s);
|
||||
@@ -288,8 +284,7 @@ struct us_socket_context_t {
|
||||
struct us_socket_t *iterator;
|
||||
struct us_socket_context_t *prev, *next;
|
||||
|
||||
struct us_socket_t *(*on_open)(struct us_socket_t *, int is_client, char *ip,
|
||||
int ip_length);
|
||||
struct us_socket_t *(*on_open)(struct us_socket_t *, int is_client, char *ip, int ip_length);
|
||||
struct us_socket_t *(*on_data)(struct us_socket_t *, char *data, int length);
|
||||
struct us_socket_t *(*on_fd)(struct us_socket_t *, int fd);
|
||||
struct us_socket_t *(*on_writable)(struct us_socket_t *);
|
||||
@@ -301,7 +296,6 @@ struct us_socket_context_t {
|
||||
struct us_connecting_socket_t *(*on_connect_error)(struct us_connecting_socket_t *, int code);
|
||||
struct us_socket_t *(*on_socket_connect_error)(struct us_socket_t *, int code);
|
||||
int (*is_low_prio)(struct us_socket_t *);
|
||||
|
||||
};
|
||||
|
||||
/* Internal SSL interface */
|
||||
|
||||
@@ -40,7 +40,6 @@ void us_internal_enable_sweep_timer(struct us_loop_t *loop) {
|
||||
us_timer_set(loop->data.sweep_timer, (void (*)(struct us_timer_t *)) sweep_timer_cb, LIBUS_TIMEOUT_GRANULARITY * 1000, LIBUS_TIMEOUT_GRANULARITY * 1000);
|
||||
Bun__internal_ensureDateHeaderTimerIsEnabled(loop);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void us_internal_disable_sweep_timer(struct us_loop_t *loop) {
|
||||
@@ -183,7 +182,7 @@ void us_internal_handle_low_priority_sockets(struct us_loop_t *loop) {
|
||||
if (s->next) s->next->prev = 0;
|
||||
s->next = 0;
|
||||
|
||||
us_internal_socket_context_link_socket(s->context, s);
|
||||
us_internal_socket_context_link_socket(0, s->context, s);
|
||||
us_poll_change(&s->p, us_socket_context(0, s)->loop, us_poll_events(&s->p) | LIBUS_SOCKET_READABLE);
|
||||
|
||||
s->flags.low_prio_state = 2;
|
||||
@@ -340,7 +339,7 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, in
|
||||
/* We always use nodelay */
|
||||
bsd_socket_nodelay(client_fd, 1);
|
||||
|
||||
us_internal_socket_context_link_socket(listen_socket->s.context, s);
|
||||
us_internal_socket_context_link_socket(0, listen_socket->s.context, s);
|
||||
|
||||
listen_socket->s.context->on_open(s, 0, bsd_addr_get_ip(&addr), bsd_addr_get_ip_length(&addr));
|
||||
|
||||
@@ -364,7 +363,7 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, in
|
||||
/* Note: if we failed a write as a socket of one loop then adopted
|
||||
* to another loop, this will be wrong. Absurd case though */
|
||||
loop->data.last_write_failed = 0;
|
||||
|
||||
|
||||
s = s->context->on_writable(s);
|
||||
|
||||
if (!s || us_socket_is_closed(0, s)) {
|
||||
|
||||
@@ -329,7 +329,7 @@ struct us_socket_t *us_socket_from_fd(struct us_socket_context_t *ctx, int socke
|
||||
bsd_socket_nodelay(fd, 1);
|
||||
apple_no_sigpipe(fd);
|
||||
bsd_set_nonblocking(fd);
|
||||
us_internal_socket_context_link_socket(ctx, s);
|
||||
us_internal_socket_context_link_socket(0, ctx, s);
|
||||
|
||||
return s;
|
||||
#endif
|
||||
|
||||
@@ -298,6 +298,22 @@ public:
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
/** Closes all connections connected to this server which are not sending a request or waiting for a response. Does not close the listen socket. */
|
||||
TemplatedApp &&closeIdle() {
|
||||
auto context = (struct us_socket_context_t *)this->httpContext;
|
||||
struct us_socket_t *s = context->head_sockets;
|
||||
while (s) {
|
||||
HttpResponseData<SSL> *httpResponseData = HttpResponse<SSL>::getHttpResponseDataS(s);
|
||||
httpResponseData->shouldCloseOnceIdle = true;
|
||||
struct us_socket_t *next = s->next;
|
||||
if (httpResponseData->isIdle) {
|
||||
us_socket_close(SSL, s, LIBUS_SOCKET_CLOSE_CODE_CLEAN_SHUTDOWN, 0);
|
||||
}
|
||||
s = next;
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
template <typename UserData>
|
||||
TemplatedApp &&ws(std::string_view pattern, WebSocketBehavior<UserData> &&behavior) {
|
||||
/* Don't compile if alignment rules cannot be satisfied */
|
||||
|
||||
@@ -386,6 +386,9 @@ public:
|
||||
/* We do not need to care for buffering here, write does that */
|
||||
return {0, true};
|
||||
}
|
||||
if (length == 0) {
|
||||
return {written, failed};
|
||||
}
|
||||
}
|
||||
|
||||
/* We should only return with new writes, not things written to cork already */
|
||||
|
||||
@@ -137,10 +137,6 @@ private:
|
||||
return (HttpContextData<SSL> *) us_socket_context_ext(SSL, getSocketContext());
|
||||
}
|
||||
|
||||
static HttpContextData<SSL> *getSocketContextDataS(us_socket_t *s) {
|
||||
return (HttpContextData<SSL> *) us_socket_context_ext(SSL, getSocketContext(s));
|
||||
}
|
||||
|
||||
/* Init the HttpContext by registering libusockets event handlers */
|
||||
HttpContext<SSL> *init() {
|
||||
|
||||
@@ -247,6 +243,7 @@ private:
|
||||
|
||||
/* Mark that we are inside the parser now */
|
||||
httpContextData->flags.isParsingHttp = true;
|
||||
httpResponseData->isIdle = false;
|
||||
// clients need to know the cursor after http parse, not servers!
|
||||
// how far did we read then? we need to know to continue with websocket parsing data? or?
|
||||
|
||||
@@ -398,6 +395,7 @@ private:
|
||||
/* Timeout on uncork failure */
|
||||
auto [written, failed] = ((AsyncSocket<SSL> *) returnedData)->uncork();
|
||||
if (written > 0 || failed) {
|
||||
httpResponseData->isIdle = true;
|
||||
/* All Http sockets timeout by this, and this behavior match the one in HttpResponse::cork */
|
||||
((HttpResponse<SSL> *) s)->resetTimeout();
|
||||
}
|
||||
@@ -642,6 +640,10 @@ public:
|
||||
}, priority);
|
||||
}
|
||||
|
||||
static HttpContextData<SSL> *getSocketContextDataS(us_socket_t *s) {
|
||||
return (HttpContextData<SSL> *) us_socket_context_ext(SSL, getSocketContext(s));
|
||||
}
|
||||
|
||||
/* Listen to port using this HttpContext */
|
||||
us_listen_socket_t *listen(const char *host, int port, int options) {
|
||||
int error = 0;
|
||||
|
||||
@@ -63,7 +63,6 @@ private:
|
||||
OnSocketClosedCallback onSocketClosed = nullptr;
|
||||
OnClientErrorCallback onClientError = nullptr;
|
||||
|
||||
HttpFlags flags;
|
||||
uint64_t maxHeaderSize = 0; // 0 means no limit
|
||||
|
||||
// TODO: SNI
|
||||
@@ -73,10 +72,8 @@ private:
|
||||
filterHandlers.clear();
|
||||
}
|
||||
|
||||
public:
|
||||
bool isAuthorized() const {
|
||||
return flags.isAuthorized;
|
||||
}
|
||||
public:
|
||||
HttpFlags flags;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -50,6 +50,11 @@ public:
|
||||
HttpResponseData<SSL> *getHttpResponseData() {
|
||||
return (HttpResponseData<SSL> *) Super::getAsyncSocketData();
|
||||
}
|
||||
|
||||
static HttpResponseData<SSL> *getHttpResponseDataS(us_socket_t *s) {
|
||||
return (HttpResponseData<SSL> *) us_socket_ext(SSL, s);
|
||||
}
|
||||
|
||||
void setTimeout(uint8_t seconds) {
|
||||
auto* data = getHttpResponseData();
|
||||
data->idleTimeout = seconds;
|
||||
@@ -132,7 +137,7 @@ public:
|
||||
|
||||
/* Terminating 0 chunk */
|
||||
Super::write("0\r\n\r\n", 5);
|
||||
httpResponseData->markDone();
|
||||
httpResponseData->markDone(this);
|
||||
|
||||
/* We need to check if we should close this socket here now */
|
||||
if (!Super::isCorked()) {
|
||||
@@ -198,7 +203,7 @@ public:
|
||||
|
||||
/* Remove onAborted function if we reach the end */
|
||||
if (httpResponseData->offset == totalSize) {
|
||||
httpResponseData->markDone();
|
||||
httpResponseData->markDone(this);
|
||||
|
||||
/* We need to check if we should close this socket here now */
|
||||
if (!Super::isCorked()) {
|
||||
|
||||
@@ -22,11 +22,15 @@
|
||||
#include "HttpParser.h"
|
||||
#include "AsyncSocketData.h"
|
||||
#include "ProxyParser.h"
|
||||
#include "HttpContext.h"
|
||||
|
||||
#include "MoveOnlyFunction.h"
|
||||
|
||||
namespace uWS {
|
||||
|
||||
template <bool SSL>
|
||||
struct HttpContext;
|
||||
|
||||
template <bool SSL>
|
||||
struct HttpResponseData : AsyncSocketData<SSL>, HttpParser {
|
||||
template <bool> friend struct HttpResponse;
|
||||
@@ -38,7 +42,7 @@ struct HttpResponseData : AsyncSocketData<SSL>, HttpParser {
|
||||
using OnDataCallback = void (*)(uWS::HttpResponse<SSL>* response, const char* chunk, size_t chunk_length, bool, void*);
|
||||
|
||||
/* When we are done with a response we mark it like so */
|
||||
void markDone() {
|
||||
void markDone(uWS::HttpResponse<SSL> *uwsRes) {
|
||||
onAborted = nullptr;
|
||||
/* Also remove onWritable so that we do not emit when draining behind the scenes. */
|
||||
onWritable = nullptr;
|
||||
@@ -50,6 +54,9 @@ struct HttpResponseData : AsyncSocketData<SSL>, HttpParser {
|
||||
|
||||
/* We are done with this request */
|
||||
this->state &= ~HttpResponseData<SSL>::HTTP_RESPONSE_PENDING;
|
||||
|
||||
HttpResponseData<SSL> *httpResponseData = uwsRes->getHttpResponseData();
|
||||
httpResponseData->isIdle = true;
|
||||
}
|
||||
|
||||
/* Caller of onWritable. It is possible onWritable calls markDone so we need to borrow it. */
|
||||
@@ -101,6 +108,8 @@ struct HttpResponseData : AsyncSocketData<SSL>, HttpParser {
|
||||
uint8_t state = 0;
|
||||
uint8_t idleTimeout = 10; // default HTTP_TIMEOUT 10 seconds
|
||||
bool fromAncientRequest = false;
|
||||
bool isIdle = true;
|
||||
bool shouldCloseOnceIdle = false;
|
||||
|
||||
|
||||
#ifdef UWS_WITH_PROXY
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { spawn as nodeSpawn } from "node:child_process";
|
||||
import { chmodSync, cpSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
||||
import { basename, join, relative, resolve } from "node:path";
|
||||
@@ -14,6 +12,10 @@ import {
|
||||
startGroup,
|
||||
} from "./utils.mjs";
|
||||
|
||||
if (globalThis.Bun) {
|
||||
await import("./glob-sources.mjs");
|
||||
}
|
||||
|
||||
// https://cmake.org/cmake/help/latest/manual/cmake.1.html#generate-a-project-buildsystem
|
||||
const generateFlags = [
|
||||
["-S", "string", "path to source directory"],
|
||||
|
||||
107
scripts/buildkite-slow-tests.js
Executable file
107
scripts/buildkite-slow-tests.js
Executable file
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import { readFileSync } from "fs";
|
||||
|
||||
function parseLogFile(filename) {
|
||||
const testDetails = new Map(); // Track individual attempts and total for each test
|
||||
let currentTest = null;
|
||||
let startTime = null;
|
||||
|
||||
// Pattern to match test group start: --- [90m[N/TOTAL][0m test/path
|
||||
// Note: there are escape sequences before _bk
|
||||
const startPattern = /_bk;t=(\d+).*?--- .*?\[90m\[(\d+)\/(\d+)\].*?\[0m (.+)/;
|
||||
|
||||
const content = readFileSync(filename, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
|
||||
for (const line of lines) {
|
||||
const match = line.match(startPattern);
|
||||
if (match) {
|
||||
// If we have a previous test, calculate its duration
|
||||
if (currentTest && startTime) {
|
||||
const endTime = parseInt(match[1]);
|
||||
const duration = endTime - startTime;
|
||||
|
||||
// Extract attempt info - match the actual ANSI pattern
|
||||
const attemptMatch = currentTest.match(/\s+\x1b\[90m\[attempt #(\d+)\]\x1b\[0m$/);
|
||||
const cleanName = currentTest.replace(/\s+\x1b\[90m\[attempt #\d+\]\x1b\[0m$/, "").trim();
|
||||
const attemptNum = attemptMatch ? parseInt(attemptMatch[1]) : 1;
|
||||
|
||||
if (!testDetails.has(cleanName)) {
|
||||
testDetails.set(cleanName, { total: 0, attempts: [] });
|
||||
}
|
||||
|
||||
const testInfo = testDetails.get(cleanName);
|
||||
testInfo.total += duration;
|
||||
testInfo.attempts.push({ attempt: attemptNum, duration });
|
||||
}
|
||||
|
||||
// Start new test
|
||||
startTime = parseInt(match[1]);
|
||||
currentTest = match[4].trim();
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to array and sort by total duration
|
||||
const testGroups = Array.from(testDetails.entries())
|
||||
.map(([name, info]) => ({
|
||||
name,
|
||||
totalDuration: info.total,
|
||||
attempts: info.attempts.sort((a, b) => a.attempt - b.attempt),
|
||||
}))
|
||||
.sort((a, b) => b.totalDuration - a.totalDuration);
|
||||
|
||||
return testGroups;
|
||||
}
|
||||
|
||||
function formatAttempts(attempts) {
|
||||
if (attempts.length <= 1) return "";
|
||||
|
||||
const attemptStrings = attempts.map(
|
||||
({ attempt, duration }) => `${(duration / 1000).toFixed(1)}s attempt #${attempt}`,
|
||||
);
|
||||
return ` [${attemptStrings.join(", ")}]`;
|
||||
}
|
||||
|
||||
if (process.argv.length !== 3) {
|
||||
console.log("Usage: bun parse_test_logs.js <log_file>");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const filename = process.argv[2];
|
||||
const testGroups = parseLogFile(filename);
|
||||
|
||||
const totalTime = testGroups.reduce((sum, group) => sum + group.totalDuration, 0) / 1000;
|
||||
const avgTime = testGroups.length > 0 ? totalTime / testGroups.length : 0;
|
||||
|
||||
console.log(
|
||||
`## Slowest Tests Analysis - ${testGroups.length} tests (${totalTime.toFixed(1)}s total, ${avgTime.toFixed(2)}s avg)`,
|
||||
);
|
||||
console.log("");
|
||||
|
||||
// Top 10 summary
|
||||
console.log("**Top 10 slowest tests:**");
|
||||
for (let i = 0; i < Math.min(10, testGroups.length); i++) {
|
||||
const { name, totalDuration, attempts } = testGroups[i];
|
||||
const durationSec = totalDuration / 1000;
|
||||
const testName = name.replace("test/", "").replace(".test.ts", "").replace(".test.js", "");
|
||||
const attemptInfo = formatAttempts(attempts);
|
||||
console.log(`- **${durationSec.toFixed(1)}s** ${testName}${attemptInfo}`);
|
||||
}
|
||||
|
||||
console.log("");
|
||||
|
||||
// Filter tests > 1 second
|
||||
const slowTests = testGroups.filter(test => test.totalDuration > 1000);
|
||||
|
||||
console.log("```");
|
||||
console.log(`All tests > 1s (${slowTests.length} tests):`);
|
||||
|
||||
for (let i = 0; i < slowTests.length; i++) {
|
||||
const { name, totalDuration, attempts } = slowTests[i];
|
||||
const durationSec = totalDuration / 1000;
|
||||
const attemptInfo = formatAttempts(attempts);
|
||||
console.log(`${(i + 1).toString().padStart(3)}. ${durationSec.toFixed(2).padStart(7)}s ${name}${attemptInfo}`);
|
||||
}
|
||||
|
||||
console.log("```");
|
||||
@@ -41,7 +41,7 @@ fn createImportRecord(this: *HTMLScanner, input_path: []const u8, kind: ImportKi
|
||||
const record = ImportRecord{
|
||||
.path = fs.Path.init(try this.allocator.dupeZ(u8, path_to_use)),
|
||||
.kind = kind,
|
||||
.range = logger.Range.None,
|
||||
.range = .none,
|
||||
};
|
||||
|
||||
try this.import_records.push(this.allocator, record);
|
||||
@@ -56,7 +56,7 @@ pub fn onWriteHTML(_: *HTMLScanner, bytes: []const u8) void {
|
||||
pub fn onHTMLParseError(this: *HTMLScanner, message: []const u8) void {
|
||||
this.log.addError(
|
||||
this.source,
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
message,
|
||||
) catch |err| bun.handleOom(err);
|
||||
}
|
||||
|
||||
@@ -44,11 +44,20 @@ pub const StandaloneModuleGraph = struct {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isBunStandaloneFilePath(str: []const u8) bool {
|
||||
pub fn isBunStandaloneFilePathCanonicalized(str: []const u8) bool {
|
||||
return bun.strings.hasPrefixComptime(str, base_path) or
|
||||
(Environment.isWindows and bun.strings.hasPrefixComptime(str, base_public_path));
|
||||
}
|
||||
|
||||
pub fn isBunStandaloneFilePath(str: []const u8) bool {
|
||||
if (Environment.isWindows) {
|
||||
// On Windows, remove NT path prefixes before checking
|
||||
const canonicalized = strings.withoutNTPrefix(u8, str);
|
||||
return isBunStandaloneFilePathCanonicalized(canonicalized);
|
||||
}
|
||||
return isBunStandaloneFilePathCanonicalized(str);
|
||||
}
|
||||
|
||||
pub fn entryPoint(this: *const StandaloneModuleGraph) *File {
|
||||
return &this.files.values()[this.entry_point_id];
|
||||
}
|
||||
@@ -980,27 +989,54 @@ pub const StandaloneModuleGraph = struct {
|
||||
}
|
||||
|
||||
if (Environment.isWindows) {
|
||||
var outfile_buf: bun.OSPathBuffer = undefined;
|
||||
const outfile_slice = brk: {
|
||||
const outfile_w = bun.strings.toWPathNormalized(&outfile_buf, std.fs.path.basenameWindows(outfile));
|
||||
bun.assert(outfile_w.ptr == &outfile_buf);
|
||||
const outfile_buf_u16 = bun.reinterpretSlice(u16, &outfile_buf);
|
||||
outfile_buf_u16[outfile_w.len] = 0;
|
||||
break :brk outfile_buf_u16[0..outfile_w.len :0];
|
||||
// Get the current path of the temp file
|
||||
var temp_buf: bun.PathBuffer = undefined;
|
||||
const temp_path = bun.getFdPath(fd, &temp_buf) catch |err| {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "Failed to get temp file path: {s}", .{@errorName(err)}) catch "Failed to get temp file path");
|
||||
};
|
||||
|
||||
bun.windows.moveOpenedFileAtLoose(fd, .fromStdDir(root_dir), outfile_slice, true).unwrap() catch |err| {
|
||||
_ = bun.windows.deleteOpenedFile(fd);
|
||||
if (err == error.EISDIR) {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "{s} is a directory. Please choose a different --outfile or delete the directory", .{outfile}) catch "outfile is a directory");
|
||||
} else {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "failed to move executable to result path: {s}", .{@errorName(err)}) catch "failed to move executable");
|
||||
}
|
||||
// Build the absolute destination path
|
||||
// On Windows, we need an absolute path for MoveFileExW
|
||||
// Get the current working directory and join with outfile
|
||||
var cwd_buf: bun.PathBuffer = undefined;
|
||||
const cwd_path = bun.getcwd(&cwd_buf) catch |err| {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "Failed to get current directory: {s}", .{@errorName(err)}) catch "Failed to get current directory");
|
||||
};
|
||||
const dest_path = if (std.fs.path.isAbsolute(outfile))
|
||||
outfile
|
||||
else
|
||||
bun.path.joinAbsString(cwd_path, &[_][]const u8{outfile}, .auto);
|
||||
|
||||
// Convert paths to Windows UTF-16
|
||||
var temp_buf_w: bun.OSPathBuffer = undefined;
|
||||
var dest_buf_w: bun.OSPathBuffer = undefined;
|
||||
const temp_w = bun.strings.toWPathNormalized(&temp_buf_w, temp_path);
|
||||
const dest_w = bun.strings.toWPathNormalized(&dest_buf_w, dest_path);
|
||||
|
||||
// Ensure null termination
|
||||
const temp_buf_u16 = bun.reinterpretSlice(u16, &temp_buf_w);
|
||||
const dest_buf_u16 = bun.reinterpretSlice(u16, &dest_buf_w);
|
||||
temp_buf_u16[temp_w.len] = 0;
|
||||
dest_buf_u16[dest_w.len] = 0;
|
||||
|
||||
// Close the file handle before moving (Windows requires this)
|
||||
fd.close();
|
||||
fd = bun.invalid_fd;
|
||||
|
||||
// Move the file using MoveFileExW
|
||||
if (bun.windows.kernel32.MoveFileExW(temp_buf_u16[0..temp_w.len :0].ptr, dest_buf_u16[0..dest_w.len :0].ptr, bun.windows.MOVEFILE_COPY_ALLOWED | bun.windows.MOVEFILE_REPLACE_EXISTING | bun.windows.MOVEFILE_WRITE_THROUGH) == bun.windows.FALSE) {
|
||||
const err = bun.windows.Win32Error.get();
|
||||
if (err.toSystemErrno()) |sys_err| {
|
||||
if (sys_err == .EISDIR) {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "{s} is a directory. Please choose a different --outfile or delete the directory", .{outfile}) catch "outfile is a directory");
|
||||
} else {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "failed to move executable to {s}: {s}", .{ dest_path, @tagName(sys_err) }) catch "failed to move executable");
|
||||
}
|
||||
} else {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "failed to move executable to {s}", .{dest_path}) catch "failed to move executable");
|
||||
}
|
||||
}
|
||||
|
||||
// Set Windows icon and/or metadata using unified function
|
||||
if (windows_options.icon != null or
|
||||
windows_options.title != null or
|
||||
@@ -1009,25 +1045,9 @@ pub const StandaloneModuleGraph = struct {
|
||||
windows_options.description != null or
|
||||
windows_options.copyright != null)
|
||||
{
|
||||
// Need to get the full path to the executable
|
||||
var full_path_buf: bun.OSPathBuffer = undefined;
|
||||
const full_path = brk: {
|
||||
// Get the directory path
|
||||
var dir_buf: bun.PathBuffer = undefined;
|
||||
const dir_path = bun.getFdPath(bun.FD.fromStdDir(root_dir), &dir_buf) catch |err| {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "Failed to get directory path: {s}", .{@errorName(err)}) catch "Failed to get directory path");
|
||||
};
|
||||
|
||||
// Join with the outfile name
|
||||
const full_path_str = bun.path.joinAbsString(dir_path, &[_][]const u8{outfile}, .auto);
|
||||
const full_path_w = bun.strings.toWPathNormalized(&full_path_buf, full_path_str);
|
||||
const buf_u16 = bun.reinterpretSlice(u16, &full_path_buf);
|
||||
buf_u16[full_path_w.len] = 0;
|
||||
break :brk buf_u16[0..full_path_w.len :0];
|
||||
};
|
||||
|
||||
// The file has been moved to dest_path
|
||||
bun.windows.rescle.setWindowsMetadata(
|
||||
full_path.ptr,
|
||||
dest_buf_u16[0..dest_w.len :0].ptr,
|
||||
windows_options.icon,
|
||||
windows_options.title,
|
||||
windows_options.publisher,
|
||||
|
||||
@@ -3,11 +3,16 @@ pub const z_allocator = basic.z_allocator;
|
||||
pub const freeWithoutSize = basic.freeWithoutSize;
|
||||
pub const mimalloc = @import("./allocators/mimalloc.zig");
|
||||
pub const MimallocArena = @import("./allocators/MimallocArena.zig");
|
||||
pub const AllocationScope = @import("./allocators/AllocationScope.zig");
|
||||
|
||||
pub const allocation_scope = @import("./allocators/allocation_scope.zig");
|
||||
pub const AllocationScope = allocation_scope.AllocationScope;
|
||||
pub const AllocationScopeIn = allocation_scope.AllocationScopeIn;
|
||||
|
||||
pub const NullableAllocator = @import("./allocators/NullableAllocator.zig");
|
||||
pub const MaxHeapAllocator = @import("./allocators/MaxHeapAllocator.zig");
|
||||
pub const MemoryReportingAllocator = @import("./allocators/MemoryReportingAllocator.zig");
|
||||
pub const LinuxMemFdAllocator = @import("./allocators/LinuxMemFdAllocator.zig");
|
||||
pub const MaybeOwned = @import("./allocators/maybe_owned.zig").MaybeOwned;
|
||||
|
||||
pub fn isSliceInBufferT(comptime T: type, slice: []const T, buffer: []const T) bool {
|
||||
return (@intFromPtr(buffer.ptr) <= @intFromPtr(slice.ptr) and
|
||||
@@ -228,7 +233,7 @@ pub fn BSSList(comptime ValueType: type, comptime _count: anytype) type {
|
||||
|
||||
const Self = @This();
|
||||
|
||||
allocator: Allocator,
|
||||
allocator: std.mem.Allocator,
|
||||
mutex: Mutex = .{},
|
||||
head: *OverflowBlock,
|
||||
tail: OverflowBlock,
|
||||
@@ -316,7 +321,7 @@ pub fn BSSStringList(comptime _count: usize, comptime _item_length: usize) type
|
||||
backing_buf: [count * item_length]u8,
|
||||
backing_buf_used: u64,
|
||||
overflow_list: Overflow,
|
||||
allocator: Allocator,
|
||||
allocator: std.mem.Allocator,
|
||||
slice_buf: [count][]const u8,
|
||||
slice_buf_used: u16,
|
||||
mutex: Mutex = .{},
|
||||
@@ -499,7 +504,7 @@ pub fn BSSMap(comptime ValueType: type, comptime count: anytype, comptime store_
|
||||
|
||||
index: IndexMap,
|
||||
overflow_list: Overflow,
|
||||
allocator: Allocator,
|
||||
allocator: std.mem.Allocator,
|
||||
mutex: Mutex = .{},
|
||||
backing_buf: [count]ValueType,
|
||||
backing_buf_used: u16,
|
||||
@@ -770,36 +775,119 @@ pub fn BSSMap(comptime ValueType: type, comptime count: anytype, comptime store_
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isDefault(allocator: Allocator) bool {
|
||||
/// Checks whether `allocator` is the default allocator.
|
||||
pub fn isDefault(allocator: std.mem.Allocator) bool {
|
||||
return allocator.vtable == c_allocator.vtable;
|
||||
}
|
||||
|
||||
/// Allocate memory for a value of type `T` using the provided allocator, and initialize the memory
|
||||
/// with `value`.
|
||||
///
|
||||
/// If `allocator` is `bun.default_allocator`, this will internally use `bun.tryNew` to benefit from
|
||||
/// the added assertions.
|
||||
pub fn create(comptime T: type, allocator: Allocator, value: T) OOM!*T {
|
||||
if ((comptime Environment.allow_assert) and isDefault(allocator)) {
|
||||
return bun.tryNew(T, value);
|
||||
}
|
||||
const ptr = try allocator.create(T);
|
||||
ptr.* = value;
|
||||
return ptr;
|
||||
// The following functions operate on generic allocators. A generic allocator is a type that
|
||||
// satisfies the `GenericAllocator` interface:
|
||||
//
|
||||
// ```
|
||||
// const GenericAllocator = struct {
|
||||
// // Required.
|
||||
// pub fn allocator(self: Self) std.mem.Allocator;
|
||||
//
|
||||
// // Optional, to allow default-initialization. `.{}` will also be tried.
|
||||
// pub fn init() Self;
|
||||
//
|
||||
// // Optional, if this allocator owns auxiliary resources that need to be deinitialized.
|
||||
// pub fn deinit(self: *Self) void;
|
||||
//
|
||||
// // Optional. Defining a borrowed type makes it clear who owns the allocator and prevents
|
||||
// // `deinit` from being called twice.
|
||||
// pub const Borrowed: type;
|
||||
// pub fn borrow(self: Self) Borrowed;
|
||||
// };
|
||||
// ```
|
||||
//
|
||||
// Generic allocators must support being moved. They cannot contain self-references, and they cannot
|
||||
// serve allocations from a buffer that exists within the allocator itself (have your allocator type
|
||||
// contain a pointer to the buffer instead).
|
||||
//
|
||||
// As an exception, `std.mem.Allocator` is also treated as a generic allocator, and receives
|
||||
// special handling in the following functions to achieve this.
|
||||
|
||||
/// Gets the `std.mem.Allocator` for a given generic allocator.
|
||||
pub fn asStd(allocator: anytype) std.mem.Allocator {
|
||||
return if (comptime @TypeOf(allocator) == std.mem.Allocator)
|
||||
allocator
|
||||
else
|
||||
allocator.allocator();
|
||||
}
|
||||
|
||||
/// Free memory previously allocated by `create`.
|
||||
/// A borrowed version of an allocator.
|
||||
///
|
||||
/// The memory must have been allocated by the `create` function in this namespace, not
|
||||
/// directly by `allocator.create`.
|
||||
pub fn destroy(allocator: Allocator, ptr: anytype) void {
|
||||
if ((comptime Environment.allow_assert) and isDefault(allocator)) {
|
||||
bun.destroy(ptr);
|
||||
} else {
|
||||
allocator.destroy(ptr);
|
||||
}
|
||||
/// Some allocators have a `deinit` method that would be invalid to call multiple times (e.g.,
|
||||
/// `AllocationScope` and `MimallocArena`).
|
||||
///
|
||||
/// If multiple structs or functions need access to the same allocator, we want to avoid simply
|
||||
/// passing the allocator by value, as this could easily lead to `deinit` being called multiple
|
||||
/// times if we forget who really owns the allocator.
|
||||
///
|
||||
/// Passing a pointer is not always a good approach, as this results in a performance penalty for
|
||||
/// zero-sized allocators, and adds another level of indirection in all cases.
|
||||
///
|
||||
/// This function allows allocators that have a concept of being "owned" to define a "borrowed"
|
||||
/// version of the allocator. If no such type is defined, it is assumed the allocator does not
|
||||
/// own any data, and `Borrowed(Allocator)` is simply the same as `Allocator`.
|
||||
pub fn Borrowed(comptime Allocator: type) type {
|
||||
return if (comptime @hasDecl(Allocator, "Borrowed"))
|
||||
Allocator.Borrowed
|
||||
else
|
||||
Allocator;
|
||||
}
|
||||
|
||||
/// Borrows an allocator.
|
||||
///
|
||||
/// See `Borrowed` for the rationale.
|
||||
pub fn borrow(allocator: anytype) Borrowed(@TypeOf(allocator)) {
|
||||
return if (comptime @hasDecl(@TypeOf(allocator), "Borrowed"))
|
||||
allocator.borrow()
|
||||
else
|
||||
allocator;
|
||||
}
|
||||
|
||||
/// A type that behaves like `?Allocator`. This function will either return `?Allocator` itself,
|
||||
/// or an optimized type that behaves like `?Allocator`.
|
||||
///
|
||||
/// Use `initNullable` and `unpackNullable` to work with the returned type.
|
||||
pub fn Nullable(comptime Allocator: type) type {
|
||||
return if (comptime Allocator == std.mem.Allocator)
|
||||
NullableAllocator
|
||||
else if (comptime @hasDecl(Allocator, "Nullable"))
|
||||
Allocator.Nullable
|
||||
else
|
||||
?Allocator;
|
||||
}
|
||||
|
||||
/// Creates a `Nullable(Allocator)` from an optional `Allocator`.
|
||||
pub fn initNullable(comptime Allocator: type, allocator: ?Allocator) Nullable(Allocator) {
|
||||
return if (comptime Allocator == std.mem.Allocator or @hasDecl(Allocator, "Nullable"))
|
||||
.init(allocator)
|
||||
else
|
||||
allocator;
|
||||
}
|
||||
|
||||
/// Turns a `Nullable(Allocator)` back into an optional `Allocator`.
|
||||
pub fn unpackNullable(comptime Allocator: type, allocator: Nullable(Allocator)) ?Allocator {
|
||||
return if (comptime Allocator == std.mem.Allocator or @hasDecl(Allocator, "Nullable"))
|
||||
.get()
|
||||
else
|
||||
allocator;
|
||||
}
|
||||
|
||||
/// The default allocator. This is a zero-sized type whose `allocator` method returns
|
||||
/// `bun.default_allocator`.
|
||||
///
|
||||
/// This type is a `GenericAllocator`; see `src/allocators.zig`.
|
||||
pub const Default = struct {
|
||||
pub fn allocator(self: Default) std.mem.Allocator {
|
||||
_ = self;
|
||||
return c_allocator;
|
||||
}
|
||||
};
|
||||
|
||||
const basic = if (bun.use_mimalloc)
|
||||
@import("./allocators/basic.zig")
|
||||
else
|
||||
@@ -807,7 +895,6 @@ else
|
||||
|
||||
const Environment = @import("./env.zig");
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const bun = @import("bun");
|
||||
const OOM = bun.OOM;
|
||||
|
||||
@@ -1,288 +0,0 @@
|
||||
//! AllocationScope wraps another allocator, providing leak and invalid free assertions.
|
||||
//! It also allows measuring how much memory a scope has allocated.
|
||||
//!
|
||||
//! AllocationScope is conceptually a pointer, so it can be moved without invalidating allocations.
|
||||
//! Therefore, it isn't necessary to pass an AllocationScope by pointer.
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub const enabled = bun.Environment.enableAllocScopes;
|
||||
|
||||
internal_state: if (enabled) *State else Allocator,
|
||||
|
||||
const State = struct {
|
||||
parent: Allocator,
|
||||
mutex: bun.Mutex,
|
||||
total_memory_allocated: usize,
|
||||
allocations: std.AutoHashMapUnmanaged([*]const u8, Allocation),
|
||||
frees: std.AutoArrayHashMapUnmanaged([*]const u8, Free),
|
||||
/// Once `frees` fills up, entries are overwritten from start to end.
|
||||
free_overwrite_index: std.math.IntFittingRange(0, max_free_tracking + 1),
|
||||
};
|
||||
|
||||
pub const max_free_tracking = 2048 - 1;
|
||||
|
||||
pub const Allocation = struct {
|
||||
allocated_at: StoredTrace,
|
||||
len: usize,
|
||||
extra: Extra,
|
||||
};
|
||||
|
||||
pub const Free = struct {
|
||||
allocated_at: StoredTrace,
|
||||
freed_at: StoredTrace,
|
||||
};
|
||||
|
||||
pub const Extra = union(enum) {
|
||||
none,
|
||||
ref_count: *RefCountDebugData(false),
|
||||
ref_count_threadsafe: *RefCountDebugData(true),
|
||||
|
||||
const RefCountDebugData = @import("../ptr/ref_count.zig").DebugData;
|
||||
};
|
||||
|
||||
pub fn init(parent_alloc: Allocator) Self {
|
||||
const state = if (comptime enabled)
|
||||
bun.new(State, .{
|
||||
.parent = parent_alloc,
|
||||
.total_memory_allocated = 0,
|
||||
.allocations = .empty,
|
||||
.frees = .empty,
|
||||
.free_overwrite_index = 0,
|
||||
.mutex = .{},
|
||||
})
|
||||
else
|
||||
parent_alloc;
|
||||
return .{ .internal_state = state };
|
||||
}
|
||||
|
||||
pub fn deinit(scope: Self) void {
|
||||
if (comptime !enabled) return;
|
||||
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer bun.destroy(state);
|
||||
defer state.allocations.deinit(state.parent);
|
||||
const count = state.allocations.count();
|
||||
if (count == 0) return;
|
||||
Output.errGeneric("Allocation scope leaked {d} allocations ({})", .{
|
||||
count,
|
||||
bun.fmt.size(state.total_memory_allocated, .{}),
|
||||
});
|
||||
var it = state.allocations.iterator();
|
||||
var n: usize = 0;
|
||||
while (it.next()) |entry| {
|
||||
Output.prettyErrorln("- {any}, len {d}, at:", .{ entry.key_ptr.*, entry.value_ptr.len });
|
||||
bun.crash_handler.dumpStackTrace(entry.value_ptr.allocated_at.trace(), trace_limits);
|
||||
|
||||
switch (entry.value_ptr.extra) {
|
||||
.none => {},
|
||||
inline else => |t| t.onAllocationLeak(@constCast(entry.key_ptr.*[0..entry.value_ptr.len])),
|
||||
}
|
||||
|
||||
n += 1;
|
||||
if (n >= 8) {
|
||||
Output.prettyErrorln("(only showing first 10 leaks)", .{});
|
||||
break;
|
||||
}
|
||||
}
|
||||
Output.panic("Allocation scope leaked {}", .{bun.fmt.size(state.total_memory_allocated, .{})});
|
||||
}
|
||||
|
||||
pub fn allocator(scope: Self) Allocator {
|
||||
const state = scope.internal_state;
|
||||
return if (comptime enabled) .{ .ptr = state, .vtable = &vtable } else state;
|
||||
}
|
||||
|
||||
pub fn parent(scope: Self) Allocator {
|
||||
const state = scope.internal_state;
|
||||
return if (comptime enabled) state.parent else state;
|
||||
}
|
||||
|
||||
pub fn total(self: Self) usize {
|
||||
if (comptime !enabled) @compileError("AllocationScope must be enabled");
|
||||
return self.internal_state.total_memory_allocated;
|
||||
}
|
||||
|
||||
pub fn numAllocations(self: Self) usize {
|
||||
if (comptime !enabled) @compileError("AllocationScope must be enabled");
|
||||
return self.internal_state.allocations.count();
|
||||
}
|
||||
|
||||
const vtable: Allocator.VTable = .{
|
||||
.alloc = alloc,
|
||||
.resize = &std.mem.Allocator.noResize,
|
||||
.remap = &std.mem.Allocator.noRemap,
|
||||
.free = free,
|
||||
};
|
||||
|
||||
// Smaller traces since AllocationScope prints so many
|
||||
pub const trace_limits: bun.crash_handler.WriteStackTraceLimits = .{
|
||||
.frame_count = 6,
|
||||
.stop_at_jsc_llint = true,
|
||||
.skip_stdlib = true,
|
||||
};
|
||||
pub const free_trace_limits: bun.crash_handler.WriteStackTraceLimits = .{
|
||||
.frame_count = 3,
|
||||
.stop_at_jsc_llint = true,
|
||||
.skip_stdlib = true,
|
||||
};
|
||||
|
||||
fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: usize) ?[*]u8 {
|
||||
const state: *State = @ptrCast(@alignCast(ctx));
|
||||
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
state.allocations.ensureUnusedCapacity(state.parent, 1) catch
|
||||
return null;
|
||||
const result = state.parent.vtable.alloc(state.parent.ptr, len, alignment, ret_addr) orelse
|
||||
return null;
|
||||
trackAllocationAssumeCapacity(state, result[0..len], ret_addr, .none);
|
||||
return result;
|
||||
}
|
||||
|
||||
fn trackAllocationAssumeCapacity(state: *State, buf: []const u8, ret_addr: usize, extra: Extra) void {
|
||||
const trace = StoredTrace.capture(ret_addr);
|
||||
state.allocations.putAssumeCapacityNoClobber(buf.ptr, .{
|
||||
.allocated_at = trace,
|
||||
.len = buf.len,
|
||||
.extra = extra,
|
||||
});
|
||||
state.total_memory_allocated += buf.len;
|
||||
}
|
||||
|
||||
fn free(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, ret_addr: usize) void {
|
||||
const state: *State = @ptrCast(@alignCast(ctx));
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
const invalid = trackFreeAssumeLocked(state, buf, ret_addr);
|
||||
|
||||
state.parent.vtable.free(state.parent.ptr, buf, alignment, ret_addr);
|
||||
|
||||
// If asan did not catch the free, panic now.
|
||||
if (invalid) @panic("Invalid free");
|
||||
}
|
||||
|
||||
fn trackFreeAssumeLocked(state: *State, buf: []const u8, ret_addr: usize) bool {
|
||||
if (state.allocations.fetchRemove(buf.ptr)) |entry| {
|
||||
state.total_memory_allocated -= entry.value.len;
|
||||
|
||||
free_entry: {
|
||||
state.frees.put(state.parent, buf.ptr, .{
|
||||
.allocated_at = entry.value.allocated_at,
|
||||
.freed_at = StoredTrace.capture(ret_addr),
|
||||
}) catch break :free_entry;
|
||||
// Store a limited amount of free entries
|
||||
if (state.frees.count() >= max_free_tracking) {
|
||||
const i = state.free_overwrite_index;
|
||||
state.free_overwrite_index = @mod(state.free_overwrite_index + 1, max_free_tracking);
|
||||
state.frees.swapRemoveAt(i);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
bun.Output.errGeneric("Invalid free, pointer {any}, len {d}", .{ buf.ptr, buf.len });
|
||||
|
||||
if (state.frees.get(buf.ptr)) |free_entry_const| {
|
||||
var free_entry = free_entry_const;
|
||||
bun.Output.printErrorln("Pointer allocated here:", .{});
|
||||
bun.crash_handler.dumpStackTrace(free_entry.allocated_at.trace(), trace_limits);
|
||||
bun.Output.printErrorln("Pointer first freed here:", .{});
|
||||
bun.crash_handler.dumpStackTrace(free_entry.freed_at.trace(), free_trace_limits);
|
||||
}
|
||||
|
||||
// do not panic because address sanitizer will catch this case better.
|
||||
// the log message is in case there is a situation where address
|
||||
// sanitizer does not catch the invalid free.
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn assertOwned(scope: Self, ptr: anytype) void {
|
||||
if (comptime !enabled) return;
|
||||
const cast_ptr: [*]const u8 = @ptrCast(switch (@typeInfo(@TypeOf(ptr)).pointer.size) {
|
||||
.c, .one, .many => ptr,
|
||||
.slice => if (ptr.len > 0) ptr.ptr else return,
|
||||
});
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
_ = state.allocations.getPtr(cast_ptr) orelse
|
||||
@panic("this pointer was not owned by the allocation scope");
|
||||
}
|
||||
|
||||
pub fn assertUnowned(scope: Self, ptr: anytype) void {
|
||||
if (comptime !enabled) return;
|
||||
const cast_ptr: [*]const u8 = @ptrCast(switch (@typeInfo(@TypeOf(ptr)).pointer.size) {
|
||||
.c, .one, .many => ptr,
|
||||
.slice => if (ptr.len > 0) ptr.ptr else return,
|
||||
});
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
if (state.allocations.getPtr(cast_ptr)) |owned| {
|
||||
Output.warn("Owned pointer allocated here:");
|
||||
bun.crash_handler.dumpStackTrace(owned.allocated_at.trace(), trace_limits, trace_limits);
|
||||
}
|
||||
@panic("this pointer was owned by the allocation scope when it was not supposed to be");
|
||||
}
|
||||
|
||||
/// Track an arbitrary pointer. Extra data can be stored in the allocation,
|
||||
/// which will be printed when a leak is detected.
|
||||
pub fn trackExternalAllocation(scope: Self, ptr: []const u8, ret_addr: ?usize, extra: Extra) void {
|
||||
if (comptime !enabled) return;
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
bun.handleOom(state.allocations.ensureUnusedCapacity(state.parent, 1));
|
||||
trackAllocationAssumeCapacity(state, ptr, ptr.len, ret_addr orelse @returnAddress(), extra);
|
||||
}
|
||||
|
||||
/// Call when the pointer from `trackExternalAllocation` is freed.
|
||||
/// Returns true if the free was invalid.
|
||||
pub fn trackExternalFree(scope: Self, slice: anytype, ret_addr: ?usize) bool {
|
||||
if (comptime !enabled) return false;
|
||||
const ptr: []const u8 = switch (@typeInfo(@TypeOf(slice))) {
|
||||
.pointer => |p| switch (p.size) {
|
||||
.slice => brk: {
|
||||
if (p.child != u8) @compileError("This function only supports []u8 or [:sentinel]u8 types, you passed in: " ++ @typeName(@TypeOf(slice)));
|
||||
if (p.sentinel_ptr == null) break :brk slice;
|
||||
// Ensure we include the sentinel value
|
||||
break :brk slice[0 .. slice.len + 1];
|
||||
},
|
||||
else => @compileError("This function only supports []u8 or [:sentinel]u8 types, you passed in: " ++ @typeName(@TypeOf(slice))),
|
||||
},
|
||||
else => @compileError("This function only supports []u8 or [:sentinel]u8 types, you passed in: " ++ @typeName(@TypeOf(slice))),
|
||||
};
|
||||
// Empty slice usually means invalid pointer
|
||||
if (ptr.len == 0) return false;
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
return trackFreeAssumeLocked(state, ptr, ret_addr orelse @returnAddress());
|
||||
}
|
||||
|
||||
pub fn setPointerExtra(scope: Self, ptr: *anyopaque, extra: Extra) void {
|
||||
if (comptime !enabled) return;
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
const allocation = state.allocations.getPtr(ptr) orelse
|
||||
@panic("Pointer not owned by allocation scope");
|
||||
allocation.extra = extra;
|
||||
}
|
||||
|
||||
pub inline fn downcast(a: Allocator) ?Self {
|
||||
return if (enabled and a.vtable == &vtable)
|
||||
.{ .internal_state = @ptrCast(@alignCast(a.ptr)) }
|
||||
else
|
||||
null;
|
||||
}
|
||||
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const bun = @import("bun");
|
||||
const Output = bun.Output;
|
||||
const StoredTrace = bun.crash_handler.StoredTrace;
|
||||
@@ -1,29 +1,95 @@
|
||||
//! This type is a `GenericAllocator`; see `src/allocators.zig`.
|
||||
|
||||
const Self = @This();
|
||||
|
||||
heap: HeapPtr,
|
||||
#heap: if (safety_checks) Owned(*DebugHeap) else *mimalloc.Heap,
|
||||
|
||||
const HeapPtr = if (safety_checks) *DebugHeap else *mimalloc.Heap;
|
||||
/// Uses the default thread-local heap. This type is zero-sized.
|
||||
///
|
||||
/// This type is a `GenericAllocator`; see `src/allocators.zig`.
|
||||
pub const Default = struct {
|
||||
pub fn allocator(self: Default) std.mem.Allocator {
|
||||
_ = self;
|
||||
return Borrowed.getDefault().allocator();
|
||||
}
|
||||
};
|
||||
|
||||
/// Borrowed version of `MimallocArena`, returned by `MimallocArena.borrow`.
|
||||
/// Using this type makes it clear who actually owns the `MimallocArena`, and prevents
|
||||
/// `deinit` from being called twice.
|
||||
///
|
||||
/// This type is a `GenericAllocator`; see `src/allocators.zig`.
|
||||
pub const Borrowed = struct {
|
||||
#heap: BorrowedHeap,
|
||||
|
||||
pub fn allocator(self: Borrowed) std.mem.Allocator {
|
||||
return .{ .ptr = self.#heap, .vtable = &c_allocator_vtable };
|
||||
}
|
||||
|
||||
pub fn getDefault() Borrowed {
|
||||
return .{ .#heap = getThreadHeap() };
|
||||
}
|
||||
|
||||
pub fn gc(self: Borrowed) void {
|
||||
mimalloc.mi_heap_collect(self.getMimallocHeap(), false);
|
||||
}
|
||||
|
||||
pub fn helpCatchMemoryIssues(self: Borrowed) void {
|
||||
if (comptime bun.FeatureFlags.help_catch_memory_issues) {
|
||||
self.gc();
|
||||
bun.mimalloc.mi_collect(false);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ownsPtr(self: Borrowed, ptr: *const anyopaque) bool {
|
||||
return mimalloc.mi_heap_check_owned(self.getMimallocHeap(), ptr);
|
||||
}
|
||||
|
||||
fn fromOpaque(ptr: *anyopaque) Borrowed {
|
||||
return .{ .#heap = @ptrCast(@alignCast(ptr)) };
|
||||
}
|
||||
|
||||
fn getMimallocHeap(self: Borrowed) *mimalloc.Heap {
|
||||
return if (comptime safety_checks) self.#heap.inner else self.#heap;
|
||||
}
|
||||
|
||||
fn assertThreadLock(self: Borrowed) void {
|
||||
if (comptime safety_checks) self.#heap.thread_lock.assertLocked();
|
||||
}
|
||||
|
||||
fn alignedAlloc(self: Borrowed, len: usize, alignment: Alignment) ?[*]u8 {
|
||||
log("Malloc: {d}\n", .{len});
|
||||
|
||||
const heap = self.getMimallocHeap();
|
||||
const ptr: ?*anyopaque = if (mimalloc.mustUseAlignedAlloc(alignment))
|
||||
mimalloc.mi_heap_malloc_aligned(heap, len, alignment.toByteUnits())
|
||||
else
|
||||
mimalloc.mi_heap_malloc(heap, len);
|
||||
|
||||
if (comptime bun.Environment.isDebug) {
|
||||
const usable = mimalloc.mi_malloc_usable_size(ptr);
|
||||
if (usable < len) {
|
||||
std.debug.panic("mimalloc: allocated size is too small: {d} < {d}", .{ usable, len });
|
||||
}
|
||||
}
|
||||
|
||||
return if (ptr) |p|
|
||||
@as([*]u8, @ptrCast(p))
|
||||
else
|
||||
null;
|
||||
}
|
||||
};
|
||||
|
||||
const BorrowedHeap = if (safety_checks) *DebugHeap else *mimalloc.Heap;
|
||||
|
||||
const DebugHeap = struct {
|
||||
inner: *mimalloc.Heap,
|
||||
thread_lock: bun.safety.ThreadLock,
|
||||
};
|
||||
|
||||
fn getMimallocHeap(self: Self) *mimalloc.Heap {
|
||||
return if (comptime safety_checks) self.heap.inner else self.heap;
|
||||
}
|
||||
|
||||
fn fromOpaque(ptr: *anyopaque) Self {
|
||||
return .{ .heap = bun.cast(HeapPtr, ptr) };
|
||||
}
|
||||
|
||||
fn assertThreadLock(self: Self) void {
|
||||
if (comptime safety_checks) self.heap.thread_lock.assertLocked();
|
||||
}
|
||||
|
||||
threadlocal var thread_heap: if (safety_checks) ?DebugHeap else void = if (safety_checks) null;
|
||||
|
||||
fn getThreadHeap() HeapPtr {
|
||||
fn getThreadHeap() BorrowedHeap {
|
||||
if (comptime !safety_checks) return mimalloc.mi_heap_get_default();
|
||||
if (thread_heap == null) {
|
||||
thread_heap = .{
|
||||
@@ -36,23 +102,27 @@ fn getThreadHeap() HeapPtr {
|
||||
|
||||
const log = bun.Output.scoped(.mimalloc, .hidden);
|
||||
|
||||
pub fn allocator(self: Self) std.mem.Allocator {
|
||||
return self.borrow().allocator();
|
||||
}
|
||||
|
||||
pub fn borrow(self: Self) Borrowed {
|
||||
return .{ .#heap = if (comptime safety_checks) self.#heap.get() else self.#heap };
|
||||
}
|
||||
|
||||
/// Internally, mimalloc calls mi_heap_get_default()
|
||||
/// to get the default heap.
|
||||
/// It uses pthread_getspecific to do that.
|
||||
/// We can save those extra calls if we just do it once in here
|
||||
pub fn getThreadLocalDefault() Allocator {
|
||||
return Allocator{ .ptr = getThreadHeap(), .vtable = &c_allocator_vtable };
|
||||
pub fn getThreadLocalDefault() std.mem.Allocator {
|
||||
return Borrowed.getDefault().allocator();
|
||||
}
|
||||
|
||||
pub fn backingAllocator(_: Self) Allocator {
|
||||
pub fn backingAllocator(_: Self) std.mem.Allocator {
|
||||
return getThreadLocalDefault();
|
||||
}
|
||||
|
||||
pub fn allocator(self: Self) Allocator {
|
||||
return Allocator{ .ptr = self.heap, .vtable = &c_allocator_vtable };
|
||||
}
|
||||
|
||||
pub fn dumpThreadStats(_: *Self) void {
|
||||
pub fn dumpThreadStats(_: Self) void {
|
||||
const dump_fn = struct {
|
||||
pub fn dump(textZ: [*:0]const u8, _: ?*anyopaque) callconv(.C) void {
|
||||
const text = bun.span(textZ);
|
||||
@@ -63,7 +133,7 @@ pub fn dumpThreadStats(_: *Self) void {
|
||||
bun.Output.flush();
|
||||
}
|
||||
|
||||
pub fn dumpStats(_: *Self) void {
|
||||
pub fn dumpStats(_: Self) void {
|
||||
const dump_fn = struct {
|
||||
pub fn dump(textZ: [*:0]const u8, _: ?*anyopaque) callconv(.C) void {
|
||||
const text = bun.span(textZ);
|
||||
@@ -75,9 +145,9 @@ pub fn dumpStats(_: *Self) void {
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self) void {
|
||||
const mimalloc_heap = self.getMimallocHeap();
|
||||
const mimalloc_heap = self.borrow().getMimallocHeap();
|
||||
if (comptime safety_checks) {
|
||||
bun.destroy(self.heap);
|
||||
self.#heap.deinit();
|
||||
}
|
||||
mimalloc.mi_heap_destroy(mimalloc_heap);
|
||||
self.* = undefined;
|
||||
@@ -85,70 +155,43 @@ pub fn deinit(self: *Self) void {
|
||||
|
||||
pub fn init() Self {
|
||||
const mimalloc_heap = mimalloc.mi_heap_new() orelse bun.outOfMemory();
|
||||
const heap = if (comptime safety_checks)
|
||||
bun.new(DebugHeap, .{
|
||||
.inner = mimalloc_heap,
|
||||
.thread_lock = .initLocked(),
|
||||
})
|
||||
else
|
||||
mimalloc_heap;
|
||||
return .{ .heap = heap };
|
||||
if (comptime !safety_checks) return .{ .#heap = mimalloc_heap };
|
||||
const heap: Owned(*DebugHeap) = .new(.{
|
||||
.inner = mimalloc_heap,
|
||||
.thread_lock = .initLocked(),
|
||||
});
|
||||
return .{ .#heap = heap };
|
||||
}
|
||||
|
||||
pub fn gc(self: Self) void {
|
||||
mimalloc.mi_heap_collect(self.getMimallocHeap(), false);
|
||||
self.borrow().gc();
|
||||
}
|
||||
|
||||
pub inline fn helpCatchMemoryIssues(self: Self) void {
|
||||
if (comptime bun.FeatureFlags.help_catch_memory_issues) {
|
||||
self.gc();
|
||||
bun.mimalloc.mi_collect(false);
|
||||
}
|
||||
pub fn helpCatchMemoryIssues(self: Self) void {
|
||||
self.borrow().helpCatchMemoryIssues();
|
||||
}
|
||||
|
||||
pub fn ownsPtr(self: Self, ptr: *const anyopaque) bool {
|
||||
return mimalloc.mi_heap_check_owned(self.getMimallocHeap(), ptr);
|
||||
}
|
||||
|
||||
fn alignedAlloc(self: Self, len: usize, alignment: Alignment) ?[*]u8 {
|
||||
log("Malloc: {d}\n", .{len});
|
||||
|
||||
const heap = self.getMimallocHeap();
|
||||
const ptr: ?*anyopaque = if (mimalloc.mustUseAlignedAlloc(alignment))
|
||||
mimalloc.mi_heap_malloc_aligned(heap, len, alignment.toByteUnits())
|
||||
else
|
||||
mimalloc.mi_heap_malloc(heap, len);
|
||||
|
||||
if (comptime bun.Environment.isDebug) {
|
||||
const usable = mimalloc.mi_malloc_usable_size(ptr);
|
||||
if (usable < len) {
|
||||
std.debug.panic("mimalloc: allocated size is too small: {d} < {d}", .{ usable, len });
|
||||
}
|
||||
}
|
||||
|
||||
return if (ptr) |p|
|
||||
@as([*]u8, @ptrCast(p))
|
||||
else
|
||||
null;
|
||||
return self.borrow().ownsPtr(ptr);
|
||||
}
|
||||
|
||||
fn alignedAllocSize(ptr: [*]u8) usize {
|
||||
return mimalloc.mi_malloc_usable_size(ptr);
|
||||
}
|
||||
|
||||
fn alloc(ptr: *anyopaque, len: usize, alignment: Alignment, _: usize) ?[*]u8 {
|
||||
const self = fromOpaque(ptr);
|
||||
fn vtable_alloc(ptr: *anyopaque, len: usize, alignment: Alignment, _: usize) ?[*]u8 {
|
||||
const self: Borrowed = .fromOpaque(ptr);
|
||||
self.assertThreadLock();
|
||||
return alignedAlloc(self, len, alignment);
|
||||
return self.alignedAlloc(len, alignment);
|
||||
}
|
||||
|
||||
fn resize(ptr: *anyopaque, buf: []u8, _: Alignment, new_len: usize, _: usize) bool {
|
||||
const self = fromOpaque(ptr);
|
||||
fn vtable_resize(ptr: *anyopaque, buf: []u8, _: Alignment, new_len: usize, _: usize) bool {
|
||||
const self: Borrowed = .fromOpaque(ptr);
|
||||
self.assertThreadLock();
|
||||
return mimalloc.mi_expand(buf.ptr, new_len) != null;
|
||||
}
|
||||
|
||||
fn free(
|
||||
fn vtable_free(
|
||||
_: *anyopaque,
|
||||
buf: []u8,
|
||||
alignment: Alignment,
|
||||
@@ -187,8 +230,8 @@ fn free(
|
||||
/// `ret_addr` is optionally provided as the first return address of the
|
||||
/// allocation call stack. If the value is `0` it means no return address
|
||||
/// has been provided.
|
||||
fn remap(ptr: *anyopaque, buf: []u8, alignment: Alignment, new_len: usize, _: usize) ?[*]u8 {
|
||||
const self = fromOpaque(ptr);
|
||||
fn vtable_remap(ptr: *anyopaque, buf: []u8, alignment: Alignment, new_len: usize, _: usize) ?[*]u8 {
|
||||
const self: Borrowed = .fromOpaque(ptr);
|
||||
self.assertThreadLock();
|
||||
const heap = self.getMimallocHeap();
|
||||
const aligned_size = alignment.toByteUnits();
|
||||
@@ -196,23 +239,22 @@ fn remap(ptr: *anyopaque, buf: []u8, alignment: Alignment, new_len: usize, _: us
|
||||
return @ptrCast(value);
|
||||
}
|
||||
|
||||
pub fn isInstance(allocator_: Allocator) bool {
|
||||
return allocator_.vtable == &c_allocator_vtable;
|
||||
pub fn isInstance(alloc: std.mem.Allocator) bool {
|
||||
return alloc.vtable == &c_allocator_vtable;
|
||||
}
|
||||
|
||||
const c_allocator_vtable = Allocator.VTable{
|
||||
.alloc = &Self.alloc,
|
||||
.resize = &Self.resize,
|
||||
.remap = &Self.remap,
|
||||
.free = &Self.free,
|
||||
const c_allocator_vtable = std.mem.Allocator.VTable{
|
||||
.alloc = vtable_alloc,
|
||||
.resize = vtable_resize,
|
||||
.remap = vtable_remap,
|
||||
.free = vtable_free,
|
||||
};
|
||||
|
||||
const std = @import("std");
|
||||
const Alignment = std.mem.Alignment;
|
||||
|
||||
const bun = @import("bun");
|
||||
const assert = bun.assert;
|
||||
const mimalloc = bun.mimalloc;
|
||||
const Owned = bun.ptr.Owned;
|
||||
const safety_checks = bun.Environment.ci_assert;
|
||||
|
||||
const Alignment = std.mem.Alignment;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
@@ -4,8 +4,7 @@ const NullableAllocator = @This();
|
||||
|
||||
ptr: *anyopaque = undefined,
|
||||
// Utilize the null pointer optimization on the vtable instead of
|
||||
// the regular ptr because some allocator implementations might tag their
|
||||
// `ptr` property.
|
||||
// the regular `ptr` because `ptr` may be undefined.
|
||||
vtable: ?*const std.mem.Allocator.VTable = null,
|
||||
|
||||
pub inline fn init(allocator: ?std.mem.Allocator) NullableAllocator {
|
||||
|
||||
555
src/allocators/allocation_scope.zig
Normal file
555
src/allocators/allocation_scope.zig
Normal file
@@ -0,0 +1,555 @@
|
||||
//! AllocationScope wraps another allocator, providing leak and invalid free assertions.
|
||||
//! It also allows measuring how much memory a scope has allocated.
|
||||
|
||||
const allocation_scope = @This();
|
||||
|
||||
/// An allocation scope with a dynamically typed parent allocator. Prefer using a concrete type,
|
||||
/// like `AllocationScopeIn(bun.DefaultAllocator)`.
|
||||
pub const AllocationScope = AllocationScopeIn(std.mem.Allocator);
|
||||
|
||||
pub const Allocation = struct {
|
||||
allocated_at: StoredTrace,
|
||||
len: usize,
|
||||
extra: Extra,
|
||||
};
|
||||
|
||||
pub const Free = struct {
|
||||
allocated_at: StoredTrace,
|
||||
freed_at: StoredTrace,
|
||||
};
|
||||
|
||||
pub const Extra = struct {
|
||||
ptr: *anyopaque,
|
||||
vtable: ?*const VTable,
|
||||
|
||||
pub const none: Extra = .{ .ptr = undefined, .vtable = null };
|
||||
|
||||
pub const VTable = struct {
|
||||
onAllocationLeak: *const fn (*anyopaque, data: []u8) void,
|
||||
};
|
||||
};
|
||||
|
||||
pub const Stats = struct {
|
||||
total_memory_allocated: usize,
|
||||
num_allocations: usize,
|
||||
};
|
||||
|
||||
pub const FreeError = error{
|
||||
/// Tried to free memory that wasn't allocated by this `AllocationScope`, or was already freed.
|
||||
NotAllocated,
|
||||
};
|
||||
|
||||
pub const enabled = bun.Environment.enableAllocScopes;
|
||||
pub const max_free_tracking = 2048 - 1;
|
||||
|
||||
const History = struct {
|
||||
const Self = @This();
|
||||
|
||||
total_memory_allocated: usize = 0,
|
||||
/// Allocated by `State.parent`.
|
||||
allocations: std.AutoHashMapUnmanaged([*]const u8, Allocation) = .empty,
|
||||
/// Allocated by `State.parent`.
|
||||
frees: std.AutoArrayHashMapUnmanaged([*]const u8, Free) = .empty,
|
||||
/// Once `frees` fills up, entries are overwritten from start to end.
|
||||
free_overwrite_index: std.math.IntFittingRange(0, max_free_tracking + 1) = 0,
|
||||
|
||||
/// `allocator` should be `State.parent`.
|
||||
fn deinit(self: *Self, allocator: std.mem.Allocator) void {
|
||||
self.allocations.deinit(allocator);
|
||||
self.frees.deinit(allocator);
|
||||
self.* = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const LockedState = struct {
|
||||
const Self = @This();
|
||||
|
||||
/// Should be the same as `State.parent`.
|
||||
parent: std.mem.Allocator,
|
||||
history: *History,
|
||||
|
||||
fn alloc(self: Self, len: usize, alignment: std.mem.Alignment, ret_addr: usize) bun.OOM![*]u8 {
|
||||
const result = self.parent.rawAlloc(len, alignment, ret_addr) orelse
|
||||
return error.OutOfMemory;
|
||||
errdefer self.parent.rawFree(result[0..len], alignment, ret_addr);
|
||||
try self.trackAllocation(result[0..len], ret_addr, .none);
|
||||
return result;
|
||||
}
|
||||
|
||||
fn free(self: Self, buf: []u8, alignment: std.mem.Alignment, ret_addr: usize) void {
|
||||
const success = if (self.trackFree(buf, ret_addr))
|
||||
true
|
||||
else |err| switch (err) {
|
||||
error.NotAllocated => false,
|
||||
};
|
||||
if (success or bun.Environment.enable_asan) {
|
||||
self.parent.rawFree(buf, alignment, ret_addr);
|
||||
}
|
||||
if (!success) {
|
||||
// If asan did not catch the free, panic now.
|
||||
std.debug.panic("Invalid free: {*}", .{buf});
|
||||
}
|
||||
}
|
||||
|
||||
fn assertOwned(self: Self, ptr: anytype) void {
|
||||
const cast_ptr: [*]const u8 = @ptrCast(switch (@typeInfo(@TypeOf(ptr)).pointer.size) {
|
||||
.c, .one, .many => ptr,
|
||||
.slice => if (ptr.len > 0) ptr.ptr else return,
|
||||
});
|
||||
if (!self.history.allocations.contains(cast_ptr)) {
|
||||
@panic("this pointer was not owned by the allocation scope");
|
||||
}
|
||||
}
|
||||
|
||||
fn assertUnowned(self: Self, ptr: anytype) void {
|
||||
const cast_ptr: [*]const u8 = @ptrCast(switch (@typeInfo(@TypeOf(ptr)).pointer.size) {
|
||||
.c, .one, .many => ptr,
|
||||
.slice => if (ptr.len > 0) ptr.ptr else return,
|
||||
});
|
||||
if (self.history.allocations.getPtr(cast_ptr)) |owned| {
|
||||
Output.warn("Owned pointer allocated here:");
|
||||
bun.crash_handler.dumpStackTrace(
|
||||
owned.allocated_at.trace(),
|
||||
trace_limits,
|
||||
trace_limits,
|
||||
);
|
||||
@panic("this pointer was owned by the allocation scope when it was not supposed to be");
|
||||
}
|
||||
}
|
||||
|
||||
fn trackAllocation(self: Self, buf: []const u8, ret_addr: usize, extra: Extra) bun.OOM!void {
|
||||
const trace = StoredTrace.capture(ret_addr);
|
||||
try self.history.allocations.putNoClobber(self.parent, buf.ptr, .{
|
||||
.allocated_at = trace,
|
||||
.len = buf.len,
|
||||
.extra = extra,
|
||||
});
|
||||
self.history.total_memory_allocated += buf.len;
|
||||
}
|
||||
|
||||
fn trackFree(self: Self, buf: []const u8, ret_addr: usize) FreeError!void {
|
||||
const entry = self.history.allocations.fetchRemove(buf.ptr) orelse {
|
||||
Output.errGeneric("Invalid free, pointer {any}, len {d}", .{ buf.ptr, buf.len });
|
||||
|
||||
if (self.history.frees.getPtr(buf.ptr)) |free_entry| {
|
||||
Output.printErrorln("Pointer allocated here:", .{});
|
||||
bun.crash_handler.dumpStackTrace(free_entry.allocated_at.trace(), trace_limits);
|
||||
Output.printErrorln("Pointer first freed here:", .{});
|
||||
bun.crash_handler.dumpStackTrace(free_entry.freed_at.trace(), free_trace_limits);
|
||||
}
|
||||
|
||||
// do not panic because address sanitizer will catch this case better.
|
||||
// the log message is in case there is a situation where address
|
||||
// sanitizer does not catch the invalid free.
|
||||
return error.NotAllocated;
|
||||
};
|
||||
|
||||
self.history.total_memory_allocated -= entry.value.len;
|
||||
|
||||
// Store a limited amount of free entries
|
||||
if (self.history.frees.count() >= max_free_tracking) {
|
||||
const i = self.history.free_overwrite_index;
|
||||
self.history.free_overwrite_index =
|
||||
@mod(self.history.free_overwrite_index + 1, max_free_tracking);
|
||||
self.history.frees.swapRemoveAt(i);
|
||||
}
|
||||
|
||||
self.history.frees.put(self.parent, buf.ptr, .{
|
||||
.allocated_at = entry.value.allocated_at,
|
||||
.freed_at = StoredTrace.capture(ret_addr),
|
||||
}) catch |err| bun.handleOom(err);
|
||||
}
|
||||
};
|
||||
|
||||
const State = struct {
|
||||
const Self = @This();
|
||||
|
||||
/// This field should not be modified. Therefore, it doesn't need to be protected by the mutex.
|
||||
parent: std.mem.Allocator,
|
||||
history: bun.threading.Guarded(History),
|
||||
|
||||
fn init(parent_alloc: std.mem.Allocator) Self {
|
||||
return .{
|
||||
.parent = parent_alloc,
|
||||
.history = .init(.{}),
|
||||
};
|
||||
}
|
||||
|
||||
fn lock(self: *Self) LockedState {
|
||||
return .{
|
||||
.parent = self.parent,
|
||||
.history = self.history.lock(),
|
||||
};
|
||||
}
|
||||
|
||||
fn unlock(self: *Self) void {
|
||||
self.history.unlock();
|
||||
}
|
||||
|
||||
fn deinit(self: *Self) void {
|
||||
defer self.* = undefined;
|
||||
var history = self.history.intoUnprotected();
|
||||
defer history.deinit();
|
||||
|
||||
const count = history.allocations.count();
|
||||
if (count == 0) return;
|
||||
Output.errGeneric("Allocation scope leaked {d} allocations ({})", .{
|
||||
count,
|
||||
bun.fmt.size(history.total_memory_allocated, .{}),
|
||||
});
|
||||
|
||||
var it = history.allocations.iterator();
|
||||
var n: usize = 0;
|
||||
while (it.next()) |entry| : (n += 1) {
|
||||
if (n >= 10) {
|
||||
Output.prettyErrorln("(only showing first 10 leaks)", .{});
|
||||
break;
|
||||
}
|
||||
Output.prettyErrorln(
|
||||
"- {any}, len {d}, at:",
|
||||
.{ entry.key_ptr.*, entry.value_ptr.len },
|
||||
);
|
||||
bun.crash_handler.dumpStackTrace(
|
||||
entry.value_ptr.allocated_at.trace(),
|
||||
trace_limits,
|
||||
);
|
||||
const extra = entry.value_ptr.extra;
|
||||
if (extra.vtable) |extra_vtable| {
|
||||
extra_vtable.onAllocationLeak(
|
||||
extra.ptr,
|
||||
@constCast(entry.key_ptr.*[0..entry.value_ptr.len]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Output.panic(
|
||||
"Allocation scope leaked {}",
|
||||
.{bun.fmt.size(history.total_memory_allocated, .{})},
|
||||
);
|
||||
}
|
||||
|
||||
fn trackExternalAllocation(self: *Self, ptr: []const u8, ret_addr: ?usize, extra: Extra) void {
|
||||
const locked = self.lock();
|
||||
defer self.unlock();
|
||||
locked.trackAllocation(ptr, ret_addr orelse @returnAddress(), extra) catch |err|
|
||||
bun.handleOom(err);
|
||||
}
|
||||
|
||||
fn trackExternalFree(self: *Self, slice: anytype, ret_addr: ?usize) FreeError!void {
|
||||
const invalidType = struct {
|
||||
fn invalidType() noreturn {
|
||||
@compileError(std.fmt.comptimePrint(
|
||||
"This function only supports []u8 or [:sentinel]u8 types, you passed in: {s}",
|
||||
.{@typeName(@TypeOf(slice))},
|
||||
));
|
||||
}
|
||||
}.invalidType;
|
||||
|
||||
const ptr: []const u8 = switch (@typeInfo(@TypeOf(slice))) {
|
||||
.pointer => |p| switch (p.size) {
|
||||
.slice => brk: {
|
||||
if (p.child != u8) invalidType();
|
||||
if (p.sentinel_ptr == null) break :brk slice;
|
||||
// Ensure we include the sentinel value
|
||||
break :brk slice[0 .. slice.len + 1];
|
||||
},
|
||||
else => invalidType(),
|
||||
},
|
||||
else => invalidType(),
|
||||
};
|
||||
// Empty slice usually means invalid pointer
|
||||
if (ptr.len == 0) return;
|
||||
const locked = self.lock();
|
||||
defer self.unlock();
|
||||
return locked.trackFree(ptr, ret_addr orelse @returnAddress());
|
||||
}
|
||||
|
||||
fn setPointerExtra(self: *Self, ptr: *anyopaque, extra: Extra) void {
|
||||
const locked = self.lock();
|
||||
defer self.unlock();
|
||||
const allocation = locked.history.allocations.getPtr(@ptrCast(ptr)) orelse
|
||||
@panic("Pointer not owned by allocation scope");
|
||||
allocation.extra = extra;
|
||||
}
|
||||
};
|
||||
|
||||
/// An allocation scope that uses a specific kind of parent allocator.
|
||||
///
|
||||
/// This type is a `GenericAllocator`; see `src/allocators.zig`.
|
||||
pub fn AllocationScopeIn(comptime Allocator: type) type {
|
||||
const BorrowedAllocator = bun.allocators.Borrowed(Allocator);
|
||||
|
||||
// Borrowed version of `AllocationScope`. Access this type as `AllocationScope.Borrowed`.
|
||||
const BorrowedScope = struct {
|
||||
const Self = @This();
|
||||
|
||||
#parent: BorrowedAllocator,
|
||||
#state: if (enabled) *State else void,
|
||||
|
||||
pub fn allocator(self: Self) std.mem.Allocator {
|
||||
return if (comptime enabled)
|
||||
.{ .ptr = self.#state, .vtable = &vtable }
|
||||
else
|
||||
bun.allocators.asStd(self.#parent);
|
||||
}
|
||||
|
||||
pub fn parent(self: Self) BorrowedAllocator {
|
||||
return self.#parent;
|
||||
}
|
||||
|
||||
/// Deinitializes a borrowed allocation scope. This does not deinitialize the
|
||||
/// `AllocationScope` itself; only the owner of the `AllocationScope` should do that.
|
||||
///
|
||||
/// This method doesn't need to be called unless `bun.allocators.Borrowed(Allocator)` has
|
||||
/// a `deinit` method.
|
||||
pub fn deinit(self: *Self) void {
|
||||
bun.memory.deinit(&self.#parent);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn stats(self: Self) Stats {
|
||||
if (comptime !enabled) @compileError("AllocationScope must be enabled");
|
||||
const state = self.#state.lock();
|
||||
defer self.#state.unlock();
|
||||
return .{
|
||||
.total_memory_allocated = state.history.total_memory_allocated,
|
||||
.num_allocations = state.history.allocations.count(),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn assertOwned(self: Self, ptr: anytype) void {
|
||||
if (comptime !enabled) return;
|
||||
const state = self.#state.lock();
|
||||
defer self.#state.unlock();
|
||||
state.assertOwned(ptr);
|
||||
}
|
||||
|
||||
pub fn assertUnowned(self: Self, ptr: anytype) void {
|
||||
if (comptime !enabled) return;
|
||||
const state = self.#state.lock();
|
||||
defer self.#state.unlock();
|
||||
state.assertUnowned(ptr);
|
||||
}
|
||||
|
||||
pub fn trackExternalAllocation(
|
||||
self: Self,
|
||||
ptr: []const u8,
|
||||
ret_addr: ?usize,
|
||||
extra: Extra,
|
||||
) void {
|
||||
if (comptime enabled) self.#state.trackExternalAllocation(ptr, ret_addr, extra);
|
||||
}
|
||||
|
||||
pub fn trackExternalFree(self: Self, slice: anytype, ret_addr: ?usize) FreeError!void {
|
||||
return if (comptime enabled) self.#state.trackExternalFree(slice, ret_addr);
|
||||
}
|
||||
|
||||
pub fn setPointerExtra(self: Self, ptr: *anyopaque, extra: Extra) void {
|
||||
if (comptime enabled) self.#state.setPointerExtra(ptr, extra);
|
||||
}
|
||||
|
||||
fn downcastImpl(
|
||||
std_alloc: std.mem.Allocator,
|
||||
parent_alloc: if (Allocator == std.mem.Allocator)
|
||||
?BorrowedAllocator
|
||||
else
|
||||
BorrowedAllocator,
|
||||
) Self {
|
||||
const state = if (comptime enabled) blk: {
|
||||
bun.assertf(
|
||||
std_alloc.vtable == &vtable,
|
||||
"allocator is not an allocation scope (has vtable {*})",
|
||||
.{std_alloc.vtable},
|
||||
);
|
||||
const state: *State = @ptrCast(@alignCast(std_alloc.ptr));
|
||||
break :blk state;
|
||||
};
|
||||
|
||||
const current_std_parent = if (comptime enabled)
|
||||
state.parent
|
||||
else
|
||||
std_alloc;
|
||||
|
||||
const new_parent = if (comptime Allocator == std.mem.Allocator)
|
||||
parent_alloc orelse current_std_parent
|
||||
else
|
||||
parent_alloc;
|
||||
|
||||
const new_std_parent = bun.allocators.asStd(new_parent);
|
||||
bun.safety.alloc.assertEqFmt(
|
||||
current_std_parent,
|
||||
new_std_parent,
|
||||
"tried to downcast allocation scope with wrong parent allocator",
|
||||
.{},
|
||||
);
|
||||
return .{ .#parent = new_parent, .#state = state };
|
||||
}
|
||||
|
||||
/// Converts an `std.mem.Allocator` into a borrowed allocation scope, with a given parent
|
||||
/// allocator.
|
||||
///
|
||||
/// Requirements:
|
||||
///
|
||||
/// * `std_alloc` must have come from `AllocationScopeIn(Allocator).allocator` (or the
|
||||
/// equivalent method on a `Borrowed` instance).
|
||||
///
|
||||
/// * `parent_alloc` must be equivalent to the (borrowed) parent allocator of the original
|
||||
/// allocation scope (that is, the return value of `AllocationScopeIn(Allocator).parent`).
|
||||
/// In particular, `bun.allocators.asStd` must return the same value for each allocator.
|
||||
pub fn downcastIn(std_alloc: std.mem.Allocator, parent_alloc: BorrowedAllocator) Self {
|
||||
return downcastImpl(std_alloc, parent_alloc);
|
||||
}
|
||||
|
||||
/// Converts an `std.mem.Allocator` into a borrowed allocation scope.
|
||||
///
|
||||
/// Requirements:
|
||||
///
|
||||
/// * `std_alloc` must have come from `AllocationScopeIn(Allocator).allocator` (or the
|
||||
/// equivalent method on a `Borrowed` instance).
|
||||
///
|
||||
/// * One of the following must be true:
|
||||
///
|
||||
/// 1. `Allocator` is `std.mem.Allocator`.
|
||||
///
|
||||
/// 2. The parent allocator of the original allocation scope is equivalent to a
|
||||
/// default-initialized borrowed `Allocator`, as returned by
|
||||
/// `bun.memory.initDefault(bun.allocators.Borrowed(Allocator))`. This is the case
|
||||
/// for `bun.DefaultAllocator`.
|
||||
pub fn downcast(std_alloc: std.mem.Allocator) Self {
|
||||
return downcastImpl(std_alloc, if (comptime Allocator == std.mem.Allocator)
|
||||
null
|
||||
else
|
||||
bun.memory.initDefault(BorrowedAllocator));
|
||||
}
|
||||
};
|
||||
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
#parent: Allocator,
|
||||
#state: if (Self.enabled) Owned(*State) else void,
|
||||
|
||||
pub const enabled = allocation_scope.enabled;
|
||||
|
||||
/// Borrowed version of `AllocationScope`, returned by `AllocationScope.borrow`.
|
||||
/// Using this type makes it clear who actually owns the `AllocationScope`, and prevents
|
||||
/// `deinit` from being called twice.
|
||||
///
|
||||
/// This type is a `GenericAllocator`; see `src/allocators.zig`.
|
||||
pub const Borrowed = BorrowedScope;
|
||||
|
||||
pub fn init(parent_alloc: Allocator) Self {
|
||||
return .{
|
||||
.#parent = parent_alloc,
|
||||
.#state = if (comptime Self.enabled) .new(.init(
|
||||
bun.allocators.asStd(parent_alloc),
|
||||
)),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn initDefault() Self {
|
||||
return .init(bun.memory.initDefault(Allocator));
|
||||
}
|
||||
|
||||
/// Borrows this `AllocationScope`. Use this method instead of copying `self`, as that makes
|
||||
/// it hard to know who owns the `AllocationScope`, and could lead to `deinit` being called
|
||||
/// twice.
|
||||
pub fn borrow(self: Self) Borrowed {
|
||||
return .{
|
||||
.#parent = self.parent(),
|
||||
.#state = if (comptime Self.enabled) self.#state.get(),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn allocator(self: Self) std.mem.Allocator {
|
||||
return self.borrow().allocator();
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self) void {
|
||||
bun.memory.deinit(&self.#parent);
|
||||
if (comptime Self.enabled) self.#state.deinit();
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn parent(self: Self) BorrowedAllocator {
|
||||
return bun.allocators.borrow(self.#parent);
|
||||
}
|
||||
|
||||
pub fn stats(self: Self) Stats {
|
||||
return self.borrow().stats();
|
||||
}
|
||||
|
||||
pub fn assertOwned(self: Self, ptr: anytype) void {
|
||||
self.borrow().assertOwned(ptr);
|
||||
}
|
||||
|
||||
pub fn assertUnowned(self: Self, ptr: anytype) void {
|
||||
self.borrow().assertUnowned(ptr);
|
||||
}
|
||||
|
||||
/// Track an arbitrary pointer. Extra data can be stored in the allocation, which will be
|
||||
/// printed when a leak is detected.
|
||||
pub fn trackExternalAllocation(
|
||||
self: Self,
|
||||
ptr: []const u8,
|
||||
ret_addr: ?usize,
|
||||
extra: Extra,
|
||||
) void {
|
||||
self.borrow().trackExternalAllocation(ptr, ret_addr, extra);
|
||||
}
|
||||
|
||||
/// Call when the pointer from `trackExternalAllocation` is freed.
|
||||
pub fn trackExternalFree(self: Self, slice: anytype, ret_addr: ?usize) FreeError!void {
|
||||
return self.borrow().trackExternalFree(slice, ret_addr);
|
||||
}
|
||||
|
||||
pub fn setPointerExtra(self: Self, ptr: *anyopaque, extra: Extra) void {
|
||||
return self.borrow().setPointerExtra(ptr, extra);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const vtable: std.mem.Allocator.VTable = .{
|
||||
.alloc = vtable_alloc,
|
||||
.resize = std.mem.Allocator.noResize,
|
||||
.remap = std.mem.Allocator.noRemap,
|
||||
.free = vtable_free,
|
||||
};
|
||||
|
||||
// Smaller traces since AllocationScope prints so many
|
||||
pub const trace_limits: bun.crash_handler.WriteStackTraceLimits = .{
|
||||
.frame_count = 6,
|
||||
.stop_at_jsc_llint = true,
|
||||
.skip_stdlib = true,
|
||||
};
|
||||
|
||||
pub const free_trace_limits: bun.crash_handler.WriteStackTraceLimits = .{
|
||||
.frame_count = 3,
|
||||
.stop_at_jsc_llint = true,
|
||||
.skip_stdlib = true,
|
||||
};
|
||||
|
||||
fn vtable_alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: usize) ?[*]u8 {
|
||||
const raw_state: *State = @ptrCast(@alignCast(ctx));
|
||||
const state = raw_state.lock();
|
||||
defer raw_state.unlock();
|
||||
return state.alloc(len, alignment, ret_addr) catch null;
|
||||
}
|
||||
|
||||
fn vtable_free(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, ret_addr: usize) void {
|
||||
const raw_state: *State = @ptrCast(@alignCast(ctx));
|
||||
const state = raw_state.lock();
|
||||
defer raw_state.unlock();
|
||||
state.free(buf, alignment, ret_addr);
|
||||
}
|
||||
|
||||
pub inline fn isInstance(allocator: std.mem.Allocator) bool {
|
||||
return (comptime enabled) and allocator.vtable == &vtable;
|
||||
}
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
const bun = @import("bun");
|
||||
const Output = bun.Output;
|
||||
const Owned = bun.ptr.Owned;
|
||||
const StoredTrace = bun.crash_handler.StoredTrace;
|
||||
112
src/allocators/maybe_owned.zig
Normal file
112
src/allocators/maybe_owned.zig
Normal file
@@ -0,0 +1,112 @@
|
||||
/// This type can be used with `bun.ptr.Owned` to model "maybe owned" pointers:
|
||||
///
|
||||
/// ```
|
||||
/// // Either owned by the default allocator, or borrowed
|
||||
/// const MaybeOwnedFoo = bun.ptr.Owned(*Foo, bun.allocators.MaybeOwned(bun.DefaultAllocator));
|
||||
///
|
||||
/// var owned_foo: MaybeOwnedFoo = .new(makeFoo());
|
||||
/// var borrowed_foo: MaybeOwnedFoo = .fromRawIn(some_foo_ptr, .initBorrowed());
|
||||
///
|
||||
/// owned_foo.deinit(); // calls `Foo.deinit` and frees the memory
|
||||
/// borrowed_foo.deinit(); // no-op
|
||||
/// ```
|
||||
///
|
||||
/// This type is a `GenericAllocator`; see `src/allocators.zig`.
|
||||
pub fn MaybeOwned(comptime Allocator: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
_parent: bun.allocators.Nullable(Allocator),
|
||||
|
||||
/// Same as `.initBorrowed()`. This allocator cannot be used to allocate memory; a panic
|
||||
/// will occur.
|
||||
pub const borrowed = .initBorrowed();
|
||||
|
||||
/// Creates a `MaybeOwned` allocator that owns memory.
|
||||
///
|
||||
/// Allocations are forwarded to a default-initialized `Allocator`.
|
||||
pub fn init() Self {
|
||||
return .initOwned(bun.memory.initDefault(Allocator));
|
||||
}
|
||||
|
||||
/// Creates a `MaybeOwned` allocator that owns memory, and forwards to a specific
|
||||
/// allocator.
|
||||
///
|
||||
/// Allocations are forwarded to `parent_alloc`.
|
||||
pub fn initOwned(parent_alloc: Allocator) Self {
|
||||
return .initRaw(parent_alloc);
|
||||
}
|
||||
|
||||
/// Creates a `MaybeOwned` allocator that does not own any memory. This allocator cannot
|
||||
/// be used to allocate new memory (a panic will occur), and its implementation of `free`
|
||||
/// is a no-op.
|
||||
pub fn initBorrowed() Self {
|
||||
return .initRaw(null);
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self) void {
|
||||
var maybe_parent = self.intoParent();
|
||||
if (maybe_parent) |*parent_alloc| {
|
||||
bun.memory.deinit(parent_alloc);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn isOwned(self: Self) bool {
|
||||
return self.rawParent() != null;
|
||||
}
|
||||
|
||||
pub fn allocator(self: Self) std.mem.Allocator {
|
||||
const maybe_parent = self.rawParent();
|
||||
return if (maybe_parent) |parent_alloc|
|
||||
bun.allocators.asStd(parent_alloc)
|
||||
else
|
||||
.{ .ptr = undefined, .vtable = &null_vtable };
|
||||
}
|
||||
|
||||
const BorrowedParent = bun.allocators.Borrowed(Allocator);
|
||||
|
||||
pub fn parent(self: Self) ?BorrowedParent {
|
||||
const maybe_parent = self.rawParent();
|
||||
return if (maybe_parent) |parent_alloc|
|
||||
bun.allocators.borrow(parent_alloc)
|
||||
else
|
||||
null;
|
||||
}
|
||||
|
||||
pub fn intoParent(self: *Self) ?Allocator {
|
||||
defer self.* = undefined;
|
||||
return self.rawParent();
|
||||
}
|
||||
|
||||
/// Used by smart pointer types and allocator wrappers. See `bun.allocators.borrow`.
|
||||
pub const Borrowed = MaybeOwned(BorrowedParent);
|
||||
|
||||
pub fn borrow(self: Self) Borrowed {
|
||||
return .{ ._parent = bun.allocators.initNullable(BorrowedParent, self.parent()) };
|
||||
}
|
||||
|
||||
fn initRaw(parent_alloc: ?Allocator) Self {
|
||||
return .{ ._parent = bun.allocators.initNullable(Allocator, parent_alloc) };
|
||||
}
|
||||
|
||||
fn rawParent(self: Self) ?Allocator {
|
||||
return bun.allocators.unpackNullable(Allocator, self._parent);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn nullAlloc(ptr: *anyopaque, len: usize, alignment: Alignment, ret_addr: usize) ?[*]u8 {
|
||||
_ = .{ ptr, len, alignment, ret_addr };
|
||||
std.debug.panic("cannot allocate with a borrowed `MaybeOwned` allocator", .{});
|
||||
}
|
||||
|
||||
const null_vtable: std.mem.Allocator.VTable = .{
|
||||
.alloc = nullAlloc,
|
||||
.resize = std.mem.Allocator.noResize,
|
||||
.remap = std.mem.Allocator.noRemap,
|
||||
.free = std.mem.Allocator.noFree,
|
||||
};
|
||||
|
||||
const bun = @import("bun");
|
||||
const std = @import("std");
|
||||
const Alignment = std.mem.Alignment;
|
||||
@@ -799,6 +799,9 @@ pub const api = struct {
|
||||
/// import_source
|
||||
import_source: []const u8,
|
||||
|
||||
/// side_effects
|
||||
side_effects: bool = false,
|
||||
|
||||
pub fn decode(reader: anytype) anyerror!Jsx {
|
||||
var this = std.mem.zeroes(Jsx);
|
||||
|
||||
@@ -807,6 +810,7 @@ pub const api = struct {
|
||||
this.fragment = try reader.readValue([]const u8);
|
||||
this.development = try reader.readValue(bool);
|
||||
this.import_source = try reader.readValue([]const u8);
|
||||
this.side_effects = try reader.readValue(bool);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -816,6 +820,7 @@ pub const api = struct {
|
||||
try writer.writeValue(@TypeOf(this.fragment), this.fragment);
|
||||
try writer.writeInt(@as(u8, @intFromBool(this.development)));
|
||||
try writer.writeValue(@TypeOf(this.import_source), this.import_source);
|
||||
try writer.writeInt(@as(u8, @intFromBool(this.side_effects)));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ pub const AssignTarget = enum(u2) {
|
||||
};
|
||||
|
||||
pub const LocRef = struct {
|
||||
loc: logger.Loc = logger.Loc.Empty,
|
||||
loc: logger.Loc = .none,
|
||||
|
||||
// TODO: remove this optional and make Ref a function getter
|
||||
// That will make this struct 128 bits instead of 192 bits and we can remove some heap allocations
|
||||
@@ -121,7 +121,7 @@ pub const ClauseItem = struct {
|
||||
/// For exports: `export { foo as bar }` - "bar" is the alias
|
||||
/// For re-exports: `export { foo as bar } from 'path'` - "bar" is the alias
|
||||
alias: string,
|
||||
alias_loc: logger.Loc = logger.Loc.Empty,
|
||||
alias_loc: logger.Loc = .none,
|
||||
/// Reference to the actual symbol being imported/exported.
|
||||
/// For imports: `import { foo as bar }` - ref to the symbol representing "foo" from the source module
|
||||
/// For exports: `export { foo as bar }` - ref to the local symbol "foo"
|
||||
|
||||
@@ -22,9 +22,9 @@ exports_kind: ExportsKind = ExportsKind.none,
|
||||
|
||||
// This is a list of ES6 features. They are ranges instead of booleans so
|
||||
// that they can be used in log messages. Check to see if "Len > 0".
|
||||
import_keyword: logger.Range = logger.Range.None, // Does not include TypeScript-specific syntax or "import()"
|
||||
export_keyword: logger.Range = logger.Range.None, // Does not include TypeScript-specific syntax
|
||||
top_level_await_keyword: logger.Range = logger.Range.None,
|
||||
import_keyword: logger.Range = .none, // Does not include TypeScript-specific syntax or "import()"
|
||||
export_keyword: logger.Range = .none, // Does not include TypeScript-specific syntax
|
||||
top_level_await_keyword: logger.Range = .none,
|
||||
|
||||
/// These are stored at the AST level instead of on individual AST nodes so
|
||||
/// they can be manipulated efficiently without a full AST traversal
|
||||
|
||||
@@ -111,7 +111,7 @@ pub fn toAST(this: *const BundledAst) Ast {
|
||||
.uses_exports_ref = this.flags.uses_exports_ref,
|
||||
.uses_module_ref = this.flags.uses_module_ref,
|
||||
// .uses_require_ref = ast.uses_require_ref,
|
||||
.export_keyword = .{ .len = if (this.flags.uses_export_keyword) 1 else 0, .loc = .{} },
|
||||
.export_keyword = .{ .len = if (this.flags.uses_export_keyword) 1 else 0, .loc = .none },
|
||||
.force_cjs_to_esm = this.flags.force_cjs_to_esm,
|
||||
.has_lazy_export = this.flags.has_lazy_export,
|
||||
.commonjs_module_exports_assigned_deoptimized = this.flags.commonjs_module_exports_assigned_deoptimized,
|
||||
|
||||
@@ -439,19 +439,19 @@ pub fn finalize(ctx: *ConvertESMExportsForHmr, p: anytype, all_parts: []js_ast.P
|
||||
if (ctx.export_props.items.len > 0) {
|
||||
const obj = Expr.init(E.Object, .{
|
||||
.properties = G.Property.List.fromList(ctx.export_props),
|
||||
}, logger.Loc.Empty);
|
||||
}, .none);
|
||||
|
||||
// `hmr.exports = ...`
|
||||
try ctx.stmts.append(p.allocator, Stmt.alloc(S.SExpr, .{
|
||||
.value = Expr.assign(
|
||||
Expr.init(E.Dot, .{
|
||||
.target = Expr.initIdentifier(p.hmr_api_ref, logger.Loc.Empty),
|
||||
.target = Expr.initIdentifier(p.hmr_api_ref, .none),
|
||||
.name = "exports",
|
||||
.name_loc = logger.Loc.Empty,
|
||||
}, logger.Loc.Empty),
|
||||
.name_loc = .none,
|
||||
}, .none),
|
||||
obj,
|
||||
),
|
||||
}, logger.Loc.Empty));
|
||||
}, .none));
|
||||
|
||||
// mark a dependency on module_ref so it is renamed
|
||||
try ctx.last_part.symbol_uses.put(p.allocator, p.module_ref, .{ .count_estimate = 1 });
|
||||
@@ -462,13 +462,13 @@ pub fn finalize(ctx: *ConvertESMExportsForHmr, p: anytype, all_parts: []js_ast.P
|
||||
try ctx.stmts.append(p.allocator, Stmt.alloc(S.SExpr, .{
|
||||
.value = Expr.init(E.Call, .{
|
||||
.target = Expr.init(E.Dot, .{
|
||||
.target = Expr.initIdentifier(p.hmr_api_ref, .Empty),
|
||||
.target = Expr.initIdentifier(p.hmr_api_ref, .none),
|
||||
.name = "reactRefreshAccept",
|
||||
.name_loc = .Empty,
|
||||
}, .Empty),
|
||||
.name_loc = .none,
|
||||
}, .none),
|
||||
.args = .init(&.{}),
|
||||
}, .Empty),
|
||||
}, .Empty));
|
||||
}, .none),
|
||||
}, .none));
|
||||
}
|
||||
|
||||
// Merge all part metadata into the first part.
|
||||
|
||||
@@ -15,7 +15,7 @@ pub const Array = struct {
|
||||
is_single_line: bool = false,
|
||||
is_parenthesized: bool = false,
|
||||
was_originally_macro: bool = false,
|
||||
close_bracket_loc: logger.Loc = logger.Loc.Empty,
|
||||
close_bracket_loc: logger.Loc = .none,
|
||||
|
||||
pub fn push(this: *Array, allocator: std.mem.Allocator, item: Expr) !void {
|
||||
try this.items.push(allocator, item);
|
||||
@@ -161,7 +161,7 @@ pub const Call = struct {
|
||||
args: ExprNodeList = ExprNodeList{},
|
||||
optional_chain: ?OptionalChain = null,
|
||||
is_direct_eval: bool = false,
|
||||
close_paren_loc: logger.Loc = logger.Loc.Empty,
|
||||
close_paren_loc: logger.Loc = .none,
|
||||
|
||||
// True if there is a comment containing "@__PURE__" or "#__PURE__" preceding
|
||||
// this call expression. This is an annotation used for tree shaking, and
|
||||
@@ -233,7 +233,7 @@ pub const Arrow = struct {
|
||||
pub const noop_return_undefined: Arrow = .{
|
||||
.args = &.{},
|
||||
.body = .{
|
||||
.loc = .Empty,
|
||||
.loc = .none,
|
||||
.stmts = &.{},
|
||||
},
|
||||
};
|
||||
@@ -365,7 +365,7 @@ pub const JSXElement = struct {
|
||||
|
||||
flags: Flags.JSXElement.Bitset = Flags.JSXElement.Bitset{},
|
||||
|
||||
close_tag_loc: logger.Loc = logger.Loc.Empty,
|
||||
close_tag_loc: logger.Loc = .none,
|
||||
|
||||
pub const SpecialProp = enum {
|
||||
__self, // old react transform used this as a prop
|
||||
@@ -493,7 +493,7 @@ pub const Object = struct {
|
||||
is_parenthesized: bool = false,
|
||||
was_originally_macro: bool = false,
|
||||
|
||||
close_brace_loc: logger.Loc = logger.Loc.Empty,
|
||||
close_brace_loc: logger.Loc = .none,
|
||||
|
||||
// used in TOML parser to merge properties
|
||||
pub const Rope = struct {
|
||||
@@ -544,7 +544,7 @@ pub const Object = struct {
|
||||
}
|
||||
|
||||
pub fn putString(self: *Object, allocator: std.mem.Allocator, key: string, value: string) !void {
|
||||
return try put(self, allocator, key, Expr.init(E.String, E.String.init(value), logger.Loc.Empty));
|
||||
return try put(self, allocator, key, Expr.init(E.String, E.String.init(value), .none));
|
||||
}
|
||||
|
||||
pub const SetError = error{ OutOfMemory, Clobber };
|
||||
@@ -1246,7 +1246,7 @@ pub const Template = struct {
|
||||
if (part.value.data == .e_string and part.tail.cooked.isUTF8() and part.value.data.e_string.isUTF8()) {
|
||||
if (parts.items.len == 0) {
|
||||
if (part.value.data.e_string.len() > 0) {
|
||||
head.data.e_string.push(Expr.init(E.String, part.value.data.e_string.*, logger.Loc.Empty).data.e_string);
|
||||
head.data.e_string.push(Expr.init(E.String, part.value.data.e_string.*, .none).data.e_string);
|
||||
}
|
||||
|
||||
if (part.tail.cooked.len() > 0) {
|
||||
@@ -1260,7 +1260,7 @@ pub const Template = struct {
|
||||
|
||||
if (prev_part.tail.cooked.isUTF8()) {
|
||||
if (part.value.data.e_string.len() > 0) {
|
||||
prev_part.tail.cooked.push(Expr.init(E.String, part.value.data.e_string.*, logger.Loc.Empty).data.e_string);
|
||||
prev_part.tail.cooked.push(Expr.init(E.String, part.value.data.e_string.*, .none).data.e_string);
|
||||
}
|
||||
|
||||
if (part.tail.cooked.len() > 0) {
|
||||
@@ -1361,7 +1361,7 @@ pub const RequireString = struct {
|
||||
pub const RequireResolveString = struct {
|
||||
import_record_index: u32,
|
||||
|
||||
// close_paren_loc: logger.Loc = logger.Loc.Empty,
|
||||
// close_paren_loc: logger.Loc = .none,
|
||||
};
|
||||
|
||||
pub const InlinedEnum = struct {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
loc: logger.Loc,
|
||||
data: Data,
|
||||
|
||||
pub const empty = Expr{ .data = .{ .e_missing = E.Missing{} }, .loc = logger.Loc.Empty };
|
||||
pub const empty = Expr{ .data = .{ .e_missing = E.Missing{} }, .loc = .none };
|
||||
|
||||
pub fn isAnonymousNamed(expr: Expr) bool {
|
||||
return switch (expr.data) {
|
||||
@@ -275,7 +275,7 @@ pub fn set(expr: *Expr, allocator: std.mem.Allocator, name: string, value: Expr)
|
||||
|
||||
var new_props = expr.data.e_object.properties.listManaged(allocator);
|
||||
try new_props.append(.{
|
||||
.key = Expr.init(E.String, .{ .data = name }, logger.Loc.Empty),
|
||||
.key = Expr.init(E.String, .{ .data = name }, .none),
|
||||
.value = value,
|
||||
});
|
||||
|
||||
@@ -293,15 +293,15 @@ pub fn setString(expr: *Expr, allocator: std.mem.Allocator, name: string, value:
|
||||
const key = prop.key orelse continue;
|
||||
if (std.meta.activeTag(key.data) != .e_string) continue;
|
||||
if (key.data.e_string.eql(string, name)) {
|
||||
prop.value = Expr.init(E.String, .{ .data = value }, logger.Loc.Empty);
|
||||
prop.value = Expr.init(E.String, .{ .data = value }, .none);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var new_props = expr.data.e_object.properties.listManaged(allocator);
|
||||
try new_props.append(.{
|
||||
.key = Expr.init(E.String, .{ .data = name }, logger.Loc.Empty),
|
||||
.value = Expr.init(E.String, .{ .data = value }, logger.Loc.Empty),
|
||||
.key = Expr.init(E.String, .{ .data = name }, .none),
|
||||
.value = Expr.init(E.String, .{ .data = value }, .none),
|
||||
});
|
||||
|
||||
expr.data.e_object.properties = BabyList(G.Property).fromList(new_props);
|
||||
@@ -3088,7 +3088,7 @@ pub const Data = union(Tag) {
|
||||
|
||||
// brk: {
|
||||
// // var node = try allocator.create(Macro.JSNode);
|
||||
// // node.* = Macro.JSNode.initExpr(Expr{ .data = this, .loc = logger.Loc.Empty });
|
||||
// // node.* = Macro.JSNode.initExpr(Expr{ .data = this, .loc = .none });
|
||||
// // break :brk jsc.JSValue.c(Macro.JSNode.Class.make(globalObject, node));
|
||||
// },
|
||||
|
||||
|
||||
@@ -24,12 +24,12 @@ pub const ExportStarAlias = struct {
|
||||
};
|
||||
|
||||
pub const Class = struct {
|
||||
class_keyword: logger.Range = logger.Range.None,
|
||||
class_keyword: logger.Range = .none,
|
||||
ts_decorators: ExprNodeList = ExprNodeList{},
|
||||
class_name: ?LocRef = null,
|
||||
extends: ?ExprNodeIndex = null,
|
||||
body_loc: logger.Loc = logger.Loc.Empty,
|
||||
close_brace_loc: logger.Loc = logger.Loc.Empty,
|
||||
body_loc: logger.Loc = .none,
|
||||
close_brace_loc: logger.Loc = .none,
|
||||
properties: []Property = &([_]Property{}),
|
||||
has_decorators: bool = false,
|
||||
|
||||
@@ -157,11 +157,11 @@ pub const FnBody = struct {
|
||||
|
||||
pub const Fn = struct {
|
||||
name: ?LocRef = null,
|
||||
open_parens_loc: logger.Loc = logger.Loc.Empty,
|
||||
open_parens_loc: logger.Loc = .none,
|
||||
args: []Arg = &.{},
|
||||
// This was originally nullable, but doing so I believe caused a miscompilation
|
||||
// Specifically, the body was always null.
|
||||
body: FnBody = .{ .loc = logger.Loc.Empty, .stmts = &.{} },
|
||||
body: FnBody = .{ .loc = .none, .stmts = &.{} },
|
||||
arguments_ref: ?Ref = null,
|
||||
|
||||
flags: Flags.Function.Set = Flags.Function.None,
|
||||
|
||||
202
src/ast/P.zig
202
src/ast/P.zig
@@ -150,15 +150,15 @@ pub fn NewParser_(
|
||||
latest_return_had_semicolon: bool = false,
|
||||
has_import_meta: bool = false,
|
||||
has_es_module_syntax: bool = false,
|
||||
top_level_await_keyword: logger.Range = logger.Range.None,
|
||||
top_level_await_keyword: logger.Range = .none,
|
||||
fn_or_arrow_data_parse: FnOrArrowDataParse = FnOrArrowDataParse{},
|
||||
fn_or_arrow_data_visit: FnOrArrowDataVisit = FnOrArrowDataVisit{},
|
||||
fn_only_data_visit: FnOnlyDataVisit = FnOnlyDataVisit{},
|
||||
allocated_names: List(string) = .{},
|
||||
// allocated_names: ListManaged(string) = ListManaged(string).init(bun.default_allocator),
|
||||
// allocated_names_pool: ?*AllocatedNamesPool.Node = null,
|
||||
latest_arrow_arg_loc: logger.Loc = logger.Loc.Empty,
|
||||
forbid_suffix_after_as_loc: logger.Loc = logger.Loc.Empty,
|
||||
latest_arrow_arg_loc: logger.Loc = .none,
|
||||
forbid_suffix_after_as_loc: logger.Loc = .none,
|
||||
current_scope: *js_ast.Scope = undefined,
|
||||
scopes_for_current_part: List(*js_ast.Scope) = .{},
|
||||
symbols: ListManaged(js_ast.Symbol) = undefined,
|
||||
@@ -284,9 +284,9 @@ pub fn NewParser_(
|
||||
import_symbol_property_uses: SymbolPropertyUseMap = .{},
|
||||
|
||||
// These are for handling ES6 imports and exports
|
||||
esm_import_keyword: logger.Range = logger.Range.None,
|
||||
esm_export_keyword: logger.Range = logger.Range.None,
|
||||
enclosing_class_keyword: logger.Range = logger.Range.None,
|
||||
esm_import_keyword: logger.Range = .none,
|
||||
esm_export_keyword: logger.Range = .none,
|
||||
enclosing_class_keyword: logger.Range = .none,
|
||||
import_items_for_namespace: std.AutoHashMapUnmanaged(Ref, ImportItemForNamespaceMap) = .{},
|
||||
is_import_item: RefMap = .{},
|
||||
named_imports: NamedImportsType,
|
||||
@@ -327,7 +327,7 @@ pub fn NewParser_(
|
||||
delete_target: Expr.Data,
|
||||
loop_body: Stmt.Data,
|
||||
module_scope: *js_ast.Scope = undefined,
|
||||
module_scope_directive_loc: logger.Loc = .{},
|
||||
module_scope_directive_loc: logger.Loc = .none,
|
||||
is_control_flow_dead: bool = false,
|
||||
|
||||
/// We must be careful to avoid revisiting nodes that have scopes.
|
||||
@@ -429,7 +429,7 @@ pub fn NewParser_(
|
||||
// AssignmentExpression
|
||||
// Expression , AssignmentExpression
|
||||
//
|
||||
after_arrow_body_loc: logger.Loc = logger.Loc.Empty,
|
||||
after_arrow_body_loc: logger.Loc = .none,
|
||||
import_transposer: ImportTransposer,
|
||||
require_transposer: RequireTransposer,
|
||||
require_resolve_transposer: RequireResolveTransposer,
|
||||
@@ -1231,7 +1231,7 @@ pub fn NewParser_(
|
||||
comptime is_internal: bool,
|
||||
) anyerror!void {
|
||||
const allocator = p.allocator;
|
||||
const import_record_i = p.addImportRecordByRange(.stmt, logger.Range.None, import_path);
|
||||
const import_record_i = p.addImportRecordByRange(.stmt, .none, import_path);
|
||||
var import_record: *ImportRecord = &p.import_records.items[import_record_i];
|
||||
if (comptime is_internal)
|
||||
import_record.path.namespace = "runtime";
|
||||
@@ -1257,8 +1257,8 @@ pub fn NewParser_(
|
||||
clause_item.* = js_ast.ClauseItem{
|
||||
.alias = alias_name,
|
||||
.original_name = alias_name,
|
||||
.alias_loc = logger.Loc{},
|
||||
.name = LocRef{ .ref = ref, .loc = logger.Loc{} },
|
||||
.alias_loc = .none,
|
||||
.name = LocRef{ .ref = ref, .loc = .none },
|
||||
};
|
||||
declared_symbols.appendAssumeCapacity(.{ .ref = ref, .is_top_level = true });
|
||||
|
||||
@@ -1277,7 +1277,7 @@ pub fn NewParser_(
|
||||
try p.is_import_item.put(allocator, ref, {});
|
||||
try p.named_imports.put(allocator, ref, js_ast.NamedImport{
|
||||
.alias = alias_name,
|
||||
.alias_loc = logger.Loc{},
|
||||
.alias_loc = .none,
|
||||
.namespace_ref = namespace_ref,
|
||||
.import_record_index = import_record_i,
|
||||
});
|
||||
@@ -1290,7 +1290,7 @@ pub fn NewParser_(
|
||||
.import_record_index = import_record_i,
|
||||
.is_single_line = true,
|
||||
},
|
||||
logger.Loc{},
|
||||
.none,
|
||||
);
|
||||
if (additional_stmt) |add| {
|
||||
stmts[1] = add;
|
||||
@@ -1343,7 +1343,7 @@ pub fn NewParser_(
|
||||
// already a CommonJS module, and it will actually be more efficient
|
||||
// at runtime this way.
|
||||
const allocator = p.allocator;
|
||||
const import_record_index = p.addImportRecordByRange(.stmt, logger.Range.None, import_path);
|
||||
const import_record_index = p.addImportRecordByRange(.stmt, .none, import_path);
|
||||
|
||||
const Item = if (hot_module_reloading) B.Object.Property else js_ast.ClauseItem;
|
||||
|
||||
@@ -1365,20 +1365,20 @@ pub fn NewParser_(
|
||||
for (clauses) |entry| {
|
||||
if (entry.enabled) {
|
||||
items.appendAssumeCapacity(if (hot_module_reloading) .{
|
||||
.key = p.newExpr(E.String{ .data = entry.name }, logger.Loc.Empty),
|
||||
.value = p.b(B.Identifier{ .ref = entry.ref }, logger.Loc.Empty),
|
||||
.key = p.newExpr(E.String{ .data = entry.name }, .none),
|
||||
.value = p.b(B.Identifier{ .ref = entry.ref }, .none),
|
||||
} else .{
|
||||
.alias = entry.name,
|
||||
.original_name = entry.name,
|
||||
.alias_loc = logger.Loc{},
|
||||
.name = LocRef{ .ref = entry.ref, .loc = logger.Loc{} },
|
||||
.alias_loc = .none,
|
||||
.name = LocRef{ .ref = entry.ref, .loc = .none },
|
||||
});
|
||||
declared_symbols.appendAssumeCapacity(.{ .ref = entry.ref, .is_top_level = true });
|
||||
try p.module_scope.generated.push(allocator, entry.ref);
|
||||
try p.is_import_item.put(allocator, entry.ref, {});
|
||||
try p.named_imports.put(allocator, entry.ref, .{
|
||||
.alias = entry.name,
|
||||
.alias_loc = logger.Loc.Empty,
|
||||
.alias_loc = .none,
|
||||
.namespace_ref = namespace_ref,
|
||||
.import_record_index = import_record_index,
|
||||
});
|
||||
@@ -1391,10 +1391,10 @@ pub fn NewParser_(
|
||||
.decls = try Decl.List.fromSlice(p.allocator, &.{.{
|
||||
.binding = p.b(B.Object{
|
||||
.properties = items.items,
|
||||
}, logger.Loc.Empty),
|
||||
}, .none),
|
||||
.value = p.newExpr(E.RequireString{
|
||||
.import_record_index = import_record_index,
|
||||
}, logger.Loc.Empty),
|
||||
}, .none),
|
||||
}}),
|
||||
}
|
||||
else
|
||||
@@ -1403,7 +1403,7 @@ pub fn NewParser_(
|
||||
.items = items.items,
|
||||
.import_record_index = import_record_index,
|
||||
.is_single_line = false,
|
||||
}, logger.Loc.Empty);
|
||||
}, .none);
|
||||
|
||||
try parts.append(.{
|
||||
.stmts = stmts,
|
||||
@@ -2022,7 +2022,7 @@ pub fn NewParser_(
|
||||
fn ensureRequireSymbol(p: *P) void {
|
||||
if (p.runtime_imports.__require != null) return;
|
||||
const static_symbol = generatedSymbolName("__require");
|
||||
p.runtime_imports.__require = bun.handleOom(declareSymbolMaybeGenerated(p, .other, logger.Loc.Empty, static_symbol, true));
|
||||
p.runtime_imports.__require = bun.handleOom(declareSymbolMaybeGenerated(p, .other, .none, static_symbol, true));
|
||||
p.runtime_imports.put("__require", p.runtime_imports.__require.?);
|
||||
}
|
||||
|
||||
@@ -2231,7 +2231,7 @@ pub fn NewParser_(
|
||||
|
||||
// Sanity-check that the scopes generated by the first and second passes match
|
||||
if (bun.Environment.allow_assert and
|
||||
order.loc.start != loc.start or order.scope.kind != kind)
|
||||
order.loc != loc or order.scope.kind != kind)
|
||||
{
|
||||
p.log.level = .verbose;
|
||||
|
||||
@@ -2281,11 +2281,11 @@ pub fn NewParser_(
|
||||
}
|
||||
|
||||
if (p.scopes_in_order.items[last_i]) |prev_scope| {
|
||||
if (prev_scope.loc.start >= loc.start) {
|
||||
if (prev_scope.loc.get() >= loc.get()) {
|
||||
p.log.level = .verbose;
|
||||
bun.handleOom(p.log.addDebugFmt(p.source, prev_scope.loc, p.allocator, "Previous Scope", .{}));
|
||||
bun.handleOom(p.log.addDebugFmt(p.source, loc, p.allocator, "Next Scope", .{}));
|
||||
p.panic("Scope location {d} must be greater than {d}", .{ loc.start, prev_scope.loc.start });
|
||||
p.panic("Scope location {d} must be greater than {d}", .{ loc.get(), prev_scope.loc.get() });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2477,7 +2477,7 @@ pub fn NewParser_(
|
||||
}
|
||||
|
||||
if (errors.invalid_expr_after_question) |r| {
|
||||
p.log.addRangeErrorFmt(p.source, r, p.allocator, "Unexpected {s}", .{p.source.contents[r.loc.i()..r.endI()]}) catch unreachable;
|
||||
p.log.addRangeErrorFmt(p.source, r, p.allocator, "Unexpected {s}", .{p.source.contents[r.loc.getUsize()..r.endI()]}) catch unreachable;
|
||||
}
|
||||
|
||||
// if (errors.array_spread_feature) |err| {
|
||||
@@ -2991,7 +2991,7 @@ pub fn NewParser_(
|
||||
const scope = p.current_scope;
|
||||
if (p.isStrictMode()) {
|
||||
var why: string = "";
|
||||
var where: logger.Range = logger.Range.None;
|
||||
var where: logger.Range = .none;
|
||||
switch (scope.strict_mode) {
|
||||
.implicit_strict_mode_import => {
|
||||
where = p.esm_import_keyword;
|
||||
@@ -3059,7 +3059,7 @@ pub fn NewParser_(
|
||||
const ref = try p.newSymbol(kind, name);
|
||||
|
||||
if (member == null) {
|
||||
try p.module_scope.members.put(p.allocator, name, Scope.Member{ .ref = ref, .loc = logger.Loc.Empty });
|
||||
try p.module_scope.members.put(p.allocator, name, Scope.Member{ .ref = ref, .loc = .none });
|
||||
return ref;
|
||||
}
|
||||
|
||||
@@ -3074,10 +3074,10 @@ pub fn NewParser_(
|
||||
fn declareGeneratedSymbol(p: *P, kind: Symbol.Kind, comptime name: string) !Ref {
|
||||
// The bundler runs the renamer, so it is ok to not append a hash
|
||||
if (p.options.bundle) {
|
||||
return try declareSymbolMaybeGenerated(p, kind, logger.Loc.Empty, name, true);
|
||||
return try declareSymbolMaybeGenerated(p, kind, .none, name, true);
|
||||
}
|
||||
|
||||
return try declareSymbolMaybeGenerated(p, kind, logger.Loc.Empty, generatedSymbolName(name), true);
|
||||
return try declareSymbolMaybeGenerated(p, kind, .none, generatedSymbolName(name), true);
|
||||
}
|
||||
|
||||
pub fn declareSymbol(p: *P, kind: Symbol.Kind, loc: logger.Loc, name: string) !Ref {
|
||||
@@ -3324,7 +3324,7 @@ pub fn NewParser_(
|
||||
// panic during visit pass leaves the lexer at the end, which
|
||||
// would make this location absolutely useless.
|
||||
const location = loc orelse p.lexer.loc();
|
||||
if (location.start < p.lexer.source.contents.len and !location.isEmpty()) {
|
||||
if (location.get() < p.lexer.source.contents.len and location != .none) {
|
||||
p.log.addRangeErrorFmt(
|
||||
p.source,
|
||||
.{ .loc = location },
|
||||
@@ -3691,14 +3691,14 @@ pub fn NewParser_(
|
||||
bun.assert(p.options.features.allow_runtime);
|
||||
|
||||
p.ensureRequireSymbol();
|
||||
p.recordUsage(p.runtimeIdentifierRef(logger.Loc.Empty, "__require"));
|
||||
p.recordUsage(p.runtimeIdentifierRef(.none, "__require"));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ignoreUsageOfRuntimeRequire(p: *P) void {
|
||||
if (p.options.features.auto_polyfill_require) {
|
||||
bun.assert(p.runtime_imports.__require != null);
|
||||
p.ignoreUsage(p.runtimeIdentifierRef(logger.Loc.Empty, "__require"));
|
||||
p.ignoreUsage(p.runtimeIdentifierRef(.none, "__require"));
|
||||
p.symbols.items[p.require_ref.innerIndex()].use_count_estimate -|= 1;
|
||||
}
|
||||
}
|
||||
@@ -4740,7 +4740,7 @@ pub fn NewParser_(
|
||||
{
|
||||
// design:type
|
||||
var args = p.allocator.alloc(Expr, 2) catch unreachable;
|
||||
args[0] = p.newExpr(E.String{ .data = "design:type" }, logger.Loc.Empty);
|
||||
args[0] = p.newExpr(E.String{ .data = "design:type" }, .none);
|
||||
args[1] = p.serializeMetadata(prop.ts_metadata) catch unreachable;
|
||||
array.append(p.callRuntime(loc, "__legacyMetadataTS", args)) catch unreachable;
|
||||
}
|
||||
@@ -4749,7 +4749,7 @@ pub fn NewParser_(
|
||||
if (prop.value) |prop_value| {
|
||||
{
|
||||
var args = p.allocator.alloc(Expr, 2) catch unreachable;
|
||||
args[0] = p.newExpr(E.String{ .data = "design:paramtypes" }, logger.Loc.Empty);
|
||||
args[0] = p.newExpr(E.String{ .data = "design:paramtypes" }, .none);
|
||||
|
||||
const method_args = prop_value.data.e_function.func.args;
|
||||
const args_array = p.allocator.alloc(Expr, method_args.len) catch unreachable;
|
||||
@@ -4757,13 +4757,13 @@ pub fn NewParser_(
|
||||
entry.* = p.serializeMetadata(method_arg.ts_metadata) catch unreachable;
|
||||
}
|
||||
|
||||
args[1] = p.newExpr(E.Array{ .items = ExprNodeList.init(args_array) }, logger.Loc.Empty);
|
||||
args[1] = p.newExpr(E.Array{ .items = ExprNodeList.init(args_array) }, .none);
|
||||
|
||||
array.append(p.callRuntime(loc, "__legacyMetadataTS", args)) catch unreachable;
|
||||
}
|
||||
{
|
||||
var args = p.allocator.alloc(Expr, 2) catch unreachable;
|
||||
args[0] = p.newExpr(E.String{ .data = "design:returntype" }, logger.Loc.Empty);
|
||||
args[0] = p.newExpr(E.String{ .data = "design:returntype" }, .none);
|
||||
args[1] = p.serializeMetadata(prop_value.data.e_function.func.return_ts_metadata) catch unreachable;
|
||||
array.append(p.callRuntime(loc, "__legacyMetadataTS", args)) catch unreachable;
|
||||
}
|
||||
@@ -4775,14 +4775,14 @@ pub fn NewParser_(
|
||||
if (prop.value) |prop_value| {
|
||||
{
|
||||
var args = p.allocator.alloc(Expr, 2) catch unreachable;
|
||||
args[0] = p.newExpr(E.String{ .data = "design:type" }, logger.Loc.Empty);
|
||||
args[0] = p.newExpr(E.String{ .data = "design:type" }, .none);
|
||||
args[1] = p.serializeMetadata(prop_value.data.e_function.func.return_ts_metadata) catch unreachable;
|
||||
array.append(p.callRuntime(loc, "__legacyMetadataTS", args)) catch unreachable;
|
||||
}
|
||||
{
|
||||
var args = p.allocator.alloc(Expr, 2) catch unreachable;
|
||||
args[0] = p.newExpr(E.String{ .data = "design:paramtypes" }, logger.Loc.Empty);
|
||||
args[1] = p.newExpr(E.Array{ .items = ExprNodeList.init(&[_]Expr{}) }, logger.Loc.Empty);
|
||||
args[0] = p.newExpr(E.String{ .data = "design:paramtypes" }, .none);
|
||||
args[1] = p.newExpr(E.Array{ .items = ExprNodeList.init(&[_]Expr{}) }, .none);
|
||||
array.append(p.callRuntime(loc, "__legacyMetadataTS", args)) catch unreachable;
|
||||
}
|
||||
}
|
||||
@@ -4795,20 +4795,20 @@ pub fn NewParser_(
|
||||
const method_args = prop_value.data.e_function.func.args;
|
||||
{
|
||||
var args = p.allocator.alloc(Expr, 2) catch unreachable;
|
||||
args[0] = p.newExpr(E.String{ .data = "design:paramtypes" }, logger.Loc.Empty);
|
||||
args[0] = p.newExpr(E.String{ .data = "design:paramtypes" }, .none);
|
||||
|
||||
const args_array = p.allocator.alloc(Expr, method_args.len) catch unreachable;
|
||||
for (args_array, method_args) |*entry, method_arg| {
|
||||
entry.* = p.serializeMetadata(method_arg.ts_metadata) catch unreachable;
|
||||
}
|
||||
|
||||
args[1] = p.newExpr(E.Array{ .items = ExprNodeList.init(args_array) }, logger.Loc.Empty);
|
||||
args[1] = p.newExpr(E.Array{ .items = ExprNodeList.init(args_array) }, .none);
|
||||
|
||||
array.append(p.callRuntime(loc, "__legacyMetadataTS", args)) catch unreachable;
|
||||
}
|
||||
if (method_args.len >= 1) {
|
||||
var args = p.allocator.alloc(Expr, 2) catch unreachable;
|
||||
args[0] = p.newExpr(E.String{ .data = "design:type" }, logger.Loc.Empty);
|
||||
args[0] = p.newExpr(E.String{ .data = "design:type" }, .none);
|
||||
args[1] = p.serializeMetadata(method_args[0].ts_metadata) catch unreachable;
|
||||
array.append(p.callRuntime(loc, "__legacyMetadataTS", args)) catch unreachable;
|
||||
}
|
||||
@@ -4897,7 +4897,7 @@ pub fn NewParser_(
|
||||
.key = p.newExpr(E.String{ .data = "constructor" }, stmt.loc),
|
||||
.value = p.newExpr(E.Function{ .func = G.Fn{
|
||||
.name = null,
|
||||
.open_parens_loc = logger.Loc.Empty,
|
||||
.open_parens_loc = .none,
|
||||
.args = &[_]Arg{},
|
||||
.body = .{ .loc = stmt.loc, .stmts = constructor_stmts.items },
|
||||
.flags = Flags.Function.init(.{}),
|
||||
@@ -4939,7 +4939,7 @@ pub fn NewParser_(
|
||||
if (constructor_function != null) {
|
||||
// design:paramtypes
|
||||
var args = p.allocator.alloc(Expr, 2) catch unreachable;
|
||||
args[0] = p.newExpr(E.String{ .data = "design:paramtypes" }, logger.Loc.Empty);
|
||||
args[0] = p.newExpr(E.String{ .data = "design:paramtypes" }, .none);
|
||||
|
||||
const constructor_args = constructor_function.?.func.args;
|
||||
if (constructor_args.len > 0) {
|
||||
@@ -4949,9 +4949,9 @@ pub fn NewParser_(
|
||||
param_array[i] = p.serializeMetadata(constructor_arg.ts_metadata) catch unreachable;
|
||||
}
|
||||
|
||||
args[1] = p.newExpr(E.Array{ .items = ExprNodeList.init(param_array) }, logger.Loc.Empty);
|
||||
args[1] = p.newExpr(E.Array{ .items = ExprNodeList.init(param_array) }, .none);
|
||||
} else {
|
||||
args[1] = p.newExpr(E.Array{ .items = ExprNodeList.init(&[_]Expr{}) }, logger.Loc.Empty);
|
||||
args[1] = p.newExpr(E.Array{ .items = ExprNodeList.init(&[_]Expr{}) }, .none);
|
||||
}
|
||||
|
||||
array.append(p.callRuntime(stmt.loc, "__legacyMetadataTS", args)) catch unreachable;
|
||||
@@ -4988,9 +4988,9 @@ pub fn NewParser_(
|
||||
.m_object,
|
||||
=> p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "Object") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "Object") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
|
||||
.m_never,
|
||||
@@ -4999,63 +4999,63 @@ pub fn NewParser_(
|
||||
.m_void,
|
||||
=> p.newExpr(
|
||||
E.Undefined{},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
|
||||
.m_string => p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "String") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "String") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
.m_number => p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "Number") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "Number") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
.m_function => p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "Function") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "Function") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
.m_boolean => p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "Boolean") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "Boolean") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
.m_array => p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "Array") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "Array") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
|
||||
.m_bigint => p.maybeDefinedHelper(
|
||||
p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "BigInt") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "BigInt") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
),
|
||||
|
||||
.m_symbol => p.maybeDefinedHelper(
|
||||
p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "Symbol") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "Symbol") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
),
|
||||
|
||||
.m_promise => p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "Promise") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "Promise") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
|
||||
.m_identifier => |ref| {
|
||||
@@ -5065,13 +5065,13 @@ pub fn NewParser_(
|
||||
E.ImportIdentifier{
|
||||
.ref = ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
));
|
||||
}
|
||||
|
||||
return p.maybeDefinedHelper(p.newExpr(
|
||||
E.Identifier{ .ref = ref },
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
));
|
||||
},
|
||||
|
||||
@@ -5083,10 +5083,10 @@ pub fn NewParser_(
|
||||
var dots = p.newExpr(
|
||||
E.Dot{
|
||||
.name = p.loadNameFromRef(refs.items[refs.items.len - 1]),
|
||||
.name_loc = logger.Loc.Empty,
|
||||
.name_loc = .none,
|
||||
.target = undefined,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
|
||||
var current_expr = &dots.data.e_dot.target;
|
||||
@@ -5094,9 +5094,9 @@ pub fn NewParser_(
|
||||
while (i > 0) {
|
||||
current_expr.* = p.newExpr(E.Dot{
|
||||
.name = p.loadNameFromRef(refs.items[i]),
|
||||
.name_loc = logger.Loc.Empty,
|
||||
.name_loc = .none,
|
||||
.target = undefined,
|
||||
}, logger.Loc.Empty);
|
||||
}, .none);
|
||||
current_expr = ¤t_expr.data.e_dot.target;
|
||||
i -= 1;
|
||||
}
|
||||
@@ -5106,14 +5106,14 @@ pub fn NewParser_(
|
||||
E.ImportIdentifier{
|
||||
.ref = refs.items[0],
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
} else {
|
||||
current_expr.* = p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = refs.items[0],
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5126,7 +5126,7 @@ pub fn NewParser_(
|
||||
.right = try p.checkIfDefinedHelper(current_dot),
|
||||
.left = undefined,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
|
||||
if (i < refs.items.len - 2) {
|
||||
@@ -5141,7 +5141,7 @@ pub fn NewParser_(
|
||||
.right = try p.checkIfDefinedHelper(current_dot),
|
||||
.left = undefined,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
|
||||
current_expr = ¤t_expr.data.e_binary.left;
|
||||
@@ -5157,14 +5157,14 @@ pub fn NewParser_(
|
||||
E.If{
|
||||
.yes = p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "Object") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "Object") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
.no = dots,
|
||||
.test_ = maybe_defined_dots,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
|
||||
return root;
|
||||
@@ -5538,7 +5538,7 @@ pub fn NewParser_(
|
||||
|
||||
pub fn init(p: *P) !LowerUsingDeclarationsContext {
|
||||
return LowerUsingDeclarationsContext{
|
||||
.first_using_loc = logger.Loc.Empty,
|
||||
.first_using_loc = .none,
|
||||
.stack_ref = p.generateTempRef("__stack"),
|
||||
.has_await_using = false,
|
||||
};
|
||||
@@ -5550,7 +5550,7 @@ pub fn NewParser_(
|
||||
.s_local => |local| {
|
||||
if (!local.kind.isUsing()) continue;
|
||||
|
||||
if (ctx.first_using_loc.isEmpty()) {
|
||||
if (ctx.first_using_loc == .none) {
|
||||
ctx.first_using_loc = stmt.loc;
|
||||
}
|
||||
if (local.kind == .k_await_using) {
|
||||
@@ -5873,7 +5873,7 @@ pub fn NewParser_(
|
||||
bun.assert(p.current_scope == p.module_scope);
|
||||
|
||||
// $RefreshReg$(component, "file.ts:Original Name")
|
||||
const loc = logger.Loc.Empty;
|
||||
const loc = .none;
|
||||
try stmts.append(p.s(S.SExpr{ .value = p.newExpr(E.Call{
|
||||
.target = Expr.initIdentifier(p.react_refresh.register_ref, loc),
|
||||
.args = try ExprNodeList.fromSlice(p.allocator, &.{
|
||||
@@ -5907,7 +5907,7 @@ pub fn NewParser_(
|
||||
p.source.path.pretty
|
||||
else
|
||||
bun.todoPanic(@src(), "TODO: unique_key here", .{}),
|
||||
}, logger.Loc.Empty);
|
||||
}, .none);
|
||||
|
||||
// registerClientReference(
|
||||
// Comp,
|
||||
@@ -5915,13 +5915,13 @@ pub fn NewParser_(
|
||||
// "Comp"
|
||||
// );
|
||||
return p.newExpr(E.Call{
|
||||
.target = Expr.initIdentifier(p.server_components_wrap_ref, logger.Loc.Empty),
|
||||
.target = Expr.initIdentifier(p.server_components_wrap_ref, .none),
|
||||
.args = js_ast.ExprNodeList.fromSlice(p.allocator, &.{
|
||||
val,
|
||||
module_path,
|
||||
p.newExpr(E.String{ .data = original_name }, logger.Loc.Empty),
|
||||
p.newExpr(E.String{ .data = original_name }, .none),
|
||||
}) catch |err| bun.handleOom(err),
|
||||
}, logger.Loc.Empty);
|
||||
}, .none);
|
||||
}
|
||||
|
||||
pub fn handleReactRefreshHookCall(p: *P, hook_call: *E.Call, original_name: []const u8) void {
|
||||
@@ -5979,7 +5979,7 @@ pub fn NewParser_(
|
||||
if (!gop.found_existing) {
|
||||
gop.value_ptr.* = .{
|
||||
.data = @unionInit(Expr.Data, @tagName(tag), id),
|
||||
.loc = .Empty,
|
||||
.loc = .none,
|
||||
};
|
||||
}
|
||||
},
|
||||
@@ -6010,7 +6010,7 @@ pub fn NewParser_(
|
||||
bun.copy(Stmt, stmts.items[1..], stmts.items[0 .. stmts.items.len - 1]);
|
||||
}
|
||||
|
||||
const loc = logger.Loc.Empty;
|
||||
const loc = .none;
|
||||
const prepended_stmt = p.s(S.SExpr{ .value = p.newExpr(E.Call{
|
||||
.target = Expr.initIdentifier(hook.signature_cb, loc),
|
||||
}, loc) }, loc);
|
||||
@@ -6018,7 +6018,7 @@ pub fn NewParser_(
|
||||
}
|
||||
|
||||
pub fn getReactRefreshHookSignalDecl(p: *P, signal_cb_ref: Ref) Stmt {
|
||||
const loc = logger.Loc.Empty;
|
||||
const loc = .none;
|
||||
p.react_refresh.latest_signature_ref = signal_cb_ref;
|
||||
// var s_ = $RefreshSig$();
|
||||
return p.s(S.Local{ .decls = G.Decl.List.fromSlice(p.allocator, &.{.{
|
||||
@@ -6030,7 +6030,7 @@ pub fn NewParser_(
|
||||
}
|
||||
|
||||
pub fn getReactRefreshHookSignalInit(p: *P, ctx: *ReactRefresh.HookContext, function_with_hook_calls: Expr) Expr {
|
||||
const loc = logger.Loc.Empty;
|
||||
const loc = .none;
|
||||
|
||||
const final = ctx.hasher.final();
|
||||
const hash_data = bun.handleOom(p.allocator.alloc(u8, comptime bun.base64.encodeLenFromSize(@sizeOf(@TypeOf(final)))));
|
||||
@@ -6200,15 +6200,15 @@ pub fn NewParser_(
|
||||
// which is then called in `evaluateCommonJSModuleOnce`
|
||||
var args = bun.handleOom(allocator.alloc(Arg, 5 + @as(usize, @intFromBool(p.has_import_meta))));
|
||||
args[0..5].* = .{
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.exports_ref }, logger.Loc.Empty) },
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.require_ref }, logger.Loc.Empty) },
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.module_ref }, logger.Loc.Empty) },
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.filename_ref }, logger.Loc.Empty) },
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.dirname_ref }, logger.Loc.Empty) },
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.exports_ref }, .none) },
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.require_ref }, .none) },
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.module_ref }, .none) },
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.filename_ref }, .none) },
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.dirname_ref }, .none) },
|
||||
};
|
||||
if (p.has_import_meta) {
|
||||
p.import_meta_ref = bun.handleOom(p.newSymbol(.other, "$Bun_import_meta"));
|
||||
args[5] = Arg{ .binding = p.b(B.Identifier{ .ref = p.import_meta_ref }, logger.Loc.Empty) };
|
||||
args[5] = Arg{ .binding = p.b(B.Identifier{ .ref = p.import_meta_ref }, .none) };
|
||||
}
|
||||
|
||||
var total_stmts_count: usize = 0;
|
||||
@@ -6248,13 +6248,13 @@ pub fn NewParser_(
|
||||
E.Function{
|
||||
.func = G.Fn{
|
||||
.name = null,
|
||||
.open_parens_loc = logger.Loc.Empty,
|
||||
.open_parens_loc = .none,
|
||||
.args = args,
|
||||
.body = .{ .loc = logger.Loc.Empty, .stmts = stmts_to_copy },
|
||||
.body = .{ .loc = .none, .stmts = stmts_to_copy },
|
||||
.flags = Flags.Function.init(.{ .is_export = false }),
|
||||
},
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
|
||||
var top_level_stmts = bun.handleOom(p.allocator.alloc(Stmt, 1));
|
||||
@@ -6262,7 +6262,7 @@ pub fn NewParser_(
|
||||
S.SExpr{
|
||||
.value = wrapper,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
|
||||
try parts.ensureUnusedCapacity(1);
|
||||
|
||||
@@ -155,14 +155,14 @@ pub const Parser = struct {
|
||||
if (self.options.jsx.parse and p.needs_jsx_import) {
|
||||
_ = p.addImportRecord(
|
||||
.require,
|
||||
logger.Loc{ .start = 0 },
|
||||
.from(0),
|
||||
p.options.jsx.importSource(),
|
||||
);
|
||||
// Ensure we have both classic and automatic
|
||||
// This is to handle cases where they use fragments in the automatic runtime
|
||||
_ = p.addImportRecord(
|
||||
.require,
|
||||
logger.Loc{ .start = 0 },
|
||||
.from(0),
|
||||
p.options.jsx.classic_import_source,
|
||||
);
|
||||
}
|
||||
@@ -454,7 +454,7 @@ pub const Parser = struct {
|
||||
var debugger_stmts = try p.allocator.alloc(Stmt, 1);
|
||||
debugger_stmts[0] = Stmt{
|
||||
.data = .{ .s_debugger = .{} },
|
||||
.loc = logger.Loc.Empty,
|
||||
.loc = .none,
|
||||
};
|
||||
before.append(
|
||||
js_ast.Part{
|
||||
@@ -660,24 +660,24 @@ pub const Parser = struct {
|
||||
var decls = p.allocator.alloc(G.Decl, count) catch unreachable;
|
||||
if (uses_dirname) {
|
||||
decls[0] = .{
|
||||
.binding = p.b(B.Identifier{ .ref = p.dirname_ref }, logger.Loc.Empty),
|
||||
.binding = p.b(B.Identifier{ .ref = p.dirname_ref }, .none),
|
||||
.value = p.newExpr(
|
||||
E.String{
|
||||
.data = p.source.path.name.dir,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
};
|
||||
declared_symbols.appendAssumeCapacity(.{ .ref = p.dirname_ref, .is_top_level = true });
|
||||
}
|
||||
if (uses_filename) {
|
||||
decls[@as(usize, @intFromBool(uses_dirname))] = .{
|
||||
.binding = p.b(B.Identifier{ .ref = p.filename_ref }, logger.Loc.Empty),
|
||||
.binding = p.b(B.Identifier{ .ref = p.filename_ref }, .none),
|
||||
.value = p.newExpr(
|
||||
E.String{
|
||||
.data = p.source.path.text,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
};
|
||||
declared_symbols.appendAssumeCapacity(.{ .ref = p.filename_ref, .is_top_level = true });
|
||||
@@ -687,7 +687,7 @@ pub const Parser = struct {
|
||||
part_stmts[0] = p.s(S.Local{
|
||||
.kind = .k_var,
|
||||
.decls = Decl.List.init(decls),
|
||||
}, logger.Loc.Empty);
|
||||
}, .none);
|
||||
before.append(js_ast.Part{
|
||||
.stmts = part_stmts,
|
||||
.declared_symbols = declared_symbols,
|
||||
@@ -1134,14 +1134,14 @@ pub const Parser = struct {
|
||||
if (uses_dirname) {
|
||||
// var __dirname = import.meta
|
||||
decls[0] = .{
|
||||
.binding = p.b(B.Identifier{ .ref = p.dirname_ref }, logger.Loc.Empty),
|
||||
.binding = p.b(B.Identifier{ .ref = p.dirname_ref }, .none),
|
||||
.value = p.newExpr(
|
||||
E.Dot{
|
||||
.name = "dir",
|
||||
.name_loc = logger.Loc.Empty,
|
||||
.target = p.newExpr(E.ImportMeta{}, logger.Loc.Empty),
|
||||
.name_loc = .none,
|
||||
.target = p.newExpr(E.ImportMeta{}, .none),
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
};
|
||||
declared_symbols.appendAssumeCapacity(.{ .ref = p.dirname_ref, .is_top_level = true });
|
||||
@@ -1149,14 +1149,14 @@ pub const Parser = struct {
|
||||
if (uses_filename) {
|
||||
// var __filename = import.meta.path
|
||||
decls[@as(usize, @intFromBool(uses_dirname))] = .{
|
||||
.binding = p.b(B.Identifier{ .ref = p.filename_ref }, logger.Loc.Empty),
|
||||
.binding = p.b(B.Identifier{ .ref = p.filename_ref }, .none),
|
||||
.value = p.newExpr(
|
||||
E.Dot{
|
||||
.name = "path",
|
||||
.name_loc = logger.Loc.Empty,
|
||||
.target = p.newExpr(E.ImportMeta{}, logger.Loc.Empty),
|
||||
.name_loc = .none,
|
||||
.target = p.newExpr(E.ImportMeta{}, .none),
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
};
|
||||
declared_symbols.appendAssumeCapacity(.{ .ref = p.filename_ref, .is_top_level = true });
|
||||
@@ -1166,7 +1166,7 @@ pub const Parser = struct {
|
||||
part_stmts[0] = p.s(S.Local{
|
||||
.kind = .k_var,
|
||||
.decls = Decl.List.init(decls),
|
||||
}, logger.Loc.Empty);
|
||||
}, .none);
|
||||
before.append(js_ast.Part{
|
||||
.stmts = part_stmts,
|
||||
.declared_symbols = declared_symbols,
|
||||
@@ -1208,7 +1208,7 @@ pub const Parser = struct {
|
||||
if (items_count == 0)
|
||||
break :outer;
|
||||
|
||||
const import_record_id = p.addImportRecord(.stmt, logger.Loc.Empty, "bun:test");
|
||||
const import_record_id = p.addImportRecord(.stmt, .none, "bun:test");
|
||||
var import_record: *ImportRecord = &p.import_records.items[import_record_id];
|
||||
import_record.tag = .bun_test;
|
||||
|
||||
@@ -1219,9 +1219,9 @@ pub const Parser = struct {
|
||||
inline for (comptime std.meta.fieldNames(Jest)) |symbol_name| {
|
||||
if (p.symbols.items[@field(jest, symbol_name).innerIndex()].use_count_estimate > 0) {
|
||||
clauses[clause_i] = js_ast.ClauseItem{
|
||||
.name = .{ .ref = @field(jest, symbol_name), .loc = logger.Loc.Empty },
|
||||
.name = .{ .ref = @field(jest, symbol_name), .loc = .none },
|
||||
.alias = symbol_name,
|
||||
.alias_loc = logger.Loc.Empty,
|
||||
.alias_loc = .none,
|
||||
.original_name = "",
|
||||
};
|
||||
declared_symbols.appendAssumeCapacity(.{ .ref = @field(jest, symbol_name), .is_top_level = true });
|
||||
@@ -1231,11 +1231,11 @@ pub const Parser = struct {
|
||||
|
||||
const import_stmt = p.s(
|
||||
S.Import{
|
||||
.namespace_ref = p.declareSymbol(.unbound, logger.Loc.Empty, "bun_test_import_namespace_for_internal_use_only") catch unreachable,
|
||||
.namespace_ref = p.declareSymbol(.unbound, .none, "bun_test_import_namespace_for_internal_use_only") catch unreachable,
|
||||
.items = clauses,
|
||||
.import_record_index = import_record_id,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
|
||||
var part_stmts = try p.allocator.alloc(Stmt, 1);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pub const Block = struct {
|
||||
stmts: StmtNodeList,
|
||||
close_brace_loc: logger.Loc = logger.Loc.Empty,
|
||||
close_brace_loc: logger.Loc = .none,
|
||||
};
|
||||
|
||||
pub const SExpr = struct {
|
||||
@@ -123,7 +123,7 @@ pub const While = struct {
|
||||
pub const With = struct {
|
||||
value: ExprNodeIndex,
|
||||
body: StmtNodeIndex,
|
||||
body_loc: logger.Loc = logger.Loc.Empty,
|
||||
body_loc: logger.Loc = .none,
|
||||
};
|
||||
|
||||
pub const Try = struct {
|
||||
|
||||
@@ -74,7 +74,7 @@ pub const Member = struct {
|
||||
loc: logger.Loc,
|
||||
|
||||
pub fn eql(a: Member, b: Member) bool {
|
||||
return @call(bun.callmod_inline, Ref.eql, .{ a.ref, b.ref }) and a.loc.start == b.loc.start;
|
||||
return @call(bun.callmod_inline, Ref.eql, .{ a.ref, b.ref }) and a.loc == b.loc;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ pub fn isMissingExpr(self: Stmt) bool {
|
||||
}
|
||||
|
||||
pub fn empty() Stmt {
|
||||
return Stmt{ .data = .{ .s_empty = None }, .loc = logger.Loc{} };
|
||||
return Stmt{ .data = .{ .s_empty = None }, .loc = .none };
|
||||
}
|
||||
|
||||
pub fn toEmpty(this: Stmt) Stmt {
|
||||
|
||||
@@ -22,7 +22,7 @@ pub fn AstMaybe(
|
||||
return .{ .ok = false };
|
||||
}
|
||||
|
||||
var value: Expr = Expr{ .loc = logger.Loc.Empty, .data = Expr.Data{ .e_missing = E.Missing{} } };
|
||||
var value: Expr = Expr{ .loc = .none, .data = Expr.Data{ .e_missing = E.Missing{} } };
|
||||
|
||||
for (decls) |decl| {
|
||||
const binding = Binding.toExpr(
|
||||
@@ -651,14 +651,14 @@ pub fn AstMaybe(
|
||||
.op = .un_typeof,
|
||||
.value = expr,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
.right = p.newExpr(
|
||||
E.String{ .data = "undefined" },
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -668,19 +668,19 @@ pub fn AstMaybe(
|
||||
.test_ = try p.checkIfDefinedHelper(identifier_expr),
|
||||
.yes = p.newExpr(
|
||||
E.Identifier{
|
||||
.ref = (p.findSymbol(logger.Loc.Empty, "Object") catch unreachable).ref,
|
||||
.ref = (p.findSymbol(.none, "Object") catch unreachable).ref,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
),
|
||||
.no = identifier_expr,
|
||||
},
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn maybeCommaSpreadError(p: *P, _comma_after_spread: ?logger.Loc) void {
|
||||
const comma_after_spread = _comma_after_spread orelse return;
|
||||
if (comma_after_spread.start == -1) return;
|
||||
if (comma_after_spread == .none) return;
|
||||
|
||||
p.log.addRangeError(p.source, logger.Range{ .loc = comma_after_spread, .len = 1 }, "Unexpected \",\" after rest pattern") catch unreachable;
|
||||
}
|
||||
|
||||
@@ -290,7 +290,7 @@ pub fn Parse(
|
||||
// Use NextInsideJSXElement() not Next() so we can parse a JSX-style string literal
|
||||
try p.lexer.nextInsideJSXElement();
|
||||
if (p.lexer.token == .t_string_literal) {
|
||||
previous_string_with_backslash_loc.start = @max(p.lexer.loc().start, p.lexer.previous_backslash_quote_in_jsx.loc.start);
|
||||
previous_string_with_backslash_loc.* = p.lexer.loc().max(p.lexer.previous_backslash_quote_in_jsx.loc);
|
||||
const expr = p.newExpr(try p.lexer.toEString(), previous_string_with_backslash_loc.*);
|
||||
|
||||
try p.lexer.nextInsideJSXElement();
|
||||
@@ -362,7 +362,7 @@ pub fn Parse(
|
||||
}
|
||||
|
||||
// There may be a "=" after the type (but not after an "as" cast)
|
||||
if (is_typescript_enabled and p.lexer.token == .t_equals and !p.forbid_suffix_after_as_loc.eql(p.lexer.loc())) {
|
||||
if (is_typescript_enabled and p.lexer.token == .t_equals and p.forbid_suffix_after_as_loc != p.lexer.loc()) {
|
||||
try p.lexer.next();
|
||||
item.* = Expr.assign(item.*, try p.parseExpr(.comma));
|
||||
}
|
||||
@@ -886,7 +886,7 @@ pub fn Parse(
|
||||
}
|
||||
|
||||
pub fn parsePropertyBinding(p: *P) anyerror!B.Property {
|
||||
var key: js_ast.Expr = Expr{ .loc = logger.Loc.Empty, .data = Prefill.Data.EMissing };
|
||||
var key: js_ast.Expr = Expr{ .loc = .none, .data = Prefill.Data.EMissing };
|
||||
var is_computed = false;
|
||||
|
||||
switch (p.lexer.token) {
|
||||
@@ -1213,7 +1213,7 @@ pub fn Parse(
|
||||
switch (stmt.data) {
|
||||
.s_return => |ret| {
|
||||
if (ret.value == null and !p.latest_return_had_semicolon) {
|
||||
returnWithoutSemicolonStart = stmt.loc.start;
|
||||
returnWithoutSemicolonStart = stmt.loc.get();
|
||||
needsCheck = false;
|
||||
}
|
||||
},
|
||||
@@ -1225,7 +1225,7 @@ pub fn Parse(
|
||||
.s_expr => {
|
||||
try p.log.addWarning(
|
||||
p.source,
|
||||
logger.Loc{ .start = returnWithoutSemicolonStart + 6 },
|
||||
.from(returnWithoutSemicolonStart + 6),
|
||||
"The following expression is not returned because of an automatically-inserted semicolon",
|
||||
);
|
||||
},
|
||||
|
||||
@@ -72,7 +72,7 @@ pub fn ParseFn(
|
||||
|
||||
var func = try p.parseFn(name, FnOrArrowDataParse{
|
||||
.needs_async_loc = loc,
|
||||
.async_range = asyncRange orelse logger.Range.None,
|
||||
.async_range = asyncRange orelse .none,
|
||||
.has_async_range = asyncRange != null,
|
||||
.allow_await = if (is_async) AwaitOrYield.allow_expr else AwaitOrYield.allow_ident,
|
||||
.allow_yield = if (is_generator) AwaitOrYield.allow_expr else AwaitOrYield.allow_ident,
|
||||
@@ -170,7 +170,7 @@ pub fn ParseFn(
|
||||
AwaitOrYield.allow_ident;
|
||||
|
||||
// Don't suggest inserting "async" before anything if "await" is found
|
||||
p.fn_or_arrow_data_parse.needs_async_loc = logger.Loc.Empty;
|
||||
p.fn_or_arrow_data_parse.needs_async_loc = .none;
|
||||
|
||||
// If "super()" is allowed in the body, it's allowed in the arguments
|
||||
p.fn_or_arrow_data_parse.allow_super_call = opts.allow_super_call;
|
||||
|
||||
@@ -240,7 +240,7 @@ pub fn ParseImportExport(
|
||||
var items = ListManaged(js_ast.ClauseItem).initCapacity(p.allocator, 1) catch unreachable;
|
||||
try p.lexer.expect(.t_open_brace);
|
||||
var is_single_line = !p.lexer.has_newline_before;
|
||||
var first_non_identifier_loc = logger.Loc{ .start = 0 };
|
||||
var first_non_identifier_loc: logger.Loc = .from(0);
|
||||
var had_type_only_exports = false;
|
||||
|
||||
while (p.lexer.token != .t_close_brace) {
|
||||
@@ -263,7 +263,7 @@ pub fn ParseImportExport(
|
||||
// // This is a syntax error
|
||||
// export { default }
|
||||
//
|
||||
if (p.lexer.token != .t_identifier and first_non_identifier_loc.start == 0) {
|
||||
if (p.lexer.token != .t_identifier and first_non_identifier_loc.get() == 0) {
|
||||
first_non_identifier_loc = p.lexer.loc();
|
||||
}
|
||||
try p.lexer.next();
|
||||
@@ -321,7 +321,7 @@ pub fn ParseImportExport(
|
||||
// // This is a syntax error
|
||||
// export { default }
|
||||
//
|
||||
if (p.lexer.token != .t_identifier and first_non_identifier_loc.start == 0) {
|
||||
if (p.lexer.token != .t_identifier and first_non_identifier_loc.get() == 0) {
|
||||
first_non_identifier_loc = p.lexer.loc();
|
||||
}
|
||||
|
||||
@@ -397,7 +397,7 @@ pub fn ParseImportExport(
|
||||
|
||||
// Throw an error here if we found a keyword earlier and this isn't an
|
||||
// "export from" statement after all
|
||||
if (first_non_identifier_loc.start != 0 and !p.lexer.isContextualKeyword("from")) {
|
||||
if (first_non_identifier_loc.get() != 0 and !p.lexer.isContextualKeyword("from")) {
|
||||
const r = js_lexer.rangeOfIdentifier(p.source, first_non_identifier_loc);
|
||||
try p.lexer.addRangeError(r, "Expected identifier but found \"{s}\"", .{p.source.textForRange(r)}, true);
|
||||
return error.SyntaxError;
|
||||
|
||||
@@ -21,7 +21,7 @@ pub fn ParseJSXElement(
|
||||
_ = try p.skipTypeScriptTypeArguments(true);
|
||||
}
|
||||
|
||||
var previous_string_with_backslash_loc = logger.Loc{};
|
||||
var previous_string_with_backslash_loc: logger.Loc = .none;
|
||||
var properties = G.Property.List{};
|
||||
var key_prop_i: i32 = -1;
|
||||
var flags = Flags.JSXElement.Bitset{};
|
||||
@@ -32,7 +32,7 @@ pub fn ParseJSXElement(
|
||||
if (@as(JSXTag.TagType, tag.data) == .tag) {
|
||||
start_tag = tag.data.tag;
|
||||
|
||||
var spread_loc: logger.Loc = logger.Loc.Empty;
|
||||
var spread_loc: logger.Loc = .none;
|
||||
var props = ListManaged(G.Property).init(p.allocator);
|
||||
var first_spread_prop_i: i32 = -1;
|
||||
var i: i32 = 0;
|
||||
@@ -65,7 +65,7 @@ pub fn ParseJSXElement(
|
||||
|
||||
// Implicitly true value
|
||||
// <button selected>
|
||||
value = p.newExpr(E.Boolean{ .value = true }, logger.Loc{ .start = key_range.loc.start + key_range.len });
|
||||
value = p.newExpr(E.Boolean{ .value = true }, key_range.loc.add(key_range.len));
|
||||
} else {
|
||||
value = try p.parseJSXPropValueIdentifier(&previous_string_with_backslash_loc);
|
||||
}
|
||||
@@ -168,7 +168,7 @@ pub fn ParseJSXElement(
|
||||
// There is no "=" after the JSX attribute "text", so we expect a ">"
|
||||
//
|
||||
// This code special-cases this error to provide a less obscure error message.
|
||||
if (p.lexer.token == .t_syntax_error and strings.eqlComptime(p.lexer.raw(), "\\") and previous_string_with_backslash_loc.start > 0) {
|
||||
if (p.lexer.token == .t_syntax_error and strings.eqlComptime(p.lexer.raw(), "\\") and previous_string_with_backslash_loc.get() > 0) {
|
||||
const r = p.lexer.range();
|
||||
// Not dealing with this right now.
|
||||
try p.log.addRangeError(p.source, r, "Invalid JSX escape - use XML entity codes quotes or pass a JavaScript string instead");
|
||||
|
||||
@@ -339,7 +339,7 @@ pub fn ParsePrefix(
|
||||
}
|
||||
fn t_function(noalias p: *P) anyerror!Expr {
|
||||
const loc = p.lexer.loc();
|
||||
return try p.parseFnExpr(loc, false, logger.Range.None);
|
||||
return try p.parseFnExpr(loc, false, .none);
|
||||
}
|
||||
fn t_class(noalias p: *P) anyerror!Expr {
|
||||
const loc = p.lexer.loc();
|
||||
@@ -391,7 +391,7 @@ pub fn ParsePrefix(
|
||||
try p.lexer.unexpected();
|
||||
return error.SyntaxError;
|
||||
}
|
||||
const range = logger.Range{ .loc = loc, .len = p.lexer.range().end().start - loc.start };
|
||||
const range = logger.Range{ .loc = loc, .len = p.lexer.range().end().get() - loc.get() };
|
||||
|
||||
try p.lexer.next();
|
||||
return p.newExpr(E.NewTarget{ .range = range }, loc);
|
||||
@@ -418,7 +418,7 @@ pub fn ParsePrefix(
|
||||
new.data.e_new.args = call_args.list;
|
||||
new.data.e_new.close_parens_loc = call_args.loc;
|
||||
} else {
|
||||
new.data.e_new.close_parens_loc = .Empty;
|
||||
new.data.e_new.close_parens_loc = .none;
|
||||
new.data.e_new.args = .{};
|
||||
}
|
||||
|
||||
@@ -430,7 +430,7 @@ pub fn ParsePrefix(
|
||||
var is_single_line = !p.lexer.has_newline_before;
|
||||
var items = ListManaged(Expr).init(p.allocator);
|
||||
var self_errors = DeferredErrors{};
|
||||
var comma_after_spread = logger.Loc{};
|
||||
var comma_after_spread: logger.Loc = .none;
|
||||
|
||||
// Allow "in" inside arrays
|
||||
const old_allow_in = p.allow_in;
|
||||
@@ -501,7 +501,7 @@ pub fn ParsePrefix(
|
||||
}
|
||||
return p.newExpr(E.Array{
|
||||
.items = ExprNodeList.fromList(items),
|
||||
.comma_after_spread = comma_after_spread.toNullable(),
|
||||
.comma_after_spread = if (comma_after_spread == .none) null else comma_after_spread,
|
||||
.is_single_line = is_single_line,
|
||||
.close_bracket_loc = close_bracket_loc,
|
||||
}, loc);
|
||||
@@ -512,7 +512,7 @@ pub fn ParsePrefix(
|
||||
var is_single_line = !p.lexer.has_newline_before;
|
||||
var properties = ListManaged(G.Property).init(p.allocator);
|
||||
var self_errors = DeferredErrors{};
|
||||
var comma_after_spread: logger.Loc = logger.Loc{};
|
||||
var comma_after_spread: logger.Loc = .none;
|
||||
|
||||
// Allow "in" inside object literals
|
||||
const old_allow_in = p.allow_in;
|
||||
@@ -585,7 +585,7 @@ pub fn ParsePrefix(
|
||||
|
||||
return p.newExpr(E.Object{
|
||||
.properties = G.Property.List.fromList(properties),
|
||||
.comma_after_spread = if (comma_after_spread.start > 0)
|
||||
.comma_after_spread = if (comma_after_spread.get() > 0)
|
||||
comma_after_spread
|
||||
else
|
||||
null,
|
||||
|
||||
@@ -137,7 +137,7 @@ pub fn ParseProperty(
|
||||
var errors = errors_;
|
||||
// This while loop exists to conserve stack space by reducing (but not completely eliminating) recursion.
|
||||
restart: while (true) {
|
||||
var key: Expr = Expr{ .loc = logger.Loc.Empty, .data = .{ .e_missing = E.Missing{} } };
|
||||
var key: Expr = Expr{ .loc = .none, .data = .{ .e_missing = E.Missing{} } };
|
||||
const key_range = p.lexer.range();
|
||||
var is_computed = false;
|
||||
|
||||
@@ -531,7 +531,7 @@ pub fn ParseProperty(
|
||||
.is_computed = is_computed,
|
||||
}),
|
||||
.key = key,
|
||||
.value = Expr{ .data = .e_missing, .loc = .{} },
|
||||
.value = Expr{ .data = .e_missing, .loc = .none },
|
||||
};
|
||||
|
||||
try p.parseExprOrBindings(.comma, errors, &property.value.?);
|
||||
|
||||
@@ -667,7 +667,7 @@ pub fn ParseStmt(
|
||||
},
|
||||
}
|
||||
}
|
||||
try cases.append(js_ast.Case{ .value = value, .body = body.items, .loc = logger.Loc.Empty });
|
||||
try cases.append(js_ast.Case{ .value = value, .body = body.items, .loc = .none });
|
||||
}
|
||||
try p.lexer.expect(.t_close_brace);
|
||||
return p.s(S.Switch{ .test_ = test_, .body_loc = body_loc, .cases = cases.items }, loc);
|
||||
@@ -1039,7 +1039,7 @@ pub fn ParseStmt(
|
||||
// Parse TypeScript import assignment statements
|
||||
if (p.lexer.token == .t_equals or opts.is_export or (opts.is_namespace_scope and !opts.is_typescript_declare)) {
|
||||
p.esm_import_keyword = previous_import_keyword; // This wasn't an ESM import statement after all;
|
||||
return p.parseTypeScriptImportEqualsStmt(loc, opts, logger.Loc.Empty, default_name);
|
||||
return p.parseTypeScriptImportEqualsStmt(loc, opts, .none, default_name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1115,9 +1115,7 @@ pub fn ParseStmt(
|
||||
fn t_throw(p: *P, _: *ParseStatementOptions, loc: logger.Loc) anyerror!Stmt {
|
||||
try p.lexer.next();
|
||||
if (p.lexer.has_newline_before) {
|
||||
try p.log.addError(p.source, logger.Loc{
|
||||
.start = loc.start + 5,
|
||||
}, "Unexpected newline after \"throw\"");
|
||||
try p.log.addError(p.source, loc.add(5), "Unexpected newline after \"throw\"");
|
||||
return error.SyntaxError;
|
||||
}
|
||||
const expr = try p.parseExpr(.lowest);
|
||||
|
||||
@@ -298,7 +298,7 @@ pub fn ParseSuffix(
|
||||
// "(a?) => {}"
|
||||
// "(a?: b) => {}"
|
||||
// "(a?, b?) => {}"
|
||||
if (is_typescript_enabled and left.loc.start == p.latest_arrow_arg_loc.start and (p.lexer.token == .t_colon or
|
||||
if (is_typescript_enabled and left.loc == p.latest_arrow_arg_loc and (p.lexer.token == .t_colon or
|
||||
p.lexer.token == .t_close_paren or p.lexer.token == .t_comma))
|
||||
{
|
||||
if (errors == null) {
|
||||
@@ -826,7 +826,7 @@ pub fn ParseSuffix(
|
||||
var optional_chain_: ?OptionalChain = null;
|
||||
const optional_chain = &optional_chain_;
|
||||
while (true) {
|
||||
if (p.lexer.loc().start == p.after_arrow_body_loc.start) {
|
||||
if (p.lexer.loc() == p.after_arrow_body_loc) {
|
||||
while (true) {
|
||||
switch (p.lexer.token) {
|
||||
.t_comma => {
|
||||
@@ -850,7 +850,7 @@ pub fn ParseSuffix(
|
||||
|
||||
if (comptime is_typescript_enabled) {
|
||||
// Stop now if this token is forbidden to follow a TypeScript "as" cast
|
||||
if (p.forbid_suffix_after_as_loc.start > -1 and p.lexer.loc().start == p.forbid_suffix_after_as_loc.start) {
|
||||
if (p.forbid_suffix_after_as_loc.get() > -1 and p.lexer.loc() == p.forbid_suffix_after_as_loc) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -489,7 +489,7 @@ pub fn SkipTypescript(
|
||||
},
|
||||
else => {
|
||||
if (comptime get_metadata) {
|
||||
const find_result = p.findSymbol(logger.Loc.Empty, p.lexer.identifier) catch unreachable;
|
||||
const find_result = p.findSymbol(.none, p.lexer.identifier) catch unreachable;
|
||||
result.* = .{ .m_identifier = find_result.ref };
|
||||
}
|
||||
|
||||
@@ -686,12 +686,12 @@ pub fn SkipTypescript(
|
||||
if (result.* == .m_identifier) {
|
||||
var dot = List(Ref).initCapacity(p.allocator, 2) catch unreachable;
|
||||
dot.appendAssumeCapacity(result.m_identifier);
|
||||
const find_result = p.findSymbol(logger.Loc.Empty, p.lexer.identifier) catch unreachable;
|
||||
const find_result = p.findSymbol(.none, p.lexer.identifier) catch unreachable;
|
||||
dot.appendAssumeCapacity(find_result.ref);
|
||||
result.* = .{ .m_dot = dot };
|
||||
} else if (result.* == .m_dot) {
|
||||
if (p.lexer.isIdentifierOrKeyword()) {
|
||||
const find_result = p.findSymbol(logger.Loc.Empty, p.lexer.identifier) catch unreachable;
|
||||
const find_result = p.findSymbol(.none, p.lexer.identifier) catch unreachable;
|
||||
result.m_dot.append(p.allocator, find_result.ref) catch unreachable;
|
||||
}
|
||||
}
|
||||
@@ -904,7 +904,7 @@ pub fn SkipTypescript(
|
||||
var has_out = false;
|
||||
var expect_identifier = true;
|
||||
|
||||
var invalid_modifier_range = logger.Range.None;
|
||||
var invalid_modifier_range: logger.Range = .none;
|
||||
|
||||
// Scan over a sequence of "in" and "out" modifiers (a.k.a. optional
|
||||
// variance annotations) as well as "const" modifiers
|
||||
|
||||
@@ -11,7 +11,7 @@ pub fn Symbols(
|
||||
}
|
||||
|
||||
pub fn findSymbolWithRecordUsage(noalias p: *P, loc: logger.Loc, name: string, comptime record_usage: bool) !FindSymbolResult {
|
||||
var declare_loc: logger.Loc = logger.Loc.Empty;
|
||||
var declare_loc: logger.Loc = .none;
|
||||
var is_inside_with_scope = false;
|
||||
// This function can show up in profiling.
|
||||
// That's part of why we do this.
|
||||
|
||||
@@ -257,7 +257,7 @@ pub fn VisitExpr(
|
||||
.target = if (runtime == .classic) target else p.jsxImport(.createElement, expr.loc),
|
||||
.args = ExprNodeList.init(args[0..i]),
|
||||
// Enable tree shaking
|
||||
.can_be_unwrapped_if_unused = if (!p.options.ignore_dce_annotations) .if_unused else .never,
|
||||
.can_be_unwrapped_if_unused = if (!p.options.ignore_dce_annotations and !p.options.jsx.side_effects) .if_unused else .never,
|
||||
.close_paren_loc = e_.close_tag_loc,
|
||||
}, expr.loc);
|
||||
}
|
||||
@@ -362,7 +362,7 @@ pub fn VisitExpr(
|
||||
.target = p.jsxImportAutomatic(expr.loc, is_static_jsx),
|
||||
.args = ExprNodeList.init(args),
|
||||
// Enable tree shaking
|
||||
.can_be_unwrapped_if_unused = if (!p.options.ignore_dce_annotations) .if_unused else .never,
|
||||
.can_be_unwrapped_if_unused = if (!p.options.ignore_dce_annotations and !p.options.jsx.side_effects) .if_unused else .never,
|
||||
.was_jsx_element = true,
|
||||
.close_paren_loc = e_.close_tag_loc,
|
||||
}, expr.loc);
|
||||
|
||||
@@ -137,7 +137,7 @@ pub fn VisitStmt(
|
||||
|
||||
if (p.options.features.replace_exports.count() > 0) {
|
||||
if (p.options.features.replace_exports.getPtr(item.alias)) |entry| {
|
||||
_ = p.injectReplacementExport(stmts, old_ref, logger.Loc.Empty, entry);
|
||||
_ = p.injectReplacementExport(stmts, old_ref, .none, entry);
|
||||
|
||||
continue;
|
||||
}
|
||||
@@ -183,7 +183,7 @@ pub fn VisitStmt(
|
||||
if (data.alias) |alias| {
|
||||
if (p.options.features.replace_exports.count() > 0) {
|
||||
if (p.options.features.replace_exports.getPtr(alias.original_name)) |entry| {
|
||||
_ = p.injectReplacementExport(stmts, p.declareSymbol(.other, logger.Loc.Empty, alias.original_name) catch unreachable, logger.Loc.Empty, entry);
|
||||
_ = p.injectReplacementExport(stmts, p.declareSymbol(.other, .none, alias.original_name) catch unreachable, .none, entry);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -312,7 +312,7 @@ pub fn VisitStmt(
|
||||
if (entry.* == .replace) {
|
||||
data.value.expr = entry.replace;
|
||||
} else {
|
||||
_ = p.injectReplacementExport(stmts, Ref.None, logger.Loc.Empty, entry);
|
||||
_ = p.injectReplacementExport(stmts, Ref.None, .none, entry);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -358,7 +358,7 @@ pub fn VisitStmt(
|
||||
if (entry.* == .replace) {
|
||||
data.value = .{ .expr = entry.replace };
|
||||
} else {
|
||||
_ = p.injectReplacementExport(stmts, Ref.None, logger.Loc.Empty, entry);
|
||||
_ = p.injectReplacementExport(stmts, Ref.None, .none, entry);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -439,7 +439,7 @@ pub fn VisitStmt(
|
||||
if (entry.* == .replace) {
|
||||
data.value = .{ .expr = entry.replace };
|
||||
} else {
|
||||
_ = p.injectReplacementExport(stmts, Ref.None, logger.Loc.Empty, entry);
|
||||
_ = p.injectReplacementExport(stmts, Ref.None, .none, entry);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -559,8 +559,8 @@ pub fn VisitStmt(
|
||||
if (react_hook_data) |*hook| {
|
||||
try stmts.append(p.getReactRefreshHookSignalDecl(hook.signature_cb));
|
||||
try stmts.append(p.s(S.SExpr{
|
||||
.value = p.getReactRefreshHookSignalInit(hook, Expr.initIdentifier(name_ref, logger.Loc.Empty)),
|
||||
}, logger.Loc.Empty));
|
||||
.value = p.getReactRefreshHookSignalInit(hook, Expr.initIdentifier(name_ref, .none)),
|
||||
}, .none));
|
||||
}
|
||||
|
||||
if (p.current_scope == p.module_scope) {
|
||||
@@ -907,13 +907,13 @@ pub fn VisitStmt(
|
||||
pub fn s_return(noalias p: *P, noalias stmts: *ListManaged(Stmt), noalias stmt: *Stmt, noalias data: *S.Return) !void {
|
||||
// Forbid top-level return inside modules with ECMAScript-style exports
|
||||
if (p.fn_or_arrow_data_visit.is_outside_fn_or_arrow) {
|
||||
const where = where: {
|
||||
const where: logger.Range = where: {
|
||||
if (p.esm_export_keyword.len > 0) {
|
||||
break :where p.esm_export_keyword;
|
||||
} else if (p.top_level_await_keyword.len > 0) {
|
||||
break :where p.top_level_await_keyword;
|
||||
} else {
|
||||
break :where logger.Range.None;
|
||||
break :where .none;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -39,7 +39,8 @@ magic: if (Environment.isDebug)
|
||||
enum(u128) { valid = 0x1ffd363f121f5c12 }
|
||||
else
|
||||
enum { valid } = .valid,
|
||||
allocation_scope: if (AllocationScope.enabled) AllocationScope else void,
|
||||
/// No overhead in release builds.
|
||||
allocation_scope: AllocationScope,
|
||||
/// Absolute path to project root directory. For the HMR
|
||||
/// runtime, its module IDs are strings relative to this.
|
||||
root: []const u8,
|
||||
@@ -267,8 +268,7 @@ pub fn init(options: Options) bun.JSOOM!*DevServer {
|
||||
const separate_ssr_graph = if (options.framework.server_components) |sc| sc.separate_ssr_graph else false;
|
||||
|
||||
const dev = bun.new(DevServer, .{
|
||||
.allocation_scope = if (comptime AllocationScope.enabled)
|
||||
AllocationScope.init(bun.default_allocator),
|
||||
.allocation_scope = .initDefault(),
|
||||
.root = options.root,
|
||||
.vm = options.vm,
|
||||
.server = null,
|
||||
@@ -679,15 +679,17 @@ pub fn deinit(dev: *DevServer) void {
|
||||
bun.destroy(dev);
|
||||
}
|
||||
|
||||
const AllocationScope = bun.allocators.AllocationScopeIn(bun.DefaultAllocator);
|
||||
pub const DevAllocator = AllocationScope.Borrowed;
|
||||
|
||||
pub fn allocator(dev: *const DevServer) Allocator {
|
||||
return dev.dev_allocator().get();
|
||||
return dev.allocation_scope.allocator();
|
||||
}
|
||||
|
||||
pub fn dev_allocator(dev: *const DevServer) DevAllocator {
|
||||
return .{ .maybe_scope = dev.allocation_scope };
|
||||
return dev.allocation_scope.borrow();
|
||||
}
|
||||
|
||||
pub const DevAllocator = @import("./DevServer/DevAllocator.zig");
|
||||
pub const MemoryCost = @import("./DevServer/memory_cost.zig");
|
||||
pub const memoryCost = MemoryCost.memoryCost;
|
||||
pub const memoryCostDetailed = MemoryCost.memoryCostDetailed;
|
||||
@@ -3001,10 +3003,11 @@ fn printMemoryLine(dev: *DevServer) void {
|
||||
return;
|
||||
}
|
||||
if (!debug.isVisible()) return;
|
||||
const stats = dev.allocation_scope.stats();
|
||||
Output.prettyErrorln("<d>DevServer tracked {}, measured: {} ({}), process: {}<r>", .{
|
||||
bun.fmt.size(dev.memoryCost(), .{}),
|
||||
dev.allocation_scope.numAllocations(),
|
||||
bun.fmt.size(dev.allocation_scope.total(), .{}),
|
||||
stats.num_allocations,
|
||||
bun.fmt.size(stats.total_memory_allocated, .{}),
|
||||
bun.fmt.size(bun.sys.selfProcessMemoryUsage() orelse 0, .{}),
|
||||
});
|
||||
}
|
||||
@@ -3296,7 +3299,7 @@ pub fn writeMemoryVisualizerMessage(dev: *DevServer, payload: *std.ArrayList(u8)
|
||||
.assets = @truncate(cost.assets),
|
||||
.other = @truncate(cost.other),
|
||||
.devserver_tracked = if (comptime AllocationScope.enabled)
|
||||
@truncate(dev.allocation_scope.total())
|
||||
@truncate(dev.allocation_scope.stats().total_memory_allocated)
|
||||
else
|
||||
0,
|
||||
.process_used = @truncate(bun.sys.selfProcessMemoryUsage() orelse 0),
|
||||
@@ -4068,7 +4071,6 @@ pub fn getDeinitCountForTesting() usize {
|
||||
}
|
||||
|
||||
const bun = @import("bun");
|
||||
const AllocationScope = bun.AllocationScope;
|
||||
const Environment = bun.Environment;
|
||||
const Output = bun.Output;
|
||||
const SourceMap = bun.sourcemap;
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
const Self = @This();
|
||||
|
||||
maybe_scope: if (AllocationScope.enabled) AllocationScope else void,
|
||||
|
||||
pub fn get(self: Self) Allocator {
|
||||
return if (comptime AllocationScope.enabled)
|
||||
self.maybe_scope.allocator()
|
||||
else
|
||||
bun.default_allocator;
|
||||
}
|
||||
|
||||
pub fn scope(self: Self) ?AllocationScope {
|
||||
return if (comptime AllocationScope.enabled) self.maybe_scope else null;
|
||||
}
|
||||
|
||||
const bun = @import("bun");
|
||||
const std = @import("std");
|
||||
const AllocationScope = bun.allocators.AllocationScope;
|
||||
const Allocator = std.mem.Allocator;
|
||||
@@ -347,7 +347,7 @@ fn extractJsonEncodedSourceCode(contents: []const u8, target_line: u32, comptime
|
||||
.source = .initEmptyFile(""),
|
||||
.allocator = arena,
|
||||
.should_redact_logs = false,
|
||||
.prev_error_loc = .Empty,
|
||||
.prev_error_loc = .none,
|
||||
};
|
||||
defer log.deinit();
|
||||
|
||||
|
||||
@@ -314,7 +314,8 @@ pub fn IncrementalGraph(comptime side: bake.Side) type {
|
||||
bun.assertf(side == .client, "freeFileContent requires client graph", .{});
|
||||
}
|
||||
if (file.source_map.take()) |ptr| {
|
||||
ptr.deinit();
|
||||
var ptr_mut = ptr;
|
||||
ptr_mut.deinit();
|
||||
}
|
||||
defer file.content = .unknown;
|
||||
switch (file.content) {
|
||||
@@ -444,7 +445,7 @@ pub fn IncrementalGraph(comptime side: bake.Side) type {
|
||||
g: *Self,
|
||||
ctx: *HotUpdateContext,
|
||||
index: bun.ast.Index,
|
||||
content: union(enum) {
|
||||
content_: union(enum) {
|
||||
js: struct {
|
||||
code: JsCode,
|
||||
source_map: ?struct {
|
||||
@@ -456,6 +457,7 @@ pub fn IncrementalGraph(comptime side: bake.Side) type {
|
||||
},
|
||||
is_ssr_graph: bool,
|
||||
) !void {
|
||||
var content = content_;
|
||||
const dev = g.owner();
|
||||
dev.graph_safety_lock.assertLocked();
|
||||
|
||||
@@ -538,20 +540,18 @@ pub fn IncrementalGraph(comptime side: bake.Side) type {
|
||||
},
|
||||
.source_map = switch (content) {
|
||||
.css => .none,
|
||||
.js => |js| blk: {
|
||||
.js => |*js| blk: {
|
||||
// Insert new source map or patch existing empty source map.
|
||||
if (js.source_map) |source_map| {
|
||||
if (js.source_map) |*source_map| {
|
||||
bun.assert(html_route_bundle_index == null); // suspect behind #17956
|
||||
var chunk = source_map.chunk;
|
||||
var escaped_source = source_map.escaped_source;
|
||||
if (chunk.buffer.len() > 0) {
|
||||
if (source_map.chunk.buffer.len() > 0) {
|
||||
break :blk .{ .some = PackedMap.newNonEmpty(
|
||||
chunk,
|
||||
escaped_source.take().?,
|
||||
source_map.chunk,
|
||||
source_map.escaped_source.take().?,
|
||||
) };
|
||||
}
|
||||
chunk.buffer.deinit();
|
||||
escaped_source.deinit();
|
||||
source_map.chunk.buffer.deinit();
|
||||
source_map.escaped_source.deinit();
|
||||
}
|
||||
|
||||
// Must precompute this. Otherwise, source maps won't have
|
||||
@@ -634,9 +634,8 @@ pub fn IncrementalGraph(comptime side: bake.Side) type {
|
||||
if (content == .js) {
|
||||
try g.current_chunk_parts.append(dev.allocator(), content.js.code);
|
||||
g.current_chunk_len += content.js.code.len;
|
||||
if (content.js.source_map) |source_map| {
|
||||
var buffer = source_map.chunk.buffer;
|
||||
buffer.deinit();
|
||||
if (content.js.source_map) |*source_map| {
|
||||
source_map.chunk.buffer.deinit();
|
||||
source_map.escaped_source.deinit();
|
||||
}
|
||||
}
|
||||
@@ -1496,9 +1495,8 @@ pub fn IncrementalGraph(comptime side: bake.Side) type {
|
||||
|
||||
// Additionally, clear the cached entry of the file from the path to
|
||||
// source index map.
|
||||
const hash = bun.hash(abs_path);
|
||||
for (&bv2.graph.build_graphs.values) |*map| {
|
||||
_ = map.remove(hash);
|
||||
_ = map.remove(abs_path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1912,12 +1910,13 @@ pub fn IncrementalGraph(comptime side: bake.Side) type {
|
||||
return @alignCast(@fieldParentPtr(@tagName(side) ++ "_graph", g));
|
||||
}
|
||||
|
||||
fn dev_allocator(g: *Self) DevAllocator {
|
||||
return g.owner().dev_allocator();
|
||||
fn allocator(g: *const Self) Allocator {
|
||||
return g.dev_allocator().allocator();
|
||||
}
|
||||
|
||||
fn allocator(g: *Self) Allocator {
|
||||
return g.dev_allocator().get();
|
||||
fn dev_allocator(g: *const Self) DevAllocator {
|
||||
const dev_server: *const DevServer = @constCast(g).owner();
|
||||
return dev_server.dev_allocator();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ const Self = @This();
|
||||
|
||||
/// Allocated by `dev.allocator()`. Access with `.vlq()`
|
||||
/// This is stored to allow lazy construction of source map files.
|
||||
vlq_: ScopedOwned([]u8),
|
||||
vlq_: OwnedIn([]u8, DevAllocator),
|
||||
/// The bundler runs quoting on multiple threads, so it only makes
|
||||
/// sense to preserve that effort for concatenation and
|
||||
/// re-concatenation.
|
||||
@@ -22,8 +22,9 @@ end_state: struct {
|
||||
pub fn newNonEmpty(chunk: SourceMap.Chunk, escaped_source: Owned([]u8)) bun.ptr.Shared(*Self) {
|
||||
var buffer = chunk.buffer;
|
||||
assert(!buffer.isEmpty());
|
||||
const dev_allocator = DevAllocator.downcast(buffer.allocator);
|
||||
return .new(.{
|
||||
.vlq_ = .fromDynamic(buffer.toDynamicOwned()),
|
||||
.vlq_ = .fromRawIn(buffer.toOwnedSlice(), dev_allocator),
|
||||
.escaped_source = escaped_source,
|
||||
.end_state = .{
|
||||
.original_line = chunk.end_state.original_line,
|
||||
@@ -42,12 +43,12 @@ pub fn memoryCost(self: *const Self) usize {
|
||||
}
|
||||
|
||||
pub fn vlq(self: *const Self) []const u8 {
|
||||
return self.vlq_.getConst();
|
||||
return self.vlq_.get();
|
||||
}
|
||||
|
||||
// TODO: rename to `escapedSource`
|
||||
pub fn quotedContents(self: *const Self) []const u8 {
|
||||
return self.escaped_source.getConst();
|
||||
return self.escaped_source.get();
|
||||
}
|
||||
|
||||
comptime {
|
||||
@@ -94,9 +95,10 @@ pub const Shared = union(enum) {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: Shared) void {
|
||||
switch (self) {
|
||||
.some => |ptr| ptr.deinit(),
|
||||
pub fn deinit(self: *Shared) void {
|
||||
defer self.* = undefined;
|
||||
switch (self.*) {
|
||||
.some => |*ptr| ptr.deinit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
@@ -116,6 +118,7 @@ const SourceMap = bun.sourcemap;
|
||||
const assert = bun.assert;
|
||||
const assert_eql = bun.assert_eql;
|
||||
const Chunk = bun.bundle_v2.Chunk;
|
||||
const DevAllocator = bun.bake.DevServer.DevAllocator;
|
||||
|
||||
const Owned = bun.ptr.Owned;
|
||||
const ScopedOwned = bun.ptr.ScopedOwned;
|
||||
const OwnedIn = bun.ptr.OwnedIn;
|
||||
|
||||
@@ -261,7 +261,8 @@ pub const Entry = struct {
|
||||
.files = {
|
||||
const files = entry.files.slice();
|
||||
for (0..files.len) |i| {
|
||||
files.get(i).deinit();
|
||||
var file = files.get(i);
|
||||
file.deinit();
|
||||
}
|
||||
entry.files.deinit(entry.allocator());
|
||||
},
|
||||
@@ -270,7 +271,7 @@ pub const Entry = struct {
|
||||
}
|
||||
|
||||
fn allocator(entry: *const Entry) Allocator {
|
||||
return entry.dev_allocator.get();
|
||||
return entry.dev_allocator.allocator();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -305,12 +306,13 @@ pub fn owner(store: *Self) *DevServer {
|
||||
return @alignCast(@fieldParentPtr("source_maps", store));
|
||||
}
|
||||
|
||||
fn dev_allocator(store: *Self) DevAllocator {
|
||||
return store.owner().dev_allocator();
|
||||
fn allocator(store: *Self) Allocator {
|
||||
return store.dev_allocator().allocator();
|
||||
}
|
||||
|
||||
fn allocator(store: *Self) Allocator {
|
||||
return store.dev_allocator().get();
|
||||
fn dev_allocator(store: *const Self) DevAllocator {
|
||||
const dev_server: *const DevServer = @constCast(store).owner();
|
||||
return dev_server.dev_allocator();
|
||||
}
|
||||
|
||||
const PutOrIncrementRefCount = union(enum) {
|
||||
|
||||
@@ -158,9 +158,11 @@ pub const BrotliReaderArrayList = struct {
|
||||
}
|
||||
this.state = .Inflating;
|
||||
if (is_done) {
|
||||
// Stream is truncated - we're at EOF but decoder needs more data
|
||||
this.state = .Error;
|
||||
return error.BrotliDecompressionError;
|
||||
}
|
||||
|
||||
// Not at EOF - we can retry with more data
|
||||
return error.ShortRead;
|
||||
},
|
||||
.needs_more_output => {
|
||||
|
||||
@@ -36,7 +36,6 @@ pub const Run = struct {
|
||||
.args = ctx.args,
|
||||
.graph = graph_ptr,
|
||||
.is_main_thread = true,
|
||||
.destruct_main_thread_on_exit = bun.getRuntimeFeatureFlag(.BUN_DESTRUCT_VM_ON_EXIT),
|
||||
}),
|
||||
.arena = arena,
|
||||
.ctx = ctx,
|
||||
@@ -174,7 +173,6 @@ pub const Run = struct {
|
||||
.debugger = ctx.runtime_options.debugger,
|
||||
.dns_result_order = DNSResolver.Order.fromStringOrDie(ctx.runtime_options.dns_result_order),
|
||||
.is_main_thread = true,
|
||||
.destruct_main_thread_on_exit = bun.getRuntimeFeatureFlag(.BUN_DESTRUCT_VM_ON_EXIT),
|
||||
},
|
||||
),
|
||||
.arena = arena,
|
||||
|
||||
@@ -2050,6 +2050,12 @@ pub const Formatter = struct {
|
||||
return error.JSError;
|
||||
}
|
||||
|
||||
// If we call
|
||||
// `return try this.printAs`
|
||||
//
|
||||
// Then we can get a spurious `[Circular]` due to the value already being present in the map.
|
||||
var remove_before_recurse = false;
|
||||
|
||||
var writer = WrappedWriter(Writer){ .ctx = writer_, .estimated_line_length = &this.estimated_line_length };
|
||||
defer {
|
||||
if (writer.failed) {
|
||||
@@ -2075,12 +2081,16 @@ pub const Formatter = struct {
|
||||
if (entry.found_existing) {
|
||||
writer.writeAll(comptime Output.prettyFmt("<r><cyan>[Circular]<r>", enable_ansi_colors));
|
||||
return;
|
||||
} else {
|
||||
remove_before_recurse = true;
|
||||
}
|
||||
}
|
||||
|
||||
defer {
|
||||
if (comptime Format.canHaveCircularReferences()) {
|
||||
_ = this.map.remove(value);
|
||||
if (remove_before_recurse) {
|
||||
_ = this.map.remove(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2617,12 +2627,25 @@ pub const Formatter = struct {
|
||||
} else if (try JestPrettyFormat.printAsymmetricMatcher(this, Format, &writer, writer_, name_buf, value, enable_ansi_colors)) {
|
||||
return;
|
||||
} else if (jsType != .DOMWrapper) {
|
||||
if (remove_before_recurse) {
|
||||
remove_before_recurse = false;
|
||||
_ = this.map.remove(value);
|
||||
}
|
||||
|
||||
if (value.isCallable()) {
|
||||
remove_before_recurse = true;
|
||||
return try this.printAs(.Function, Writer, writer_, value, jsType, enable_ansi_colors);
|
||||
}
|
||||
|
||||
remove_before_recurse = true;
|
||||
return try this.printAs(.Object, Writer, writer_, value, jsType, enable_ansi_colors);
|
||||
}
|
||||
if (remove_before_recurse) {
|
||||
remove_before_recurse = false;
|
||||
_ = this.map.remove(value);
|
||||
}
|
||||
|
||||
remove_before_recurse = true;
|
||||
return try this.printAs(.Object, Writer, writer_, value, .Event, enable_ansi_colors);
|
||||
},
|
||||
.NativeCode => {
|
||||
@@ -2887,6 +2910,12 @@ pub const Formatter = struct {
|
||||
const event_type = switch (try EventType.map.fromJS(this.globalThis, event_type_value) orelse .unknown) {
|
||||
.MessageEvent, .ErrorEvent => |evt| evt,
|
||||
else => {
|
||||
if (remove_before_recurse) {
|
||||
_ = this.map.remove(value);
|
||||
}
|
||||
|
||||
// We must potentially remove it again.
|
||||
remove_before_recurse = true;
|
||||
return try this.printAs(.Object, Writer, writer_, value, .Event, enable_ansi_colors);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -176,12 +176,6 @@ channel_ref_overridden: bool = false,
|
||||
// if one disconnect event listener should be ignored
|
||||
channel_ref_should_ignore_one_disconnect_event_listener: bool = false,
|
||||
|
||||
/// Whether this VM should be destroyed after it exits, even if it is the main thread's VM.
|
||||
/// Worker VMs are always destroyed on exit, regardless of this setting. Setting this to
|
||||
/// true may expose bugs that would otherwise only occur using Workers. Controlled by
|
||||
/// Options.destruct_main_thread_on_exit.
|
||||
destruct_main_thread_on_exit: bool,
|
||||
|
||||
/// A set of extensions that exist in the require.extensions map. Keys
|
||||
/// contain the leading '.'. Value is either a loader for built in
|
||||
/// functions, or an index into JSCommonJSExtensions.
|
||||
@@ -218,6 +212,13 @@ pub fn initRequestBodyValue(this: *VirtualMachine, body: jsc.WebCore.Body.Value)
|
||||
return .init(body, &this.body_value_hive_allocator);
|
||||
}
|
||||
|
||||
/// Whether this VM should be destroyed after it exits, even if it is the main thread's VM.
|
||||
/// Worker VMs are always destroyed on exit, regardless of this setting. Setting this to
|
||||
/// true may expose bugs that would otherwise only occur using Workers. Controlled by
|
||||
pub fn shouldDestructMainThreadOnExit(_: *const VirtualMachine) bool {
|
||||
return bun.getRuntimeFeatureFlag(.BUN_DESTRUCT_VM_ON_EXIT);
|
||||
}
|
||||
|
||||
pub threadlocal var is_bundler_thread_for_bytecode_cache: bool = false;
|
||||
|
||||
pub fn uwsLoop(this: *const VirtualMachine) *uws.Loop {
|
||||
@@ -836,7 +837,7 @@ pub fn onExit(this: *VirtualMachine) void {
|
||||
extern fn Zig__GlobalObject__destructOnExit(*JSGlobalObject) void;
|
||||
|
||||
pub fn globalExit(this: *VirtualMachine) noreturn {
|
||||
if (this.destruct_main_thread_on_exit and this.is_main_thread) {
|
||||
if (this.shouldDestructMainThreadOnExit()) {
|
||||
Zig__GlobalObject__destructOnExit(this.global);
|
||||
this.deinit();
|
||||
}
|
||||
@@ -989,7 +990,7 @@ pub fn initWithModuleGraph(
|
||||
.ref_strings_mutex = .{},
|
||||
.standalone_module_graph = opts.graph.?,
|
||||
.debug_thread_id = if (Environment.allow_assert) std.Thread.getCurrentId(),
|
||||
.destruct_main_thread_on_exit = opts.destruct_main_thread_on_exit,
|
||||
|
||||
.initial_script_execution_context_identifier = if (opts.is_main_thread) 1 else std.math.maxInt(i32),
|
||||
};
|
||||
vm.source_mappings.init(&vm.saved_source_map_table);
|
||||
@@ -1011,6 +1012,9 @@ pub fn initWithModuleGraph(
|
||||
.onDependencyError = ModuleLoader.AsyncModule.Queue.onDependencyError,
|
||||
};
|
||||
|
||||
// Emitting "@__PURE__" comments at runtime is a waste of memory and time.
|
||||
vm.transpiler.options.emit_dce_annotations = false;
|
||||
|
||||
vm.transpiler.resolver.standalone_module_graph = opts.graph.?;
|
||||
|
||||
// Avoid reading from tsconfig.json & package.json when we're in standalone mode
|
||||
@@ -1111,7 +1115,7 @@ pub fn init(opts: Options) !*VirtualMachine {
|
||||
.ref_strings = jsc.RefString.Map.init(allocator),
|
||||
.ref_strings_mutex = .{},
|
||||
.debug_thread_id = if (Environment.allow_assert) std.Thread.getCurrentId(),
|
||||
.destruct_main_thread_on_exit = opts.destruct_main_thread_on_exit,
|
||||
|
||||
.initial_script_execution_context_identifier = if (opts.is_main_thread) 1 else std.math.maxInt(i32),
|
||||
};
|
||||
vm.source_mappings.init(&vm.saved_source_map_table);
|
||||
@@ -1124,6 +1128,9 @@ pub fn init(opts: Options) !*VirtualMachine {
|
||||
vm.regular_event_loop.concurrent_tasks = .{};
|
||||
vm.event_loop = &vm.regular_event_loop;
|
||||
|
||||
// Emitting "@__PURE__" comments at runtime is a waste of memory and time.
|
||||
vm.transpiler.options.emit_dce_annotations = false;
|
||||
|
||||
vm.transpiler.macro_context = null;
|
||||
vm.transpiler.resolver.store_fd = opts.store_fd;
|
||||
vm.transpiler.resolver.prefer_module_field = false;
|
||||
@@ -1270,8 +1277,6 @@ pub fn initWorker(
|
||||
.standalone_module_graph = worker.parent.standalone_module_graph,
|
||||
.worker = worker,
|
||||
.debug_thread_id = if (Environment.allow_assert) std.Thread.getCurrentId(),
|
||||
// This option is irrelevant for Workers
|
||||
.destruct_main_thread_on_exit = false,
|
||||
.initial_script_execution_context_identifier = @as(i32, @intCast(worker.execution_context_id)),
|
||||
};
|
||||
vm.source_mappings.init(&vm.saved_source_map_table);
|
||||
@@ -1279,6 +1284,9 @@ pub fn initWorker(
|
||||
default_allocator,
|
||||
);
|
||||
|
||||
// Emitting "@__PURE__" comments at runtime is a waste of memory and time.
|
||||
vm.transpiler.options.emit_dce_annotations = false;
|
||||
|
||||
vm.regular_event_loop.virtual_machine = vm;
|
||||
vm.regular_event_loop.tasks.ensureUnusedCapacity(64) catch unreachable;
|
||||
vm.regular_event_loop.concurrent_tasks = .{};
|
||||
@@ -1363,7 +1371,7 @@ pub fn initBake(opts: Options) anyerror!*VirtualMachine {
|
||||
.ref_strings = jsc.RefString.Map.init(allocator),
|
||||
.ref_strings_mutex = .{},
|
||||
.debug_thread_id = if (Environment.allow_assert) std.Thread.getCurrentId(),
|
||||
.destruct_main_thread_on_exit = opts.destruct_main_thread_on_exit,
|
||||
|
||||
.initial_script_execution_context_identifier = if (opts.is_main_thread) 1 else std.math.maxInt(i32),
|
||||
};
|
||||
vm.source_mappings.init(&vm.saved_source_map_table);
|
||||
@@ -1715,7 +1723,7 @@ pub fn resolveMaybeNeedsTrailingSlash(
|
||||
const msg = logger.Msg{
|
||||
.data = logger.rangeData(
|
||||
null,
|
||||
logger.Range.None,
|
||||
.none,
|
||||
printed,
|
||||
),
|
||||
};
|
||||
@@ -1796,7 +1804,7 @@ pub fn resolveMaybeNeedsTrailingSlash(
|
||||
break :brk logger.Msg{
|
||||
.data = logger.rangeData(
|
||||
null,
|
||||
logger.Range.None,
|
||||
.none,
|
||||
printed,
|
||||
),
|
||||
.metadata = .{
|
||||
@@ -1843,14 +1851,14 @@ pub fn processFetchLog(globalThis: *JSGlobalObject, specifier: bun.String, refer
|
||||
break :brk logger.Msg{
|
||||
.data = logger.rangeData(
|
||||
null,
|
||||
logger.Range.None,
|
||||
.none,
|
||||
std.fmt.allocPrint(globalThis.allocator(), "Unexpected pending import in \"{}\". To automatically install npm packages with Bun, please use an import statement instead of require() or dynamic import().\nThis error can also happen if dependencies import packages which are not referenced anywhere. Worst case, run `bun install` and opt-out of the node_modules folder until we come up with a better way to handle this error.", .{specifier}) catch unreachable,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
break :brk logger.Msg{
|
||||
.data = logger.rangeData(null, logger.Range.None, std.fmt.allocPrint(globalThis.allocator(), "{s} while building {}", .{ @errorName(err), specifier }) catch unreachable),
|
||||
.data = logger.rangeData(null, .none, std.fmt.allocPrint(globalThis.allocator(), "{s} while building {}", .{ @errorName(err), specifier }) catch unreachable),
|
||||
};
|
||||
};
|
||||
{
|
||||
@@ -2022,7 +2030,7 @@ fn loadPreloads(this: *VirtualMachine) !?*JSInternalPromise {
|
||||
.failure => |e| {
|
||||
this.log.addErrorFmt(
|
||||
null,
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
this.allocator,
|
||||
"{s} resolving preload {}",
|
||||
.{
|
||||
@@ -2035,7 +2043,7 @@ fn loadPreloads(this: *VirtualMachine) !?*JSInternalPromise {
|
||||
.pending, .not_found => {
|
||||
this.log.addErrorFmt(
|
||||
null,
|
||||
logger.Loc.Empty,
|
||||
.none,
|
||||
this.allocator,
|
||||
"preload not found {}",
|
||||
.{
|
||||
|
||||
@@ -72,9 +72,6 @@ pub const BunObject = struct {
|
||||
pub const inspect = toJSLazyPropertyCallback(Bun.getInspect);
|
||||
pub const origin = toJSLazyPropertyCallback(Bun.getOrigin);
|
||||
pub const semver = toJSLazyPropertyCallback(Bun.getSemver);
|
||||
pub const stderr = toJSLazyPropertyCallback(Bun.getStderr);
|
||||
pub const stdin = toJSLazyPropertyCallback(Bun.getStdin);
|
||||
pub const stdout = toJSLazyPropertyCallback(Bun.getStdout);
|
||||
pub const unsafe = toJSLazyPropertyCallback(Bun.getUnsafe);
|
||||
pub const S3Client = toJSLazyPropertyCallback(Bun.getS3ClientConstructor);
|
||||
pub const s3 = toJSLazyPropertyCallback(Bun.getS3DefaultClient);
|
||||
@@ -139,9 +136,6 @@ pub const BunObject = struct {
|
||||
@export(&BunObject.hash, .{ .name = lazyPropertyCallbackName("hash") });
|
||||
@export(&BunObject.inspect, .{ .name = lazyPropertyCallbackName("inspect") });
|
||||
@export(&BunObject.origin, .{ .name = lazyPropertyCallbackName("origin") });
|
||||
@export(&BunObject.stderr, .{ .name = lazyPropertyCallbackName("stderr") });
|
||||
@export(&BunObject.stdin, .{ .name = lazyPropertyCallbackName("stdin") });
|
||||
@export(&BunObject.stdout, .{ .name = lazyPropertyCallbackName("stdout") });
|
||||
@export(&BunObject.unsafe, .{ .name = lazyPropertyCallbackName("unsafe") });
|
||||
@export(&BunObject.semver, .{ .name = lazyPropertyCallbackName("semver") });
|
||||
@export(&BunObject.embeddedFiles, .{ .name = lazyPropertyCallbackName("embeddedFiles") });
|
||||
@@ -188,6 +182,12 @@ pub const BunObject = struct {
|
||||
@export(&BunObject.zstdDecompress, .{ .name = callbackName("zstdDecompress") });
|
||||
// --- Callbacks ---
|
||||
|
||||
// --- LazyProperty initializers ---
|
||||
@export(&createBunStdin, .{ .name = "BunObject__createBunStdin" });
|
||||
@export(&createBunStderr, .{ .name = "BunObject__createBunStderr" });
|
||||
@export(&createBunStdout, .{ .name = "BunObject__createBunStdout" });
|
||||
// --- LazyProperty initializers ---
|
||||
|
||||
// --- Getters ---
|
||||
@export(&BunObject.main, .{ .name = "BunObject_getter_main" });
|
||||
// --- Getters ---
|
||||
@@ -559,39 +559,6 @@ pub fn getOrigin(globalThis: *jsc.JSGlobalObject, _: *jsc.JSObject) jsc.JSValue
|
||||
return ZigString.init(VirtualMachine.get().origin.origin).toJS(globalThis);
|
||||
}
|
||||
|
||||
pub fn getStdin(globalThis: *jsc.JSGlobalObject, _: *jsc.JSObject) jsc.JSValue {
|
||||
var rare_data = globalThis.bunVM().rareData();
|
||||
var store = rare_data.stdin();
|
||||
store.ref();
|
||||
var blob = jsc.WebCore.Blob.new(
|
||||
jsc.WebCore.Blob.initWithStore(store, globalThis),
|
||||
);
|
||||
blob.allocator = bun.default_allocator;
|
||||
return blob.toJS(globalThis);
|
||||
}
|
||||
|
||||
pub fn getStderr(globalThis: *jsc.JSGlobalObject, _: *jsc.JSObject) jsc.JSValue {
|
||||
var rare_data = globalThis.bunVM().rareData();
|
||||
var store = rare_data.stderr();
|
||||
store.ref();
|
||||
var blob = jsc.WebCore.Blob.new(
|
||||
jsc.WebCore.Blob.initWithStore(store, globalThis),
|
||||
);
|
||||
blob.allocator = bun.default_allocator;
|
||||
return blob.toJS(globalThis);
|
||||
}
|
||||
|
||||
pub fn getStdout(globalThis: *jsc.JSGlobalObject, _: *jsc.JSObject) jsc.JSValue {
|
||||
var rare_data = globalThis.bunVM().rareData();
|
||||
var store = rare_data.stdout();
|
||||
store.ref();
|
||||
var blob = jsc.WebCore.Blob.new(
|
||||
jsc.WebCore.Blob.initWithStore(store, globalThis),
|
||||
);
|
||||
blob.allocator = bun.default_allocator;
|
||||
return blob.toJS(globalThis);
|
||||
}
|
||||
|
||||
pub fn enableANSIColors(globalThis: *jsc.JSGlobalObject, _: *jsc.JSObject) jsc.JSValue {
|
||||
_ = globalThis;
|
||||
return JSValue.jsBoolean(Output.enable_ansi_colors);
|
||||
@@ -1609,7 +1576,7 @@ pub const JSZlib = struct {
|
||||
return globalThis.throwError(err, "Zlib error") catch return .zero;
|
||||
};
|
||||
|
||||
reader.readAll() catch {
|
||||
reader.readAll(true) catch {
|
||||
defer reader.deinit();
|
||||
return globalThis.throwValue(ZigString.init(reader.errorMessage() orelse "Zlib returned an error").toErrorInstance(globalThis));
|
||||
};
|
||||
@@ -2068,6 +2035,40 @@ comptime {
|
||||
|
||||
const string = []const u8;
|
||||
|
||||
// LazyProperty initializers for stdin/stderr/stdout
|
||||
pub fn createBunStdin(globalThis: *jsc.JSGlobalObject) callconv(.C) jsc.JSValue {
|
||||
var rare_data = globalThis.bunVM().rareData();
|
||||
var store = rare_data.stdin();
|
||||
store.ref();
|
||||
var blob = jsc.WebCore.Blob.new(
|
||||
jsc.WebCore.Blob.initWithStore(store, globalThis),
|
||||
);
|
||||
blob.allocator = bun.default_allocator;
|
||||
return blob.toJS(globalThis);
|
||||
}
|
||||
|
||||
pub fn createBunStderr(globalThis: *jsc.JSGlobalObject) callconv(.C) jsc.JSValue {
|
||||
var rare_data = globalThis.bunVM().rareData();
|
||||
var store = rare_data.stderr();
|
||||
store.ref();
|
||||
var blob = jsc.WebCore.Blob.new(
|
||||
jsc.WebCore.Blob.initWithStore(store, globalThis),
|
||||
);
|
||||
blob.allocator = bun.default_allocator;
|
||||
return blob.toJS(globalThis);
|
||||
}
|
||||
|
||||
pub fn createBunStdout(globalThis: *jsc.JSGlobalObject) callconv(.C) jsc.JSValue {
|
||||
var rare_data = globalThis.bunVM().rareData();
|
||||
var store = rare_data.stdout();
|
||||
store.ref();
|
||||
var blob = jsc.WebCore.Blob.new(
|
||||
jsc.WebCore.Blob.initWithStore(store, globalThis),
|
||||
);
|
||||
blob.allocator = bun.default_allocator;
|
||||
return blob.toJS(globalThis);
|
||||
}
|
||||
|
||||
const Braces = @import("../../shell/braces.zig");
|
||||
const Which = @import("../../which.zig");
|
||||
const options = @import("../../options.zig");
|
||||
|
||||
@@ -774,7 +774,7 @@ pub const JSBundler = struct {
|
||||
specifier: string = "",
|
||||
importer_source_index: u32,
|
||||
import_record_index: u32 = 0,
|
||||
range: logger.Range = logger.Range.None,
|
||||
range: logger.Range = .none,
|
||||
original_target: Target,
|
||||
|
||||
// pub inline fn loader(_: *const MiniImportRecord) ?options.Loader {
|
||||
|
||||
@@ -591,7 +591,7 @@ fn exportReplacementValue(value: JSValue, globalThis: *JSGlobalObject, allocator
|
||||
.value = value.toBoolean(),
|
||||
},
|
||||
},
|
||||
.loc = logger.Loc.Empty,
|
||||
.loc = .none,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -600,7 +600,7 @@ fn exportReplacementValue(value: JSValue, globalThis: *JSGlobalObject, allocator
|
||||
.data = .{
|
||||
.e_number = .{ .value = value.asNumber() },
|
||||
},
|
||||
.loc = logger.Loc.Empty,
|
||||
.loc = .none,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -609,7 +609,7 @@ fn exportReplacementValue(value: JSValue, globalThis: *JSGlobalObject, allocator
|
||||
.data = .{
|
||||
.e_null = .{},
|
||||
},
|
||||
.loc = logger.Loc.Empty,
|
||||
.loc = .none,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -618,7 +618,7 @@ fn exportReplacementValue(value: JSValue, globalThis: *JSGlobalObject, allocator
|
||||
.data = .{
|
||||
.e_undefined = .{},
|
||||
},
|
||||
.loc = logger.Loc.Empty,
|
||||
.loc = .none,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -632,7 +632,7 @@ fn exportReplacementValue(value: JSValue, globalThis: *JSGlobalObject, allocator
|
||||
.data = .{
|
||||
.e_string = out,
|
||||
},
|
||||
.loc = logger.Loc.Empty,
|
||||
.loc = .none,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -649,8 +649,12 @@ pub fn constructor(globalThis: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) b
|
||||
.transpiler = undefined,
|
||||
.scan_pass_result = ScanPassResult.init(bun.default_allocator),
|
||||
});
|
||||
errdefer bun.destroy(this);
|
||||
errdefer this.arena.deinit();
|
||||
errdefer {
|
||||
this.config.log.deinit();
|
||||
this.arena.deinit();
|
||||
this.ref_count.clearWithoutDestructor();
|
||||
bun.destroy(this);
|
||||
}
|
||||
|
||||
const config_arg = if (arguments.len > 0) arguments.ptr[0] else .js_undefined;
|
||||
const allocator = this.arena.allocator();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub fn create(globalThis: *jsc.JSGlobalObject) jsc.JSValue {
|
||||
const object = JSValue.createEmptyObject(globalThis, 2);
|
||||
const object = JSValue.createEmptyObject(globalThis, 1);
|
||||
object.put(
|
||||
globalThis,
|
||||
ZigString.static("parse"),
|
||||
@@ -10,16 +10,6 @@ pub fn create(globalThis: *jsc.JSGlobalObject) jsc.JSValue {
|
||||
parse,
|
||||
),
|
||||
);
|
||||
object.put(
|
||||
globalThis,
|
||||
ZigString.static("stringify"),
|
||||
jsc.createCallback(
|
||||
globalThis,
|
||||
ZigString.static("stringify"),
|
||||
1,
|
||||
stringify,
|
||||
),
|
||||
);
|
||||
|
||||
return object;
|
||||
}
|
||||
@@ -32,12 +22,12 @@ pub fn parse(
|
||||
const allocator = arena.allocator();
|
||||
defer arena.deinit();
|
||||
var log = logger.Log.init(default_allocator);
|
||||
const input = callframe.argumentsAsArray(1)[0];
|
||||
if (input.isEmptyOrUndefinedOrNull()) {
|
||||
const arguments = callframe.arguments_old(1).slice();
|
||||
if (arguments.len == 0 or arguments[0].isEmptyOrUndefinedOrNull()) {
|
||||
return globalThis.throwInvalidArguments("Expected a string to parse", .{});
|
||||
}
|
||||
|
||||
var input_slice = try input.toSlice(globalThis, bun.default_allocator);
|
||||
var input_slice = try arguments[0].toSlice(globalThis, bun.default_allocator);
|
||||
defer input_slice.deinit();
|
||||
const source = &logger.Source.initPathString("input.toml", input_slice.slice());
|
||||
const parse_result = TOML.parse(source, &log, allocator, false) catch {
|
||||
@@ -66,50 +56,6 @@ pub fn parse(
|
||||
return out.toJSByParseJSON(globalThis);
|
||||
}
|
||||
|
||||
pub fn stringify(
|
||||
globalThis: *jsc.JSGlobalObject,
|
||||
callframe: *jsc.CallFrame,
|
||||
) bun.JSError!jsc.JSValue {
|
||||
const arguments = callframe.arguments();
|
||||
if (arguments.len == 0) {
|
||||
return globalThis.throwInvalidArguments("Expected a value to stringify", .{});
|
||||
}
|
||||
|
||||
const value = arguments.ptr[0];
|
||||
|
||||
if (value.isUndefined()) {
|
||||
return globalThis.throwInvalidArguments("Cannot stringify undefined value to TOML", .{});
|
||||
}
|
||||
|
||||
if (value.isNull()) {
|
||||
return globalThis.throwInvalidArguments("Cannot stringify null value to TOML", .{});
|
||||
}
|
||||
|
||||
if (value.isSymbol() or value.isFunction()) {
|
||||
return .js_undefined;
|
||||
}
|
||||
|
||||
// Use a temporary allocator for stringification
|
||||
var arena = bun.ArenaAllocator.init(globalThis.allocator());
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
|
||||
const result = toml_stringify.stringify(globalThis, value, allocator) catch |err| switch (err) {
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
error.InvalidValue => return globalThis.throwInvalidArguments("Invalid value for TOML stringification", .{}),
|
||||
error.CircularReference => return globalThis.throwInvalidArguments("Circular reference detected", .{}),
|
||||
error.InvalidKey => return globalThis.throwInvalidArguments("Invalid key for TOML", .{}),
|
||||
error.UnsupportedType => return globalThis.throwInvalidArguments("Unsupported type for TOML stringification", .{}),
|
||||
error.JSError => return globalThis.throwInvalidArguments("JavaScript error occurred", .{}),
|
||||
};
|
||||
|
||||
var out = bun.String.borrowUTF8(result);
|
||||
defer out.deref();
|
||||
return out.toJS(globalThis);
|
||||
}
|
||||
|
||||
const toml_stringify = @import("../../interchange/toml_stringify.zig");
|
||||
|
||||
const bun = @import("bun");
|
||||
const default_allocator = bun.default_allocator;
|
||||
const js_printer = bun.js_printer;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub fn create(globalThis: *jsc.JSGlobalObject) jsc.JSValue {
|
||||
const object = JSValue.createEmptyObject(globalThis, 1);
|
||||
const object = JSValue.createEmptyObject(globalThis, 2);
|
||||
object.put(
|
||||
globalThis,
|
||||
ZigString.static("parse"),
|
||||
@@ -10,10 +10,898 @@ pub fn create(globalThis: *jsc.JSGlobalObject) jsc.JSValue {
|
||||
parse,
|
||||
),
|
||||
);
|
||||
object.put(
|
||||
globalThis,
|
||||
ZigString.static("stringify"),
|
||||
jsc.createCallback(
|
||||
globalThis,
|
||||
ZigString.static("stringify"),
|
||||
3,
|
||||
stringify,
|
||||
),
|
||||
);
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
pub fn stringify(global: *JSGlobalObject, callFrame: *jsc.CallFrame) JSError!JSValue {
|
||||
const value, const replacer, const space_value = callFrame.argumentsAsArray(3);
|
||||
|
||||
value.ensureStillAlive();
|
||||
|
||||
if (value.isUndefined() or value.isSymbol() or value.isFunction()) {
|
||||
return .js_undefined;
|
||||
}
|
||||
|
||||
if (!replacer.isUndefinedOrNull()) {
|
||||
return global.throw("YAML.stringify does not support the replacer argument", .{});
|
||||
}
|
||||
|
||||
var scope: bun.AllocationScope = .init(bun.default_allocator);
|
||||
defer scope.deinit();
|
||||
|
||||
var stringifier: Stringifier = try .init(scope.allocator(), global, space_value);
|
||||
defer stringifier.deinit();
|
||||
|
||||
stringifier.findAnchorsAndAliases(global, value, .root) catch |err| return switch (err) {
|
||||
error.OutOfMemory, error.JSError => |js_err| js_err,
|
||||
error.StackOverflow => global.throwStackOverflow(),
|
||||
};
|
||||
|
||||
stringifier.stringify(global, value) catch |err| return switch (err) {
|
||||
error.OutOfMemory, error.JSError => |js_err| js_err,
|
||||
error.StackOverflow => global.throwStackOverflow(),
|
||||
};
|
||||
|
||||
return stringifier.builder.toString(global);
|
||||
}
|
||||
|
||||
const Stringifier = struct {
|
||||
stack_check: bun.StackCheck,
|
||||
builder: wtf.StringBuilder,
|
||||
indent: usize,
|
||||
|
||||
known_collections: std.AutoHashMap(JSValue, AnchorAlias),
|
||||
array_item_counter: usize,
|
||||
prop_names: bun.StringHashMap(usize),
|
||||
|
||||
space: Space,
|
||||
|
||||
pub const Space = union(enum) {
|
||||
minified,
|
||||
number: u32,
|
||||
str: String,
|
||||
|
||||
pub fn init(global: *JSGlobalObject, space_value: JSValue) JSError!Space {
|
||||
if (space_value.isNumber()) {
|
||||
var num = space_value.toInt32();
|
||||
num = @max(0, @min(num, 10));
|
||||
if (num == 0) {
|
||||
return .minified;
|
||||
}
|
||||
return .{ .number = @intCast(num) };
|
||||
}
|
||||
|
||||
if (space_value.isString()) {
|
||||
const str = try space_value.toBunString(global);
|
||||
if (str.length() == 0) {
|
||||
str.deref();
|
||||
return .minified;
|
||||
}
|
||||
return .{ .str = str };
|
||||
}
|
||||
|
||||
return .minified;
|
||||
}
|
||||
|
||||
pub fn deinit(this: *const Space) void {
|
||||
switch (this.*) {
|
||||
.minified => {},
|
||||
.number => {},
|
||||
.str => |str| {
|
||||
str.deref();
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const AnchorOrigin = enum {
|
||||
root,
|
||||
array_item,
|
||||
prop_value,
|
||||
};
|
||||
|
||||
const AnchorAlias = struct {
|
||||
anchored: bool,
|
||||
used: bool,
|
||||
name: Name,
|
||||
|
||||
pub fn init(origin: ValueOrigin) AnchorAlias {
|
||||
return .{
|
||||
.anchored = false,
|
||||
.used = false,
|
||||
.name = switch (origin) {
|
||||
.root => .root,
|
||||
.array_item => .{ .array_item = 0 },
|
||||
.prop_value => .{ .prop_value = .{ .prop_name = origin.prop_value, .counter = 0 } },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
pub const Name = union(AnchorOrigin) {
|
||||
// only one root anchor is possible
|
||||
root,
|
||||
array_item: usize,
|
||||
prop_value: struct {
|
||||
prop_name: String,
|
||||
// added after the name
|
||||
counter: usize,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, global: *JSGlobalObject, space_value: JSValue) JSError!Stringifier {
|
||||
var prop_names: bun.StringHashMap(usize) = .init(allocator);
|
||||
// always rename anchors named "root" to avoid collision with
|
||||
// root anchor/alias
|
||||
try prop_names.put("root", 0);
|
||||
|
||||
return .{
|
||||
.stack_check = .init(),
|
||||
.builder = .init(),
|
||||
.indent = 0,
|
||||
.known_collections = .init(allocator),
|
||||
.array_item_counter = 0,
|
||||
.prop_names = prop_names,
|
||||
.space = try .init(global, space_value),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(this: *Stringifier) void {
|
||||
this.builder.deinit();
|
||||
this.known_collections.deinit();
|
||||
this.prop_names.deinit();
|
||||
this.space.deinit();
|
||||
}
|
||||
|
||||
const ValueOrigin = union(AnchorOrigin) {
|
||||
root,
|
||||
array_item,
|
||||
prop_value: String,
|
||||
};
|
||||
|
||||
pub fn findAnchorsAndAliases(this: *Stringifier, global: *JSGlobalObject, value: JSValue, origin: ValueOrigin) StringifyError!void {
|
||||
if (!this.stack_check.isSafeToRecurse()) {
|
||||
return error.StackOverflow;
|
||||
}
|
||||
|
||||
const unwrapped = try value.unwrapBoxedPrimitive(global);
|
||||
|
||||
if (unwrapped.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (unwrapped.isNumber()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (unwrapped.isBigInt()) {
|
||||
return global.throw("YAML.stringify cannot serialize BigInt", .{});
|
||||
}
|
||||
|
||||
if (unwrapped.isBoolean()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (unwrapped.isString()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (comptime Environment.ci_assert) {
|
||||
bun.assertWithLocation(unwrapped.isObject(), @src());
|
||||
}
|
||||
|
||||
const object_entry = try this.known_collections.getOrPut(unwrapped);
|
||||
if (object_entry.found_existing) {
|
||||
// this will become an alias. increment counters here because
|
||||
// now the anchor/alias is confirmed used.
|
||||
|
||||
if (object_entry.value_ptr.used) {
|
||||
return;
|
||||
}
|
||||
|
||||
object_entry.value_ptr.used = true;
|
||||
|
||||
switch (object_entry.value_ptr.name) {
|
||||
.root => {
|
||||
// only one possible
|
||||
},
|
||||
.array_item => |*counter| {
|
||||
counter.* = this.array_item_counter;
|
||||
this.array_item_counter += 1;
|
||||
},
|
||||
.prop_value => |*prop_value| {
|
||||
const name_entry = try this.prop_names.getOrPut(prop_value.prop_name.byteSlice());
|
||||
if (name_entry.found_existing) {
|
||||
name_entry.value_ptr.* += 1;
|
||||
} else {
|
||||
name_entry.value_ptr.* = 0;
|
||||
}
|
||||
|
||||
prop_value.counter = name_entry.value_ptr.*;
|
||||
},
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
object_entry.value_ptr.* = .init(origin);
|
||||
|
||||
if (unwrapped.isArray()) {
|
||||
var iter = try unwrapped.arrayIterator(global);
|
||||
while (try iter.next()) |item| {
|
||||
if (item.isUndefined() or item.isSymbol() or item.isFunction()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try this.findAnchorsAndAliases(global, item, .array_item);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var iter: jsc.JSPropertyIterator(.{ .skip_empty_name = false, .include_value = true }) = try .init(
|
||||
global,
|
||||
try unwrapped.toObject(global),
|
||||
);
|
||||
defer iter.deinit();
|
||||
|
||||
while (try iter.next()) |prop_name| {
|
||||
if (iter.value.isUndefined() or iter.value.isSymbol() or iter.value.isFunction()) {
|
||||
continue;
|
||||
}
|
||||
try this.findAnchorsAndAliases(global, iter.value, .{ .prop_value = prop_name });
|
||||
}
|
||||
}
|
||||
|
||||
const StringifyError = JSError || bun.StackOverflow;
|
||||
|
||||
pub fn stringify(this: *Stringifier, global: *JSGlobalObject, value: JSValue) StringifyError!void {
|
||||
if (!this.stack_check.isSafeToRecurse()) {
|
||||
return error.StackOverflow;
|
||||
}
|
||||
|
||||
const unwrapped = try value.unwrapBoxedPrimitive(global);
|
||||
|
||||
if (unwrapped.isNull()) {
|
||||
this.builder.append(.latin1, "null");
|
||||
return;
|
||||
}
|
||||
|
||||
if (unwrapped.isNumber()) {
|
||||
if (unwrapped.isInt32()) {
|
||||
this.builder.append(.int, unwrapped.asInt32());
|
||||
return;
|
||||
}
|
||||
|
||||
const num = unwrapped.asNumber();
|
||||
if (std.math.isNegativeInf(num)) {
|
||||
this.builder.append(.latin1, "-.inf");
|
||||
// } else if (std.math.isPositiveInf(num)) {
|
||||
// builder.append(.latin1, "+.inf");
|
||||
} else if (std.math.isInf(num)) {
|
||||
this.builder.append(.latin1, ".inf");
|
||||
} else if (std.math.isNan(num)) {
|
||||
this.builder.append(.latin1, ".nan");
|
||||
} else if (std.math.isNegativeZero(num)) {
|
||||
this.builder.append(.latin1, "-0");
|
||||
} else if (std.math.isPositiveZero(num)) {
|
||||
this.builder.append(.latin1, "+0");
|
||||
} else {
|
||||
this.builder.append(.double, num);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (unwrapped.isBigInt()) {
|
||||
return global.throw("YAML.stringify cannot serialize BigInt", .{});
|
||||
}
|
||||
|
||||
if (unwrapped.isBoolean()) {
|
||||
if (unwrapped.asBoolean()) {
|
||||
this.builder.append(.latin1, "true");
|
||||
} else {
|
||||
this.builder.append(.latin1, "false");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (unwrapped.isString()) {
|
||||
const value_str = try unwrapped.toBunString(global);
|
||||
defer value_str.deref();
|
||||
|
||||
this.appendString(value_str);
|
||||
return;
|
||||
}
|
||||
|
||||
if (comptime Environment.ci_assert) {
|
||||
bun.assertWithLocation(unwrapped.isObject(), @src());
|
||||
}
|
||||
|
||||
const has_anchor: ?*AnchorAlias = has_anchor: {
|
||||
const anchor = this.known_collections.getPtr(unwrapped) orelse {
|
||||
break :has_anchor null;
|
||||
};
|
||||
|
||||
if (!anchor.used) {
|
||||
break :has_anchor null;
|
||||
}
|
||||
|
||||
break :has_anchor anchor;
|
||||
};
|
||||
|
||||
if (has_anchor) |anchor| {
|
||||
this.builder.append(.lchar, if (anchor.anchored) '*' else '&');
|
||||
|
||||
switch (anchor.name) {
|
||||
.root => {
|
||||
this.builder.append(.latin1, "root");
|
||||
},
|
||||
.array_item => {
|
||||
this.builder.append(.latin1, "item");
|
||||
this.builder.append(.usize, anchor.name.array_item);
|
||||
},
|
||||
.prop_value => |prop_value| {
|
||||
if (prop_value.prop_name.length() == 0) {
|
||||
this.builder.append(.latin1, "value");
|
||||
this.builder.append(.usize, prop_value.counter);
|
||||
} else {
|
||||
this.builder.append(.string, anchor.name.prop_value.prop_name);
|
||||
if (anchor.name.prop_value.counter != 0) {
|
||||
this.builder.append(.usize, anchor.name.prop_value.counter);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
if (anchor.anchored) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (this.space) {
|
||||
.minified => {
|
||||
this.builder.append(.lchar, ' ');
|
||||
},
|
||||
.number, .str => {
|
||||
this.newline();
|
||||
},
|
||||
}
|
||||
anchor.anchored = true;
|
||||
}
|
||||
|
||||
if (unwrapped.isArray()) {
|
||||
var iter = try unwrapped.arrayIterator(global);
|
||||
|
||||
if (iter.len == 0) {
|
||||
this.builder.append(.latin1, "[]");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (this.space) {
|
||||
.minified => {
|
||||
this.builder.append(.lchar, '[');
|
||||
var first = true;
|
||||
while (try iter.next()) |item| {
|
||||
if (item.isUndefined() or item.isSymbol() or item.isFunction()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!first) {
|
||||
this.builder.append(.lchar, ',');
|
||||
}
|
||||
first = false;
|
||||
|
||||
try this.stringify(global, item);
|
||||
}
|
||||
this.builder.append(.lchar, ']');
|
||||
},
|
||||
.number, .str => {
|
||||
this.builder.ensureUnusedCapacity(iter.len * "- ".len);
|
||||
var first = true;
|
||||
while (try iter.next()) |item| {
|
||||
if (item.isUndefined() or item.isSymbol() or item.isFunction()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!first) {
|
||||
this.newline();
|
||||
}
|
||||
first = false;
|
||||
|
||||
this.builder.append(.latin1, "- ");
|
||||
|
||||
// don't need to print a newline here for any value
|
||||
|
||||
this.indent += 1;
|
||||
try this.stringify(global, item);
|
||||
this.indent -= 1;
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var iter: jsc.JSPropertyIterator(.{ .skip_empty_name = false, .include_value = true }) = try .init(
|
||||
global,
|
||||
try unwrapped.toObject(global),
|
||||
);
|
||||
defer iter.deinit();
|
||||
|
||||
if (iter.len == 0) {
|
||||
this.builder.append(.latin1, "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (this.space) {
|
||||
.minified => {
|
||||
this.builder.append(.lchar, '{');
|
||||
var first = true;
|
||||
while (try iter.next()) |prop_name| {
|
||||
if (iter.value.isUndefined() or iter.value.isSymbol() or iter.value.isFunction()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!first) {
|
||||
this.builder.append(.lchar, ',');
|
||||
}
|
||||
first = false;
|
||||
|
||||
this.appendString(prop_name);
|
||||
this.builder.append(.latin1, ": ");
|
||||
|
||||
try this.stringify(global, iter.value);
|
||||
}
|
||||
this.builder.append(.lchar, '}');
|
||||
},
|
||||
.number, .str => {
|
||||
this.builder.ensureUnusedCapacity(iter.len * ": ".len);
|
||||
|
||||
var first = true;
|
||||
while (try iter.next()) |prop_name| {
|
||||
if (iter.value.isUndefined() or iter.value.isSymbol() or iter.value.isFunction()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!first) {
|
||||
this.newline();
|
||||
}
|
||||
first = false;
|
||||
|
||||
this.appendString(prop_name);
|
||||
this.builder.append(.latin1, ": ");
|
||||
|
||||
this.indent += 1;
|
||||
|
||||
if (propValueNeedsNewline(iter.value)) {
|
||||
this.newline();
|
||||
}
|
||||
|
||||
try this.stringify(global, iter.value);
|
||||
this.indent -= 1;
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Does this object property value need a newline? True for arrays and objects.
|
||||
fn propValueNeedsNewline(value: JSValue) bool {
|
||||
return !value.isNumber() and !value.isBoolean() and !value.isNull() and !value.isString();
|
||||
}
|
||||
|
||||
fn newline(this: *Stringifier) void {
|
||||
const indent_count = this.indent;
|
||||
|
||||
switch (this.space) {
|
||||
.minified => {},
|
||||
.number => |space_num| {
|
||||
this.builder.append(.lchar, '\n');
|
||||
this.builder.ensureUnusedCapacity(indent_count * space_num);
|
||||
for (0..indent_count * space_num) |_| {
|
||||
this.builder.append(.lchar, ' ');
|
||||
}
|
||||
},
|
||||
.str => |space_str| {
|
||||
this.builder.append(.lchar, '\n');
|
||||
|
||||
const clamped = if (space_str.length() > 10)
|
||||
space_str.substringWithLen(0, 10)
|
||||
else
|
||||
space_str;
|
||||
|
||||
this.builder.ensureUnusedCapacity(indent_count * clamped.length());
|
||||
for (0..indent_count) |_| {
|
||||
this.builder.append(.string, clamped);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn appendDoubleQuotedString(this: *Stringifier, str: String) void {
|
||||
this.builder.append(.lchar, '"');
|
||||
|
||||
for (0..str.length()) |i| {
|
||||
const c = str.charAt(i);
|
||||
|
||||
switch (c) {
|
||||
0x00 => this.builder.append(.latin1, "\\0"),
|
||||
0x01 => this.builder.append(.latin1, "\\x01"),
|
||||
0x02 => this.builder.append(.latin1, "\\x02"),
|
||||
0x03 => this.builder.append(.latin1, "\\x03"),
|
||||
0x04 => this.builder.append(.latin1, "\\x04"),
|
||||
0x05 => this.builder.append(.latin1, "\\x05"),
|
||||
0x06 => this.builder.append(.latin1, "\\x06"),
|
||||
0x07 => this.builder.append(.latin1, "\\a"), // bell
|
||||
0x08 => this.builder.append(.latin1, "\\b"), // backspace
|
||||
0x09 => this.builder.append(.latin1, "\\t"), // tab
|
||||
0x0a => this.builder.append(.latin1, "\\n"), // line feed
|
||||
0x0b => this.builder.append(.latin1, "\\v"), // vertical tab
|
||||
0x0c => this.builder.append(.latin1, "\\f"), // form feed
|
||||
0x0d => this.builder.append(.latin1, "\\r"), // carriage return
|
||||
0x0e => this.builder.append(.latin1, "\\x0e"),
|
||||
0x0f => this.builder.append(.latin1, "\\x0f"),
|
||||
0x10 => this.builder.append(.latin1, "\\x10"),
|
||||
0x11 => this.builder.append(.latin1, "\\x11"),
|
||||
0x12 => this.builder.append(.latin1, "\\x12"),
|
||||
0x13 => this.builder.append(.latin1, "\\x13"),
|
||||
0x14 => this.builder.append(.latin1, "\\x14"),
|
||||
0x15 => this.builder.append(.latin1, "\\x15"),
|
||||
0x16 => this.builder.append(.latin1, "\\x16"),
|
||||
0x17 => this.builder.append(.latin1, "\\x17"),
|
||||
0x18 => this.builder.append(.latin1, "\\x18"),
|
||||
0x19 => this.builder.append(.latin1, "\\x19"),
|
||||
0x1a => this.builder.append(.latin1, "\\x1a"),
|
||||
0x1b => this.builder.append(.latin1, "\\e"), // escape
|
||||
0x1c => this.builder.append(.latin1, "\\x1c"),
|
||||
0x1d => this.builder.append(.latin1, "\\x1d"),
|
||||
0x1e => this.builder.append(.latin1, "\\x1e"),
|
||||
0x1f => this.builder.append(.latin1, "\\x1f"),
|
||||
0x22 => this.builder.append(.latin1, "\\\""), // "
|
||||
0x5c => this.builder.append(.latin1, "\\\\"), // \
|
||||
0x7f => this.builder.append(.latin1, "\\x7f"), // delete
|
||||
0x85 => this.builder.append(.latin1, "\\N"), // next line
|
||||
0xa0 => this.builder.append(.latin1, "\\_"), // non-breaking space
|
||||
0xa8 => this.builder.append(.latin1, "\\L"), // line separator
|
||||
0xa9 => this.builder.append(.latin1, "\\P"), // paragraph separator
|
||||
|
||||
0x20...0x21,
|
||||
0x23...0x5b,
|
||||
0x5d...0x7e,
|
||||
0x80...0x84,
|
||||
0x86...0x9f,
|
||||
0xa1...0xa7,
|
||||
0xaa...std.math.maxInt(u16),
|
||||
=> this.builder.append(.uchar, c),
|
||||
}
|
||||
}
|
||||
|
||||
this.builder.append(.lchar, '"');
|
||||
}
|
||||
|
||||
fn appendString(this: *Stringifier, str: String) void {
|
||||
if (stringNeedsQuotes(str)) {
|
||||
this.appendDoubleQuotedString(str);
|
||||
return;
|
||||
}
|
||||
this.builder.append(.string, str);
|
||||
}
|
||||
|
||||
fn stringNeedsQuotes(str: String) bool {
|
||||
if (str.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (str.charAt(str.length() - 1)) {
|
||||
// whitespace characters
|
||||
' ',
|
||||
'\t',
|
||||
'\n',
|
||||
'\r',
|
||||
=> return true,
|
||||
else => {},
|
||||
}
|
||||
|
||||
switch (str.charAt(0)) {
|
||||
// starting with indicators or whitespace requires quotes
|
||||
'&',
|
||||
'*',
|
||||
'?',
|
||||
'|',
|
||||
'-',
|
||||
'<',
|
||||
'>',
|
||||
'!',
|
||||
'%',
|
||||
'@',
|
||||
' ',
|
||||
'\t',
|
||||
'\n',
|
||||
'\r',
|
||||
'#',
|
||||
=> return true,
|
||||
|
||||
else => {},
|
||||
}
|
||||
|
||||
const keywords = &.{
|
||||
"true",
|
||||
"True",
|
||||
"TRUE",
|
||||
"false",
|
||||
"False",
|
||||
"FALSE",
|
||||
"yes",
|
||||
"Yes",
|
||||
"YES",
|
||||
"no",
|
||||
"No",
|
||||
"NO",
|
||||
"on",
|
||||
"On",
|
||||
"ON",
|
||||
"off",
|
||||
"Off",
|
||||
"OFF",
|
||||
"n",
|
||||
"N",
|
||||
"y",
|
||||
"Y",
|
||||
"null",
|
||||
"Null",
|
||||
"NULL",
|
||||
"~",
|
||||
".inf",
|
||||
".Inf",
|
||||
".INF",
|
||||
".nan",
|
||||
".NaN",
|
||||
".NAN",
|
||||
};
|
||||
|
||||
inline for (keywords) |keyword| {
|
||||
if (str.eqlComptime(keyword)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < str.length()) {
|
||||
switch (str.charAt(i)) {
|
||||
// flow indicators need to be quoted always
|
||||
'{',
|
||||
'}',
|
||||
'[',
|
||||
']',
|
||||
',',
|
||||
=> return true,
|
||||
|
||||
':',
|
||||
=> {
|
||||
if (i + 1 < str.length()) {
|
||||
switch (str.charAt(i + 1)) {
|
||||
' ',
|
||||
'\t',
|
||||
'\n',
|
||||
'\r',
|
||||
=> return true,
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
},
|
||||
|
||||
'#',
|
||||
'`',
|
||||
'\'',
|
||||
=> return true,
|
||||
|
||||
'-' => {
|
||||
if (i + 2 < str.length() and str.charAt(i + 1) == '-' and str.charAt(i + 2) == '-') {
|
||||
if (i + 3 >= str.length()) {
|
||||
return true;
|
||||
}
|
||||
switch (str.charAt(i + 3)) {
|
||||
' ',
|
||||
'\t',
|
||||
'\r',
|
||||
'\n',
|
||||
'[',
|
||||
']',
|
||||
'{',
|
||||
'}',
|
||||
',',
|
||||
=> return true,
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
if (i == 0 and stringIsNumber(str, &i)) {
|
||||
return true;
|
||||
}
|
||||
i += 1;
|
||||
},
|
||||
'.' => {
|
||||
if (i + 2 < str.length() and str.charAt(i + 1) == '.' and str.charAt(i + 2) == '.') {
|
||||
if (i + 3 >= str.length()) {
|
||||
return true;
|
||||
}
|
||||
switch (str.charAt(i + 3)) {
|
||||
' ',
|
||||
'\t',
|
||||
'\r',
|
||||
'\n',
|
||||
'[',
|
||||
']',
|
||||
'{',
|
||||
'}',
|
||||
',',
|
||||
=> return true,
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
if (i == 0 and stringIsNumber(str, &i)) {
|
||||
return true;
|
||||
}
|
||||
i += 1;
|
||||
},
|
||||
|
||||
'0'...'9' => {
|
||||
if (i == 0 and stringIsNumber(str, &i)) {
|
||||
return true;
|
||||
}
|
||||
i += 1;
|
||||
},
|
||||
|
||||
0x00...0x1f,
|
||||
0x22,
|
||||
0x7f,
|
||||
0x85,
|
||||
0xa0,
|
||||
0xa8,
|
||||
0xa9,
|
||||
=> return true,
|
||||
|
||||
else => {
|
||||
i += 1;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fn stringIsNumber(str: String, offset: *usize) bool {
|
||||
const start = offset.*;
|
||||
var i = start;
|
||||
|
||||
var @"+" = false;
|
||||
var @"-" = false;
|
||||
var e = false;
|
||||
var dot = false;
|
||||
|
||||
var base: enum { dec, hex, oct } = .dec;
|
||||
|
||||
next: switch (str.charAt(i)) {
|
||||
'.' => {
|
||||
if (dot or base != .dec) {
|
||||
offset.* = i;
|
||||
return false;
|
||||
}
|
||||
dot = true;
|
||||
i += 1;
|
||||
if (i < str.length()) {
|
||||
continue :next str.charAt(i);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
'+' => {
|
||||
if (@"+") {
|
||||
offset.* = i;
|
||||
return false;
|
||||
}
|
||||
@"+" = true;
|
||||
i += 1;
|
||||
if (i < str.length()) {
|
||||
continue :next str.charAt(i);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
'-' => {
|
||||
if (@"-") {
|
||||
offset.* = i;
|
||||
return false;
|
||||
}
|
||||
@"-" = true;
|
||||
i += 1;
|
||||
if (i < str.length()) {
|
||||
continue :next str.charAt(i);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
'0' => {
|
||||
if (i == start) {
|
||||
if (i + 1 < str.length()) {
|
||||
const nc = str.charAt(i + 1);
|
||||
if (nc == 'x' or nc == 'X') {
|
||||
base = .hex;
|
||||
} else if (nc == 'o' or nc == 'O') {
|
||||
base = .oct;
|
||||
} else {
|
||||
offset.* = i;
|
||||
return false;
|
||||
}
|
||||
i += 1;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
i += 1;
|
||||
if (i < str.length()) {
|
||||
continue :next str.charAt(i);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
'e',
|
||||
'E',
|
||||
=> {
|
||||
if (base == .oct or (e and base == .dec)) {
|
||||
offset.* = i;
|
||||
return false;
|
||||
}
|
||||
e = true;
|
||||
i += 1;
|
||||
if (i < str.length()) {
|
||||
continue :next str.charAt(i);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
'a'...'d',
|
||||
'f',
|
||||
'A'...'D',
|
||||
'F',
|
||||
=> {
|
||||
if (base != .hex) {
|
||||
offset.* = i;
|
||||
return false;
|
||||
}
|
||||
i += 1;
|
||||
if (i < str.length()) {
|
||||
continue :next str.charAt(i);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
'1'...'9' => {
|
||||
i += 1;
|
||||
if (i < str.length()) {
|
||||
continue :next str.charAt(i);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
else => {
|
||||
offset.* = i;
|
||||
return false;
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub fn parse(
|
||||
global: *jsc.JSGlobalObject,
|
||||
callFrame: *jsc.CallFrame,
|
||||
@@ -23,8 +911,10 @@ pub fn parse(
|
||||
|
||||
const input_value = callFrame.argumentsAsArray(1)[0];
|
||||
|
||||
const input_str = try input_value.toBunString(global);
|
||||
const input = input_str.toSlice(arena.allocator());
|
||||
const input: jsc.Node.BlobOrStringOrBuffer = try jsc.Node.BlobOrStringOrBuffer.fromJS(global, arena.allocator(), input_value) orelse input: {
|
||||
const str = try input_value.toBunString(global);
|
||||
break :input .{ .string_or_buffer = .{ .string = str.toSlice(arena.allocator()) } };
|
||||
};
|
||||
defer input.deinit();
|
||||
|
||||
var log = logger.Log.init(bun.default_allocator);
|
||||
@@ -75,12 +965,18 @@ const ParserCtx = struct {
|
||||
ctx.result = .zero;
|
||||
return;
|
||||
},
|
||||
error.StackOverflow => {
|
||||
ctx.result = ctx.global.throwStackOverflow() catch .zero;
|
||||
return;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn toJS(ctx: *ParserCtx, args: *MarkedArgumentBuffer, expr: Expr) JSError!JSValue {
|
||||
const ToJSError = JSError || bun.StackOverflow;
|
||||
|
||||
pub fn toJS(ctx: *ParserCtx, args: *MarkedArgumentBuffer, expr: Expr) ToJSError!JSValue {
|
||||
if (!ctx.stack_check.isSafeToRecurse()) {
|
||||
return ctx.global.throwStackOverflow();
|
||||
return error.StackOverflow;
|
||||
}
|
||||
switch (expr.data) {
|
||||
.e_null => return .null,
|
||||
@@ -143,7 +1039,9 @@ const ParserCtx = struct {
|
||||
const std = @import("std");
|
||||
|
||||
const bun = @import("bun");
|
||||
const Environment = bun.Environment;
|
||||
const JSError = bun.JSError;
|
||||
const String = bun.String;
|
||||
const default_allocator = bun.default_allocator;
|
||||
const logger = bun.logger;
|
||||
const YAML = bun.interchange.yaml.YAML;
|
||||
@@ -156,3 +1054,4 @@ const JSGlobalObject = jsc.JSGlobalObject;
|
||||
const JSValue = jsc.JSValue;
|
||||
const MarkedArgumentBuffer = jsc.MarkedArgumentBuffer;
|
||||
const ZigString = jsc.ZigString;
|
||||
const wtf = bun.jsc.wtf;
|
||||
|
||||
@@ -14,16 +14,19 @@ pub fn getHTTP2CommonString(globalObject: *jsc.JSGlobalObject, hpack_index: u32)
|
||||
if (value.isEmptyOrUndefinedOrNull()) return null;
|
||||
return value;
|
||||
}
|
||||
|
||||
const MAX_WINDOW_SIZE = std.math.maxInt(i32);
|
||||
const MAX_HEADER_TABLE_SIZE = std.math.maxInt(u32);
|
||||
const MAX_STREAM_ID = std.math.maxInt(i32);
|
||||
const MAX_FRAME_SIZE = std.math.maxInt(u24);
|
||||
const DEFAULT_WINDOW_SIZE = std.math.maxInt(u16);
|
||||
|
||||
const PaddingStrategy = enum {
|
||||
none,
|
||||
aligned,
|
||||
max,
|
||||
};
|
||||
|
||||
const FrameType = enum(u8) {
|
||||
HTTP_FRAME_DATA = 0x00,
|
||||
HTTP_FRAME_HEADERS = 0x01,
|
||||
@@ -42,16 +45,19 @@ const FrameType = enum(u8) {
|
||||
const PingFrameFlags = enum(u8) {
|
||||
ACK = 0x1,
|
||||
};
|
||||
|
||||
const DataFrameFlags = enum(u8) {
|
||||
END_STREAM = 0x1,
|
||||
PADDED = 0x8,
|
||||
};
|
||||
|
||||
const HeadersFrameFlags = enum(u8) {
|
||||
END_STREAM = 0x1,
|
||||
END_HEADERS = 0x4,
|
||||
PADDED = 0x8,
|
||||
PRIORITY = 0x20,
|
||||
};
|
||||
|
||||
const SettingsFlags = enum(u8) {
|
||||
ACK = 0x1,
|
||||
};
|
||||
@@ -676,6 +682,7 @@ pub const H2FrameParser = struct {
|
||||
paddingStrategy: PaddingStrategy = .none,
|
||||
|
||||
threadlocal var shared_request_buffer: [16384]u8 = undefined;
|
||||
|
||||
/// The streams hashmap may mutate when growing we use this when we need to make sure its safe to iterate over it
|
||||
pub const StreamResumableIterator = struct {
|
||||
parser: *H2FrameParser,
|
||||
@@ -696,11 +703,13 @@ pub const H2FrameParser = struct {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
pub const FlushState = enum {
|
||||
no_action,
|
||||
flushed,
|
||||
backpressure,
|
||||
};
|
||||
|
||||
const Stream = struct {
|
||||
id: u32 = 0,
|
||||
state: enum(u8) {
|
||||
@@ -1454,11 +1463,13 @@ pub const H2FrameParser = struct {
|
||||
value.ensureStillAlive();
|
||||
return this.handlers.callEventHandlerWithResult(event, this_value, &[_]jsc.JSValue{ ctx_value, value });
|
||||
}
|
||||
|
||||
pub fn dispatchWriteCallback(this: *H2FrameParser, callback: jsc.JSValue) void {
|
||||
jsc.markBinding(@src());
|
||||
|
||||
_ = this.handlers.callWriteCallback(callback, &[_]jsc.JSValue{});
|
||||
}
|
||||
|
||||
pub fn dispatchWithExtra(this: *H2FrameParser, comptime event: js.gc, value: jsc.JSValue, extra: jsc.JSValue) void {
|
||||
jsc.markBinding(@src());
|
||||
|
||||
@@ -1479,6 +1490,7 @@ pub const H2FrameParser = struct {
|
||||
extra2.ensureStillAlive();
|
||||
_ = this.handlers.callEventHandler(event, this_value, ctx_value, &[_]jsc.JSValue{ ctx_value, value, extra, extra2 });
|
||||
}
|
||||
|
||||
pub fn dispatchWith3Extra(this: *H2FrameParser, comptime event: js.gc, value: jsc.JSValue, extra: jsc.JSValue, extra2: jsc.JSValue, extra3: jsc.JSValue) void {
|
||||
jsc.markBinding(@src());
|
||||
|
||||
@@ -1490,6 +1502,7 @@ pub const H2FrameParser = struct {
|
||||
extra3.ensureStillAlive();
|
||||
_ = this.handlers.callEventHandler(event, this_value, ctx_value, &[_]jsc.JSValue{ ctx_value, value, extra, extra2, extra3 });
|
||||
}
|
||||
|
||||
fn cork(this: *H2FrameParser) void {
|
||||
if (CORKED_H2) |corked| {
|
||||
if (@intFromPtr(corked) == @intFromPtr(this)) {
|
||||
@@ -1590,6 +1603,7 @@ pub const H2FrameParser = struct {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// be sure that we dont have any backpressure/data queued on writerBuffer before calling this
|
||||
fn flushStreamQueue(this: *H2FrameParser) usize {
|
||||
log("flushStreamQueue {}", .{this.outboundQueueSize});
|
||||
@@ -1719,6 +1733,7 @@ pub const H2FrameParser = struct {
|
||||
this.ref();
|
||||
AutoFlusher.registerDeferredMicrotaskWithTypeUnchecked(H2FrameParser, this, this.globalThis.bunVM());
|
||||
}
|
||||
|
||||
fn unregisterAutoFlush(this: *H2FrameParser) void {
|
||||
if (!this.auto_flusher.registered) return;
|
||||
AutoFlusher.unregisterDeferredMicrotaskWithTypeUnchecked(H2FrameParser, this, this.globalThis.bunVM());
|
||||
@@ -1996,6 +2011,7 @@ pub const H2FrameParser = struct {
|
||||
|
||||
return end;
|
||||
}
|
||||
|
||||
pub fn handleGoAwayFrame(this: *H2FrameParser, frame: FrameHeader, data: []const u8, stream_: ?*Stream) usize {
|
||||
log("handleGoAwayFrame {} {s}", .{ frame.streamIdentifier, data });
|
||||
if (stream_ != null) {
|
||||
@@ -2080,6 +2096,7 @@ pub const H2FrameParser = struct {
|
||||
}
|
||||
return data.len;
|
||||
}
|
||||
|
||||
pub fn handleAltsvcFrame(this: *H2FrameParser, frame: FrameHeader, data: []const u8, stream_: ?*Stream) bun.JSError!usize {
|
||||
log("handleAltsvcFrame {s}", .{data});
|
||||
if (this.isServer) {
|
||||
@@ -2114,6 +2131,7 @@ pub const H2FrameParser = struct {
|
||||
}
|
||||
return data.len;
|
||||
}
|
||||
|
||||
pub fn handleRSTStreamFrame(this: *H2FrameParser, frame: FrameHeader, data: []const u8, stream_: ?*Stream) usize {
|
||||
log("handleRSTStreamFrame {s}", .{data});
|
||||
var stream = stream_ orelse {
|
||||
@@ -2149,6 +2167,7 @@ pub const H2FrameParser = struct {
|
||||
}
|
||||
return data.len;
|
||||
}
|
||||
|
||||
pub fn handlePingFrame(this: *H2FrameParser, frame: FrameHeader, data: []const u8, stream_: ?*Stream) usize {
|
||||
if (stream_ != null) {
|
||||
this.sendGoAway(frame.streamIdentifier, ErrorCode.PROTOCOL_ERROR, "Ping frame on stream", this.lastStreamID, true);
|
||||
@@ -2177,6 +2196,7 @@ pub const H2FrameParser = struct {
|
||||
}
|
||||
return data.len;
|
||||
}
|
||||
|
||||
pub fn handlePriorityFrame(this: *H2FrameParser, frame: FrameHeader, data: []const u8, stream_: ?*Stream) usize {
|
||||
var stream = stream_ orelse {
|
||||
this.sendGoAway(frame.streamIdentifier, ErrorCode.PROTOCOL_ERROR, "Priority frame on connection stream", this.lastStreamID, true);
|
||||
@@ -2208,6 +2228,7 @@ pub const H2FrameParser = struct {
|
||||
}
|
||||
return data.len;
|
||||
}
|
||||
|
||||
pub fn handleContinuationFrame(this: *H2FrameParser, frame: FrameHeader, data: []const u8, stream_: ?*Stream) bun.JSError!usize {
|
||||
log("handleContinuationFrame", .{});
|
||||
var stream = stream_ orelse {
|
||||
@@ -2315,6 +2336,7 @@ pub const H2FrameParser = struct {
|
||||
// needs more data
|
||||
return data.len;
|
||||
}
|
||||
|
||||
pub fn handleSettingsFrame(this: *H2FrameParser, frame: FrameHeader, data: []const u8) usize {
|
||||
const isACK = frame.flags & @intFromEnum(SettingsFlags.ACK) != 0;
|
||||
|
||||
@@ -2727,6 +2749,7 @@ pub const H2FrameParser = struct {
|
||||
result.put(globalObject, jsc.ZigString.static("outboundQueueSize"), jsc.JSValue.jsNumber(this.outboundQueueSize));
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn goaway(this: *H2FrameParser, globalObject: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!JSValue {
|
||||
jsc.markBinding(@src());
|
||||
const args_list = callframe.arguments_old(3);
|
||||
@@ -2984,6 +3007,7 @@ pub const H2FrameParser = struct {
|
||||
// closed with cancel = aborted
|
||||
return jsc.JSValue.jsBoolean(stream.state == .CLOSED and stream.rstCode == @intFromEnum(ErrorCode.CANCEL));
|
||||
}
|
||||
|
||||
pub fn getStreamState(this: *H2FrameParser, globalObject: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!JSValue {
|
||||
jsc.markBinding(@src());
|
||||
const args_list = callframe.arguments_old(1);
|
||||
@@ -3113,6 +3137,7 @@ pub const H2FrameParser = struct {
|
||||
}
|
||||
return .true;
|
||||
}
|
||||
|
||||
pub fn rstStream(this: *H2FrameParser, globalObject: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!JSValue {
|
||||
log("rstStream", .{});
|
||||
jsc.markBinding(@src());
|
||||
@@ -3160,10 +3185,12 @@ pub const H2FrameParser = struct {
|
||||
return data.len;
|
||||
}
|
||||
};
|
||||
|
||||
// get memory usage in MB
|
||||
fn getSessionMemoryUsage(this: *H2FrameParser) usize {
|
||||
return (this.writeBuffer.len + this.queuedDataSize) / 1024 / 1024;
|
||||
}
|
||||
|
||||
// get memory in bytes
|
||||
pub fn getBufferSize(this: *H2FrameParser, _: *jsc.JSGlobalObject, _: *jsc.CallFrame) bun.JSError!JSValue {
|
||||
jsc.markBinding(@src());
|
||||
@@ -3266,6 +3293,7 @@ pub const H2FrameParser = struct {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn noTrailers(this: *H2FrameParser, globalObject: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!JSValue {
|
||||
jsc.markBinding(@src());
|
||||
const args_list = callframe.arguments_old(1);
|
||||
@@ -3302,6 +3330,7 @@ pub const H2FrameParser = struct {
|
||||
this.dispatchWithExtra(.onStreamEnd, identifier, jsc.JSValue.jsNumber(@intFromEnum(stream.state)));
|
||||
return .js_undefined;
|
||||
}
|
||||
|
||||
/// validate header name and convert to lowecase if needed
|
||||
fn toValidHeaderName(in: []const u8, out: []u8) ![]const u8 {
|
||||
var in_slice = in;
|
||||
@@ -3522,6 +3551,7 @@ pub const H2FrameParser = struct {
|
||||
this.dispatchWithExtra(.onStreamEnd, identifier, jsc.JSValue.jsNumber(@intFromEnum(stream.state)));
|
||||
return .js_undefined;
|
||||
}
|
||||
|
||||
pub fn writeStream(this: *H2FrameParser, globalObject: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!JSValue {
|
||||
jsc.markBinding(@src());
|
||||
const args = callframe.argumentsUndef(5);
|
||||
@@ -4412,6 +4442,7 @@ pub const H2FrameParser = struct {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
pub fn detachFromJS(this: *H2FrameParser, _: *jsc.JSGlobalObject, _: *jsc.CallFrame) bun.JSError!JSValue {
|
||||
jsc.markBinding(@src());
|
||||
var it = this.streams.valueIterator();
|
||||
@@ -4425,6 +4456,7 @@ pub const H2FrameParser = struct {
|
||||
}
|
||||
return .js_undefined;
|
||||
}
|
||||
|
||||
/// be careful when calling detach be sure that the socket is closed and the parser not accesible anymore
|
||||
/// this function can be called multiple times, it will erase stream info
|
||||
pub fn detach(this: *H2FrameParser) void {
|
||||
|
||||
@@ -178,7 +178,10 @@ pub const HTMLRewriter = struct {
|
||||
return global.throwInvalidArguments("Response body already used", .{});
|
||||
}
|
||||
const out = try this.beginTransform(global, response);
|
||||
if (out.toError()) |err| return global.throwValue(err);
|
||||
// Check if the returned value is an error and throw it properly
|
||||
if (out.toError()) |err| {
|
||||
return global.throwValue(err);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -203,6 +206,10 @@ pub const HTMLRewriter = struct {
|
||||
});
|
||||
defer resp.finalize();
|
||||
const out_response_value = try this.beginTransform(global, resp);
|
||||
// Check if the returned value is an error and throw it properly
|
||||
if (out_response_value.toError()) |err| {
|
||||
return global.throwValue(err);
|
||||
}
|
||||
out_response_value.ensureStillAlive();
|
||||
var out_response = out_response_value.as(Response) orelse return out_response_value;
|
||||
var blob = out_response.body.value.useAsAnyBlobAllowNonUTF8String();
|
||||
@@ -1114,13 +1121,12 @@ pub const TextChunk = struct {
|
||||
}
|
||||
|
||||
fn contentHandler(this: *TextChunk, comptime Callback: (fn (*LOLHTML.TextChunk, []const u8, bool) LOLHTML.Error!void), thisObject: JSValue, globalObject: *JSGlobalObject, content: ZigString, contentOptions: ?ContentOptions) JSValue {
|
||||
if (this.text_chunk == null)
|
||||
return .js_undefined;
|
||||
const text_chunk = this.text_chunk orelse return .js_undefined;
|
||||
var content_slice = content.toSlice(bun.default_allocator);
|
||||
defer content_slice.deinit();
|
||||
|
||||
Callback(
|
||||
this.text_chunk.?,
|
||||
text_chunk,
|
||||
content_slice.slice(),
|
||||
contentOptions != null and contentOptions.?.html,
|
||||
) catch return createLOLHTMLError(globalObject);
|
||||
@@ -1167,27 +1173,27 @@ pub const TextChunk = struct {
|
||||
_: *JSGlobalObject,
|
||||
callFrame: *jsc.CallFrame,
|
||||
) bun.JSError!JSValue {
|
||||
if (this.text_chunk == null)
|
||||
return .js_undefined;
|
||||
this.text_chunk.?.remove();
|
||||
const text_chunk = this.text_chunk orelse return .js_undefined;
|
||||
text_chunk.remove();
|
||||
return callFrame.this();
|
||||
}
|
||||
|
||||
pub fn getText(
|
||||
this: *TextChunk,
|
||||
global: *JSGlobalObject,
|
||||
) JSValue {
|
||||
if (this.text_chunk == null)
|
||||
return .js_undefined;
|
||||
return ZigString.init(this.text_chunk.?.getContent().slice()).withEncoding().toJS(global);
|
||||
) bun.JSError!JSValue {
|
||||
const text_chunk = this.text_chunk orelse return .js_undefined;
|
||||
return bun.String.createUTF8ForJS(global, text_chunk.getContent().slice());
|
||||
}
|
||||
|
||||
pub fn removed(this: *TextChunk, _: *JSGlobalObject) JSValue {
|
||||
return JSValue.jsBoolean(this.text_chunk.?.isRemoved());
|
||||
const text_chunk = this.text_chunk orelse return .js_undefined;
|
||||
return JSValue.jsBoolean(text_chunk.isRemoved());
|
||||
}
|
||||
|
||||
pub fn lastInTextNode(this: *TextChunk, _: *JSGlobalObject) JSValue {
|
||||
return JSValue.jsBoolean(this.text_chunk.?.isLastInTextNode());
|
||||
const text_chunk = this.text_chunk orelse return .js_undefined;
|
||||
return JSValue.jsBoolean(text_chunk.isLastInTextNode());
|
||||
}
|
||||
|
||||
pub fn finalize(this: *TextChunk) void {
|
||||
|
||||
@@ -29,6 +29,10 @@ function generate(name) {
|
||||
fn: "dispose",
|
||||
length: 0,
|
||||
},
|
||||
closeIdleConnections: {
|
||||
fn: "closeIdleConnections",
|
||||
length: 0,
|
||||
},
|
||||
stop: {
|
||||
fn: "doStop",
|
||||
length: 1,
|
||||
|
||||
@@ -741,12 +741,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d
|
||||
}
|
||||
}
|
||||
|
||||
pub fn onUpgrade(
|
||||
this: *ThisServer,
|
||||
globalThis: *jsc.JSGlobalObject,
|
||||
object: jsc.JSValue,
|
||||
optional: ?JSValue,
|
||||
) bun.JSError!JSValue {
|
||||
pub fn onUpgrade(this: *ThisServer, globalThis: *jsc.JSGlobalObject, object: jsc.JSValue, optional: ?JSValue) bun.JSError!JSValue {
|
||||
if (this.config.websocket == null) {
|
||||
return globalThis.throwInvalidArguments("To enable websocket support, set the \"websocket\" object in Bun.serve({})", .{});
|
||||
}
|
||||
@@ -1132,11 +1127,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d
|
||||
return this.js_value.get();
|
||||
}
|
||||
|
||||
pub fn onFetch(
|
||||
this: *ThisServer,
|
||||
ctx: *jsc.JSGlobalObject,
|
||||
callframe: *jsc.CallFrame,
|
||||
) bun.JSError!jsc.JSValue {
|
||||
pub fn onFetch(this: *ThisServer, ctx: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!jsc.JSValue {
|
||||
jsc.markBinding(@src());
|
||||
|
||||
if (this.config.onRequest == .zero) {
|
||||
@@ -1253,6 +1244,14 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d
|
||||
return jsc.JSPromise.resolvedPromiseValue(ctx, response_value);
|
||||
}
|
||||
|
||||
pub fn closeIdleConnections(this: *ThisServer, globalObject: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!jsc.JSValue {
|
||||
_ = globalObject;
|
||||
_ = callframe;
|
||||
if (this.app == null) return .js_undefined;
|
||||
this.app.?.closeIdleConnections();
|
||||
return .js_undefined;
|
||||
}
|
||||
|
||||
pub fn stopFromJS(this: *ThisServer, abruptly: ?JSValue) jsc.JSValue {
|
||||
const rc = this.getAllClosedPromise(this.globalThis);
|
||||
|
||||
@@ -1280,10 +1279,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d
|
||||
return .js_undefined;
|
||||
}
|
||||
|
||||
pub fn getPort(
|
||||
this: *ThisServer,
|
||||
_: *jsc.JSGlobalObject,
|
||||
) jsc.JSValue {
|
||||
pub fn getPort(this: *ThisServer, _: *jsc.JSGlobalObject) jsc.JSValue {
|
||||
switch (this.config.address) {
|
||||
.unix => return .js_undefined,
|
||||
else => {},
|
||||
@@ -1412,10 +1408,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d
|
||||
return bun.String.static(if (ssl_enabled) "https" else "http").toJS(globalThis);
|
||||
}
|
||||
|
||||
pub fn getDevelopment(
|
||||
_: *ThisServer,
|
||||
_: *jsc.JSGlobalObject,
|
||||
) jsc.JSValue {
|
||||
pub fn getDevelopment(_: *ThisServer, _: *jsc.JSGlobalObject) jsc.JSValue {
|
||||
return jsc.JSValue.jsBoolean(debug_mode);
|
||||
}
|
||||
|
||||
@@ -1989,11 +1982,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d
|
||||
}
|
||||
}
|
||||
|
||||
pub fn onNodeHTTPRequest(
|
||||
this: *ThisServer,
|
||||
req: *uws.Request,
|
||||
resp: *App.Response,
|
||||
) void {
|
||||
pub fn onNodeHTTPRequest(this: *ThisServer, req: *uws.Request, resp: *App.Response) void {
|
||||
jsc.markBinding(@src());
|
||||
onNodeHTTPRequestWithUpgradeCtx(this, req, resp, null);
|
||||
}
|
||||
@@ -2073,11 +2062,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d
|
||||
ctx.toAsync(req, prepared.request_object);
|
||||
}
|
||||
|
||||
pub fn onRequest(
|
||||
this: *ThisServer,
|
||||
req: *uws.Request,
|
||||
resp: *App.Response,
|
||||
) void {
|
||||
pub fn onRequest(this: *ThisServer, req: *uws.Request, resp: *App.Response) void {
|
||||
var should_deinit_context = false;
|
||||
const prepared = this.prepareJsRequestContext(req, resp, &should_deinit_context, true, null) orelse return;
|
||||
|
||||
@@ -2094,14 +2079,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d
|
||||
this.handleRequest(&should_deinit_context, prepared, req, response_value);
|
||||
}
|
||||
|
||||
pub fn onRequestFromSaved(
|
||||
this: *ThisServer,
|
||||
req: SavedRequest.Union,
|
||||
resp: *App.Response,
|
||||
callback: JSValue,
|
||||
comptime arg_count: comptime_int,
|
||||
extra_args: [arg_count]JSValue,
|
||||
) void {
|
||||
pub fn onRequestFromSaved(this: *ThisServer, req: SavedRequest.Union, resp: *App.Response, callback: JSValue, comptime arg_count: comptime_int, extra_args: [arg_count]JSValue) void {
|
||||
const prepared: PreparedRequest = switch (req) {
|
||||
.stack => |r| this.prepareJsRequestContext(r, resp, null, true, null) orelse return,
|
||||
.saved => |data| .{
|
||||
@@ -2291,13 +2269,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d
|
||||
server.handleRequest(&should_deinit_context, prepared, req, response_value);
|
||||
}
|
||||
|
||||
pub fn onWebSocketUpgrade(
|
||||
this: *ThisServer,
|
||||
resp: *App.Response,
|
||||
req: *uws.Request,
|
||||
upgrade_ctx: *uws.SocketContext,
|
||||
id: usize,
|
||||
) void {
|
||||
pub fn onWebSocketUpgrade(this: *ThisServer, resp: *App.Response, req: *uws.Request, upgrade_ctx: *uws.SocketContext, id: usize) void {
|
||||
jsc.markBinding(@src());
|
||||
if (id == 1) {
|
||||
// This is actually a UserRoute if id is 1 so it's safe to cast
|
||||
|
||||
@@ -31,9 +31,6 @@
|
||||
macro(origin) \
|
||||
macro(s3) \
|
||||
macro(semver) \
|
||||
macro(stderr) \
|
||||
macro(stdin) \
|
||||
macro(stdout) \
|
||||
macro(unsafe) \
|
||||
macro(valkey) \
|
||||
|
||||
|
||||
@@ -875,6 +875,25 @@ static JSC_DEFINE_CUSTOM_SETTER(setBunObjectMain, (JSC::JSGlobalObject * globalO
|
||||
#define bunObjectReadableStreamToJSONCodeGenerator WebCore::readableStreamReadableStreamToJSONCodeGenerator
|
||||
#define bunObjectReadableStreamToTextCodeGenerator WebCore::readableStreamReadableStreamToTextCodeGenerator
|
||||
|
||||
// LazyProperty wrappers for stdin/stderr/stdout
|
||||
static JSValue BunObject_lazyPropCb_wrap_stdin(VM& vm, JSObject* bunObject)
|
||||
{
|
||||
auto* zigGlobalObject = jsCast<Zig::GlobalObject*>(bunObject->globalObject());
|
||||
return zigGlobalObject->m_bunStdin.getInitializedOnMainThread(zigGlobalObject);
|
||||
}
|
||||
|
||||
static JSValue BunObject_lazyPropCb_wrap_stderr(VM& vm, JSObject* bunObject)
|
||||
{
|
||||
auto* zigGlobalObject = jsCast<Zig::GlobalObject*>(bunObject->globalObject());
|
||||
return zigGlobalObject->m_bunStderr.getInitializedOnMainThread(zigGlobalObject);
|
||||
}
|
||||
|
||||
static JSValue BunObject_lazyPropCb_wrap_stdout(VM& vm, JSObject* bunObject)
|
||||
{
|
||||
auto* zigGlobalObject = jsCast<Zig::GlobalObject*>(bunObject->globalObject());
|
||||
return zigGlobalObject->m_bunStdout.getInitializedOnMainThread(zigGlobalObject);
|
||||
}
|
||||
|
||||
#include "BunObject.lut.h"
|
||||
|
||||
#undef bunObjectReadableStreamToArrayCodeGenerator
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user