summaryrefslogtreecommitdiffstats
path: root/contrib/libs/protoc/src/google/protobuf/compiler/js
diff options
context:
space:
mode:
authorpg <[email protected]>2023-02-08 14:47:59 +0300
committerpg <[email protected]>2023-02-08 14:47:59 +0300
commit2999295666a93b51c9226cee88ea70b996d43727 (patch)
tree6cd1a5290a2653689a33c6269c0eebb7dd05729d /contrib/libs/protoc/src/google/protobuf/compiler/js
parente615f3f87f0fc3dcc0d45c304883339ab1eab8c1 (diff)
3.19.0
Diffstat (limited to 'contrib/libs/protoc/src/google/protobuf/compiler/js')
-rw-r--r--contrib/libs/protoc/src/google/protobuf/compiler/js/js_generator.cc11
1 files changed, 10 insertions, 1 deletions
diff --git a/contrib/libs/protoc/src/google/protobuf/compiler/js/js_generator.cc b/contrib/libs/protoc/src/google/protobuf/compiler/js/js_generator.cc
index ac334018dfb..019ef3a5b5a 100644
--- a/contrib/libs/protoc/src/google/protobuf/compiler/js/js_generator.cc
+++ b/contrib/libs/protoc/src/google/protobuf/compiler/js/js_generator.cc
@@ -3625,7 +3625,16 @@ void Generator::GenerateFile(const GeneratorOptions& options,
if (options.import_style == GeneratorOptions::kImportCommonJsStrict) {
printer->Print("var proto = {};\n\n");
} else {
- printer->Print("var global = Function('return this')();\n\n");
+ // To get the global object we call a function with .call(null), this will 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):
+ // - 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");
}
for (int i = 0; i < file->dependency_count(); i++) {