Permalink
Browse files

Use universal method of finding the global object (#2132)

The module store, runtime and symbol polyfills need access to the global
object. This was done by checking the existence of window, global, self
and falling back to this if none were defined. This can fail for two
reasons. First, 'this' should be expected to be set to undefined during
module definitions (as is the case for symbols.js), thus rendering the
fallback useless. Secondly some JavaScript implementations (e.g. JavaScript
-Core Framework) might not even define a name for the global object that
could be accessed within modules.
By using the Function-Constructor we create a new function in non-strict
mode which has its thisArg set to the global object if no other thisArg
is provided.
1 parent e1f2c39 commit caa7b751d5150622e13cdd18865e09681d8c6691 @cyco cyco committed with arv Sep 15, 2016
Showing with 45 additions and 8 deletions.
  1. +1 −0 AUTHORS
  2. +14 −1 src/loader/ModuleStoreImpl.js
  3. +16 −4 src/runtime/modules/symbols.js
  4. +14 −3 src/runtime/runtime.js
View
@@ -56,3 +56,4 @@ Srinivasan Sekar <srinivasan.sekar1990@gmail.com>
Jeff Shen<jeffshen86@gmail.com>
Erik Arvidsson <erik.arvidsson@gmail.com>
Mathias Rangel Wulff <m@rawu.dk>
+Christoph Leimbrock <christoph.leimbrock@gmx.de>
@@ -301,4 +301,17 @@
$traceurRuntime.setModule = ModuleStore.set;
$traceurRuntime.normalizeModuleName = ModuleStore.normalize;
-})(typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this);
+})((function() {
+ try {
+ return new Function('return this;')();
+ } catch(e) {
+ if (typeof window !== 'undefined')
+ return window;
+ if (typeof global !== 'undefined')
+ return global;
+ if (typeof self !== 'undefined')
+ return self;
+
+ return this;
+ }
+})());
@@ -169,10 +169,22 @@ function polyfillSymbol(global) {
}
}
-let g = typeof window !== 'undefined' ? window :
- typeof global !== 'undefined' ? global :
- typeof self !== 'undefined' ? self : this;
-polyfillSymbol(g);
+function findGlobal() {
+ try {
+ return new Function('return this;')();
+ } catch(e) {
+ if (typeof window !== 'undefined')
+ return window;
+ if (typeof global !== 'undefined')
+ return global;
+ if (typeof self !== 'undefined')
+ return self;
+
+ return this;
+ }
+}
+
+polyfillSymbol(findGlobal());
let typeOf = hasNativeSymbol() ?
x => typeof x :
@@ -37,6 +37,17 @@
setupGlobals: setupGlobals,
typeof: typeOf,
};
-})(typeof window !== 'undefined' ? window :
- typeof global !== 'undefined' ? global :
- typeof self !== 'undefined' ? self : this);
+})((function() {
+ try {
+ return new Function('return this;')();
+ } catch(e) {
+ if (typeof window !== 'undefined')
+ return window;
+ if (typeof global !== 'undefined')
+ return global;
+ if (typeof self !== 'undefined')
+ return self;
+
+ return this;
+ }
+})());

0 comments on commit caa7b75

Please sign in to comment.