Skip to content

Commit

Permalink
JS: Fixed ReferenceError: window is not defined when getting the gl…
Browse files Browse the repository at this point in the history
…obal object (protocolbuffers#9156)
  • Loading branch information
MarnixBouhuis authored and acozzette committed Oct 28, 2021
1 parent c7dfd0d commit e58469b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/google/protobuf/compiler/js/js_generator.cc
Expand Up @@ -3634,7 +3634,14 @@ void Generator::GenerateFile(const GeneratorOptions& options,
// - self: defined inside Web Workers (WorkerGlobalScope)
// - Function('return this')(): this will work on most platforms, but it 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");
printer->Print(
"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++) {
Expand Down

0 comments on commit e58469b

Please sign in to comment.