Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert lz4-compress.js to mjs. NFC #21507

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading