Skip to content

Commit cbca7d9

Browse files
committed
Modernize and run eslint on tools/lz4-compress.js. NFC
Followup to #15836
1 parent a8e7d3b commit cbca7d9

File tree

3 files changed

+50
-133
lines changed

3 files changed

+50
-133
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"wasm2c": "1.0.0"
1515
},
1616
"scripts": {
17-
"lint": "eslint src/parseTools.js tools/acorn-optimizer.js"
17+
"lint": "eslint src/parseTools.js tools/acorn-optimizer.js tools/lz4-compress.js"
1818
}
1919
}

third_party/mini-lz4.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ THE SOFTWARE.
2828
changes have the same license
2929
*/
3030

31+
function assert(condition, message) {
32+
if (!condition) {
33+
throw new Error(message);
34+
}
35+
}
36+
3137
var MiniLZ4 = (function() {
3238

3339
var exports = {};
@@ -282,14 +288,14 @@ exports.compressPackage = function(data, verify) {
282288
// compress the data in chunks
283289
assert(data instanceof ArrayBuffer);
284290
data = new Uint8Array(data);
285-
console.log('compressing package of size ' + data.length);
291+
console.error('compressing package of size ' + data.length);
286292
var compressedChunks = [];
287293
var successes = [];
288294
var offset = 0;
289295
var total = 0;
290296
while (offset < data.length) {
291297
var chunk = data.subarray(offset, offset + exports.CHUNK_SIZE);
292-
//console.log('compress a chunk ' + [offset, total, data.length]);
298+
//console.error('compress a chunk ' + [offset, total, data.length]);
293299
offset += exports.CHUNK_SIZE;
294300
var bound = exports.compressBound(chunk.length);
295301
var compressed = new Uint8Array(bound);
@@ -332,7 +338,7 @@ exports.compressPackage = function(data, verify) {
332338
compressedData['sizes'][i] = compressedChunks[i].length
333339
offset += compressedChunks[i].length;
334340
}
335-
console.log('compressed package into ' + [compressedData['data'].length]);
341+
console.error('compressed package into ' + [compressedData['data'].length]);
336342
assert(offset === total);
337343
return compressedData;
338344
};

tools/lz4-compress.js

100644100755
Lines changed: 40 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,68 @@
1+
#!/usr/bin/env node
12
// Copyright 2015 The Emscripten Authors. All rights reserved.
23
// Emscripten is available under two separate licenses, the MIT license and the
34
// University of Illinois/NCSA Open Source License. Both these licenses can be
45
// found in the LICENSE file.
56

6-
// *** Environment setup code ***
7-
var arguments_ = [];
8-
var debug = false;
7+
const nodeFS = require('fs');
8+
const nodePath = require('path');
99

10-
var ENVIRONMENT_IS_NODE = typeof process === 'object';
11-
var ENVIRONMENT_IS_WEB = typeof window === 'object';
12-
var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
13-
var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
10+
const arguments_ = process['argv'].slice(2);
11+
const debug = false;
1412

15-
if (ENVIRONMENT_IS_NODE) {
16-
// Expose functionality in the same simple way that the shells work
17-
// Note that we pollute the global namespace here, otherwise we break in node
18-
print = function(x) {
19-
process['stdout'].write(x + '\n');
20-
};
21-
printErr = function(x) {
22-
process['stderr'].write(x + '\n');
23-
};
24-
25-
var nodeFS = require('fs');
26-
var nodePath = require('path');
27-
28-
if (!nodeFS.existsSync) {
29-
nodeFS.existsSync = function(path) {
30-
try {
31-
return !!nodeFS.readFileSync(path);
32-
} catch(e) {
33-
return false;
34-
}
35-
}
36-
}
13+
print = function(x) {
14+
process['stdout'].write(x + '\n');
15+
};
3716

38-
function find(filename) {
39-
var prefixes = [nodePath.join(__dirname, '..', 'src'), process.cwd()];
40-
for (var i = 0; i < prefixes.length; ++i) {
41-
var combined = nodePath.join(prefixes[i], filename);
42-
if (nodeFS.existsSync(combined)) {
43-
return combined;
44-
}
45-
}
46-
return filename;
47-
}
17+
printErr = function(x) {
18+
process['stderr'].write(x + '\n');
19+
};
4820

49-
read = function(filename, binary) {
50-
filename = nodePath['normalize'](filename);
51-
var ret = nodeFS['readFileSync'](filename);
52-
// The path is absolute if the normalized version is the same as the resolved.
53-
if (!ret && filename != nodePath['resolve'](filename)) {
54-
filename = path.join(__dirname, '..', 'src', filename);
55-
ret = nodeFS['readFileSync'](filename);
21+
function find(filename) {
22+
const prefixes = [nodePath.join(__dirname, '..', 'src'), process.cwd()];
23+
for (let i = 0; i < prefixes.length; ++i) {
24+
const combined = nodePath.join(prefixes[i], filename);
25+
if (nodeFS.existsSync(combined)) {
26+
return combined;
5627
}
57-
if (ret && !binary) ret = ret.toString();
58-
return ret;
59-
};
60-
61-
readBinary = function(filename) { return read(filename, true) };
62-
63-
load = function(f) {
64-
globalEval(read(f));
65-
};
66-
67-
arguments_ = process['argv'].slice(2);
68-
69-
} else if (ENVIRONMENT_IS_SHELL) {
70-
// Polyfill over SpiderMonkey/V8 differences
71-
if (!this['read']) {
72-
this['read'] = function(f) { snarf(f) };
7328
}
29+
return filename;
30+
}
7431

75-
if (typeof scriptArgs != 'undefined') {
76-
arguments_ = scriptArgs;
77-
} else if (typeof arguments != 'undefined') {
78-
arguments_ = arguments;
79-
}
80-
81-
} else if (ENVIRONMENT_IS_WEB) {
82-
this['print'] = printErr = function(x) {
83-
console.log(x);
84-
};
85-
86-
this['read'] = function(url) {
87-
var xhr = new XMLHttpRequest();
88-
xhr.open('GET', url, false);
89-
xhr.send(null);
90-
return xhr.responseText;
91-
};
92-
93-
if (this['arguments']) {
94-
arguments_ = arguments;
32+
function read(filename, binary) {
33+
filename = nodePath['normalize'](filename);
34+
let ret = nodeFS['readFileSync'](filename);
35+
// The path is absolute if the normalized version is the same as the resolved.
36+
if (!ret && filename != nodePath['resolve'](filename)) {
37+
filename = path.join(__dirname, '..', 'src', filename);
38+
ret = nodeFS['readFileSync'](filename);
9539
}
96-
} else if (ENVIRONMENT_IS_WORKER) {
97-
// We can do very little here...
98-
99-
this['load'] = importScripts;
40+
if (ret && !binary) ret = ret.toString();
41+
return ret;
42+
};
10043

101-
} else {
102-
throw 'Unknown runtime environment. Where are we?';
44+
function readBinary(filename) {
45+
return read(filename, true);
10346
}
10447

10548
function globalEval(x) {
10649
eval.call(null, x);
10750
}
10851

109-
if (typeof load === 'undefined' && typeof read != 'undefined') {
110-
this['load'] = function(f) {
111-
globalEval(read(f));
112-
};
113-
}
114-
115-
if (typeof printErr === 'undefined') {
116-
this['printErr'] = function(){};
117-
}
118-
119-
if (typeof print === 'undefined') {
120-
this['print'] = printErr;
121-
}
122-
123-
assert = function(x, message) {
124-
if (!x) throw 'assertion failed: ' + message + ' : ' + new Error().stack;
52+
function load(f) {
53+
globalEval(read(f));
12554
}
12655

127-
if (!Math['imul'] || Math['imul'](0xffffffff, 5) !== -5) Math['imul'] = function imul(a, b) {
128-
var ah = a >>> 16;
129-
var al = a & 0xffff;
130-
var bh = b >>> 16;
131-
var bl = b & 0xffff;
132-
return (al*bl + ((ah*bl + al*bh) << 16))|0;
133-
};
134-
135-
console.log = printErr;
136-
137-
// *** Environment setup code ***
138-
139-
var lz4 = arguments_[0];
140-
var input = arguments_[1];
141-
var output = arguments_[2];
56+
const lz4 = arguments_[0];
57+
const input = arguments_[1];
58+
const output = arguments_[2];
14259

14360
load(lz4);
14461

145-
var data = readBinary(input);
146-
if (!(data instanceof ArrayBuffer)) {
147-
printErr('converting to ArrayBuffer');
148-
data = new Uint8Array(data).buffer;
149-
}
150-
151-
var start = Date.now();
152-
var compressedData = MiniLZ4.compressPackage(data);
153-
nodeFS['writeFileSync'](output, Buffer.from(compressedData['data']));
62+
const data = new Uint8Array(readBinary(input)).buffer;
63+
const start = Date.now();
64+
const compressedData = MiniLZ4.compressPackage(data);
65+
nodeFS.writeFileSync(output, Buffer.from(compressedData['data']));
15466
compressedData['data'] = null;
15567
printErr('compressed in ' + (Date.now() - start) + ' ms');
15668
print(JSON.stringify(compressedData));
157-

0 commit comments

Comments
 (0)