mirror of
https://github.com/oven-sh/bun
synced 2026-02-11 03:18:53 +00:00
Fixed memory leaks, but SSR is slower. Should move cleanup & restart steps to a real idle timeout
Former-commit-id: 9499ee3109cb336deb9380f0190631a30c9da51c
This commit is contained in:
@@ -2,35 +2,61 @@
|
||||
#include "helpers.h"
|
||||
|
||||
#include "ZigConsoleClient.h"
|
||||
#include <JavaScriptCore/AggregateError.h>
|
||||
#include <JavaScriptCore/BytecodeIndex.h>
|
||||
#include <JavaScriptCore/CallFrameInlines.h>
|
||||
#include <JavaScriptCore/CatchScope.h>
|
||||
#include <JavaScriptCore/ClassInfo.h>
|
||||
#include <JavaScriptCore/CodeBlock.h>
|
||||
#include <JavaScriptCore/CodeCache.h>
|
||||
#include <JavaScriptCore/Completion.h>
|
||||
#include <JavaScriptCore/Error.h>
|
||||
#include <JavaScriptCore/ErrorInstance.h>
|
||||
#include <JavaScriptCore/Exception.h>
|
||||
#include <JavaScriptCore/ExceptionScope.h>
|
||||
#include <JavaScriptCore/FunctionConstructor.h>
|
||||
#include <JavaScriptCore/HashMapImpl.h>
|
||||
#include <JavaScriptCore/HashMapImplInlines.h>
|
||||
#include <JavaScriptCore/Heap.h>
|
||||
#include <JavaScriptCore/Identifier.h>
|
||||
#include <JavaScriptCore/InitializeThreading.h>
|
||||
#include <JavaScriptCore/IteratorOperations.h>
|
||||
#include <JavaScriptCore/JSArray.h>
|
||||
#include <JavaScriptCore/JSCInlines.h>
|
||||
#include <JavaScriptCore/JSCallbackObject.h>
|
||||
#include <JavaScriptCore/JSCast.h>
|
||||
#include <JavaScriptCore/JSClassRef.h>
|
||||
#include <JavaScriptCore/JSContextInternal.h>
|
||||
#include <JavaScriptCore/JSInternalPromise.h>
|
||||
#include <JavaScriptCore/JSLock.h>
|
||||
#include <JavaScriptCore/JSMap.h>
|
||||
#include <JavaScriptCore/JSModuleLoader.h>
|
||||
#include <JavaScriptCore/JSModuleRecord.h>
|
||||
#include <JavaScriptCore/JSNativeStdFunction.h>
|
||||
#include <JavaScriptCore/JSObject.h>
|
||||
#include <JavaScriptCore/JSPromise.h>
|
||||
#include <JavaScriptCore/JSSet.h>
|
||||
#include <JavaScriptCore/JSSourceCode.h>
|
||||
#include <JavaScriptCore/JSString.h>
|
||||
#include <JavaScriptCore/JSValueInternal.h>
|
||||
#include <JavaScriptCore/JSVirtualMachineInternal.h>
|
||||
#include <JavaScriptCore/ObjectConstructor.h>
|
||||
#include <JavaScriptCore/OptionsList.h>
|
||||
#include <JavaScriptCore/ParserError.h>
|
||||
#include <JavaScriptCore/ScriptExecutable.h>
|
||||
#include <JavaScriptCore/SourceOrigin.h>
|
||||
#include <JavaScriptCore/StackFrame.h>
|
||||
#include <JavaScriptCore/StackVisitor.h>
|
||||
#include <JavaScriptCore/VM.h>
|
||||
#include <JavaScriptCore/VMEntryScope.h>
|
||||
#include <JavaScriptCore/WasmFaultSignalHandler.h>
|
||||
#include <wtf/URL.h>
|
||||
|
||||
#include <JavaScriptCore/JSLock.h>
|
||||
#include <wtf/StdLibExtras.h>
|
||||
#include <wtf/URL.h>
|
||||
#include <wtf/text/ExternalStringImpl.h>
|
||||
#include <wtf/text/StringCommon.h>
|
||||
#include <wtf/text/StringImpl.h>
|
||||
#include <wtf/text/StringView.h>
|
||||
#include <wtf/text/WTFString.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <exception>
|
||||
@@ -60,8 +86,13 @@ extern "C" JSC__JSGlobalObject *Zig__GlobalObject__create(JSClassRef *globalObje
|
||||
WTF::initializeMainThread();
|
||||
JSC::initialize();
|
||||
|
||||
JSC::VM &vm = JSC::VM::create(JSC::LargeHeap).leakRef();
|
||||
// JSC::Options::useCodeCache() = false;
|
||||
JSC::Options::useSourceProviderCache() = true;
|
||||
JSC::Options::useUnlinkedCodeBlockJettisoning() = false;
|
||||
JSC::Options::useTopLevelAwait() = true;
|
||||
|
||||
JSC::VM &vm = JSC::VM::create(JSC::LargeHeap).leakRef();
|
||||
vm.heap.acquireAccess();
|
||||
#if ENABLE(WEBASSEMBLY)
|
||||
JSC::Wasm::enableFastMemory();
|
||||
#endif
|
||||
@@ -244,39 +275,80 @@ extern "C" bool Zig__GlobalObject__resetModuleRegistryMap(JSC__JSGlobalObject *g
|
||||
void *map_ptr) {
|
||||
if (map_ptr == nullptr) return false;
|
||||
JSC::JSMap *map = reinterpret_cast<JSC::JSMap *>(map_ptr);
|
||||
|
||||
JSC::VM &vm = globalObject->vm();
|
||||
if (JSC::JSObject *obj =
|
||||
JSC::jsDynamicCast<JSC::JSObject *>(globalObject->vm(), globalObject->moduleLoader())) {
|
||||
auto identifier = JSC::Identifier::fromString(globalObject->vm(), "registry");
|
||||
|
||||
if (JSC::JSMap *oldMap = JSC::jsDynamicCast<JSC::JSMap *>(
|
||||
globalObject->vm(), obj->getDirect(globalObject->vm(), identifier))) {
|
||||
// Help the GC by releasing the old map.
|
||||
oldMap->clear(globalObject);
|
||||
// forEachInIterable(
|
||||
// globalObject, oldMap, [&](VM &vm, JSGlobalObject *globalObject, JSValue nextValue) {
|
||||
// auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
// JSC::JSValue key = nextObject->getIndex(globalObject, static_cast<unsigned>(0));
|
||||
// RETURN_IF_EXCEPTION(scope, void());
|
||||
// vm.finalizeSynchronousJSExecution();
|
||||
|
||||
// if (!map->has(globalObject, key)) {
|
||||
obj->putDirect(globalObject->vm(), identifier,
|
||||
map->clone(globalObject, globalObject->vm(), globalObject->mapStructure()));
|
||||
|
||||
// JSC::JSValue value = nextObject->getIndex(globalObject, static_cast<unsigned>(1));
|
||||
// RETURN_IF_EXCEPTION(scope, void());
|
||||
vm.codeCache()->write(vm);
|
||||
vm.shrinkFootprintWhenIdle();
|
||||
// vm.deleteAllLinkedCode(JSC::DeleteAllCodeEffort::DeleteAllCodeIfNotCollecting);
|
||||
// JSC::Heap::PreventCollectionScope(vm.heap);
|
||||
|
||||
// vm.heap.completeAllJITPlans();
|
||||
|
||||
// vm.forEachScriptExecutableSpace([&](auto &spaceAndSet) {
|
||||
// JSC::HeapIterationScope heapIterationScope(vm.heap);
|
||||
// auto &set = spaceAndSet.set;
|
||||
// set.forEachLiveCell([&](JSC::HeapCell *cell, JSC::HeapCell::Kind) {
|
||||
// if (JSC::ModuleProgramExecutable *executable =
|
||||
// JSC::jsDynamicCast<JSC::ModuleProgramExecutable *>(cell)) {
|
||||
// executable->clearCode(set);
|
||||
// }
|
||||
// scope.release();
|
||||
// });
|
||||
};
|
||||
// });
|
||||
}
|
||||
// globalObject->vm().heap.deleteAllUnlinkedCodeBlocks(
|
||||
// JSC::DeleteAllCodeEffort::PreventCollectionAndDeleteAllCode);
|
||||
// vm.whenIdle([globalObject, oldMap, map]() {
|
||||
// auto recordIdentifier = JSC::Identifier::fromString(globalObject->vm(), "module");
|
||||
|
||||
return obj->putDirect(
|
||||
globalObject->vm(), identifier,
|
||||
map->clone(globalObject, globalObject->vm(), globalObject->mapStructure()));
|
||||
// JSC::JSModuleRecord *record;
|
||||
// JSC::JSValue key;
|
||||
// JSC::JSValue value;
|
||||
// JSC::JSObject *mod;
|
||||
// JSC::JSObject *nextObject;
|
||||
// JSC::forEachInIterable(
|
||||
// globalObject, oldMap,
|
||||
// [&](JSC::VM &vm, JSC::JSGlobalObject *globalObject, JSC::JSValue nextValue) {
|
||||
// nextObject = JSC::jsDynamicCast<JSC::JSObject *>(vm, nextValue);
|
||||
// key = nextObject->getIndex(globalObject, static_cast<unsigned>(0));
|
||||
|
||||
// if (!map->has(globalObject, key)) {
|
||||
// value = nextObject->getIndex(globalObject, static_cast<unsigned>(1));
|
||||
// mod = JSC::jsDynamicCast<JSC::JSObject *>(vm, value);
|
||||
// if (mod) {
|
||||
// record = JSC::jsDynamicCast<JSC::JSModuleRecord *>(
|
||||
// vm, mod->getDirect(vm, recordIdentifier));
|
||||
// if (record) {
|
||||
// auto code = &record->sourceCode();
|
||||
// if (code) {
|
||||
|
||||
// Zig::SourceProvider *provider =
|
||||
// reinterpret_cast<Zig::SourceProvider *>(code->provider());
|
||||
// // code->~SourceCode();
|
||||
// if (provider) { provider->freeSourceCode(); }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
// oldMap->clear(globalObject);
|
||||
// }
|
||||
// }
|
||||
// map
|
||||
}
|
||||
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
JSC::JSInternalPromise *GlobalObject::moduleLoaderFetch(JSGlobalObject *globalObject,
|
||||
JSModuleLoader *loader, JSValue key,
|
||||
JSValue value1, JSValue value2) {
|
||||
@@ -319,7 +391,7 @@ JSC::JSInternalPromise *GlobalObject::moduleLoaderFetch(JSGlobalObject *globalOb
|
||||
promise->resolve(globalObject, jsSourceCode);
|
||||
globalObject->vm().drainMicrotasks();
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
JSC::JSObject *GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObject *globalObject,
|
||||
JSModuleLoader *loader,
|
||||
|
||||
@@ -23,11 +23,34 @@ using String = WTF::String;
|
||||
using SourceProviderSourceType = JSC::SourceProviderSourceType;
|
||||
|
||||
Ref<SourceProvider> SourceProvider::create(ResolvedSource resolvedSource) {
|
||||
return adoptRef(*new SourceProvider(
|
||||
resolvedSource,
|
||||
JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath(toString(resolvedSource.source_url))),
|
||||
toStringNotConst(resolvedSource.source_url), TextPosition(),
|
||||
JSC::SourceProviderSourceType::Module));
|
||||
void *allocator = resolvedSource.allocator;
|
||||
|
||||
WTF::StringImpl *stringImpl = nullptr;
|
||||
if (allocator) {
|
||||
Ref<WTF::ExternalStringImpl> stringImpl_ = WTF::ExternalStringImpl::create(
|
||||
resolvedSource.source_code.ptr, resolvedSource.source_code.len,
|
||||
[allocator](WTF::ExternalStringImpl *str, void *ptr, unsigned int len) {
|
||||
ZigString__free((const unsigned char *)ptr, len, allocator);
|
||||
});
|
||||
return adoptRef(*new SourceProvider(
|
||||
resolvedSource, reinterpret_cast<WTF::StringImpl *>(stringImpl_.ptr()),
|
||||
JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath(toString(resolvedSource.source_url))),
|
||||
toStringNotConst(resolvedSource.source_url), TextPosition(),
|
||||
JSC::SourceProviderSourceType::Module));
|
||||
|
||||
} else {
|
||||
Ref<WTF::ExternalStringImpl> stringImpl_ = WTF::ExternalStringImpl::create(
|
||||
resolvedSource.source_code.ptr, resolvedSource.source_code.len,
|
||||
[=](WTF::ExternalStringImpl *str, void *ptr, unsigned int len) {
|
||||
// ZigString__free((const unsigned char *)ptr, len,
|
||||
// allocator);
|
||||
});
|
||||
return adoptRef(*new SourceProvider(
|
||||
resolvedSource, reinterpret_cast<WTF::StringImpl *>(stringImpl_.ptr()),
|
||||
JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath(toString(resolvedSource.source_url))),
|
||||
toStringNotConst(resolvedSource.source_url), TextPosition(),
|
||||
JSC::SourceProviderSourceType::Module));
|
||||
}
|
||||
}
|
||||
|
||||
unsigned SourceProvider::getHash() {
|
||||
@@ -37,6 +60,20 @@ unsigned SourceProvider::getHash() {
|
||||
return m_hash;
|
||||
}
|
||||
|
||||
void SourceProvider::freeSourceCode() {
|
||||
if (did_free_source_code) { return; }
|
||||
did_free_source_code = true;
|
||||
if (m_resolvedSource.allocator != 0) { // // WTF::ExternalStringImpl::destroy(m_source.ptr());
|
||||
this->m_source = WTF::StringImpl::empty()->isolatedCopy();
|
||||
this->m_hash = 0;
|
||||
m_resolvedSource.allocator = 0;
|
||||
}
|
||||
// if (m_resolvedSource.allocator != 0) {
|
||||
// ZigString__free(m_resolvedSource.source_code.ptr, m_resolvedSource.source_code.len,
|
||||
// m_resolvedSource.allocator);
|
||||
// }
|
||||
}
|
||||
|
||||
void SourceProvider::updateCache(const UnlinkedFunctionExecutable *executable, const SourceCode &,
|
||||
CodeSpecializationKind kind,
|
||||
const UnlinkedFunctionCodeBlock *codeBlock) {
|
||||
@@ -122,15 +159,14 @@ int SourceProvider::readCache(JSC::VM &vm, const JSC::SourceCode &sourceCode) {
|
||||
if (fileTotalSize == 0) return 0;
|
||||
|
||||
Ref<JSC::CachedBytecode> cachedBytecode = JSC::CachedBytecode::create(WTFMove(mappedFile));
|
||||
auto key = JSC::sourceCodeKeyForSerializedModule(vm, sourceCode);
|
||||
if (isCachedBytecodeStillValid(vm, cachedBytecode.copyRef(), key,
|
||||
JSC::SourceCodeType::ModuleType)) {
|
||||
m_cachedBytecode = WTFMove(cachedBytecode);
|
||||
return 1;
|
||||
} else {
|
||||
FileSystem::truncateFile(fd, 0);
|
||||
return 0;
|
||||
}
|
||||
// auto key = JSC::sourceCodeKeyForSerializedModule(vm, sourceCode);
|
||||
// if (isCachedBytecodeStillValid(vm, cachedBytecode.copyRef(), key,
|
||||
// JSC::SourceCodeType::ModuleType)) {
|
||||
m_cachedBytecode = WTFMove(cachedBytecode);
|
||||
return 1;
|
||||
// } else {
|
||||
// FileSystem::truncateFile(fd, 0);
|
||||
// return 0;
|
||||
// }
|
||||
}
|
||||
|
||||
}; // namespace Zig
|
||||
@@ -34,7 +34,11 @@ class SourceProvider final : public JSC::SourceProvider {
|
||||
|
||||
public:
|
||||
static Ref<SourceProvider> create(ResolvedSource resolvedSource);
|
||||
~SourceProvider() { commitCachedBytecode(); }
|
||||
~SourceProvider() {
|
||||
freeSourceCode();
|
||||
|
||||
commitCachedBytecode();
|
||||
}
|
||||
|
||||
unsigned hash() const { return m_hash; };
|
||||
StringView source() const { return StringView(m_source.get()); }
|
||||
@@ -52,23 +56,25 @@ class SourceProvider final : public JSC::SourceProvider {
|
||||
void readOrGenerateByteCodeCache(JSC::VM &vm, const JSC::SourceCode &sourceCode);
|
||||
ResolvedSource m_resolvedSource;
|
||||
int readCache(JSC::VM &vm, const JSC::SourceCode &sourceCode);
|
||||
void freeSourceCode();
|
||||
|
||||
private:
|
||||
SourceProvider(ResolvedSource resolvedSource, const SourceOrigin &sourceOrigin,
|
||||
WTF::String &&sourceURL, const TextPosition &startPosition,
|
||||
JSC::SourceProviderSourceType sourceType)
|
||||
: Base(sourceOrigin, WTFMove(sourceURL), startPosition, sourceType),
|
||||
m_source(
|
||||
*WTF::String(resolvedSource.source_code.ptr, resolvedSource.source_code.len).impl()) {
|
||||
SourceProvider(ResolvedSource resolvedSource, WTF::StringImpl *sourceImpl,
|
||||
const SourceOrigin &sourceOrigin, WTF::String &&sourceURL,
|
||||
const TextPosition &startPosition, JSC::SourceProviderSourceType sourceType)
|
||||
: Base(sourceOrigin, WTFMove(sourceURL), startPosition, sourceType), m_source(*sourceImpl) {
|
||||
|
||||
m_resolvedSource = resolvedSource;
|
||||
|
||||
m_hash = resolvedSource.hash;
|
||||
getHash();
|
||||
}
|
||||
|
||||
unsigned m_hash;
|
||||
unsigned getHash();
|
||||
RefPtr<JSC::CachedBytecode> m_cachedBytecode;
|
||||
Ref<WTF::StringImpl> m_source;
|
||||
bool did_free_source_code = false;
|
||||
// JSC::SourceCodeKey key;
|
||||
};
|
||||
|
||||
|
||||
@@ -217,10 +217,19 @@ pub const ResolvedSource = extern struct {
|
||||
source_url: ZigString,
|
||||
hash: u32,
|
||||
|
||||
allocator: ?*c_void,
|
||||
|
||||
// 0 means disabled
|
||||
bytecodecache_fd: u64,
|
||||
};
|
||||
|
||||
export fn ZigString__free(ptr: [*]const u8, len: usize, allocator_: ?*c_void) void {
|
||||
var allocator: *std.mem.Allocator = @ptrCast(*std.mem.Allocator, @alignCast(@alignOf(*std.mem.Allocator), allocator_ orelse return));
|
||||
|
||||
var str = ptr[0..len];
|
||||
allocator.free(str);
|
||||
}
|
||||
|
||||
pub const JSErrorCode = enum(u8) {
|
||||
Error = 0,
|
||||
EvalError = 1,
|
||||
|
||||
@@ -67,15 +67,15 @@ pub fn cTypeLabel(comptime Type: type) ?[]const u8 {
|
||||
};
|
||||
}
|
||||
|
||||
var buffer = std.ArrayList(u8).init(default_allocator);
|
||||
var buffer = std.ArrayList(u8).init(std.heap.c_allocator);
|
||||
var writer = buffer.writer();
|
||||
var impl_buffer = std.ArrayList(u8).init(default_allocator);
|
||||
var impl_buffer = std.ArrayList(u8).init(std.heap.c_allocator);
|
||||
var impl_writer = impl_buffer.writer();
|
||||
var bufset = std.BufSet.init(default_allocator);
|
||||
var type_names = TypeNameMap.init(default_allocator);
|
||||
var opaque_types = std.BufSet.init(default_allocator);
|
||||
var size_map = std.StringHashMap(u32).init(default_allocator);
|
||||
var align_map = std.StringHashMap(u29).init(default_allocator);
|
||||
var bufset = std.BufSet.init(std.heap.c_allocator);
|
||||
var type_names = TypeNameMap.init(std.heap.c_allocator);
|
||||
var opaque_types = std.BufSet.init(std.heap.c_allocator);
|
||||
var size_map = std.StringHashMap(u32).init(std.heap.c_allocator);
|
||||
var align_map = std.StringHashMap(u29).init(std.heap.c_allocator);
|
||||
|
||||
pub const C_Generator = struct {
|
||||
filebase: []const u8,
|
||||
@@ -618,13 +618,13 @@ pub fn HeaderGen(comptime import: type, comptime fname: []const u8) type {
|
||||
\\
|
||||
) catch {};
|
||||
|
||||
var impl_second_buffer = std.ArrayList(u8).init(default_allocator);
|
||||
var impl_second_buffer = std.ArrayList(u8).init(std.heap.c_allocator);
|
||||
var impl_second_writer = impl_second_buffer.writer();
|
||||
|
||||
var impl_third_buffer = std.ArrayList(u8).init(default_allocator);
|
||||
var impl_third_buffer = std.ArrayList(u8).init(std.heap.c_allocator);
|
||||
var impl_third_writer = impl_third_buffer.writer();
|
||||
|
||||
var impl_fourth_buffer = std.ArrayList(u8).init(default_allocator);
|
||||
var impl_fourth_buffer = std.ArrayList(u8).init(std.heap.c_allocator);
|
||||
var impl_fourth_writer = impl_fourth_buffer.writer();
|
||||
|
||||
// inline for (import.all_static_externs) |static_extern, i| {
|
||||
@@ -770,7 +770,7 @@ pub fn HeaderGen(comptime import: type, comptime fname: []const u8) type {
|
||||
var iter = type_names.iterator();
|
||||
|
||||
const NamespaceMap = std.StringArrayHashMap(std.BufMap);
|
||||
var namespaces = NamespaceMap.init(default_allocator);
|
||||
var namespaces = NamespaceMap.init(std.heap.c_allocator);
|
||||
|
||||
var size_iter = size_map.iterator();
|
||||
while (size_iter.next()) |size| {
|
||||
@@ -808,7 +808,7 @@ pub fn HeaderGen(comptime import: type, comptime fname: []const u8) type {
|
||||
}
|
||||
|
||||
if (!namespaces.contains(namespace)) {
|
||||
namespaces.put(namespace, std.BufMap.init(default_allocator)) catch unreachable;
|
||||
namespaces.put(namespace, std.BufMap.init(std.heap.c_allocator)) catch unreachable;
|
||||
}
|
||||
const class = key[namespace_start + 2 ..];
|
||||
namespaces.getPtr(namespace).?.put(class, value) catch unreachable;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//-- AUTOGENERATED FILE -- 1628467440
|
||||
//-- AUTOGENERATED FILE -- 1630718473
|
||||
// clang-format off
|
||||
#pragma once
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
typedef uint16_t ZigErrorCode;
|
||||
|
||||
typedef struct ZigString {
|
||||
@@ -21,6 +23,7 @@ typedef struct ResolvedSource {
|
||||
ZigString source_code;
|
||||
ZigString source_url;
|
||||
uint32_t hash;
|
||||
void *allocator;
|
||||
uint64_t bytecodecache_fd;
|
||||
} ResolvedSource;
|
||||
typedef union ErrorableResolvedSourceResult {
|
||||
@@ -93,4 +96,5 @@ const JSErrorCode JSErrorCodeUserErrorCode = 254;
|
||||
#ifdef __cplusplus
|
||||
extern "C" ZigErrorCode Zig_ErrorCodeParserError;
|
||||
|
||||
extern "C" void ZigString__free(const unsigned char *ptr, size_t len, void *allocator);
|
||||
#endif
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//-- AUTOGENERATED FILE -- 1628467440
|
||||
//-- AUTOGENERATED FILE -- 1630718473
|
||||
// clang-format: off
|
||||
#pragma once
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -350,6 +350,7 @@ pub const VirtualMachine = struct {
|
||||
flush_list: std.ArrayList(string),
|
||||
entry_point: ServerEntryPoint = undefined,
|
||||
|
||||
arena: *std.heap.ArenaAllocator = undefined,
|
||||
has_loaded: bool = false,
|
||||
|
||||
transpiled_count: usize = 0,
|
||||
@@ -415,6 +416,14 @@ pub const VirtualMachine = struct {
|
||||
);
|
||||
VirtualMachine.vm_loaded = true;
|
||||
|
||||
if (!source_code_printer_loaded) {
|
||||
var writer = try js_printer.BufferWriter.init(allocator);
|
||||
source_code_printer = js_printer.BufferPrinter.init(writer);
|
||||
source_code_printer.ctx.append_null_byte = false;
|
||||
|
||||
source_code_printer_loaded = true;
|
||||
}
|
||||
|
||||
return VirtualMachine.vm;
|
||||
}
|
||||
|
||||
@@ -460,6 +469,7 @@ pub const VirtualMachine = struct {
|
||||
const code = try vm.node_modules.?.readCodeAsStringSlow(vm.allocator);
|
||||
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = ZigString.init(code),
|
||||
.specifier = ZigString.init(vm.bundler.linker.nodeModuleBundleImportPath()),
|
||||
.source_url = ZigString.init(vm.bundler.options.node_modules_bundle_pretty_path),
|
||||
@@ -471,6 +481,7 @@ pub const VirtualMachine = struct {
|
||||
};
|
||||
} else if (strings.eqlComptime(_specifier, Runtime.Runtime.Imports.Name)) {
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = ZigString.init(Runtime.Runtime.sourceContent()),
|
||||
.specifier = ZigString.init(Runtime.Runtime.Imports.Name),
|
||||
.source_url = ZigString.init(Runtime.Runtime.Imports.Name),
|
||||
@@ -521,14 +532,6 @@ pub const VirtualMachine = struct {
|
||||
false,
|
||||
);
|
||||
|
||||
if (!source_code_printer_loaded) {
|
||||
var writer = try js_printer.BufferWriter.init(vm.allocator);
|
||||
source_code_printer = js_printer.BufferPrinter.init(writer);
|
||||
source_code_printer.ctx.append_null_byte = false;
|
||||
|
||||
source_code_printer_loaded = true;
|
||||
}
|
||||
|
||||
source_code_printer.ctx.reset();
|
||||
|
||||
var written = try vm.bundler.print(
|
||||
@@ -543,6 +546,7 @@ pub const VirtualMachine = struct {
|
||||
}
|
||||
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = ZigString.init(vm.allocator.dupe(u8, source_code_printer.ctx.written) catch unreachable),
|
||||
.specifier = ZigString.init(std.mem.span(main_file_name)),
|
||||
.source_url = ZigString.init(std.mem.span(main_file_name)),
|
||||
@@ -564,6 +568,8 @@ pub const VirtualMachine = struct {
|
||||
vm.bundler.resetStore();
|
||||
const hash = http.Watcher.getHash(path.text);
|
||||
|
||||
var allocator = if (vm.has_loaded) &vm.arena.allocator else vm.allocator;
|
||||
|
||||
var fd: ?StoredFileDescriptorType = null;
|
||||
|
||||
if (vm.watcher) |watcher| {
|
||||
@@ -583,7 +589,7 @@ pub const VirtualMachine = struct {
|
||||
}
|
||||
|
||||
var parse_result = vm.bundler.parse(
|
||||
vm.bundler.allocator,
|
||||
allocator,
|
||||
path,
|
||||
loader,
|
||||
0,
|
||||
@@ -606,14 +612,6 @@ pub const VirtualMachine = struct {
|
||||
vm.resolved_count += vm.bundler.linker.import_counter - start_count;
|
||||
vm.bundler.linker.import_counter = 0;
|
||||
|
||||
if (!source_code_printer_loaded) {
|
||||
var writer = try js_printer.BufferWriter.init(vm.allocator);
|
||||
source_code_printer = js_printer.BufferPrinter.init(writer);
|
||||
source_code_printer.ctx.append_null_byte = false;
|
||||
|
||||
source_code_printer_loaded = true;
|
||||
}
|
||||
|
||||
source_code_printer.ctx.reset();
|
||||
|
||||
var written = try vm.bundler.print(
|
||||
@@ -628,6 +626,7 @@ pub const VirtualMachine = struct {
|
||||
}
|
||||
|
||||
return ResolvedSource{
|
||||
.allocator = if (vm.has_loaded) vm.allocator else null,
|
||||
.source_code = ZigString.init(vm.allocator.dupe(u8, source_code_printer.ctx.written) catch unreachable),
|
||||
.specifier = ZigString.init(specifier),
|
||||
.source_url = ZigString.init(path.text),
|
||||
@@ -637,6 +636,7 @@ pub const VirtualMachine = struct {
|
||||
},
|
||||
else => {
|
||||
return ResolvedSource{
|
||||
.allocator = vm.allocator,
|
||||
.source_code = ZigString.init(try strings.quotedAlloc(VirtualMachine.vm.allocator, path.pretty)),
|
||||
.specifier = ZigString.init(path.text),
|
||||
.source_url = ZigString.init(path.text),
|
||||
@@ -1334,6 +1334,7 @@ pub const EventListenerMixin = struct {
|
||||
|
||||
// Rely on JS finalizer
|
||||
var fetch_event = try vm.allocator.create(FetchEvent);
|
||||
|
||||
fetch_event.* = FetchEvent{
|
||||
.request_context = request_context,
|
||||
.request = Request{ .request_context = request_context },
|
||||
|
||||
Reference in New Issue
Block a user