rotemdan / lzutf8.js

A high-performance Javascript string compression library
MIT License
322 stars 26 forks source link

overwrites global variable: i #24

Closed KolyaKorruptis closed 4 years ago

KolyaKorruptis commented 4 years ago

I noticed lzutf8.js overwrites my global variable: i. I tested this by trying to declare a const i and got the error:

lzutf8..js:1 Uncaught SyntaxError: Identifier 'i' has already been declared
    at lzutf8.js:1

I see you placed everything in IIFEs which seems safe enough but maybe 'i' is hoisted somewhere?

rotemdan commented 4 years ago

Hi, the JavaScript code is compiled from TypeScript with strict options (no imlicit any, strict null checks) and all code is scoped within namespaces (which are compiled to IIFEs). It is very unlikely that it modifies the global scope somehow. I'm not sure how to interpret the error message. I've never seen an error like this before with any TypeScript compiled code. Might be due to some bundler behavior? (webpack?).

rotemdan commented 4 years ago

There's a global piece of code made to work around a bug with IE10, and which declares the i variable:

    if (globalObject !== undefined) {
        for (let i = 0; i < types.length; i++) {
            if (globalObject[types[i]])
                globalObject[types[i]].prototype.subarray = subarray;
        }
    }

When it compiles, the let i is changed to var i. This seems to be the only place where i is declared in the global scope.

The thing is, this piece of code only executes when the subarray method is detected to have buggy behavior (which only occurs on IE10, as far as I know) , so it is very unlikely to be the cause. What environment/browser are you running on?

rotemdan commented 4 years ago

Update: published lzutf8@0.5.6 to NPM with IE10SubarrayBugPatcher now executed in an IIFE. This may fix the issue (it might be the case the i variable declaration was detected as a part of compile-time analysis).

KolyaKorruptis commented 4 years ago

The error occurred on Chrome with LZ-UTF8 v0.5.5.

LZ-UTF8 v0.5.6 fixed it. Thank you!