diff options
-rw-r--r-- | src/library_fs.js | 12 | ||||
-rw-r--r-- | src/library_memfs.js | 23 | ||||
-rw-r--r-- | tools/file_packager.py | 4 |
3 files changed, 26 insertions, 13 deletions
diff --git a/src/library_fs.js b/src/library_fs.js index 9d1f0cfd..e1397356 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -509,7 +509,7 @@ mergeInto(LibraryManager.library, { var mode = FS.getMode(canRead, canWrite); return FS.create(path, mode); }, - createDataFile: function(parent, name, data, canRead, canWrite) { + createDataFile: function(parent, name, data, canRead, canWrite, canOwn) { var path = PATH.join(typeof parent === 'string' ? parent : FS.getPath(parent), name); var mode = FS.getMode(canRead, canWrite); var node = FS.create(path, mode); @@ -522,7 +522,7 @@ mergeInto(LibraryManager.library, { // make sure we can write to the file FS.chmod(path, mode | {{{ cDefine('S_IWUGO') }}}); var stream = FS.open(path, 'w'); - FS.write(stream, data, 0, data.length, 0); + FS.write(stream, data, 0, data.length, 0, canOwn); FS.close(stream); FS.chmod(path, mode); } @@ -766,7 +766,7 @@ mergeInto(LibraryManager.library, { // You can also call this with a typed array instead of a url. It will then // do preloading for the Image/Audio part, as if the typed array were the // result of an XHR that you did manually. - createPreloadedFile: function(parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile) { + createPreloadedFile: function(parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn) { Browser.init(); // TODO we should allow people to just pass in a complete filename instead // of parent and name being that we just join them anyways @@ -774,7 +774,7 @@ mergeInto(LibraryManager.library, { function processData(byteArray) { function finish(byteArray) { if (!dontCreateFile) { - FS.createDataFile(parent, name, byteArray, canRead, canWrite); + FS.createDataFile(parent, name, byteArray, canRead, canWrite, canOwn); } if (onload) onload(); removeRunDependency('cp ' + fullname); @@ -1323,7 +1323,7 @@ mergeInto(LibraryManager.library, { if (!seeking) stream.position += bytesRead; return bytesRead; }, - write: function(stream, buffer, offset, length, position) { + write: function(stream, buffer, offset, length, position, canOwn) { if (length < 0 || position < 0) { throw new FS.ErrnoError(ERRNO_CODES.EINVAL); } @@ -1347,7 +1347,7 @@ mergeInto(LibraryManager.library, { // seek to the end before writing in append mode FS.llseek(stream, 0, {{{ cDefine('SEEK_END') }}}); } - var bytesWritten = stream.stream_ops.write(stream, buffer, offset, length, position); + var bytesWritten = stream.stream_ops.write(stream, buffer, offset, length, position, canOwn); if (!seeking) stream.position += bytesWritten; return bytesWritten; }, diff --git a/src/library_memfs.js b/src/library_memfs.js index 0ed0a61a..9f9f054e 100644 --- a/src/library_memfs.js +++ b/src/library_memfs.js @@ -2,11 +2,17 @@ mergeInto(LibraryManager.library, { $MEMFS__deps: ['$FS'], $MEMFS: { // content modes - CONTENT_ORIGINAL: 1, // contains the original content written into it (in a typed array most likely) + CONTENT_OWNING: 1, // contains a subarray into the heap, and we own it - need to free() when no longer needed CONTENT_FLEXIBLE: 2, // has been modified or never set to anything, and is a flexible js array that can grow/shrink + CONTENT_FIXED: 3, // contains some fixed-size content written into it, in a typed array ensureFlexible: function(node) { if (node.contentMode !== MEMFS.CONTENT_FLEXIBLE) { - node.contents = Array.prototype.slice.call(node.contents); + var contents = node.contents; + node.contents = Array.prototype.slice.call(contents); + if (node.contentMode === MEMFS.CONTENT_OWNING) { + assert(contents.byteOffset); + Module['_free'](contents.byteOffset); + } node.contentMode = MEMFS.CONTENT_FLEXIBLE; } }, @@ -188,17 +194,24 @@ mergeInto(LibraryManager.library, { } return size; }, - write: function(stream, buffer, offset, length, position) { + write: function(stream, buffer, offset, length, position, canOwn) { var node = stream.node; node.timestamp = Date.now(); var contents = node.contents; +#if USE_TYPED_ARRAYS == 2 if (length && contents.length === 0 && position === 0 && buffer.subarray) { // just replace it with the new data assert(buffer.length); - node.contents = new Uint8Array(buffer.subarray(offset, offset+length)); // TODO: do not copy when unnecessary - node.contentMode = MEMFS.CONTENT_ORIGINAL; + if (canOwn && buffer.buffer === HEAP8.buffer && offset === 0) { + node.contents = buffer; // this is a subarray of the heap, and we can own it + node.contentMode = MEMFS.CONTENT_OWNING; + } else { + node.contents = new Uint8Array(buffer.subarray(offset, offset+length)); + node.contentMode = MEMFS.CONTENT_FIXED; + } return length; } +#endif MEMFS.ensureFlexible(node); var contents = node.contents; while (contents.length < position) contents.push(0); diff --git a/tools/file_packager.py b/tools/file_packager.py index 33ccebad..33bd93d2 100644 --- a/tools/file_packager.py +++ b/tools/file_packager.py @@ -394,7 +394,7 @@ for file_ in data_files: %(prepare)s Module['FS_createPreloadedFile']('%(dirname)s', '%(basename)s', byteArray, true, true, function() { %(finish)s - }%(fail)s); + }, %(fail)s, null, true); }; Module['addRunDependency']('fp %(filename)s'); %(varname)s.send(null); @@ -406,7 +406,7 @@ for file_ in data_files: 'basename': basename, 'prepare': prepare, 'finish': finish, - 'fail': '' if filename[-4:] not in AUDIO_SUFFIXES else ''', function() { Module['removeRunDependency']('fp %s') }''' % filename # workaround for chromium bug 124926 (still no audio with this, but at least we don't hang) + 'fail': 'null' if filename[-4:] not in AUDIO_SUFFIXES else '''function() { Module['removeRunDependency']('fp %s') }''' % filename # workaround for chromium bug 124926 (still no audio with this, but at least we don't hang) } else: assert 0 |