diff --git a/src/file-system-cache-blob-storage.js b/src/file-system-cache-blob-storage.js index e07754a02..1e4115ebc 100644 --- a/src/file-system-cache-blob-storage.js +++ b/src/file-system-cache-blob-storage.js @@ -1,94 +1,88 @@ -(function() { - var FileSystemCacheBlobStorage; - var fs = require('fs-plus'); - var path = require('path'); +var fs = require('fs-plus') +var path = require('path') - module.exports = FileSystemCacheBlobStorage = (function() { - FileSystemCacheBlobStorage.load = function(directory) { - var instance = new FileSystemCacheBlobStorage(directory); - instance.load(); - return instance; - }; +module.exports = (function () { + FileSystemCacheBlobStorage.load = function (directory) { + var instance = new FileSystemCacheBlobStorage(directory) + instance.load() + return instance + } - function FileSystemCacheBlobStorage(directory) { - this.inMemoryCache = new Map; - this.cacheBlobFilename = path.join(directory, "v8-compile-cache.blob"); - this.cacheMapFilename = path.join(directory, "v8-compile-cache.map"); - this.storedCacheBlob = new Buffer(0); - this.storedCacheMap = {}; + function FileSystemCacheBlobStorage (directory) { + this.inMemoryCache = new Map() + this.cacheBlobFilename = path.join(directory, 'v8-compile-cache.blob') + this.cacheMapFilename = path.join(directory, 'v8-compile-cache.map') + this.storedCacheBlob = new Buffer(0) + this.storedCacheMap = {} + } + + FileSystemCacheBlobStorage.prototype.load = function () { + if (!fs.existsSync(this.cacheMapFilename)) { + return + } + if (!fs.existsSync(this.cacheBlobFilename)) { + return + } + this.storedCacheBlob = fs.readFileSync(this.cacheBlobFilename) + this.storedCacheMap = JSON.parse(fs.readFileSync(this.cacheMapFilename)) + } + + FileSystemCacheBlobStorage.prototype.save = function () { + var dump = this.getDump() + var cacheBlob = Buffer.concat(dump[0]) + var cacheMap = JSON.stringify(dump[1]) + fs.writeFileSync(this.cacheBlobFilename, cacheBlob) + fs.writeFileSync(this.cacheMapFilename, cacheMap) + } + + FileSystemCacheBlobStorage.prototype.has = function (key) { + return this.inMemoryCache.hasOwnProperty(key) || this.storedCacheMap.hasOwnProperty(key) + } + + FileSystemCacheBlobStorage.prototype.get = function (key) { + return this.getFromMemory(key) || this.getFromStorage(key) + } + + FileSystemCacheBlobStorage.prototype.set = function (key, buffer) { + return this.inMemoryCache.set(key, buffer) + } + + FileSystemCacheBlobStorage.prototype.getFromMemory = function (key) { + return this.inMemoryCache.get(key) + } + + FileSystemCacheBlobStorage.prototype.getFromStorage = function (key) { + if (this.storedCacheMap[key] == null) { + return } - FileSystemCacheBlobStorage.prototype.load = function() { - if (!fs.existsSync(this.cacheMapFilename)) { - return; + return this.storedCacheBlob.slice.apply(this.storedCacheBlob, this.storedCacheMap[key]) + } + + FileSystemCacheBlobStorage.prototype.getDump = function () { + var self = this + var buffers = [] + var cacheMap = {} + var currentBufferStart = 0 + function dump (key, getBufferByKey) { + var buffer = getBufferByKey.bind(self)(key) + buffers.push(buffer) + cacheMap[key] = [currentBufferStart, currentBufferStart + buffer.length] + currentBufferStart += buffer.length + } + + this.inMemoryCache.forEach(function (__, key) { + dump(key, self.getFromMemory) + }) + Object.keys(this.storedCacheMap).forEach(function (key) { + if (!cacheMap[key]) { + dump(key, self.getFromStorage) } - if (!fs.existsSync(this.cacheBlobFilename)) { - return; - } - this.storedCacheBlob = fs.readFileSync(this.cacheBlobFilename); - this.storedCacheMap = JSON.parse(fs.readFileSync(this.cacheMapFilename)); - }; + }) - FileSystemCacheBlobStorage.prototype.save = function() { - var dump = this.getDump(); - var buffers = dump[0]; - var cacheMap = dump[1]; - cacheMap = JSON.stringify(cacheMap); - cacheBlob = Buffer.concat(buffers); - fs.writeFileSync(this.cacheBlobFilename, cacheBlob); - fs.writeFileSync(this.cacheMapFilename, cacheMap); - }; + return [buffers, cacheMap] + } - FileSystemCacheBlobStorage.prototype.has = function(key) { - return this.inMemoryCache.hasOwnProperty(key) || this.storedCacheMap.hasOwnProperty(key); - }; + return FileSystemCacheBlobStorage - FileSystemCacheBlobStorage.prototype.get = function(key) { - return this.getFromMemory(key) || this.getFromStorage(key); - }; - - FileSystemCacheBlobStorage.prototype.set = function(key, buffer) { - return this.inMemoryCache.set(key, buffer); - }; - - FileSystemCacheBlobStorage.prototype.getFromMemory = function(key) { - return this.inMemoryCache.get(key); - }; - - FileSystemCacheBlobStorage.prototype.getFromStorage = function(key) { - if (this.storedCacheMap[key] == null) { - return; - } - - return this.storedCacheBlob.slice.apply(this.storedCacheBlob, this.storedCacheMap[key]); - }; - - FileSystemCacheBlobStorage.prototype.getDump = function() { - var self = this; - var buffers = []; - var cacheMap = {}; - var currentBufferStart = 0; - function dump(key, getBufferByKey) { - var buffer = getBufferByKey.bind(self)(key); - buffers.push(buffer); - cacheMap[key] = [currentBufferStart, currentBufferStart + buffer.length]; - currentBufferStart += buffer.length; - }; - - this.inMemoryCache.forEach(function(__, key) { - dump(key, self.getFromMemory); - }); - Object.keys(this.storedCacheMap).forEach(function(key) { - if (!cacheMap[key]) { - dump(key, self.getFromStorage); - } - }); - - return [buffers, cacheMap]; - }; - - return FileSystemCacheBlobStorage; - - })(); - -}).call(this); +})() diff --git a/src/native-compile-cache.js b/src/native-compile-cache.js index dd8dedd19..d7d1c465e 100644 --- a/src/native-compile-cache.js +++ b/src/native-compile-cache.js @@ -1,78 +1,79 @@ var Module = require('module') -var fs = require('fs-plus') var path = require('path') var cachedVm = require('cached-run-in-this-context') -NativeCompileCache = (function() { - function NativeCompileCache() {} +var NativeCompileCache +NativeCompileCache = (function () { + function NativeCompileCache () {} - NativeCompileCache.prototype.setCacheStorage = function(storage) { - this.cacheStorage = storage; - }; + NativeCompileCache.prototype.setCacheStorage = function (storage) { + this.cacheStorage = storage + } - NativeCompileCache.prototype.getCacheStorage = function() { - return this.cacheStorage; - }; + NativeCompileCache.prototype.getCacheStorage = function () { + return this.cacheStorage + } - NativeCompileCache.prototype.install = function() { - this.savePreviousModuleCompile(); - this.overrideModuleCompile(); - }; + NativeCompileCache.prototype.install = function () { + this.savePreviousModuleCompile() + this.overrideModuleCompile() + } - NativeCompileCache.prototype.uninstall = function() { - this.restorePreviousModuleCompile(); - }; + NativeCompileCache.prototype.uninstall = function () { + this.restorePreviousModuleCompile() + } - NativeCompileCache.prototype.savePreviousModuleCompile = function() { - this.previousModuleCompile = Module.prototype._compile; - }; + NativeCompileCache.prototype.savePreviousModuleCompile = function () { + this.previousModuleCompile = Module.prototype._compile + } - NativeCompileCache.prototype.restorePreviousModuleCompile = function() { - Module.prototype._compile = this.previousModuleCompile; - }; + NativeCompileCache.prototype.restorePreviousModuleCompile = function () { + Module.prototype._compile = this.previousModuleCompile + } - NativeCompileCache.prototype.overrideModuleCompile = function() { - var cacheStorage = this.cacheStorage; - Module.prototype._compile = function(content, filename) { - var self = this; + NativeCompileCache.prototype.overrideModuleCompile = function () { + var cacheStorage = this.cacheStorage + var resolvedArgv = null + Module.prototype._compile = function (content, filename) { + var self = this // remove shebang - content = content.replace(/^\#\!.*/, ''); - function require(path) { - return self.require(path); + content = content.replace(/^\#\!.*/, '') + function require (path) { + return self.require(path) } - require.resolve = function(request) { - return Module._resolveFilename(request, self); - }; - require.main = process.mainModule; + require.resolve = function (request) { + return Module._resolveFilename(request, self) + } + require.main = process.mainModule // Enable support to add extra extension types - require.extensions = Module._extensions; - require.cache = Module._cache; + require.extensions = Module._extensions + require.cache = Module._cache - var dirname = path.dirname(filename); + var dirname = path.dirname(filename) // create wrapper function - var wrapper = Module.wrap(content); + var wrapper = Module.wrap(content) - var compiledWrapper = null; + var compiledWrapper = null if (cacheStorage.has(filename)) { - var buffer = cacheStorage.get(filename); + var buffer = cacheStorage.get(filename) compiledWrapper = - cachedVm.runInThisContextCached(wrapper, filename, buffer).result; + cachedVm.runInThisContextCached(wrapper, filename, buffer).result } else { - var compilationResult = cachedVm.runInThisContext(wrapper, filename); + var compilationResult = cachedVm.runInThisContext(wrapper, filename) if (compilationResult.cacheBuffer) { - cacheStorage.set(filename, compilationResult.cacheBuffer); + cacheStorage.set(filename, compilationResult.cacheBuffer) } - compiledWrapper = compilationResult.result; + compiledWrapper = compilationResult.result } if (global.v8debug) { if (!resolvedArgv) { // we enter the repl if we're not given a filename argument. if (process.argv[1]) { - resolvedArgv = Module._resolveFilename(process.argv[1], null); + resolvedArgv = Module._resolveFilename(process.argv[1], null) } else { - resolvedArgv = 'repl'; + resolvedArgv = 'repl' } } @@ -81,17 +82,17 @@ NativeCompileCache = (function() { // Installing this dummy debug event listener tells V8 to start // the debugger. Without it, the setBreakPoint() fails with an // 'illegal access' error. - global.v8debug.Debug.setListener(function() {}); - global.v8debug.Debug.setBreakPoint(compiledWrapper, 0, 0); + global.v8debug.Debug.setListener(function () {}) + global.v8debug.Debug.setBreakPoint(compiledWrapper, 0, 0) } } - var args = [self.exports, require, self, filename, dirname, process, global]; - return compiledWrapper.apply(self.exports, args); - }; - }; + var args = [self.exports, require, self, filename, dirname, process, global] + return compiledWrapper.apply(self.exports, args) + } + } - return NativeCompileCache; + return NativeCompileCache -})(); +})() -module.exports = new NativeCompileCache; +module.exports = new NativeCompileCache() diff --git a/static/index.js b/static/index.js index 8bf42dae9..2e2b0980f 100644 --- a/static/index.js +++ b/static/index.js @@ -3,7 +3,7 @@ var fs = require('fs-plus') var path = require('path') var FileSystemCacheBlobStorage = require('../src/file-system-cache-blob-storage') - var NativeCompileCache = require("../src/native-compile-cache") + var NativeCompileCache = require('../src/native-compile-cache') var loadSettings = null var loadSettingsError = null @@ -11,7 +11,7 @@ app.on('before-quit', function () { if (cacheStorage) { - cacheStorage.save(); + cacheStorage.save() } }) @@ -27,7 +27,7 @@ setupAtomHome() cacheStorage = FileSystemCacheBlobStorage.load( - path.join(process.env.ATOM_HOME, "native-compile-cache/") + path.join(process.env.ATOM_HOME, 'native-compile-cache/') ) NativeCompileCache.setCacheStorage(cacheStorage) NativeCompileCache.install()