Files
bun.sh/test/napi/napi-app/main.js
190n efabdcbe1f Start fixing bugs discovered by Node.js's Node-API tests (#14501)
Co-authored-by: Kai Tamkun <kai@tamkun.io>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: Ashcon Partovi <ashcon@partovi.net>
Co-authored-by: Ciro Spaciari <ciro.spaciari@gmail.com>
Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>
Co-authored-by: 190n <190n@users.noreply.github.com>
2025-02-26 22:11:42 -08:00

51 lines
1.4 KiB
JavaScript

const tests = require("./module");
if (process.argv[2] === "self") {
console.log(
tests(function (str) {
return str + "!";
}),
);
process.exit(0);
}
const fn = tests[process.argv[2]];
if (typeof fn !== "function") {
throw new Error("Unknown test:", process.argv[2]);
}
process.on("uncaughtException", (error, _origin) => {
console.log("uncaught exception:", error.toString());
// 0 because one of the tests intentionally does this, and we don't want it to fail due to crashing
process.exit(0);
});
// pass GC runner as first argument
try {
// napi.test.ts:147 tries to read this variable and shouldn't be able to
let shouldNotExist = 5;
const result = fn.apply(null, [
() => {
if (process.isBun) {
Bun.gc(true);
} else if (global.gc) {
global.gc();
}
console.log("GC did run");
},
...eval(process.argv[3] ?? "[]"),
]);
if (result instanceof Promise) {
result
.then(x => console.log("resolved to", x))
.catch(e => {
console.error("rejected:", e);
});
} else if (process.argv[2] == "eval_wrapper") {
// eval_wrapper just returns the result of the expression so it shouldn't be an error
console.log(result);
} else if (result) {
throw new Error(result);
}
} catch (e) {
console.log(`synchronously threw ${e.name}: message ${JSON.stringify(e.message)}, code ${JSON.stringify(e.code)}`);
}