Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions generator/js_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3630,15 +3630,22 @@ 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: 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)
// - 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");
"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++) {
Expand Down