Skip to content

Commit

Permalink
Convert lz4-compress.js to mjs. NFC (#21507)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Mar 11, 2024
1 parent 44b3031 commit aa0079f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 39 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"scripts": {
"lint": "eslint .",
"fmt": "prettier --write tools/*.js",
"check": "prettier --check tools/*.js"
"fmt": "prettier --write tools/*.mjs",
"check": "prettier --check tools/*.mjs"
}
}
2 changes: 1 addition & 1 deletion test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2529,7 +2529,7 @@ def test_preload_module(self, args):
''')
self.btest_exit(
'main.c',
args=['-sMAIN_MODULE=2', '--preload-file', '.@/', '-O2', '--use-preload-plugins'] + args)
args=['-sMAIN_MODULE=2', '--preload-file', '.@/', '--use-preload-plugins'] + args)

# This does not actually verify anything except that --cpuprofiler and --memoryprofiler compiles.
# Run interactive.test_cpuprofiler_memoryprofiler for interactive testing.
Expand Down
3 changes: 3 additions & 0 deletions third_party/mini-lz4.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,6 @@ return exports;

})();

if (typeof module != 'undefined') {
module.exports = MiniLZ4;
}
5 changes: 2 additions & 3 deletions tools/file_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,8 @@ def generate_js(data_target, data_files, metadata):
# LZ4FS usage
temp = data_target + '.orig'
shutil.move(data_target, temp)
meta = shared.run_js_tool(utils.path_from_root('tools/lz4-compress.js'),
[utils.path_from_root('third_party/mini-lz4.js'),
temp, data_target], stdout=PIPE)
meta = shared.run_js_tool(utils.path_from_root('tools/lz4-compress.mjs'),
[temp, data_target], stdout=PIPE)
os.unlink(temp)
use_data = '''var compressedData = %s;
compressedData['data'] = byteArray;
Expand Down
43 changes: 12 additions & 31 deletions tools/lz4-compress.js → tools/lz4-compress.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

'use strict';

const fs = require('fs');
const path = require('path');

const arguments_ = process.argv.slice(2);
const debug = false;
import * as fs from 'fs';
import * as path from 'path';

function print(x) {
process.stdout.write(x + '\n');
Expand All @@ -20,38 +15,24 @@ function printErr(x) {
process.stderr.write(x + '\n');
}

function read(filename, binary) {
filename = path.normalize(filename);
let ret = fs.readFileSync(filename);
if (ret && !binary) ret = ret.toString();
return ret;
}

function readBinary(filename) {
return read(filename, true);
}

function globalEval(x) {
eval.call(null, x);
}

function load(f) {
globalEval(read(f));
}

global.assert = (x, message) => {
globalThis.assert = (x, message) => {
if (!x) throw new Error(message);
};

// Redirect console.log message from MiniLZ4 to stderr since stdout is
// where we return the decompressed data.
console.log = printErr;

const lz4 = arguments_[0];
const input = arguments_[1];
const output = arguments_[2];
const MiniLZ4 = await import('../third_party/mini-lz4.js');

load(lz4);
function readBinary(filename) {
filename = path.normalize(filename);
return fs.readFileSync(filename);
}

const arguments_ = process.argv.slice(2);
const input = arguments_[0];
const output = arguments_[1];

const data = new Uint8Array(readBinary(input)).buffer;
const start = Date.now();
Expand Down
4 changes: 2 additions & 2 deletions tools/preprocessor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ global.read = (filename) => {
};

global.load = (f) => {
vm.runInThisContext(read(f), { filename: find(f) });
vm.runInThisContext(read(f), {filename: find(f)});
};

assert(args.length >= 2);
Expand All @@ -67,6 +67,6 @@ load('parseTools.js');

let output = preprocess(inputFile);
if (expandMacros) {
output = processMacros(output, inputFile)
output = processMacros(output, inputFile);
}
process.stdout.write(output);

0 comments on commit aa0079f

Please sign in to comment.