Skip to content

Commit 69ae238

Browse files
JS: Fixed ReferenceError: window is not defined when getting the global object (#9156)
1 parent 4db750a commit 69ae238

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/google/protobuf/compiler/js/js_generator.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3634,7 +3634,14 @@ void Generator::GenerateFile(const GeneratorOptions& options,
36343634
// - self: defined inside Web Workers (WorkerGlobalScope)
36353635
// - Function('return this')(): this will work on most platforms, but it may be blocked by things like CSP.
36363636
// Function('') is almost the same as eval('')
3637-
printer->Print("var global = (function() { return this || window || global || self || Function('return this')(); }).call(null);\n\n");
3637+
printer->Print(
3638+
"var global = (function() {\n"
3639+
" if (this) { return this; }\n"
3640+
" if (typeof window !== 'undefined') { return window; }\n"
3641+
" if (typeof global !== 'undefined') { return global; }\n"
3642+
" if (typeof self !== 'undefined') { return self; }\n"
3643+
" return Function('return this')();\n"
3644+
"}.call(null));\n\n");
36383645
}
36393646

36403647
for (int i = 0; i < file->dependency_count(); i++) {

0 commit comments

Comments
 (0)