Skip to content

Modernize and run eslint on tools/lz4-compress.js. NFC - #15846

Merged
sbc100 merged 1 commit into
mainfrom
lint_lz4_compress
Dec 27, 2021
Merged

Modernize and run eslint on tools/lz4-compress.js. NFC#15846
sbc100 merged 1 commit into
mainfrom
lint_lz4_compress

Conversation

@sbc100

@sbc100 sbc100 commented Dec 23, 2021

Copy link
Copy Markdown
Collaborator

Followup to #15836

@sbc100
sbc100 requested a review from aheejin December 23, 2021 11:24
Comment thread tools/lz4-compress.js
Comment on lines -10 to -15
var ENVIRONMENT_IS_NODE = typeof process === 'object';
var ENVIRONMENT_IS_WEB = typeof window === 'object';
var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
const arguments_ = process['argv'].slice(2);
const debug = false;

if (ENVIRONMENT_IS_NODE) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not necessary anymore? It seems we use these variables in other js files..?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this script is only ever run under node these days.. and its standalone. This was just copyied and pasted from another script but not needed here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we guarantee this only runs on node? I'm not very familiar with this script's usage, but searching lz4-compress in the codebase only turns up tools/file_packager.py, which looks it can run elsewhere too... Also do we not run this on d8 as well?

Anyway, if that's the case, does it give a readable error message when someone tries to run this on other environments?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a completely internal tool just like src/compiler.js and tools/preprocessor.js. These tool may once have worked in non-node environments but today we assume they all run under node only. The code here is completely internal to emscripten and there is exactly one call site.

All of these internal JS scripts require node these days and there is no reason anyone should try to run them under d8 (at least not that I know of).

If a d8 user did try to run this script and saw require() is not a function is should be very obvious that they are trying to run a node script outside of node.

Comment thread tools/lz4-compress.js
var bh = b >>> 16;
var bl = b & 0xffff;
return (al*bl + ((ah*bl + al*bh) << 16))|0;
assert = function(x, message) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other functions in this file have function assert(x, message) { ... form.. Is there a reason why this function is different?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think its because this function needs to be added to the global namespace so that the script that is eval 'ed below had visibility of it.

Its a strange quirk of node and if you just say foo = 3 that creates a new name foo on the global object that is shared.

I have followup PR that will remove the use of eval completely .. but for now this smaller cleanup still relies on eval and the global shared namespace.

Comment thread tools/lz4-compress.js
Comment on lines -52 to -56
// The path is absolute if the normalized version is the same as the resolved.
if (!ret && filename != nodePath['resolve'](filename)) {
filename = path.join(__dirname, '..', 'src', filename);
ret = nodeFS['readFileSync'](filename);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part not necessary anymore?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this was just boilerplate copied from other scripts. Its not needed there.

Comment thread tools/lz4-compress.js
Comment on lines -10 to -15
var ENVIRONMENT_IS_NODE = typeof process === 'object';
var ENVIRONMENT_IS_WEB = typeof window === 'object';
var ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
const arguments_ = process['argv'].slice(2);
const debug = false;

if (ENVIRONMENT_IS_NODE) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we guarantee this only runs on node? I'm not very familiar with this script's usage, but searching lz4-compress in the codebase only turns up tools/file_packager.py, which looks it can run elsewhere too... Also do we not run this on d8 as well?

Anyway, if that's the case, does it give a readable error message when someone tries to run this on other environments?

@sbc100
sbc100 merged commit ffe4d08 into main Dec 27, 2021
@sbc100
sbc100 deleted the lint_lz4_compress branch December 27, 2021 23:45
Comment thread tools/lz4-compress.js
var bl = b & 0xffff;
return (al*bl + ((ah*bl + al*bh) << 16))|0;
assert = function(x, message) {
if (!x) throw new Errror(message);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like one too many rs in this Error?

sbc100 added a commit that referenced this pull request Jan 12, 2022
sbc100 added a commit that referenced this pull request Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants