From 0b5f4f94d84d54824cb620f816215e684fabf9ff Mon Sep 17 00:00:00 2001 From: alias-rahil Date: Wed, 1 Jun 2022 14:12:02 +0530 Subject: [PATCH 1/5] fix: #8 window is undefined error --- generator/js_generator.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/generator/js_generator.cc b/generator/js_generator.cc index eb8aa73..a49ae19 100644 --- a/generator/js_generator.cc +++ b/generator/js_generator.cc @@ -3637,8 +3637,13 @@ void Generator::GenerateFile(const GeneratorOptions& options, // may be blocked by things like CSP. // Function('') is almost the same as eval('') printer->Print( - "var global = (function() { return this || window || global || self " - "|| Function('return this')(); }).call(null);\n\n"); + "var global = (function() {\n" + " if (this) { return this; }\n" + " if (typeof window !== 'undefined') { return window; }\n" + " if (typeof global !== 'undefined') { return global; }\n" + " if (typeof self !== 'undefined') { return self; }\n" + " return Function('return this')();\n" + "}).call(null);\n\n"); } for (int i = 0; i < file->dependency_count(); i++) { From 93a511775ed8dcbcbcbd9f9b2a9a507c6ddd9112 Mon Sep 17 00:00:00 2001 From: alias-rahil Date: Tue, 26 Jul 2022 04:26:49 +0530 Subject: [PATCH 2/5] fix: add check for globalThis object --- generator/js_generator.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/generator/js_generator.cc b/generator/js_generator.cc index a49ae19..1069658 100644 --- a/generator/js_generator.cc +++ b/generator/js_generator.cc @@ -3630,6 +3630,7 @@ void Generator::GenerateFile(const GeneratorOptions& options, // set "this" inside the function to the global object. This does not work // if we are running in strict mode ("use strict"), so we fallback to the // following things (in order from first to last): + // - globalThis: defined in browsers // - window: defined in browsers // - global: defined in most server side environments like NodeJS // - self: defined inside Web Workers (WorkerGlobalScope) @@ -3639,6 +3640,7 @@ void Generator::GenerateFile(const GeneratorOptions& options, printer->Print( "var global = (function() {\n" " if (this) { return this; }\n" + " if (typeof globalThis !== 'undefined') { return globalThis; }\n" " if (typeof window !== 'undefined') { return window; }\n" " if (typeof global !== 'undefined') { return global; }\n" " if (typeof self !== 'undefined') { return self; }\n" From eef12bfebe9ca75d35f600489a226f5b4b06f2f2 Mon Sep 17 00:00:00 2001 From: alias-rahil Date: Tue, 26 Jul 2022 04:57:28 +0530 Subject: [PATCH 3/5] refactor: move global symbols checks outside the function --- generator/js_generator.cc | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/generator/js_generator.cc b/generator/js_generator.cc index 1069658..a3b92f0 100644 --- a/generator/js_generator.cc +++ b/generator/js_generator.cc @@ -3638,14 +3638,13 @@ void Generator::GenerateFile(const GeneratorOptions& options, // may be blocked by things like CSP. // Function('') is almost the same as eval('') printer->Print( - "var global = (function() {\n" - " if (this) { return this; }\n" - " if (typeof globalThis !== 'undefined') { return globalThis; }\n" - " if (typeof window !== 'undefined') { return window; }\n" - " if (typeof global !== 'undefined') { return global; }\n" - " if (typeof self !== 'undefined') { return self; }\n" - " return Function('return this')();\n" - "}).call(null);\n\n"); + "var globalThis = typeof globalThis !== 'undefined' && globalThis;\n" + "var window = typeof window !== 'undefined' && window;\n" + "var global = typeof global !== 'undefined' && global;\n" + "var self = typeof self !== 'undefined' && self;\n" + "var global = globalThis || window || global || self ||\n" + " (function () { return this; }).call(null) ||\n" + " Function('return this')();\n\n"); } for (int i = 0; i < file->dependency_count(); i++) { From a1d2ea35526c004d7ef0ae5f00bde7c983ff9770 Mon Sep 17 00:00:00 2001 From: alias-rahil Date: Wed, 27 Jul 2022 06:53:12 +0530 Subject: [PATCH 4/5] refactor: make checks inline --- generator/js_generator.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/generator/js_generator.cc b/generator/js_generator.cc index a3b92f0..b8cf966 100644 --- a/generator/js_generator.cc +++ b/generator/js_generator.cc @@ -3638,13 +3638,13 @@ void Generator::GenerateFile(const GeneratorOptions& options, // may be blocked by things like CSP. // Function('') is almost the same as eval('') printer->Print( - "var globalThis = typeof globalThis !== 'undefined' && globalThis;\n" - "var window = typeof window !== 'undefined' && window;\n" - "var global = typeof global !== 'undefined' && global;\n" - "var self = typeof self !== 'undefined' && self;\n" - "var global = globalThis || window || global || self ||\n" - " (function () { return this; }).call(null) ||\n" - " Function('return this')();\n\n"); + "var global =\n" + " (typeof globalThis !== 'undefined' && globalThis) ||\n" + " (typeof window !== 'undefined' && window) ||\n" + " (typeof global !== 'undefined' && global) ||\n" + " (typeof self !== 'undefined' && self) ||\n" + " (function () { return this; }).call(null) ||\n" + " Function('return this')();\n\n"); } for (int i = 0; i < file->dependency_count(); i++) { From dac3720cace27ed028b5b937c879d115949298d5 Mon Sep 17 00:00:00 2001 From: alias-rahil Date: Wed, 27 Jul 2022 07:45:03 +0530 Subject: [PATCH 5/5] fix: comment --- generator/js_generator.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/generator/js_generator.cc b/generator/js_generator.cc index b8cf966..19a5883 100644 --- a/generator/js_generator.cc +++ b/generator/js_generator.cc @@ -3630,7 +3630,8 @@ void Generator::GenerateFile(const GeneratorOptions& options, // set "this" inside the function to the global object. This does not work // if we are running in strict mode ("use strict"), so we fallback to the // following things (in order from first to last): - // - globalThis: defined in browsers + // - globalThis: cross-platform standard, might not be defined in older + // versions of browsers // - window: defined in browsers // - global: defined in most server side environments like NodeJS // - self: defined inside Web Workers (WorkerGlobalScope)