| 
									
										
										
										
											2018-12-06 19:11:01 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const RuntimeModule = require("../RuntimeModule"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class GlobalRuntimeModule extends RuntimeModule { | 
					
						
							|  |  |  | 	constructor() { | 
					
						
							|  |  |  | 		super("global"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {string} runtime code | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	generate() { | 
					
						
							|  |  |  | 		return Template.asString([ | 
					
						
							|  |  |  | 			`${RuntimeGlobals.global} = (function() {`, | 
					
						
							|  |  |  | 			Template.indent([ | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 				"if (typeof globalThis === 'object') return globalThis;", | 
					
						
							| 
									
										
										
										
											2018-12-06 19:11:01 +08:00
										 |  |  | 				"try {", | 
					
						
							|  |  |  | 				Template.indent( | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 					// This works in non-strict mode
 | 
					
						
							|  |  |  | 					// or
 | 
					
						
							| 
									
										
										
										
											2018-12-06 19:11:01 +08:00
										 |  |  | 					// This works if eval is allowed (see CSP)
 | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 					"return this || new Function('return this')();" | 
					
						
							| 
									
										
										
										
											2018-12-06 19:11:01 +08:00
										 |  |  | 				), | 
					
						
							|  |  |  | 				"} catch (e) {", | 
					
						
							|  |  |  | 				Template.indent( | 
					
						
							|  |  |  | 					// This works if the window reference is available
 | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 					"if (typeof window === 'object') return window;" | 
					
						
							| 
									
										
										
										
											2018-12-06 19:11:01 +08:00
										 |  |  | 				), | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 				"}" | 
					
						
							|  |  |  | 				// It can still be `undefined`, but nothing to do about it...
 | 
					
						
							| 
									
										
										
										
											2018-12-06 19:11:01 +08:00
										 |  |  | 				// We return `undefined`, instead of nothing here, so it's
 | 
					
						
							|  |  |  | 				// easier to handle this case:
 | 
					
						
							|  |  |  | 				//   if (!global) { … }
 | 
					
						
							|  |  |  | 			]), | 
					
						
							|  |  |  | 			"})();" | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = GlobalRuntimeModule; |