Fix library_lz4.js

This commit is contained in:
Michael Droettboom 2018-09-18 13:35:21 -04:00
parent 0d4b22ba77
commit 7f578a81e2
2 changed files with 16 additions and 15 deletions

View File

@ -4,6 +4,7 @@ all: emsdk/.complete
# run out of memory
emsdk/.complete:
if [ -d emsdk ]; then rm -rf emsdk; fi
git clone https://github.com/juj/emsdk.git

View File

@ -1,13 +1,12 @@
diff --git a/src/library_lz4.js b/src/library_lz4.js
index 4c3f583b7..bca230feb 100644
--- a/emsdk/emscripten/tag-1.38.10/src/library_lz4.js
+++ b/emsdk/emscripten/tag-1.38.10/src/library_lz4.js
index 4c3f583b7..9540a64fb 100644
--- a/src/library_lz4.js
+++ b/src/library_lz4.js
@@ -5,26 +5,15 @@ mergeInto(LibraryManager.library, {
DIR_MODE: {{{ cDefine('S_IFDIR') }}} | 511 /* 0777 */,
FILE_MODE: {{{ cDefine('S_IFREG') }}} | 511 /* 0777 */,
CHUNK_SIZE: -1,
- codec: null,
+ buf: null,
init: function() {
- if (LZ4.codec) return;
- LZ4.codec = (function() {
@ -29,27 +28,29 @@ index 4c3f583b7..bca230feb 100644
- compressedData.cachedOffset + (i+1)*LZ4.CHUNK_SIZE);
- assert(compressedData.cachedChunks[i].length === LZ4.CHUNK_SIZE);
- }
+ compressedData.buf = null;
pack['metadata'].files.forEach(function(file) {
var dir = PATH.dirname(file.filename);
var name = PATH.basename(file.filename);
@@ -112,8 +101,16 @@ mergeInto(LibraryManager.library, {
@@ -112,8 +101,17 @@ mergeInto(LibraryManager.library, {
//console.log('LZ4 read ' + [offset, length, position]);
length = Math.min(length, stream.node.size - position);
if (length <= 0) return 0;
+
var contents = stream.node.contents;
var compressedData = contents.compressedData;
+ if (LZ4.buf === null) {
+ LZ4.buf = Module['_malloc'](LZ4.CHUNK_SIZE);
+ if (compressedData.buf === null) {
+ compressedData.buf = Module['_malloc'](LZ4.CHUNK_SIZE * 2);
+ for (var i = 0; i < compressedData.cachedIndexes.length; i++) {
+ compressedData.cachedIndexes[i] = -1;
+ compressedData.cachedChunks[i] = Module['_malloc'](LZ4.CHUNK_SIZE);
+ compressedData.cachedChunks[i] = Module['_malloc'](LZ4.CHUNK_SIZE * 2);
+ assert(compressedData.cachedChunks[i] !== null)
+ }
+ }
var written = 0;
while (written < length) {
var start = contents.start + position + written; // start index in uncompressed data
@@ -138,18 +135,24 @@ mergeInto(LibraryManager.library, {
@@ -138,18 +136,23 @@ mergeInto(LibraryManager.library, {
Module['decompressedChunks'] = (Module['decompressedChunks'] || 0) + 1;
}
var compressed = compressedData.data.subarray(compressedStart, compressedStart + compressedSize);
@ -58,8 +59,8 @@ index 4c3f583b7..bca230feb 100644
- //console.log('decompress time: ' + (Date.now() - t));
+ // var t = Date.now();
+ // var originalSize = LZ4.codec.uncompress(compressed, currChunk);
+ Module.HEAPU8.set(compressed, LZ4.buf);
+ var originalSize = Module['_LZ4_decompress_safe'](LZ4.buf, currChunk, compressedSize, LZ4.CHUNK_SIZE);
+ Module.HEAPU8.set(compressed, compressedData.buf);
+ var originalSize = Module['_LZ4_decompress_safe'](compressedData.buf, currChunk, compressedSize, LZ4.CHUNK_SIZE);
+ // console.log('decompress time: ' + (Date.now() - t));
if (chunkIndex < compressedData.successes.length-1) assert(originalSize === LZ4.CHUNK_SIZE); // all but the last chunk must be full-size
}
@ -68,10 +69,9 @@ index 4c3f583b7..bca230feb 100644
+ else {
// uncompressed
- currChunk = compressedData.data.subarray(compressedStart, compressedStart + LZ4.CHUNK_SIZE);
+ var compressed = compressedData.data.subarray(compressedStart, compressedStart + compressedSize);
+ // var originalSize = LZ4.codec.uncompress(compressed, currChunk);
+ Module.HEAPU8.set(compressed, LZ4.buf);
+ currChunk = LZ4.buf;
+ var compressed = compressedData.data.subarray(compressedStart, compressedStart + LZ4.CHUNK_SIZE);
+ Module.HEAPU8.set(compressed, compressedData.buf);
+ currChunk = compressedData.buf;
}
var startInChunk = start % LZ4.CHUNK_SIZE;
var endInChunk = Math.min(startInChunk + desired, LZ4.CHUNK_SIZE);