Skip to content

Commit

Permalink
wip doesn't fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Dec 31, 2024
1 parent f8c092d commit 04683ed
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 98 deletions.
9 changes: 4 additions & 5 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1154,11 +1154,11 @@ pub const Crypto = struct {
};
}

pub const names: std.EnumArray(Algorithm, ZigString) = brk: {
var all = std.EnumArray(Algorithm, ZigString).initUndefined();
pub const names: std.EnumArray(Algorithm, bun.String) = brk: {
var all = std.EnumArray(Algorithm, bun.String).initUndefined();
var iter = all.iterator();
while (iter.next()) |entry| {
entry.value.* = ZigString.init(@tagName(entry.key));
entry.value.* = bun.String.init(@tagName(entry.key));
}
break :brk all;
};
Expand Down Expand Up @@ -2329,8 +2329,7 @@ pub const Crypto = struct {
_: JSValue,
_: JSValue,
) JSC.JSValue {
var values = EVP.Algorithm.names.values;
return JSC.JSValue.createStringArray(globalThis_, &values, values.len, true);
return bun.String.toJSArray(globalThis_, &EVP.Algorithm.names.values);
}

fn hashToEncoding(globalThis: *JSGlobalObject, evp: *EVP, input: JSC.Node.BlobOrStringOrBuffer, encoding: JSC.Node.Encoding) bun.JSError!JSC.JSValue {
Expand Down
20 changes: 10 additions & 10 deletions src/bun.js/api/ffi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -983,36 +983,36 @@ pub const FFI = struct {
return val;
}
JSC.markBinding(@src());
var zig_strings = allocator.alloc(ZigString, symbols.count()) catch unreachable;
for (symbols.values(), 0..) |*function, i| {
var strs = std.ArrayList(bun.String).initCapacity(allocator, symbols.count()) catch bun.outOfMemory();
defer {
for (strs.items) |str| {
str.deref();
}
strs.deinit();
}
for (symbols.values()) |*function| {
var arraylist = std.ArrayList(u8).init(allocator);
var writer = arraylist.writer();
function.printSourceCode(&writer) catch {
// an error while generating source code
for (symbols.keys()) |key| {
allocator.free(@constCast(key));
}
for (zig_strings) |zig_string| {
allocator.free(@constCast(zig_string.slice()));
}
for (symbols.values()) |*function_| {
function_.arg_types.deinit(allocator);
}

symbols.clearAndFree(allocator);
return ZigString.init("Error while printing code").toErrorInstance(global);
};
zig_strings[i] = ZigString.init(arraylist.items);
strs.appendAssumeCapacity(bun.String.createUTF8(arraylist.items));
}

const ret = JSC.JSValue.createStringArray(global, zig_strings.ptr, zig_strings.len, true);
const ret = bun.String.toJSArray(global, strs.items);

for (symbols.keys()) |key| {
allocator.free(@constCast(key));
}
for (zig_strings) |zig_string| {
allocator.free(@constCast(zig_string.slice()));
}
for (symbols.values()) |*function_| {
function_.arg_types.deinit(allocator);
if (function_.step == .compiled) {
Expand Down
7 changes: 6 additions & 1 deletion src/bun.js/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,12 @@ void Zig::GlobalObject::resetOnEachMicrotaskTick()
extern "C" JSC__JSGlobalObject* Zig__GlobalObject__create(void* console_client, int32_t executionContextId, bool miniMode, bool evalMode, void* worker_ptr)
{
auto heapSize = miniMode ? JSC::HeapType::Small : JSC::HeapType::Large;
JSC::VM& vm = JSC::VM::create(heapSize).leakRef();
RefPtr<JSC::VM> vmPtr = JSC::VM::tryCreate(heapSize);
if (UNLIKELY(!vmPtr)) {
BUN_PANIC("Failed to allocate JavaScriptCore Virtual Machine. Did your computer run out of memory? Or maybe you compiled Bun with a mismatching libc++ version or compiler?");
}
vmPtr->refSuppressingSaferCPPChecking();
JSC::VM& vm = *vmPtr;
// This must happen before JSVMClientData::create
vm.heap.acquireAccess();
JSC::JSLockHolder locker(vm);
Expand Down
8 changes: 4 additions & 4 deletions src/bun.js/bindings/ZigSourceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ static JSC::VM& getVMForBytecodeCache()
static thread_local JSC::VM* vmForBytecodeCache = nullptr;
if (!vmForBytecodeCache) {
const auto heapSize = JSC::HeapType::Small;
auto& vm = JSC::VM::create(heapSize).leakRef();
vm.ref();
vmForBytecodeCache = &vm;
vm.heap.acquireAccess();
auto vmPtr = JSC::VM::tryCreate(heapSize);
vmPtr->refSuppressingSaferCPPChecking();
vmForBytecodeCache = vmPtr.get();
vmPtr->heap.acquireAccess();
}
return *vmForBytecodeCache;
}
Expand Down
93 changes: 27 additions & 66 deletions src/bun.js/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2933,44 +2933,6 @@ CPP_DECL void JSC__JSValue__push(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg
array->push(arg1, value2);
}

JSC__JSValue JSC__JSValue__createStringArray(JSC__JSGlobalObject* globalObject, const ZigString* arg1,
size_t arg2, bool clone)
{
JSC::VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (arg2 == 0) {
return JSC::JSValue::encode(JSC::constructEmptyArray(globalObject, nullptr));
}

JSC::JSArray* array = nullptr;
{
JSC::GCDeferralContext deferralContext(vm);
JSC::ObjectInitializationScope initializationScope(vm);
if ((array = JSC::JSArray::tryCreateUninitializedRestricted(
initializationScope, &deferralContext,
globalObject->arrayStructureForIndexingTypeDuringAllocation(JSC::ArrayWithContiguous),
arg2))) {

if (!clone) {
for (size_t i = 0; i < arg2; ++i) {
array->putDirectIndex(globalObject, i, JSC::jsString(vm, Zig::toString(arg1[i]), &deferralContext));
}
} else {
for (size_t i = 0; i < arg2; ++i) {
array->putDirectIndex(globalObject, i, JSC::jsString(vm, Zig::toStringCopy(arg1[i]), &deferralContext));
}
}
}

if (!array) {
JSC::throwOutOfMemoryError(globalObject, scope);
return {};
}

RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSC::JSValue(array)));
}
}

JSC__JSValue JSC__JSGlobalObject__createAggregateError(JSC__JSGlobalObject* globalObject,
const JSValue* errors, size_t errors_count,
const ZigString* arg3)
Expand Down Expand Up @@ -5049,34 +5011,33 @@ size_t JSC__VM__runGC(JSC__VM* vm, bool sync)
{
JSC::JSLockHolder lock(vm);

#if IS_MALLOC_DEBUGGING_ENABLED && OS(DARWIN)
if (!malloc_zone_check(nullptr)) {
BUN_PANIC("Heap corruption detected!!");
}
#endif

vm->finalizeSynchronousJSExecution();
WTF::releaseFastMallocFreeMemory();

if (sync) {
vm->clearSourceProviderCaches();
vm->heap.deleteAllUnlinkedCodeBlocks(JSC::PreventCollectionAndDeleteAllCode);
vm->heap.collectNow(JSC::Sync, JSC::CollectionScope::Full);
#if IS_MALLOC_DEBUGGING_ENABLED && OS(DARWIN)
malloc_zone_pressure_relief(nullptr, 0);
#endif
} else {
vm->heap.deleteAllUnlinkedCodeBlocks(JSC::DeleteAllCodeIfNotCollecting);
vm->heap.collectSync(JSC::CollectionScope::Full);
}

vm->finalizeSynchronousJSExecution();

#if IS_MALLOC_DEBUGGING_ENABLED && OS(DARWIN)
if (!malloc_zone_check(nullptr)) {
BUN_PANIC("Heap corruption detected after GC!!");
}
#endif
// #if IS_MALLOC_DEBUGGING_ENABLED && OS(DARWIN)
// if (!malloc_zone_check(nullptr)) {
// BUN_PANIC("Heap corruption detected!!");
// }
// #endif

// vm->finalizeSynchronousJSExecution();

// if (sync) {
// vm->clearSourceProviderCaches();
// vm->heap.deleteAllUnlinkedCodeBlocks(JSC::PreventCollectionAndDeleteAllCode);
// vm->heap.collectNow(JSC::Sync, JSC::CollectionScope::Full);
// #if IS_MALLOC_DEBUGGING_ENABLED && OS(DARWIN)
// malloc_zone_pressure_relief(nullptr, 0);
// #endif
// } else {
// vm->heap.deleteAllUnlinkedCodeBlocks(JSC::DeleteAllCodeIfNotCollecting);
// vm->heap.collectSync(JSC::CollectionScope::Full);
// }

// vm->finalizeSynchronousJSExecution();

// #if IS_MALLOC_DEBUGGING_ENABLED && OS(DARWIN)
// if (!malloc_zone_check(nullptr)) {
// BUN_PANIC("Heap corruption detected after GC!!");
// }
// #endif

return vm->heap.sizeAfterLastFullCollection();
}
Expand Down
9 changes: 0 additions & 9 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4618,15 +4618,6 @@ pub const JSValue = enum(i64) {
return str;
}

pub fn createStringArray(globalThis: *JSGlobalObject, str: [*c]const ZigString, strings_count: usize, clone: bool) JSValue {
return cppFn("createStringArray", .{
globalThis,
str,
strings_count,
clone,
});
}

pub fn print(
this: JSValue,
globalObject: *JSGlobalObject,
Expand Down
1 change: 0 additions & 1 deletion src/bun.js/bindings/headers.zig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/bun.js/bindings/webcore/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,9 @@ extern "C" void WebWorker__dispatchExit(Zig::GlobalObject* globalObject, Worker*
// clang-tidy is smart enough to realize that deref() leads to freeing
// but it's not smart enough to realize that `hasOneRef()` ensures its safety
while (!vm.hasOneRef()) // NOLINT
vm.deref(); // NOLINT
vm.derefSuppressingSaferCPPChecking(); // NOLINT

vm.deref(); // NOLINT
vm.derefSuppressingSaferCPPChecking(); // NOLINT
}
}
extern "C" void WebWorker__dispatchOnline(Worker* worker, Zig::GlobalObject* globalObject)
Expand Down
1 change: 1 addition & 0 deletions test/js/node/worker_threads/worker_thread_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (isMainThread) {
action,
port: server.port,
},
env: process.env,
});
worker.ref();
const { promise, resolve } = Promise.withResolvers();
Expand Down

0 comments on commit 04683ed

Please sign in to comment.