diff options
author | 2023-08-02 16:16:22 -0700 | |
---|---|---|
committer | 2023-08-02 16:16:22 -0700 | |
commit | 7656b4b17e91f15b58eeab8f45b78c416ec6a045 (patch) | |
tree | e9dceff8d8db52855f11431f5011949dc0bf8ea5 | |
parent | 25553e62c1cb8151c901f2e09ccb1a71f6a1e7bd (diff) | |
download | bun-7656b4b17e91f15b58eeab8f45b78c416ec6a045.tar.gz bun-7656b4b17e91f15b58eeab8f45b78c416ec6a045.tar.zst bun-7656b4b17e91f15b58eeab8f45b78c416ec6a045.zip |
Fixes #3931 (#3933)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | src/bun.js/bindings/BunString.cpp | 46 | ||||
-rw-r--r-- | test/js/node/fs/fs.test.ts | 12 | ||||
-rw-r--r-- | test/js/node/fs/repro-3931.js | 4 |
3 files changed, 24 insertions, 38 deletions
diff --git a/src/bun.js/bindings/BunString.cpp b/src/bun.js/bindings/BunString.cpp index 7864ee94e..630d9be76 100644 --- a/src/bun.js/bindings/BunString.cpp +++ b/src/bun.js/bindings/BunString.cpp @@ -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) diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index e2de62480..5cb0aea2b 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -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)); diff --git a/test/js/node/fs/repro-3931.js b/test/js/node/fs/repro-3931.js new file mode 100644 index 000000000..b6651127d --- /dev/null +++ b/test/js/node/fs/repro-3931.js @@ -0,0 +1,4 @@ +import { readdir } from "fs/promises"; + +const files = await readdir(`/tmp`, {}); +console.log(files.map(a => a)); |