Unreviewed, rolling out r241612.
[WebKit-https.git] / Source / JavaScriptCore / runtime / CodeCache.h
index 2befc63..7ab80fe 100644 (file)
@@ -109,14 +109,60 @@ public:
     template<typename UnlinkedCodeBlockType>
     UnlinkedCodeBlockType* fetchFromDiskImpl(VM& vm, const SourceCodeKey& key)
     {
-        const auto* cachedBytecode = key.source().provider().cachedBytecode();
-        if (cachedBytecode && cachedBytecode->size()) {
-            VERBOSE_LOG("Found cached CodeBlock in the SourceProvider");
-            UnlinkedCodeBlockType* unlinkedCodeBlock = decodeCodeBlock<UnlinkedCodeBlockType>(vm, key, cachedBytecode->data(), cachedBytecode->size());
-            if (unlinkedCodeBlock)
-                return unlinkedCodeBlock;
+        {
+            const auto* cachedBytecode = key.source().provider().cachedBytecode();
+            if (cachedBytecode && cachedBytecode->size()) {
+                VERBOSE_LOG("Found cached CodeBlock in the SourceProvider");
+                UnlinkedCodeBlockType* unlinkedCodeBlock = decodeCodeBlock<UnlinkedCodeBlockType>(vm, key, cachedBytecode->data(), cachedBytecode->size());
+                if (unlinkedCodeBlock)
+                    return unlinkedCodeBlock;
+            }
         }
+
+#if OS(DARWIN)
+        const char* cachePath = Options::diskCachePath();
+        if (!cachePath)
+            return nullptr;
+
+        unsigned hash = key.hash();
+        char filename[512];
+        int count = snprintf(filename, 512, "%s/%u.cache", cachePath, hash);
+        if (count < 0 || count > 512)
+            return nullptr;
+
+        int fd = open(filename, O_RDONLY);
+        if (fd == -1)
+            return nullptr;
+
+        int rc = flock(fd, LOCK_SH | LOCK_NB);
+        if (rc) {
+            close(fd);
+            return nullptr;
+        }
+
+        struct stat sb;
+        int res = fstat(fd, &sb);
+        size_t size = static_cast<size_t>(sb.st_size);
+        if (res || !size) {
+            close(fd);
+            return nullptr;
+        }
+
+        void* buffer = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
+        UnlinkedCodeBlockType* unlinkedCodeBlock = decodeCodeBlock<UnlinkedCodeBlockType>(vm, key, buffer, size);
+        munmap(buffer, size);
+
+        if (!unlinkedCodeBlock)
+            return nullptr;
+
+        VERBOSE_LOG("Found cached CodeBlock on disk");
+        addCache(key, SourceCodeValue(vm, unlinkedCodeBlock, m_age, true));
+        return unlinkedCodeBlock;
+#else
+        UNUSED_PARAM(vm);
+        UNUSED_PARAM(key);
         return nullptr;
+#endif
     }
 
     template<typename UnlinkedCodeBlockType>