Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2023-08-02 16:16:22 -07:00
committed by GitHub
parent 25553e62c1
commit 7656b4b17e
3 changed files with 25 additions and 39 deletions

View File

@@ -267,47 +267,19 @@ extern "C" EncodedJSValue BunString__createArray(
auto& vm = globalObject->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);
if (length < 64) {
// We must do this or Bun.gc(true) in a loop creating large arrays of strings will crash due to GC'ing.
MarkedArgumentBuffer arguments;
arguments.fill(length, [&](JSC::JSValue* value) {
const BunString* end = ptr + length;
while (ptr != end) {
*value++ = Bun::toJS(globalObject, *ptr++);
}
});
JSC::ObjectInitializationScope scope(vm);
GCDeferralContext context(vm);
JSC::JSArray* array = JSC::JSArray::tryCreateUninitializedRestricted(
scope,
globalObject->arrayStructureForIndexingTypeDuringAllocation(JSC::ArrayWithContiguous),
length);
if (array) {
for (size_t i = 0; i < length; ++i) {
array->initializeIndex(scope, i, arguments.at(i));
}
return JSValue::encode(array);
}
// Using tryCreateUninitialized here breaks stuff..
// https://github.com/oven-sh/bun/issues/3931
JSC::JSArray* array = constructEmptyArray(globalObject, nullptr, length);
if (!array) {
JSC::throwOutOfMemoryError(globalObject, throwScope);
RELEASE_AND_RETURN(throwScope, JSValue::encode(JSC::JSValue()));
} else {
JSC::JSArray* array = constructEmptyArray(globalObject, nullptr, length);
if (!array) {
JSC::throwOutOfMemoryError(globalObject, throwScope);
RELEASE_AND_RETURN(throwScope, JSValue::encode(JSC::JSValue()));
}
for (size_t i = 0; i < length; ++i) {
array->putDirectIndex(globalObject, i, Bun::toJS(globalObject, *ptr++));
}
return JSValue::encode(array);
}
for (size_t i = 0; i < length; ++i) {
array->putDirectIndex(globalObject, i, Bun::toJS(globalObject, *ptr++));
}
return JSValue::encode(array);
}
extern "C" void BunString__toWTFString(BunString* bunString)

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "bun:test";
import { dirname } from "node:path";
import { gc } from "harness";
import { bunEnv, bunExe, gc } from "harness";
import fs, {
closeSync,
existsSync,
@@ -42,6 +42,7 @@ import { join } from "node:path";
import { ReadStream as ReadStream_, WriteStream as WriteStream_ } from "./export-from.js";
import { ReadStream as ReadStreamStar_, WriteStream as WriteStreamStar_ } from "./export-star-from.js";
import { spawnSync } from "bun";
const Buffer = globalThis.Buffer || Uint8Array;
@@ -67,6 +68,15 @@ it("writeFileSync in append should not truncate the file", () => {
expect(readFileSync(path, "utf8")).toBe(str);
});
it("await readdir #3931", async () => {
const { exitCode } = spawnSync({
cmd: [bunExe(), join(import.meta.dir, "./repro-3931.js")],
env: bunEnv,
cwd: import.meta.dir,
});
expect(exitCode).toBe(0);
});
it("writeFileSync NOT in append SHOULD truncate the file", () => {
const path = join(tmpdir(), "writeFileSync-should-not-append-" + (Date.now() * 10000).toString(16));

View File

@@ -0,0 +1,4 @@
import { readdir } from "fs/promises";
const files = await readdir(`/tmp`, {});
console.log(files.map(a => a));