Files
bun.sh/test/napi/multiple-exceptions.test.js
Claude Bot 6bcfa2fd1e Fix NAPI multiple exception handling to match Node.js behavior
- Add m_hasPendingNapiException flag to track NAPI exception state
- Modify napi_throw and throwErrorWithCStrings to check for pending exceptions
- Ignore subsequent throws when exception is already pending
- Update NAPI preamble to check for pending exceptions
- This prevents crashes from multiple ThrowAsJavaScriptException calls

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 13:52:45 +00:00

42 lines
1.3 KiB
JavaScript

import { test, expect } from "bun:test";
import { join } from "path";
// This test reproduces the bug where multiple ThrowAsJavaScriptException calls
// in the same NAPI function cause Bun to crash with an assertion failure
const addonPath = join(import.meta.dir, "napi-app", "build", "Debug", "multiple_exceptions_addon.node");
// Build the addon first if needed
import { spawnSync } from "child_process";
const buildResult = spawnSync("node-gyp", ["build"], {
cwd: join(import.meta.dir, "napi-app"),
stdio: "inherit"
});
let addon;
try {
addon = require(addonPath);
} catch (error) {
console.warn("Could not load multiple_exceptions_addon.node - skipping tests");
console.warn("Run 'cd test/napi/napi-app && node-gyp build' to build the addon");
addon = null;
}
test.skipIf(!addon)("throwing after catching exception should not crash Bun", () => {
expect(() => {
addon.throwAfterCatch();
}).toThrow("Second exception after catch");
});
test.skipIf(!addon)("multiple exceptions should not crash - second overwrites first", () => {
expect(() => {
addon.throwMultiple();
}).toThrow("Second exception");
});
test.skipIf(!addon)("exception pending check should work", () => {
expect(() => {
const result = addon.checkExceptionPending();
// This should throw before returning the result
}).toThrow("Test exception");
});