diff options
author | 2021-09-04 04:50:47 -0700 | |
---|---|---|
committer | 2021-09-04 04:50:47 -0700 | |
commit | 4f7ff6db2c393b0106260ca786172a1d8f1ea1ac (patch) | |
tree | ed2d5b712c268deaf92bc6b613df1c16171650d0 /src/javascript/jsc/bindings/ZigSourceProvider.cpp | |
parent | 0045fc36d6188d21c8999073e98e8c048f5571e9 (diff) | |
download | bun-4f7ff6db2c393b0106260ca786172a1d8f1ea1ac.tar.gz bun-4f7ff6db2c393b0106260ca786172a1d8f1ea1ac.tar.zst bun-4f7ff6db2c393b0106260ca786172a1d8f1ea1ac.zip |
Fixed memory leaks, but SSR is slower. Should move cleanup & restart steps to a real idle timeout
Former-commit-id: 9499ee3109cb336deb9380f0190631a30c9da51c
Diffstat (limited to 'src/javascript/jsc/bindings/ZigSourceProvider.cpp')
-rw-r--r-- | src/javascript/jsc/bindings/ZigSourceProvider.cpp | 66 |
1 files changed, 51 insertions, 15 deletions
diff --git a/src/javascript/jsc/bindings/ZigSourceProvider.cpp b/src/javascript/jsc/bindings/ZigSourceProvider.cpp index 63bf1dcfd..a2cb86c39 100644 --- a/src/javascript/jsc/bindings/ZigSourceProvider.cpp +++ b/src/javascript/jsc/bindings/ZigSourceProvider.cpp @@ -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
\ No newline at end of file |